Open
Conversation
Add tool calling support to the Ollama provider, enabling models like Gemma 4 to use structured tool calls through the Ollama chat API. Changes: - Add Tool, ToolCall, ToolCallFunction types to ollamaclient with custom JSON marshaling (Ollama sends arguments as object, not string) - Add Tools field to ChatRequest and ToolCalls to Message - Handle ToolCall and ToolCallResponse content parts in message conversion - Convert response tool_calls back to llms.ToolCall - Disable streaming when tools are present (Ollama limitation) - Preserve tool_calls through the response callback Signed-off-by: majiayu000 <1835304752@qq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
memory: add interfaces for X, Yorutil: add whizzbang helpers).Fixes #123).golangci-lintchecks.Fixes #1490
Adds native tool calling to the Ollama provider so models like Gemma 4 can use structured tool calls through the Ollama chat API.
What changed:
types.go- AddedTool,ToolFunction,ToolCall, andToolCallFunctionstructs matching Ollama's API format.ToolCallFunctionhas custom JSON unmarshaling since Ollama returns arguments as an object butllms.FunctionCallexpects a string. AddedToolsfield toChatRequestandToolCallsfield toMessage.ollamallm.go- Extracted message construction intomakeOllamaMessagesto handleToolCallandToolCallResponsecontent parts (tool results getrole=tool). AddedmakeOllamaToolsto convertllms.Tooltoollamaclient.Tool. Response handling now convertsToolCallresponses back tollms.ToolCall. Streaming is disabled when tools are present since Ollama doesn't support it.ollama_test.go- AddedTestToolCallwith httprr recording covering the full flow: send message with tool defs, get tool call back, send tool result, get final text response.Differs from the earlier #1022 attempt in a few ways: tool responses use
role=toolcorrectly (users reported the model wasn't seeing responses in #1022), message construction is split out to keep complexity down, and tools are passed viallms.WithTools()instead of a provider-specific option.