chat method is not able to identify add_two_numbers as tool #196

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

Originally created by @swapnilsingh on GitHub (Jan 4, 2025).

https://github.com/ollama/ollama-python/blob/ee349ecc6d05ea57c9e91bc9345e2db3bc79bb5b/examples/tools.py#L54

I am getting this error

tools=[add_two_numbers],
       ^^^^^^^^^^^^^^^

NameError: name 'add_two_numbers' is not defined

Originally created by @swapnilsingh on GitHub (Jan 4, 2025). https://github.com/ollama/ollama-python/blob/ee349ecc6d05ea57c9e91bc9345e2db3bc79bb5b/examples/tools.py#L54 I am getting this error tools=[add_two_numbers], ^^^^^^^^^^^^^^^ NameError: name 'add_two_numbers' is not defined
yindo closed this issue 2026-02-15 16:28:39 -05:00
Author
Owner

@Gaurav4604 commented on GitHub (Jan 6, 2025):

can you show your code script that you're using?

You need to define the function that ollama-python is supposed to consider as a tool

from ollama import chat

# --- this definition ---
def add_two_numbers(a: int, b: int) -> int:
    """
    Add two numbers

    Args:
      a (int): The first number
      b (int): The second number

    Returns:
      int: The sum of the two numbers
    """
    return int(a) + int(b)


functions = {"add_two_numbers": add_two_numbers}

messages = [{"role": "user", "content": "What is three plus one?"}]


response = chat(
    "llama3.2", messages=messages, tools=[add_two_numbers], options={"temperature": 0}
)

print(response.model_dump_json(indent=2))

if response.message.tool_calls:
    # There may be multiple tool calls in the response
    for tool in response.message.tool_calls:
        # Ensure the function is available, and then call it
        if function_to_call := functions.get(tool.function.name):
            print("Calling function:", tool.function.name)
            print("Arguments:", tool.function.arguments)
            output = function_to_call(**tool.function.arguments)
            print("Function output:", output)
        else:
            print("Function", tool.function.name, "not found")

# Only needed to chat with the model using the tool call results
if response.message.tool_calls:
    # Add the function response to messages for the model to use
    messages.append(response.message)
    messages.append(
        {"role": "tool", "content": str(output), "name": tool.function.name}
    )

    # Get final response from model with function outputs
    final_response = chat("llama3.2", messages=messages)
    print("Final response:", final_response.message.content)

else:
    print("No tool calls returned from model")

@Gaurav4604 commented on GitHub (Jan 6, 2025): can you show your code script that you're using? You need to define the function that ollama-python is supposed to consider as a tool ```python from ollama import chat # --- this definition --- def add_two_numbers(a: int, b: int) -> int: """ Add two numbers Args: a (int): The first number b (int): The second number Returns: int: The sum of the two numbers """ return int(a) + int(b) functions = {"add_two_numbers": add_two_numbers} messages = [{"role": "user", "content": "What is three plus one?"}] response = chat( "llama3.2", messages=messages, tools=[add_two_numbers], options={"temperature": 0} ) print(response.model_dump_json(indent=2)) if response.message.tool_calls: # There may be multiple tool calls in the response for tool in response.message.tool_calls: # Ensure the function is available, and then call it if function_to_call := functions.get(tool.function.name): print("Calling function:", tool.function.name) print("Arguments:", tool.function.arguments) output = function_to_call(**tool.function.arguments) print("Function output:", output) else: print("Function", tool.function.name, "not found") # Only needed to chat with the model using the tool call results if response.message.tool_calls: # Add the function response to messages for the model to use messages.append(response.message) messages.append( {"role": "tool", "content": str(output), "name": tool.function.name} ) # Get final response from model with function outputs final_response = chat("llama3.2", messages=messages) print("Final response:", final_response.message.content) else: print("No tool calls returned from model") ```
Author
Owner

@ParthSareen commented on GitHub (Jan 16, 2025):

@swapnilsingh any updates? Closing this out for now. Feel free to reopen

@ParthSareen commented on GitHub (Jan 16, 2025): @swapnilsingh any updates? Closing this out for now. Feel free to reopen
Author
Owner

@20246688 commented on GitHub (Jan 20, 2025):

I found that the ollama-Python/examples/tools.py script cannot specify an IP address.

@20246688 commented on GitHub (Jan 20, 2025): I found that the ollama-Python/examples/tools.py script cannot specify an IP address.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#196