33 lines
844 B
Python
33 lines
844 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
from typing import Any, Dict, List, Optional
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
|
||
|
|
def chat(
|
||
|
|
base_url: str,
|
||
|
|
model: str,
|
||
|
|
messages: List[Dict[str, str]],
|
||
|
|
options: Optional[Dict[str, Any]] = None,
|
||
|
|
timeout: float = 600.0,
|
||
|
|
) -> str:
|
||
|
|
url = base_url.rstrip("/") + "/api/chat"
|
||
|
|
payload: Dict[str, Any] = {
|
||
|
|
"model": model,
|
||
|
|
"messages": messages,
|
||
|
|
"stream": False,
|
||
|
|
}
|
||
|
|
if options:
|
||
|
|
payload["options"] = options
|
||
|
|
with httpx.Client(timeout=timeout) as client:
|
||
|
|
r = client.post(url, json=payload)
|
||
|
|
r.raise_for_status()
|
||
|
|
data = r.json()
|
||
|
|
msg = data.get("message") or {}
|
||
|
|
content = msg.get("content")
|
||
|
|
if not isinstance(content, str):
|
||
|
|
raise RuntimeError(f"Unexpected Ollama response: {json.dumps(data)[:800]}")
|
||
|
|
return content
|