An enhanced client for Ollama that now handles image data for VLM models.
| 7 | import httpx, asyncio |
| 8 | |
| 9 | class 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 |
no outgoing calls
no test coverage detected