ollama python 新版本支持传递python 函数

在ollama python sdk 的最近更新中,支持传递python 函数到chat 调用中,以下是一个简单示例

参考使用

  • app.py
from ollama import chat
from ollama import ChatResponse


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 a + b


def subtract_two_numbers(a: int, b: int) -> int:
  """
  Subtract two numbers
  """
  return a - b


# 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'},
      },
    },
  },
}

prompt = 'What is three plus one?'
print('Prompt:', prompt)

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

response: ChatResponse = chat(
  'qwen2.5:1.5b',
  messages=[{'role': 'user', 'content': prompt}],
  tools=[add_two_numbers, 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)
      print('Function output:', function_to_call(**tool.function.arguments))
    else:
      print('Function', tool.function.name, 'not found')
  • 效果

说明

尽管支持传递函数,但是格式是有一些要求的,需要添加函数docs 说明

参考资料

https://github.com/ollama/ollama-python/blob/main/examples/tools.py

https://google.github.io/styleguide/pyguide.html#doc-function-raises

posted on 2024-11-27 13:01  荣锋亮  阅读(24)  评论(0编辑  收藏  举报

导航