Using ollama tools via curl
Now that Ollama support tools, you can use them via curl. Here is an example of how to use the get_current_weather
tool to get the current weather for a location.
Ollama API
curl http://localhost:11434/api/chat -s -d '{
"model": "hermes3",
"messages": [
{
"role": "user",
"content": "What is the weather today in Paris?"
}
],
"stream": false,
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location", "format"]
}
}
}
]
}' | jq
Response:
{
"model": "hermes3",
"created_at": "2024-08-31T03:34:23.352387Z",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "get_current_weather",
"arguments": {
"format": "celsius",
"location": "Paris"
}
}
}
]
},
"done_reason": "stop",
"done": true,
"total_duration": 635131667,
"load_duration": 27051750,
"prompt_eval_count": 286,
"prompt_eval_duration": 146976000,
"eval_count": 29,
"eval_duration": 460009000
}
Open AI Compatible API
curl -X POST http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ollama" \
-s -d '{
"messages": [
{
"content": "What is the weather in Toronto?",
"role": "user"
}
],
"model": "llama3.1",
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location", "format"]
}
}
}
]
}' | jq
Response:
{
"id": "chatcmpl-400",
"object": "chat.completion",
"created": 1725078657,
"model": "llama3.1",
"system_fingerprint": "fp_ollama",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_bl91domx",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\"format\":\"celsius\",\"location\":\"Toronto\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 203,
"completion_tokens": 25,
"total_tokens": 228
}
}