-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathollama.py
More file actions
187 lines (166 loc) · 6.41 KB
/
Copy pathollama.py
File metadata and controls
187 lines (166 loc) · 6.41 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
import json
import os
import urllib.error
import urllib.request
from typing import Any, Dict, List, Optional
OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama2:latest")
OLLAMA_REQUEST_TIMEOUT_SECONDS = int(os.getenv("OLLAMA_REQUEST_TIMEOUT_SECONDS", "120"))
PREFERRED_CHAT_MODELS = (
"llama2:latest",
"llama3.2:1b",
"llama3.2",
"llama3.1",
"llama2",
)
def ollama_is_available(base_url: str = OLLAMA_BASE_URL) -> bool:
try:
request = urllib.request.Request(f"{base_url}/api/tags", method="GET")
with urllib.request.urlopen(request, timeout=3) as response:
return response.status == 200
except Exception:
return False
def list_local_models(base_url: str = OLLAMA_BASE_URL) -> List[str]:
try:
request = urllib.request.Request(f"{base_url}/api/tags", method="GET")
with urllib.request.urlopen(request, timeout=3) as response:
payload = json.loads(response.read().decode("utf-8"))
models = payload.get("models", [])
names = []
for model in models:
name = model.get("name")
if name:
names.append(str(name))
return names
except Exception:
return []
def resolve_chat_model(requested_model: Optional[str] = None, base_url: str = OLLAMA_BASE_URL) -> str:
if requested_model:
return requested_model
# Honor an explicit environment-configured `OLLAMA_MODEL` as the primary choice.
# This forces usage of the configured model (e.g., 'llama2:latest') unless a
# `requested_model` is explicitly passed by the caller.
if OLLAMA_MODEL:
return OLLAMA_MODEL
available = list_local_models(base_url)
for preferred in PREFERRED_CHAT_MODELS:
if preferred in available:
return preferred
if available:
return available[0]
return OLLAMA_MODEL
def _post_json(url: str, payload: Dict[str, object]) -> Dict[str, object]:
data = json.dumps(payload).encode("utf-8")
request = urllib.request.Request(
url,
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=OLLAMA_REQUEST_TIMEOUT_SECONDS) as response:
return json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as he:
# Try to read the response body to surface server-side validation errors
try:
body = he.read().decode("utf-8")
except Exception:
body = "<unable to read response body>"
raise RuntimeError(f"HTTP {he.code} {he.reason}: {body}") from he
def chat_completion_stream(
messages: List[Dict[str, str]],
model: Optional[str] = None,
*,
tools: Optional[List[Dict[str, Any]]] = None,
options: Optional[Dict[str, Any]] = None,
base_url: str = OLLAMA_BASE_URL,
):
"""Create a streaming chat completion generator that yields text chunks.
This performs a POST with `stream=True` and yields bytes as decoded
UTF-8 fragments. Consumers should provide a callback to consume chunks
and assemble the final assistant reply.
"""
resolved_model = resolve_chat_model(model, base_url)
payload: Dict[str, Any] = {
"model": resolved_model,
"messages": messages,
"stream": True,
}
if tools:
payload["tools"] = tools
if options:
payload["options"] = options
data = json.dumps(payload).encode("utf-8")
request = urllib.request.Request(
f"{base_url}/api/chat",
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=OLLAMA_REQUEST_TIMEOUT_SECONDS) as response:
# Ollama streams newline-delimited JSON objects. Yield only the
# assistant text fragments so callers do not render raw payloads.
while True:
line = response.readline()
if not line:
break
try:
text = line.decode("utf-8")
except Exception:
text = line.decode("utf-8", errors="ignore")
cleaned = text.strip()
if not cleaned:
continue
try:
payload = json.loads(cleaned)
except json.JSONDecodeError:
yield cleaned
continue
message = payload.get("message", {}) if isinstance(payload, dict) else {}
content = str(message.get("content", "") or "")
if content:
yield content
except urllib.error.HTTPError as he:
try:
body = he.read().decode("utf-8")
except Exception:
body = "<unable to read response body>"
raise RuntimeError(f"HTTP {he.code} {he.reason}: {body}") from he
def chat_completion(
messages: List[Dict[str, str]],
model: Optional[str] = None,
*,
tools: Optional[List[Dict[str, Any]]] = None,
options: Optional[Dict[str, Any]] = None,
base_url: str = OLLAMA_BASE_URL,
) -> Dict[str, Any]:
"""Send a chat completion request to Ollama.
This shared helper lets the interview flow and the agent orchestrator use
the same transport while optionally enabling tool calling.
"""
resolved_model = resolve_chat_model(model, base_url)
# Some local Ollama models (for example: llama2:latest) do not support the
# `tools` parameter. If a tools list is provided and the resolved model is
# known to not support tools, try to fall back to another available model
# that may support function/tool calling.
if tools and resolved_model and "llama2" in resolved_model:
try:
available = list_local_models(base_url)
for pref in PREFERRED_CHAT_MODELS:
if pref != resolved_model and pref in available:
resolved_model = pref
break
except Exception:
# If listing models fails, continue with the originally resolved model.
pass
payload: Dict[str, Any] = {
"model": resolved_model,
"messages": messages,
"stream": False,
}
if tools:
payload["tools"] = tools
if options:
payload["options"] = options
return _post_json(f"{base_url}/api/chat", payload)