Working on implementing and testing the /api/chat endpoint with tool calling support.
- Implemented
generate/5(non-streaming) andgenerate/6(streaming) - Created
generate_accumulator/3to accumulateresponsefield - Implemented
process_responses/6generic streaming processor with custom accumulator - Added proper error handling with status code checks for streaming
- Implemented
chat/5(non-streaming) - returns ResponseMessage with all fields from Result.message - Implemented
chat/6(streaming) - accumulates message fields throughout stream - Created sophisticated
chat_accumulator/3that:- Concatenates
contentandthinkingfields (strings) - Appends
imagesandtool_callsfields (lists) - Takes last value for
tool_name(scalar)
- Concatenates
- Initializes streaming with
message{role: "assistant", content: ""}to preserve tag - Returns complete message dict preserving all fields from stream
create_message/3andcreate_message/4- create message dicts with message tagretag/3in utils.pro - change dict tags while preserving all fields
- Unified streaming/non-streaming patterns using
Handler == false - Created
http_post_or_stream/4with proper error handling - Added
check_status/2for consistent HTTP error reporting - Fixed operator precedence issues (comma=1000, arrow=1050, semicolon=1100)
- Added status code checking for both streaming and non-streaming cases
- Basic streaming/non-streaming
- Suffix/infill with codegemma:2b
- JSON mode and structured outputs
- Raw mode, seed, temperature, system prompts
- Streaming with custom handler
- Multimodal with images (llama3.2-vision)
chat_non_streaming- basic non-streaming chatchat_streaming- basic streaming chat with ignore handlerchat_with_tools_streaming- streaming with tool calls (llama3.2)
- gemma3:270m (general)
- codegemma:2b (suffix/infill support)
- llama3.2 (tool calling)
- llama3.2-vision (multimodal)
- mistral-small3.2:24b (tool calling)
Created test/chat.pro:chat_with_tools_streaming which:
- Defines a
get_weathertool with JSON schema - Sends request to llama3.2 with tools parameter
- Verifies model returns tool_calls in response
- Checks tool call structure (function.name, function.arguments)
Key Learning: assertion/1 doesn't propagate variable bindings, so bindings must happen outside assertion calls.
Need to implement:
-
Tool Registration: A way to map tool names to Prolog predicates
- How should users register tools?
- Dynamic assertion? Dict of tool_name: predicate mappings?
-
Tool Execution: Execute tool calls and format results
- Parse tool_call from model response
- Call registered Prolog predicate with arguments
- Format result as message with
role: "tool",tool_name,content
-
Agent Loop: Multi-turn conversation with tool use
- Send initial message with tools
- Get tool_calls from model
- Execute tools
- Send results back
- Get final answer
-
Testing: Complete tool calling workflow test
- Register a mock
get_weathertool - Execute the tool call from model
- Send result back to model
- Verify final response incorporates tool result
- Register a mock
- Content/thinking: string concatenation
- Images/tool_calls: list append
- tool_name: take last value
- Based on API documentation analysis of streaming responses
- JSON parsing returns strings:
"assistant","user", etc. - Not atoms:
assistant,user
- Streaming and non-streaming both check status codes
- Use
check_status/2to throw descriptive errors - Error format:
error(http_error(Status, Response), context(_, Msg))
src/prolog/ollama.pro- Main API implementationsrc/prolog/utils.pro- Helper predicates (sha256, base64, retag)test/generate.pro- Generate endpoint teststest/chat.pro- Chat endpoint tests (in progress)docs/ollama-api.md- API referenceCLAUDE.MD- Instructions about professional tone, not inventing APIs
None currently blocking.
Implemented:
- ✅ /api/generate (streaming & non-streaming)
- ✅ /api/chat (streaming & non-streaming)
- ✅ /api/pull
- ✅ /api/push
- ✅ /api/create (including from safetensors/GGUF)
- ✅ /api/delete
- ✅ /api/copy
- ✅ /api/show
- ✅ /api/embed
- ✅ /api/ps (list running)
- ✅ /api/tags (list models)
- ✅ /api/version
- ✅ Blob operations
Not yet implemented:
- 🔄 Tool execution framework (in progress)
- Agent/multi-turn tool calling loops