Calling chat with function as tool gives an empty response #173

Closed
opened 2026-02-15 16:28:29 -05:00 by yindo · 5 comments
Owner

Originally created by @mjvmiller on GitHub (Nov 27, 2024).

I defined a test function as:

def alfalfa(a, b):
    if a % 2 == 0 and b % 2 == 0:
        return a + b
    elif (a % 2 != 0) and (b % 2 != 0):
        return a * b
    else:
        return 72340

I initialize it as a tool using:

alftool = {
      'type': 'function',
      'function': {
        'name': 'alfalfa',
        'description': 'Returns the alfalfa of two numbers',
        'parameters': {
          'type': 'object',
          'properties': {
            'a': {
              'type': 'integer',
              'description': 'First number to alfalfa',
            },
            'b': {
                'type': 'integer',
                'description': 'Second number to alfalfa',
            }
          },
          'required': ['a', 'b'],
        },
      },
    }

Finally, using oclient = ollama.Client(host="localhost"), I call:

response = oclient.chat(
    model='llama3.2:3b-instruct-q8_0',
    messages=[{'role': 'user', 'content':
        'Please alfalfa 2 and 4'}],
    tools=[alfalfa]
)

print(response['message'])
print(response.message.content)

The latter prints nothing, while the former prints role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='alfalfa', arguments={'a': '2', 'b': '4'}))]. When I omit the tools=..., I get a response as normal:

Alfalfa doesn't actually mean "to add" or perform mathematical operations. Alfalfa is a type of leguminous herb that is commonly used as hay for livestock.

If you meant to ask me to calculate the result of 2 + 4, I'd be happy to help!

Here's the answer:

2 + 4 = 6

I had similar results with other functions tested. This is on the latest version (v0.4.1).

Originally created by @mjvmiller on GitHub (Nov 27, 2024). I defined a test function as: ``` def alfalfa(a, b): if a % 2 == 0 and b % 2 == 0: return a + b elif (a % 2 != 0) and (b % 2 != 0): return a * b else: return 72340 ``` I initialize it as a tool using: ``` alftool = { 'type': 'function', 'function': { 'name': 'alfalfa', 'description': 'Returns the alfalfa of two numbers', 'parameters': { 'type': 'object', 'properties': { 'a': { 'type': 'integer', 'description': 'First number to alfalfa', }, 'b': { 'type': 'integer', 'description': 'Second number to alfalfa', } }, 'required': ['a', 'b'], }, }, } ``` Finally, using `oclient = ollama.Client(host="localhost")`, I call: ``` response = oclient.chat( model='llama3.2:3b-instruct-q8_0', messages=[{'role': 'user', 'content': 'Please alfalfa 2 and 4'}], tools=[alfalfa] ) print(response['message']) print(response.message.content) ``` The latter prints nothing, while the former prints `role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='alfalfa', arguments={'a': '2', 'b': '4'}))]`. When I omit the `tools=...`, I get a response as normal: ``` Alfalfa doesn't actually mean "to add" or perform mathematical operations. Alfalfa is a type of leguminous herb that is commonly used as hay for livestock. If you meant to ask me to calculate the result of 2 + 4, I'd be happy to help! Here's the answer: 2 + 4 = 6 ``` I had similar results with other functions tested. This is on the latest version (v0.4.1).
yindo closed this issue 2026-02-15 16:28:29 -05:00
Author
Owner

@ameen-91 commented on GitHub (Nov 29, 2024):

Its the expected behaviour. In [ToolCall(function=Function(name='alfalfa', arguments={'a': '2', 'b': '4'}))], the tool is being called with the correct parameters.

As for the json definition, I can get the output:

alftool = {
      'type': 'function',
      'function': {
        'name': 'alfalfa',
        'description': 'Returns the alfalfa of two numbers',
        'parameters': {
          'type': 'object',
          'properties': {
            'a': {
              'type': 'integer',
              'description': 'First number to alfalfa',
            },
            'b': {
                'type': 'integer',
                'description': 'Second number to alfalfa',
            }
          },
          'required': ['a', 'b'],
        },
      },
    }

response = ollama.chat(
    model='llama3.1:8b-instruct-q4_0',
    messages=[{'role': 'user', 'content':
        'Please alfalfa 2 and 4'}],
    tools=[alftool]
)

print(response['message'])

Output:

role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='alfalfa', arguments={'a': 2, 'b': 4}))]
@ameen-91 commented on GitHub (Nov 29, 2024): Its the expected behaviour. In `[ToolCall(function=Function(name='alfalfa', arguments={'a': '2', 'b': '4'}))]`, the tool is being called with the correct parameters. As for the json definition, I can get the output: ```python alftool = { 'type': 'function', 'function': { 'name': 'alfalfa', 'description': 'Returns the alfalfa of two numbers', 'parameters': { 'type': 'object', 'properties': { 'a': { 'type': 'integer', 'description': 'First number to alfalfa', }, 'b': { 'type': 'integer', 'description': 'Second number to alfalfa', } }, 'required': ['a', 'b'], }, }, } response = ollama.chat( model='llama3.1:8b-instruct-q4_0', messages=[{'role': 'user', 'content': 'Please alfalfa 2 and 4'}], tools=[alftool] ) print(response['message']) ``` Output: ```python role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='alfalfa', arguments={'a': 2, 'b': 4}))] ```
Author
Owner

@mjvmiller commented on GitHub (Nov 29, 2024):

Just to clarify, the expected behavior of asking the model a question is to receive no answer? I can see this behaviour being useful for cases such as asking the model to turn on/off a setting in an app, but does the current state of the api allow for the model to use tools to inform its response to the user?

@mjvmiller commented on GitHub (Nov 29, 2024): Just to clarify, the expected behavior of asking the model a question is to receive no answer? I can see this behaviour being useful for cases such as asking the model to turn on/off a setting in an app, but does the current state of the api allow for the model to use tools to inform its response to the user?
Author
Owner

@ParthSareen commented on GitHub (Nov 29, 2024):

@mjm9189 Hey! So the initial call to the LLM just generates a toolcall - which is what you need to use to execute your function with the provided params. You can pass the output of the tool back to the LLM and generate a response for the user. There's an example in the JS repo for this - will add one for python soon too! https://github.com/ollama/ollama-js/blob/main/examples/tools/tools.ts

Hope this helps!

@ParthSareen commented on GitHub (Nov 29, 2024): @mjm9189 Hey! So the initial call to the LLM just generates a toolcall - which is what you need to use to execute your function with the provided params. You can pass the output of the tool back to the LLM and generate a response for the user. There's an example in the JS repo for this - will add one for python soon too! https://github.com/ollama/ollama-js/blob/main/examples/tools/tools.ts Hope this helps!
Author
Owner

@mjvmiller commented on GitHub (Nov 29, 2024):

Amazing, thank you for the help! I'd only looked through the Python one so far, but I'm glad it's being updated soon. Thanks again!

@mjvmiller commented on GitHub (Nov 29, 2024): Amazing, thank you for the help! I'd only looked through the Python one so far, but I'm glad it's being updated soon. Thanks again!
Author
Owner

@ParthSareen commented on GitHub (Nov 29, 2024):

@mjm9189 Just updated! https://github.com/ollama/ollama-python/blob/main/examples/tools.py

@ParthSareen commented on GitHub (Nov 29, 2024): @mjm9189 Just updated! https://github.com/ollama/ollama-python/blob/main/examples/tools.py
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#173