Using tools with ollama #42
-
|
Hello, I am doing some tests with your framework and ollama, and I don't seem to be able to properly use tools. I have a simple agent supposed to call a simple tool: <?php
class ChatWithToolsAgent extends Agent
{
public function __construct(private readonly string $model)
{
}
protected function provider(): AIProviderInterface
{
return new Ollama(
url: 'http://localhost:11434/api',
model: $this->model,
);
}
public function instructions(): string
{
return (new SystemPrompt(
background: ["You are a playful AI Agent."],
steps: [
"When asked to clap your hands use the provided tool.",
],
))->__tostring();
}
protected function tools(): array
{
return [
Tool::make(
'clap_hands',
'Clap your hands.',
)->setCallable(function () {
echo "CLAPPING MY HANDS!";
return "CLAPPING MY HANDS!";
})
];
}
}When using several "tool-aware" models I get these replies: I see the models "understand" the tool and try to use it, but the tool callable is never called. Am I doing it wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 17 replies
-
|
My ollama was not up-to-date, after an upgrade to the latest version I made some progresses. There are problems with the neuron-ai ollama provider:
To make tools works the following changes are required: But after fixing that, problems remain. What happens when chatting with the agent is this:
This causes the next chat request payload to be duplicated and to contain the following invalid messages. {
"tool_calls": [
{
"function": {
"name": "get_time_and_date",
"arguments": []
}
}
],
"role": "assistant",
"content": ""
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "",
"content": "2025-04-25T09:43:09+00:00"
}
]
}And this fails because ollama does not know how to unserialize this. If these wrong messages are removed from the payload, since we re-send the original request, ollama will repond with another tool_call which will cause an infinite loop of tool calls/re-request to ollama. Could you please provide more information on how this could be fixed. I'm willing to help in the implementation. |
Beta Was this translation helpful? Give feedback.
-
|
I'm experiencing the same issue: The exact error (after adding the tools): |
Beta Was this translation helpful? Give feedback.
I fixed the mapping of empty arguments, and added a test to cover this case: https://github.com/inspector-apm/neuron-ai/blob/main/tests/Providers/OllamaMessageMapperTest.php#L12
Release 1.9.16