examples/tools.py ignores defined tools and spits out usual LLM results. #299

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

Originally created by @nise on GitHub (Sep 17, 2025).

I have taken the provided example and manipulated the function to add up or subtract two numbers. However, the LLM alway does the calculation without using the defined tools.

from ollama import ChatResponse, chat


def add_two_numbers(a: int, b: int) -> int:
  return int(a) + int(b) + 1

def subtract_two_numbers(a: int, b: int) -> int:
  return int(a) - int(b) - 1 


# Tools can still be manually defined and passed into chat
subtract_two_numbers_tool = {
  'type': 'function',
  'function': {
    'name': 'subtract_two_numbers',
    'description': 'Subtract two numbers',
    'parameters': {
      'type': 'object',
      'required': ['a', 'b'],
      'properties': {
        'a': {'type': 'integer', 'description': 'The first number'},
        'b': {'type': 'integer', 'description': 'The second number'},
      },
    },
  },
}

add_two_numbers_tool = {
  'type': 'function',
  'function': {
    'name': 'add_two_numbers',
    'description': 'add two numbers',
    'parameters': {
      'type': 'object',
      'required': ['a', 'b'],
      'properties': {
        'a': {'type': 'integer', 'description': 'The first number'},
        'b': {'type': 'integer', 'description': 'The second number'},
      },
    },
  },
}

messages = [{'role': 'user', 'content': 'What is three plus one?'}]
print('Prompt:', messages[0]['content'])

available_functions = {
  'add_two_numbers': add_two_numbers,
  'subtract_two_numbers': subtract_two_numbers,
}

response: ChatResponse = chat(
  'llama3.1',
  messages=messages,
  tools=[add_two_numbers_tool, subtract_two_numbers_tool],
)

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 := available_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), 'tool_name': tool.function.name})

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

else:
  print('No tool calls returned from model')
Originally created by @nise on GitHub (Sep 17, 2025). I have taken the provided example and manipulated the function to add up or subtract two numbers. However, the LLM alway does the calculation without using the defined tools. ``` from ollama import ChatResponse, chat def add_two_numbers(a: int, b: int) -> int: return int(a) + int(b) + 1 def subtract_two_numbers(a: int, b: int) -> int: return int(a) - int(b) - 1 # Tools can still be manually defined and passed into chat subtract_two_numbers_tool = { 'type': 'function', 'function': { 'name': 'subtract_two_numbers', 'description': 'Subtract two numbers', 'parameters': { 'type': 'object', 'required': ['a', 'b'], 'properties': { 'a': {'type': 'integer', 'description': 'The first number'}, 'b': {'type': 'integer', 'description': 'The second number'}, }, }, }, } add_two_numbers_tool = { 'type': 'function', 'function': { 'name': 'add_two_numbers', 'description': 'add two numbers', 'parameters': { 'type': 'object', 'required': ['a', 'b'], 'properties': { 'a': {'type': 'integer', 'description': 'The first number'}, 'b': {'type': 'integer', 'description': 'The second number'}, }, }, }, } messages = [{'role': 'user', 'content': 'What is three plus one?'}] print('Prompt:', messages[0]['content']) available_functions = { 'add_two_numbers': add_two_numbers, 'subtract_two_numbers': subtract_two_numbers, } response: ChatResponse = chat( 'llama3.1', messages=messages, tools=[add_two_numbers_tool, subtract_two_numbers_tool], ) 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 := available_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), 'tool_name': tool.function.name}) # Get final response from model with function outputs final_response = chat('llama3.1', messages=messages) print('Final response:', final_response.message.content) else: print('No tool calls returned from model') ```
yindo closed this issue 2026-02-15 16:29:44 -05:00
Author
Owner

@ParthSareen commented on GitHub (Sep 17, 2025):

it's up to the model to use tools or not - i'd also recommend qwen3 for tool calling instead of llama3.1

@ParthSareen commented on GitHub (Sep 17, 2025): it's up to the model to use tools or not - i'd also recommend `qwen3` for tool calling instead of `llama3.1`
Author
Owner

@Mohamed0Hegazi commented on GitHub (Sep 17, 2025):

استدعاء الادوات
Stop qwen3

@Mohamed0Hegazi commented on GitHub (Sep 17, 2025): استدعاء الادوات Stop qwen3
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#299