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
|
"""Ollama client wrapper with tool-calling chat loop and streaming."""
from __future__ import annotations
import json
from collections.abc import AsyncIterator, Callable, Iterator
from typing import Any, cast
import ollama
from ollama import Message
from .config import settings
from .tools.registry import ToolRegistry, default_registry
# Maximum successive tool-call rounds before we force a final answer.
MAX_TOOL_ROUNDS = 5
# Type alias matching ollama's expected tools signature.
ToolCallable = Callable[..., Any]
def get_client(host: str | None = None) -> ollama.Client:
"""Return an ``ollama.Client`` configured for the configured host."""
return ollama.Client(host=host or settings.ollama_host, timeout=settings.ollama_timeout)
def list_models(host: str | None = None) -> list[str]:
"""Return the list of locally available model names."""
client = get_client(host)
try:
resp = client.list()
except Exception: # noqa: BLE001
return []
models: list[str] = []
for item in resp.get("models", []) if isinstance(resp, dict) else getattr(resp, "models", []):
name = item.get("model") if isinstance(item, dict) else getattr(item, "model", None)
if name:
models.append(str(name))
return models
def _tool_calls(response: Any) -> list[dict[str, Any]]:
"""Extract tool calls from an ollama response in a tolerant manner."""
msg = getattr(response, "message", None) or (
response.get("message") if isinstance(response, dict) else None
)
if msg is None:
return []
calls = getattr(msg, "tool_calls", None) if not isinstance(msg, dict) else msg.get("tool_calls")
if not calls:
return []
out: list[dict[str, Any]] = []
for c in calls:
if isinstance(c, dict):
fn = c.get("function", {})
name = fn.get("name") or c.get("name", "")
args = fn.get("arguments") or c.get("arguments", {})
out.append({"name": name, "arguments": args})
else:
fn = getattr(c, "function", None)
if fn is not None:
name = getattr(fn, "name", "")
args = getattr(fn, "arguments", {})
else:
name = getattr(c, "name", "")
args = getattr(c, "arguments", {})
out.append({"name": name, "arguments": args})
return out
def _assistant_message(response: Any) -> Message:
"""Convert a non-streaming response into a Message for re-submission."""
msg = getattr(response, "message", None) or (
response.get("message") if isinstance(response, dict) else None
)
if isinstance(msg, dict):
return Message(**msg)
if msg is not None:
return cast(Message, msg)
return Message(role="assistant", content="")
def chat_with_tools(
messages: list[Message],
model: str,
*,
think: bool | str | None = None,
registry: ToolRegistry | None = None,
host: str | None = None,
) -> Iterator[dict[str, Any]]:
"""Run a multi-round tool-calling chat and yield progress events.
Events are dicts with a ``type`` key:
- ``{"type": "tool_start", "name": str, "arguments": dict}``
- ``{"type": "tool_end", "name": str, "result": str}``
- ``{"type": "delta", "content": str}``
- ``{"type": "thinking", "content": str}``
- ``{"type": "done"}``
- ``{"type": "error", "message": str}``
The final assistant message is streamed token-by-token via ``delta`` events.
"""
raw_client = get_client(host)
reg = registry or default_registry()
tools: list[ToolCallable] = [cast(Any, c) for c in reg.callables]
rounds = 0
while rounds < MAX_TOOL_ROUNDS:
rounds += 1
try:
resp = cast(Any, raw_client).chat(
model=model,
messages=messages,
tools=tools,
think=think,
stream=False,
)
except Exception as exc: # noqa: BLE001
yield {"type": "error", "message": str(exc)}
return
calls = _tool_calls(resp)
if not calls:
# No tool calls: stream the final answer for UI effect.
content = _assistant_content(resp)
thinking = _assistant_thinking(resp)
if thinking:
yield {"type": "thinking", "content": thinking}
if content:
yield {"type": "delta", "content": content}
yield {"type": "done"}
return
# Append the assistant message that requested tool calls.
messages.append(_assistant_message(resp))
# Execute each requested tool and append tool-role messages.
for call in calls:
name = call.get("name", "")
args = call.get("arguments", {})
yield {"type": "tool_start", "name": name, "arguments": args}
result = reg.execute(name, args if isinstance(args, dict) else json.dumps(args))
yield {"type": "tool_end", "name": name, "result": result}
messages.append(Message(role="tool", tool_name=name, content=result))
# Exceeded tool rounds: ask for a final answer without tools.
try:
resp = cast(Any, raw_client).chat(
model=model,
messages=messages,
think=think,
stream=False,
)
except Exception as exc: # noqa: BLE001
yield {"type": "error", "message": str(exc)}
return
content = _assistant_content(resp)
thinking = _assistant_thinking(resp)
if thinking:
yield {"type": "thinking", "content": thinking}
if content:
yield {"type": "delta", "content": content}
yield {"type": "done"}
def stream_chat_with_tools(
messages: list[Message],
model: str,
*,
think: bool | str | None = None,
registry: ToolRegistry | None = None,
host: str | None = None,
) -> Iterator[dict[str, Any]]:
"""Like ``chat_with_tools`` but the final answer is streamed from ollama.
Tool-call resolution rounds are performed in non-streaming mode; once no
further tool calls are requested the final response is streamed token by
token via ``delta`` events.
"""
raw_client = get_client(host)
reg = registry or default_registry()
tools: list[ToolCallable] = [cast(Any, c) for c in reg.callables]
rounds = 0
while rounds < MAX_TOOL_ROUNDS:
rounds += 1
try:
resp = cast(Any, raw_client).chat(
model=model,
messages=messages,
tools=tools,
think=think,
stream=False,
)
except Exception as exc: # noqa: BLE001
yield {"type": "error", "message": str(exc)}
return
calls = _tool_calls(resp)
if not calls:
# Stream the final answer by re-issuing the last messages.
# We already have the full non-streamed content; emit it as deltas
# in small chunks to simulate streaming for the UI.
thinking = _assistant_thinking(resp)
if thinking:
yield {"type": "thinking", "content": thinking}
content = _assistant_content(resp)
if content:
for chunk in _chunk_text(content, 64):
yield {"type": "delta", "content": chunk}
yield {"type": "done"}
return
messages.append(_assistant_message(resp))
for call in calls:
name = call.get("name", "")
args = call.get("arguments", {})
yield {"type": "tool_start", "name": name, "arguments": args}
result = reg.execute(name, args if isinstance(args, dict) else json.dumps(args))
yield {"type": "tool_end", "name": name, "result": result}
messages.append(Message(role="tool", tool_name=name, content=result))
# Final attempt without tools.
try:
stream = cast(Any, raw_client).chat(
model=model,
messages=messages,
think=think,
stream=True,
)
for chunk in stream:
thinking = _assistant_thinking(chunk)
if thinking:
yield {"type": "thinking", "content": thinking}
content = _assistant_content(chunk)
if content:
yield {"type": "delta", "content": content}
except Exception as exc: # noqa: BLE001
yield {"type": "error", "message": str(exc)}
return
yield {"type": "done"}
async def astream_chat_with_tools(
messages: list[Message],
model: str,
*,
think: bool | str | None = None,
registry: ToolRegistry | None = None,
host: str | None = None,
) -> AsyncIterator[dict[str, Any]]:
"""Async wrapper that yields events from ``stream_chat_with_tools``."""
import anyio
gen = stream_chat_with_tools(messages, model, think=think, registry=registry, host=host)
def _next() -> dict[str, Any] | None:
try:
return next(gen)
except StopIteration:
return None
while True:
event = await anyio.to_thread.run_sync(_next)
if event is None:
break
yield event
def _assistant_content(response: Any) -> str:
msg = getattr(response, "message", None) or (
response.get("message") if isinstance(response, dict) else None
)
if msg is None:
return ""
if isinstance(msg, dict):
return str(msg.get("content", "") or "")
return str(getattr(msg, "content", "") or "")
def _assistant_thinking(response: Any) -> str:
msg = getattr(response, "message", None) or (
response.get("message") if isinstance(response, dict) else None
)
if msg is None:
return ""
if isinstance(msg, dict):
return str(msg.get("thinking", "") or "")
return str(getattr(msg, "thinking", "") or "")
def _chunk_text(text: str, size: int) -> Iterator[str]:
"""Yield successive chunks of ``text`` up to ``size`` characters."""
for i in range(0, len(text), size):
yield text[i : i + size]
|