File size: 9,181 Bytes
a9dc537 |
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 324 325 |
"""
Ollama Client for SPARKNET
Handles communication with local Ollama LLM models
"""
import ollama
from typing import List, Dict, Optional, Generator, Any
from loguru import logger
import json
class OllamaClient:
"""Client for interacting with Ollama LLM models."""
def __init__(
self,
host: str = "localhost",
port: int = 11434,
default_model: str = "llama3.2:latest",
timeout: int = 300,
):
"""
Initialize Ollama client.
Args:
host: Ollama server host
port: Ollama server port
default_model: Default model to use
timeout: Request timeout in seconds
"""
self.host = host
self.port = port
self.base_url = f"http://{host}:{port}"
self.default_model = default_model
self.timeout = timeout
self.client = ollama.Client(host=self.base_url)
logger.info(f"Initialized Ollama client: {self.base_url}")
def list_models(self) -> List[Dict[str, Any]]:
"""
List available models.
Returns:
List of model information dictionaries
"""
try:
response = self.client.list()
models = response.get("models", [])
logger.info(f"Found {len(models)} available models")
return models
except Exception as e:
logger.error(f"Error listing models: {e}")
return []
def pull_model(self, model_name: str) -> bool:
"""
Pull/download a model.
Args:
model_name: Name of the model to pull
Returns:
True if successful, False otherwise
"""
try:
logger.info(f"Pulling model: {model_name}")
self.client.pull(model_name)
logger.info(f"Successfully pulled model: {model_name}")
return True
except Exception as e:
logger.error(f"Error pulling model {model_name}: {e}")
return False
def generate(
self,
prompt: str,
model: Optional[str] = None,
system: Optional[str] = None,
temperature: float = 0.7,
max_tokens: Optional[int] = None,
stream: bool = False,
**kwargs,
) -> str | Generator[str, None, None]:
"""
Generate completion from a prompt.
Args:
prompt: Input prompt
model: Model to use (default: self.default_model)
system: System prompt
temperature: Sampling temperature
max_tokens: Maximum tokens to generate
stream: Whether to stream the response
**kwargs: Additional generation parameters
Returns:
Generated text or generator if streaming
"""
model = model or self.default_model
options = {
"temperature": temperature,
}
if max_tokens:
options["num_predict"] = max_tokens
options.update(kwargs)
try:
logger.debug(f"Generating with model {model}, prompt length: {len(prompt)}")
if stream:
return self._generate_stream(prompt, model, system, options)
else:
response = self.client.generate(
model=model,
prompt=prompt,
system=system,
options=options,
)
generated_text = response.get("response", "")
logger.debug(f"Generated {len(generated_text)} characters")
return generated_text
except Exception as e:
logger.error(f"Error generating completion: {e}")
return ""
def _generate_stream(
self,
prompt: str,
model: str,
system: Optional[str],
options: Dict,
) -> Generator[str, None, None]:
"""
Generate streaming completion.
Args:
prompt: Input prompt
model: Model to use
system: System prompt
options: Generation options
Yields:
Generated text chunks
"""
try:
stream = self.client.generate(
model=model,
prompt=prompt,
system=system,
options=options,
stream=True,
)
for chunk in stream:
if "response" in chunk:
yield chunk["response"]
except Exception as e:
logger.error(f"Error in streaming generation: {e}")
yield ""
def chat(
self,
messages: List[Dict[str, str]],
model: Optional[str] = None,
temperature: float = 0.7,
stream: bool = False,
**kwargs,
) -> str | Generator[str, None, None]:
"""
Chat completion with conversation history.
Args:
messages: List of message dicts with 'role' and 'content'
model: Model to use (default: self.default_model)
temperature: Sampling temperature
stream: Whether to stream the response
**kwargs: Additional chat parameters
Returns:
Response text or generator if streaming
"""
model = model or self.default_model
options = {
"temperature": temperature,
}
options.update(kwargs)
try:
logger.debug(f"Chat with model {model}, {len(messages)} messages")
if stream:
return self._chat_stream(messages, model, options)
else:
response = self.client.chat(
model=model,
messages=messages,
options=options,
)
message = response.get("message", {})
content = message.get("content", "")
logger.debug(f"Chat response: {len(content)} characters")
return content
except Exception as e:
logger.error(f"Error in chat completion: {e}")
return ""
def _chat_stream(
self,
messages: List[Dict[str, str]],
model: str,
options: Dict,
) -> Generator[str, None, None]:
"""
Streaming chat completion.
Args:
messages: List of message dicts
model: Model to use
options: Chat options
Yields:
Response text chunks
"""
try:
stream = self.client.chat(
model=model,
messages=messages,
options=options,
stream=True,
)
for chunk in stream:
if "message" in chunk:
message = chunk["message"]
if "content" in message:
yield message["content"]
except Exception as e:
logger.error(f"Error in streaming chat: {e}")
yield ""
def embed(
self,
text: str | List[str],
model: str = "nomic-embed-text:latest",
) -> List[List[float]]:
"""
Generate embeddings for text.
Args:
text: Text or list of texts to embed
model: Embedding model to use
Returns:
List of embedding vectors
"""
try:
if isinstance(text, str):
text = [text]
logger.debug(f"Generating embeddings for {len(text)} texts")
embeddings = []
for t in text:
response = self.client.embeddings(model=model, prompt=t)
embedding = response.get("embedding", [])
embeddings.append(embedding)
logger.debug(f"Generated {len(embeddings)} embeddings")
return embeddings
except Exception as e:
logger.error(f"Error generating embeddings: {e}")
return []
def count_tokens(self, text: str) -> int:
"""
Estimate token count for text.
Simple estimation: ~4 characters per token for English text.
Args:
text: Text to count tokens for
Returns:
Estimated token count
"""
# Simple estimation - this can be improved with proper tokenization
return len(text) // 4
def is_available(self) -> bool:
"""
Check if Ollama server is available.
Returns:
True if server is responding, False otherwise
"""
try:
self.list_models()
return True
except Exception:
return False
# Global Ollama client instance
_ollama_client: Optional[OllamaClient] = None
def get_ollama_client(
host: str = "localhost",
port: int = 11434,
default_model: str = "llama3.2:latest",
) -> OllamaClient:
"""Get or create the global Ollama client instance."""
global _ollama_client
if _ollama_client is None:
_ollama_client = OllamaClient(host=host, port=port, default_model=default_model)
return _ollama_client
|