-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathollama_complete_guide.py
More file actions
304 lines (245 loc) · 9.51 KB
/
Copy pathollama_complete_guide.py
File metadata and controls
304 lines (245 loc) · 9.51 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
"""
Complete guide for using Ollama with ai-proxy-core
This example demonstrates all the ways to use Ollama with the library,
including direct usage, model management, and error handling.
"""
import asyncio
import aiohttp
from typing import Optional, List, Dict, Any
from ai_proxy_core import OllamaCompletions, OllamaModelProvider, ModelManager, CompletionClient
async def check_ollama_status() -> bool:
"""Check if Ollama is running and accessible"""
try:
async with aiohttp.ClientSession() as session:
async with session.get("http://localhost:11434/api/version") as response:
if response.status == 200:
data = await response.json()
print(f"✓ Ollama is running (version: {data.get('version', 'unknown')})")
return True
except Exception as e:
print(f"✗ Ollama is not accessible: {e}")
print("\nTo install Ollama:")
print("1. Visit https://ollama.ai")
print("2. Download and install for your platform")
print("3. Run: ollama serve")
print("4. Pull a model: ollama pull llama3.2")
return False
async def example_1_direct_ollama_usage():
"""Example 1: Direct usage of OllamaCompletions"""
print("\n" + "="*60)
print("Example 1: Direct OllamaCompletions Usage")
print("="*60)
# Initialize Ollama completions
ollama = OllamaCompletions()
# List available models
models = ollama.list_models()
print(f"\nAvailable models: {models}")
if not models:
print("No models found. Pull a model with: ollama pull llama3.2")
return
# Use the first available model
model = models[0]
print(f"\nUsing model: {model}")
# Create a simple completion
messages = [
{"role": "user", "content": "What is 2+2? Answer in one word."}
]
try:
response = await ollama.create_completion(
messages=messages,
model=model,
temperature=0.1
)
print(f"\nResponse: {response['content']}")
except Exception as e:
print(f"Error: {e}")
async def example_2_model_provider():
"""Example 2: Using OllamaModelProvider with ModelManager"""
print("\n" + "="*60)
print("Example 2: OllamaModelProvider with ModelManager")
print("="*60)
# Create model manager and register Ollama provider
manager = ModelManager()
provider = OllamaModelProvider()
manager.register_provider(provider)
# List all models through the manager
model_infos = await manager.list_all_models()
print("\nModels available through ModelManager:")
for info in model_infos:
print(f" - {info.name} (provider: {info.provider}, status: {info.status})")
if model_infos:
# Ensure a model is ready
model_name = model_infos[0].name
print(f"\nEnsuring {model_name} is ready...")
await manager.ensure_model_ready(model_name, "ollama")
print(f"✓ {model_name} is ready for use")
async def example_3_completion_client():
"""Example 3: Using CompletionClient for unified interface"""
print("\n" + "="*60)
print("Example 3: CompletionClient Unified Interface")
print("="*60)
# Create completion client (it auto-detects Ollama)
client = CompletionClient()
# Check available providers
providers = client.get_available_providers()
print(f"\nAvailable providers: {providers}")
if "ollama" not in providers:
print("Ollama provider not available")
return
# List models
models = await client.list_models(provider="ollama")
if not models:
print("No Ollama models available")
return
model_id = models[0]["id"]
print(f"\nUsing model: {model_id}")
# Create completion through unified interface
messages = [
{"role": "system", "content": "You are a helpful assistant. Be concise."},
{"role": "user", "content": "Explain machine learning in one sentence."}
]
try:
response = await client.create_completion(
messages=messages,
model=model_id,
provider="ollama", # Explicitly specify Ollama
temperature=0.7,
max_tokens=100
)
print(f"\nResponse: {response['content']}")
except Exception as e:
print(f"Error: {e}")
async def example_4_streaming():
"""Example 4: Streaming responses from Ollama"""
print("\n" + "="*60)
print("Example 4: Streaming Responses")
print("="*60)
ollama = OllamaCompletions()
models = ollama.list_models()
if not models:
print("No models available")
return
model = models[0]
messages = [
{"role": "user", "content": "Write a haiku about programming."}
]
print(f"\nStreaming response from {model}:")
print("-" * 40)
# Note: Streaming implementation depends on Ollama's specific API
# This is a placeholder for the streaming functionality
try:
response = await ollama.create_completion(
messages=messages,
model=model,
temperature=0.9,
stream=False # Set to True when streaming is implemented
)
print(response['content'])
except Exception as e:
print(f"Error: {e}")
async def example_5_error_handling():
"""Example 5: Robust error handling and fallbacks"""
print("\n" + "="*60)
print("Example 5: Error Handling and Fallbacks")
print("="*60)
async def safe_completion(prompt: str, model: Optional[str] = None) -> str:
"""Create a completion with proper error handling"""
try:
ollama = OllamaCompletions()
# Get available models
models = ollama.list_models()
if not models:
return "Error: No models available. Please pull a model first."
# Use specified model or first available
model = model or models[0]
# Create completion
messages = [{"role": "user", "content": prompt}]
response = await ollama.create_completion(
messages=messages,
model=model,
temperature=0.7
)
return response.get('content', 'No response content')
except ConnectionError:
return "Error: Cannot connect to Ollama. Is it running? (ollama serve)"
except ValueError as e:
return f"Error: Invalid parameters: {e}"
except Exception as e:
return f"Unexpected error: {e}"
# Test with various scenarios
test_prompts = [
"Hello, how are you?",
"What is the capital of France?",
]
for prompt in test_prompts:
print(f"\nPrompt: {prompt}")
response = await safe_completion(prompt)
print(f"Response: {response[:100]}...") # Truncate long responses
async def example_6_advanced_usage():
"""Example 6: Advanced usage with custom parameters"""
print("\n" + "="*60)
print("Example 6: Advanced Usage")
print("="*60)
# Initialize with custom model manager
manager = ModelManager()
client = CompletionClient(model_manager=manager)
# Register Ollama provider
provider = OllamaModelProvider()
manager.register_provider(provider)
# Find best model for specific requirements
requirements = {
"min_context_limit": 4096,
"multimodal": False, # Text only
}
best_model = await client.find_best_model(requirements)
if best_model:
print(f"\nBest model for requirements: {best_model['name']}")
# Use JSON response format (if supported by model)
messages = [
{
"role": "user",
"content": "List 3 programming languages with their main use cases. "
"Format as JSON with 'language' and 'use_case' fields."
}
]
models = await client.list_models(provider="ollama")
if models:
try:
response = await client.create_completion(
messages=messages,
model=models[0]["id"],
provider="ollama",
temperature=0.3,
response_format={"type": "json_object"} # Request JSON format
)
print(f"\nJSON Response: {response.get('content', 'No content')}")
except Exception as e:
print(f"Error with advanced features: {e}")
async def main():
"""Run all examples"""
print("="*60)
print("Ollama Integration Complete Guide")
print("="*60)
# Check Ollama status first
if not await check_ollama_status():
print("\n⚠️ Please install and start Ollama first")
return
# Run examples
await example_1_direct_ollama_usage()
await example_2_model_provider()
await example_3_completion_client()
await example_4_streaming()
await example_5_error_handling()
await example_6_advanced_usage()
print("\n" + "="*60)
print("Guide Complete!")
print("="*60)
print("\nKey Takeaways:")
print("1. OllamaCompletions - Direct, simple interface")
print("2. OllamaModelProvider - Integration with ModelManager")
print("3. CompletionClient - Unified interface for all providers")
print("4. Always check if Ollama is running first")
print("5. Handle errors gracefully with fallbacks")
print("6. Use model_manager parameter for custom configurations")
if __name__ == "__main__":
asyncio.run(main())