[GH-ISSUE #9106] Return function result to AI by API. #5928

Closed
opened 2026-04-12 17:16:01 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @Jauhuei on GitHub (Feb 14, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/9106

{
  "model": "llama3.2",
  "created_at": "2024-07-22T20:33:28.123648Z",
  "message": {
    "role": "assistant",
    "content": "",
    "tool_calls": [
      {
        "function": {
          "name": "get_current_weather",
          "arguments": {
            "format": "celsius",
            "location": "Paris, FR"
          }
        }
      }
    ]
  },
  "done_reason": "stop",
  "done": true,
  "total_duration": 885095291,
  "load_duration": 3753500,
  "prompt_eval_count": 122,
  "prompt_eval_duration": 328493000,
  "eval_count": 33,
  "eval_duration": 552222000
}

I executed the get_current_weather function and received the following result:

{
  "city": "Paris, FR",
  "temperature": 20
}

How do I return this result to the AI?

Originally created by @Jauhuei on GitHub (Feb 14, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/9106 ``` { "model": "llama3.2", "created_at": "2024-07-22T20:33:28.123648Z", "message": { "role": "assistant", "content": "", "tool_calls": [ { "function": { "name": "get_current_weather", "arguments": { "format": "celsius", "location": "Paris, FR" } } } ] }, "done_reason": "stop", "done": true, "total_duration": 885095291, "load_duration": 3753500, "prompt_eval_count": 122, "prompt_eval_duration": 328493000, "eval_count": 33, "eval_duration": 552222000 } ``` I executed the `get_current_weather` function and received the following result: ``` { "city": "Paris, FR", "temperature": 20 } ``` How do I return this result to the AI?
Author
Owner

@Jauhuei commented on GitHub (Feb 14, 2025):

For example:

I want to use Ollama to create an AI.

When I ask the AI through C# if I can wear a short-sleeve shirt tomorrow, the AI responds by making a Tool_Calls request to check the weather temperature.

C# responds to the AI with the weather temperature.

Then, the AI answers that it's okay to wear a short-sleeve shirt.

<!-- gh-comment-id:2659396608 --> @Jauhuei commented on GitHub (Feb 14, 2025): For example: I want to use Ollama to create an AI. When I ask the AI through C# if I can wear a short-sleeve shirt tomorrow, the AI responds by making a Tool_Calls request to check the weather temperature. C# responds to the AI with the weather temperature. Then, the AI answers that it's okay to wear a short-sleeve shirt.
Author
Owner

@rick-github commented on GitHub (Feb 14, 2025):

By sending the result back as a tool role:

tool_response = execute_tool();  // {"city": "Paris, FR","temperature": 20}
messages.Add(new Dictionary<string, string>(){
    { "role", "tool" },
    { "content", tool_response }
});

An example here is in python but the logic is the same, the data structure that is used in C# to track the messages just gets the tool call result appended to it and is sent to the LLM to generate the answer to the question.

<!-- gh-comment-id:2659563204 --> @rick-github commented on GitHub (Feb 14, 2025): By sending the result back as a tool role: ```C# tool_response = execute_tool(); // {"city": "Paris, FR","temperature": 20} messages.Add(new Dictionary<string, string>(){ { "role", "tool" }, { "content", tool_response } }); ``` An example [here](https://github.com/ollama/ollama-python/blob/main/examples/tools.py) is in python but the logic is the same, the data structure that is used in C# to track the messages just gets the tool call result appended to it and is sent to the LLM to generate the answer to the question.
Author
Owner

@Jauhuei commented on GitHub (Feb 15, 2025):

{
    "model": "llama3.2:3b",
    "messages": [
        {
            "content": "What is the temperature in Suzhou and Beijing today?",
            "role": "user"
        }
    ],
    "stream": false,
    "tools": [
        {
            "type": "function",
            "description": "Get the weather information for the specified city",
            "name": "getWeather",
            "parameters": {
                "properties": {
                    "city": {
                        "description": "The name of the city for which to query the weather (only one city name is allowed)",
                        "type": "string"
                    }
                },
                "required": [
                    "city"
                ],
                "type": "object"
            }
        }
    ]
}

The AI didn't call the method according to the format. Regardless of how smart the AI is, I always hope it can at least follow the document format.
The content in the tool response should also follow a more standardized format, so that the AI can understand the results after it makes the call.

{
    "model": "llama3.2:3b",
    "created_at": "2025-02-15T14:15:27.003837Z",
    "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
            {
                "function": {
                    "name": "wikipedia.page",
                    "arguments": {
                        "id": "Suzhou, China" // error
                    }
                }
            },
            {
                "function": {
                    "name": "wikipedia.page",
                    "arguments": {
                        "id": "Beijing, China"
                    }
                }
            }
        ]
    },
    "done_reason": "stop",
    "done": true,
    "total_duration": 6565786000,
    "load_duration": 3210631000,
    "prompt_eval_count": 143,
    "prompt_eval_duration": 1822000000,
    "eval_count": 35,
    "eval_duration": 1531000000
}
<!-- gh-comment-id:2660944860 --> @Jauhuei commented on GitHub (Feb 15, 2025): ``` { "model": "llama3.2:3b", "messages": [ { "content": "What is the temperature in Suzhou and Beijing today?", "role": "user" } ], "stream": false, "tools": [ { "type": "function", "description": "Get the weather information for the specified city", "name": "getWeather", "parameters": { "properties": { "city": { "description": "The name of the city for which to query the weather (only one city name is allowed)", "type": "string" } }, "required": [ "city" ], "type": "object" } } ] } ``` The AI didn't call the method according to the format. Regardless of how smart the AI is, I always hope it can at least follow the document format. The content in the tool response should also follow a more standardized format, so that the AI can understand the results after it makes the call. ``` { "model": "llama3.2:3b", "created_at": "2025-02-15T14:15:27.003837Z", "message": { "role": "assistant", "content": "", "tool_calls": [ { "function": { "name": "wikipedia.page", "arguments": { "id": "Suzhou, China" // error } } }, { "function": { "name": "wikipedia.page", "arguments": { "id": "Beijing, China" } } } ] }, "done_reason": "stop", "done": true, "total_duration": 6565786000, "load_duration": 3210631000, "prompt_eval_count": 143, "prompt_eval_duration": 1822000000, "eval_count": 35, "eval_duration": 1531000000 } ```
Author
Owner

@rick-github commented on GitHub (Feb 15, 2025):

It helps if the tool is formatted a bit better.

$ echo '{
  "model": "llama3.2:3b",
  "messages": [
    {
      "content": "What is the temperature in Suzhou and Beijing today?",
      "role": "user"
    }
  ],
  "stream": false,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "getWeather",
        "description": "Get the weather for a city",
        "parameters": {
          "type": "object",
          "required": [
            "city"
          ],
          "properties": {
            "city": {
              "type": "string",
              "description": "The name of the city for which to query the weather (only one city name is allowed)"
            }
          }
        }
      }
    }
  ]
}' | curl -s localhost:11434/api/chat -d@- | jq

{
  "model": "llama3.2:3b",
  "created_at": "2025-02-15T16:25:02.199693357Z",
  "message": {
    "role": "assistant",
    "content": "",
    "tool_calls": [
      {
        "function": {
          "name": "getWeather",
          "arguments": {
            "city": "Suzhou"
          }
        }
      },
      {
        "function": {
          "name": "getWeather",
          "arguments": {
            "city": "Beijing"
          }
        }
      }
    ]
  },
  "done_reason": "stop",
  "done": true,
  "total_duration": 460059385,
  "load_duration": 199528465,
  "prompt_eval_count": 175,
  "prompt_eval_duration": 9000000,
  "eval_count": 37,
  "eval_duration": 250000000
}
<!-- gh-comment-id:2660992952 --> @rick-github commented on GitHub (Feb 15, 2025): It helps if the tool is formatted a bit better. ```sh $ echo '{ "model": "llama3.2:3b", "messages": [ { "content": "What is the temperature in Suzhou and Beijing today?", "role": "user" } ], "stream": false, "tools": [ { "type": "function", "function": { "name": "getWeather", "description": "Get the weather for a city", "parameters": { "type": "object", "required": [ "city" ], "properties": { "city": { "type": "string", "description": "The name of the city for which to query the weather (only one city name is allowed)" } } } } } ] }' | curl -s localhost:11434/api/chat -d@- | jq ``` ```console { "model": "llama3.2:3b", "created_at": "2025-02-15T16:25:02.199693357Z", "message": { "role": "assistant", "content": "", "tool_calls": [ { "function": { "name": "getWeather", "arguments": { "city": "Suzhou" } } }, { "function": { "name": "getWeather", "arguments": { "city": "Beijing" } } } ] }, "done_reason": "stop", "done": true, "total_duration": 460059385, "load_duration": 199528465, "prompt_eval_count": 175, "prompt_eval_duration": 9000000, "eval_count": 37, "eval_duration": 250000000 } ```
Author
Owner

@Jauhuei commented on GitHub (Feb 16, 2025):

@rick-github
Thank you, with your format, AI can indeed successfully query the weather for Suzhou and Beijing. Now, how can I correctly tell it the temperatures for Suzhou and Beijing in two separate responses? According to the previous answerer's suggestion, should I tell the AI like this?

{
    "model": "llama3.2:3b",
    "messages": [
        {
            "content": "What is the temperature in Suzhou and Beijing today?",
            "role": "user"
        },
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "function": {
                        "name": "getWeather",
                        "arguments": {
                            "city": "Suzhou"
                        }
                    }
                },
                {
                    "function": {
                        "name": "getWeather",
                        "arguments": {
                            "city": "Beijing"
                        }
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": "{\"city\":\"Suzhou\", \"temperature\":20}",
            "name": "getWeather"
        },
        {
            "role": "tool",
            "content": "{\"city\":\"Beijing\", \"temperature\":15}",
            "name": "getWeather"
        }
    ],
    "stream": false,
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "getWeather",
                "description": "Get the weather for a city",
                "parameters": {
                    "type": "object",
                    "required": [
                        "city"
                    ],
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "The name of the city for which to query the weather (only one city name is allowed)"
                        }
                    }
                }
            }
        }
    ]
}
<!-- gh-comment-id:2661223537 --> @Jauhuei commented on GitHub (Feb 16, 2025): @rick-github Thank you, with your format, AI can indeed successfully query the weather for Suzhou and Beijing. Now, how can I correctly tell it the temperatures for Suzhou and Beijing in two separate responses? According to the previous answerer's suggestion, should I tell the AI like this? ``` { "model": "llama3.2:3b", "messages": [ { "content": "What is the temperature in Suzhou and Beijing today?", "role": "user" }, { "role": "assistant", "content": "", "tool_calls": [ { "function": { "name": "getWeather", "arguments": { "city": "Suzhou" } } }, { "function": { "name": "getWeather", "arguments": { "city": "Beijing" } } } ] }, { "role": "tool", "content": "{\"city\":\"Suzhou\", \"temperature\":20}", "name": "getWeather" }, { "role": "tool", "content": "{\"city\":\"Beijing\", \"temperature\":15}", "name": "getWeather" } ], "stream": false, "tools": [ { "type": "function", "function": { "name": "getWeather", "description": "Get the weather for a city", "parameters": { "type": "object", "required": [ "city" ], "properties": { "city": { "type": "string", "description": "The name of the city for which to query the weather (only one city name is allowed)" } } } } } ] } ```
Author
Owner

@rick-github commented on GitHub (Feb 16, 2025):

Yes.

<!-- gh-comment-id:2661439701 --> @rick-github commented on GitHub (Feb 16, 2025): Yes.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5928