-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ollama_models.py
More file actions
323 lines (261 loc) · 11.8 KB
/
test_ollama_models.py
File metadata and controls
323 lines (261 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
Test script to verify Ollama model integration with the book writer system
"""
import os
import sys
from pathlib import Path
# Add the project root to the path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from book_writer.config import model_config
from book_writer.model_manager import model_manager
from book_writer.outline import BookOutline, create_sample_outline
from book_writer.note_processor import NoteProcessor, ContentManager
from book_writer.content_expansion import ContentExpander
from book_writer.rag_writing import RAGWriter
def test_model_connectivity():
"""Test if Ollama models are accessible."""
print("Testing Ollama model connectivity...")
if model_manager.health_check():
print("✓ Ollama service is accessible")
# List available models
available_models = model_manager.list_available_models()
print(f"Available models: {available_models}")
assert True
else:
print("✗ Ollama service is not accessible")
print("Please make sure Ollama is running and accessible at the configured URL")
assert False, "Ollama service is not accessible"
def test_content_expansion_model():
"""Test the content expansion model (stable-beluga:13b)."""
print("\nTesting content expansion model...")
# Create a simple test note
test_note = "Artificial intelligence is a branch of computer science that aims to create software or machines that exhibit human-like intelligence."
try:
response = model_manager.generate_response(
prompt=f"Expand this concept into a comprehensive paragraph: {test_note}",
task="content_expansion",
temperature=0.7,
max_tokens=512
)
print(f"✓ Content expansion test completed")
print(f"Response: {response[:200]}...")
assert response is not None
except Exception as e:
print(f"✗ Content expansion test failed: {e}")
assert False, f"Content expansion test failed: {e}"
def test_outline_generation_model():
"""Test the outline generation model (phi3.5:latest)."""
print("\nTesting outline generation model...")
test_topic = "Machine Learning Fundamentals"
try:
response = model_manager.generate_response(
prompt=f"Create a detailed book outline for '{test_topic}' with 2-3 parts, 3-4 chapters per part, and 2-3 subtopics per chapter.",
task="outline_generation",
format_json=True,
temperature=0.5,
max_tokens=1024
)
print(f"✓ Outline generation test completed")
if isinstance(response, dict):
print(f"Generated {len(response.get('parts', []))} parts")
else:
print(f"Response: {str(response)[:200]}...")
assert response is not None
except Exception as e:
print(f"✗ Outline generation test failed: {e}")
assert False, f"Outline generation test failed: {e}"
def test_organization_model():
"""Test the organization model (deepseek-r1:8b)."""
print("\nTesting organization model...")
test_content = "This chapter discusses various machine learning algorithms including supervised, unsupervised, and reinforcement learning approaches."
test_outline = {
"parts": [
{
"id": "part1",
"title": "Introduction",
"chapters": [
{
"id": "chap1",
"title": "Getting Started with ML",
"subtopics": [
{"id": "sub1", "title": "Basic Concepts"}
]
},
{
"id": "chap2",
"title": "Machine Learning Algorithms",
"subtopics": [
{"id": "sub2", "title": "Supervised Learning"},
{"id": "sub3", "title": "Unsupervised Learning"}
]
}
]
}
]
}
try:
prompt = f"""
Analyze the following content and classify it into the most appropriate chapter and subtopic from the provided outline structure.
Content to classify:
{test_content}
Book Outline Structure:
{str(test_outline)}
Please return the classification in the following JSON format:
{{
"chapter_id": "the ID of the most appropriate chapter",
"chapter_title": "the title of the most appropriate chapter",
"subtopic_id": "the ID of the most appropriate subtopic",
"subtopic_title": "the title of the most appropriate subtopic",
"confidence": "a confidence score between 0 and 1"
}}
"""
response = model_manager.generate_response(
prompt=prompt,
task="organization",
format_json=True,
temperature=0.4
)
print(f"✓ Organization model test completed")
if isinstance(response, dict):
print(f"Classification: {response.get('chapter_title', 'N/A')} - {response.get('subtopic_title', 'N/A')}")
else:
print(f"Response: {str(response)[:200]}...")
assert response is not None
except Exception as e:
print(f"✗ Organization model test failed: {e}")
assert False, f"Organization model test failed: {e}"
def test_full_integration():
"""Test full integration with the book writer system."""
print("\nTesting full integration...")
try:
# Create a temporary project directory
test_project_path = project_root / "test_project"
test_project_path.mkdir(exist_ok=True)
# Create the necessary components
note_processor = NoteProcessor(test_project_path)
content_manager = ContentManager(test_project_path, note_processor)
content_expander = ContentExpander(test_project_path, note_processor, content_manager)
# Create a sample outline using AI
print("Creating sample outline...")
outline = create_sample_outline("Natural Language Processing")
print(f"Created outline with {len(outline.parts)} parts")
# Add a test note
print("Adding test note...")
note_id = note_processor.process_note(
text="Transformers are a type of neural network architecture that has revolutionized natural language processing.",
source="test",
potential_topics=["NLP", "Machine Learning", "AI"]
)
print(f"Added note with ID: {note_id}")
# Expand the note using the AI model
print("Expanding note...")
expanded_text, metadata = content_expander.expand_note(note_id, style="academic")
print(f"Expanded text length: {len(expanded_text)} characters")
# Classify the content
print("Classifying content...")
classification = content_expander.classify_content(expanded_text, outline.to_dict())
print(f"Classification: Chapter={classification['chapter']['title'] if classification['chapter'] else 'N/A'}, Subtopic={classification['subtopic']['title'] if classification['subtopic'] else 'N/A'}")
# Clean up
note_processor.close()
import shutil
shutil.rmtree(test_project_path)
print("✓ Full integration test completed successfully")
assert True
except Exception as e:
print(f"✗ Full integration test failed: {e}")
import traceback
traceback.print_exc()
assert False, f"Full integration test failed: {e}"
def test_rag_functionality():
"""Test the updated RAG functionality with Ollama models."""
print("\nTesting RAG functionality...")
try:
# Create a temporary project directory
test_project_path = project_root / "test_project"
test_project_path.mkdir(exist_ok=True)
# Create the necessary components
note_processor = NoteProcessor(test_project_path)
content_manager = ContentManager(test_project_path, note_processor)
rag_writer = RAGWriter(test_project_path, note_processor, content_manager)
# Add a test note for retrieval
test_note_id = note_processor.process_note(
text="Deep learning is a subset of machine learning that uses neural networks with multiple layers.",
source="test_source",
potential_topics=["AI", "Neural Networks", "Machine Learning"]
)
# Test context retrieval
context = rag_writer.retrieve_context("neural networks")
print(f"Retrieved {len(context.get('notes', []))} notes and {len(context.get('content', []))} content items")
# Test content generation with context
prompt = "Explain the concept of deep learning in simple terms"
generated_content = rag_writer.generate_with_context(prompt, context)
print(f"✓ RAG generation completed")
print(f"Generated content length: {len(generated_content)} characters")
# Clean up
import shutil
shutil.rmtree(test_project_path)
assert True
except Exception as e:
print(f"✗ RAG functionality test failed: {e}")
import traceback
traceback.print_exc()
assert False, f"RAG functionality test failed: {e}"
def test_function_calling():
"""Test the function calling capabilities."""
print("\nTesting function calling...")
try:
# Register a simple test function
def test_function(message: str) -> str:
return f"Processed: {message}"
from book_writer.tool_registry import tool_registry
tool_registry.register_tool(
name="test_function",
description="A simple test function",
parameters={
"type": "object",
"properties": {
"message": {"type": "string", "description": "The message to process"}
},
"required": ["message"]
},
function=test_function
)
# Test function execution
result = model_manager._execute_single_function("test_function", {"message": "Hello, world!"})
print(f"Function result: {result}")
# Test with the RAG writer's registered tools
result = model_manager._execute_single_function("search_notes", {"query": "test"})
print(f"Search notes result: {result}")
print("✓ Function calling test completed")
assert True
except Exception as e:
print(f"✗ Function calling test failed: {e}")
import traceback
traceback.print_exc()
assert False, f"Function calling test failed: {e}"
def main():
"""Run all tests."""
print("Starting Ollama model integration tests...\n")
# Run connectivity test first
if not test_model_connectivity():
print("\nCannot proceed with other tests - Ollama not accessible")
return
# Run individual model tests
results = []
results.append(test_content_expansion_model())
results.append(test_outline_generation_model())
results.append(test_organization_model())
results.append(test_full_integration())
results.append(test_rag_functionality())
results.append(test_function_calling())
# Summary
print(f"\nTest Summary:")
print(f"Passed: {sum(results)}/{len(results)}")
print(f"Failed: {len(results) - sum(results)}/{len(results)}")
if all(results):
print("🎉 All tests passed!")
else:
print("❌ Some tests failed.")
if __name__ == "__main__":
main()