MCPcopy
hub / github.com/PromtEngineer/localGPT / OllamaClient

Class OllamaClient

rag_system/utils/ollama_client.py:9–157  ·  view source on GitHub ↗

An enhanced client for Ollama that now handles image data for VLM models.

Source from the content-addressed store, hash-verified

7import httpx, asyncio
8
9class OllamaClient:
10 """
11 An enhanced client for Ollama that now handles image data for VLM models.
12 """
13 def __init__(self, host: str = "http://localhost:11434"):
14 self.host = host
15 self.api_url = f"{host}/api"
16 # (Connection check remains the same)
17
18 def _image_to_base64(self, image: Image.Image) -> str:
19 """Converts a Pillow Image to a base64 string."""
20 buffered = BytesIO()
21 image.save(buffered, format="PNG")
22 return base64.b64encode(buffered.getvalue()).decode('utf-8')
23
24 def generate_embedding(self, model: str, text: str) -> List[float]:
25 try:
26 response = requests.post(
27 f"{self.api_url}/embeddings",
28 json={"model": model, "prompt": text}
29 )
30 response.raise_for_status()
31 return response.json().get("embedding", [])
32 except requests.exceptions.RequestException as e:
33 print(f"Error generating embedding: {e}")
34 return []
35
36 def generate_completion(
37 self,
38 model: str,
39 prompt: str,
40 *,
41 format: str = "",
42 images: List[Image.Image] | None = None,
43 enable_thinking: bool | None = None,
44 ) -> Dict[str, Any]:
45 """
46 Generates a completion, now with optional support for images.
47
48 Args:
49 model: The name of the generation model (e.g., 'llava', 'qwen-vl').
50 prompt: The text prompt for the model.
51 format: The format for the response, e.g., "json".
52 images: A list of Pillow Image objects to send to the VLM.
53 enable_thinking: Optional flag to disable chain-of-thought for Qwen models.
54 """
55 try:
56 payload = {
57 "model": model,
58 "prompt": prompt,
59 "stream": False
60 }
61 if format:
62 payload["format"] = format
63
64 if images:
65 payload["images"] = [self._image_to_base64(img) for img in images]
66

Callers 6

get_agentFunction · 0.90
get_indexing_pipelineFunction · 0.90
get_agentFunction · 0.90
run_chatFunction · 0.90
ollama_client.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected