Anchor empty tool-call turns
Authored by
mfwolffe <wolffemf@dukes.jmu.edu>
- SHA
d22c38c98717bbf5b95f853226b2fa91256df0ea- Parents
-
0310078 - Tree
018f4d0
d22c38c
d22c38c98717bbf5b95f853226b2fa91256df0ea0310078
018f4d0| Status | File | + | - |
|---|---|---|---|
| M |
src/loader/llm/ollama.py
|
11 | 1 |
| M |
tests/test_ollama_backend.py
|
31 | 1 |
src/loader/llm/ollama.pymodified@@ -287,9 +287,19 @@ class OllamaBackend(LLMBackend): | ||
| 287 | 287 | """ |
| 288 | 288 | formatted = [] |
| 289 | 289 | for message in messages: |
| 290 | + content = message.content | |
| 291 | + if ( | |
| 292 | + message.role.value == "assistant" | |
| 293 | + and message.tool_calls | |
| 294 | + and not content.strip() | |
| 295 | + ): | |
| 296 | + tool_names = ", ".join(tc.name for tc in message.tool_calls[:3]) | |
| 297 | + if len(message.tool_calls) > 3: | |
| 298 | + tool_names += ", ..." | |
| 299 | + content = f"Calling tools: {tool_names}." | |
| 290 | 300 | entry: dict[str, Any] = { |
| 291 | 301 | "role": message.role.value, |
| 292 | - "content": message.content, | |
| 302 | + "content": content, | |
| 293 | 303 | } |
| 294 | 304 | if message.tool_calls: |
| 295 | 305 | entry["tool_calls"] = [ |
tests/test_ollama_backend.pymodified@@ -20,7 +20,7 @@ except ModuleNotFoundError: | ||
| 20 | 20 | |
| 21 | 21 | sys.modules["httpx"] = types.SimpleNamespace(AsyncClient=_AsyncClientStub) |
| 22 | 22 | |
| 23 | -from loader.llm.base import StreamChunk | |
| 23 | +from loader.llm.base import Message, Role, StreamChunk, ToolCall | |
| 24 | 24 | from loader.llm.ollama import OllamaBackend |
| 25 | 25 | |
| 26 | 26 | |
@@ -65,6 +65,36 @@ class FakeStreamResponse: | ||
| 65 | 65 | yield json.dumps(payload) |
| 66 | 66 | |
| 67 | 67 | |
| 68 | +def test_ollama_format_messages_adds_anchor_for_empty_tool_call_content() -> None: | |
| 69 | + backend = OllamaBackend() | |
| 70 | + | |
| 71 | + formatted = backend._format_messages( | |
| 72 | + [ | |
| 73 | + Message( | |
| 74 | + role=Role.ASSISTANT, | |
| 75 | + content="", | |
| 76 | + tool_calls=[ | |
| 77 | + ToolCall( | |
| 78 | + id="write-1", | |
| 79 | + name="write", | |
| 80 | + arguments={"file_path": "index.html", "content": "..."}, | |
| 81 | + ) | |
| 82 | + ], | |
| 83 | + ) | |
| 84 | + ] | |
| 85 | + ) | |
| 86 | + | |
| 87 | + assert formatted[0]["content"] == "Calling tools: write." | |
| 88 | + assert formatted[0]["tool_calls"] == [ | |
| 89 | + { | |
| 90 | + "function": { | |
| 91 | + "name": "write", | |
| 92 | + "arguments": {"file_path": "index.html", "content": "..."}, | |
| 93 | + } | |
| 94 | + } | |
| 95 | + ] | |
| 96 | + | |
| 97 | + | |
| 68 | 98 | @pytest.mark.asyncio |
| 69 | 99 | async def test_ollama_complete_uses_shared_parser_with_allowed_tool_names() -> None: |
| 70 | 100 | backend = OllamaBackend() |