TImon Harz

December 14, 2024

Ollama Python library 0.4 with function calling improvements

Discover how to pass Python functions as tools to Ollama and how the library automatically generates JSON schema from function definitions. Explore the new updates in the Ollama Python library for improved functionality and typing support.

Get started

To begin, install or upgrade the Ollama Python library:

pip install -U ollama

Passing Python functions as tools

Define a Python function

Start by defining a regular Python function. For better results, annotate the parameter and return types, and optionally add a Google-style docstring:

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

  Args:
    a: The first integer number
    b: The second integer number

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

Pass the function as a tool to Ollama

Next, pass the function as a tool to Ollama using the tools field:

import ollama

response = ollama.chat(
  'llama3.1',
  messages=[{'role': 'user', 'content': 'What is 10 + 10?'}],
  tools=[add_two_numbers], # Actual function reference

Call the function from the model response

Use the tool call and arguments provided by the model to call the respective function:

available_functions = {
  'add_two_numbers': add_two_numbers,
}

for tool in response.message.tool_calls or []:
  function_to_call = available_functions.get(tool.function.name)
  if function_to_call:
    print('Function output:', function_to_call(**tool.function.arguments))
  else:
    print('Function not found:', tool.function.name)

Pass existing functions as tools

You can now pass functions from existing Python libraries, SDKs, and other sources as tools. For instance, the following code demonstrates passing the request function from the requests library as a tool to fetch the contents of the Ollama website:

import ollama
import requests

available_functions = {
  'request': requests.request,
}

response = ollama.chat(
  'llama3.1',
  messages=[{
    'role': 'user',
    'content': 'get the ollama.com webpage?',
  }],
  tools=[requests.request], 
)

for tool in response.message.tool_calls or []:
  function_to_call = available_functions.get(tool.function.name)
  if function_to_call == requests.request:
    # Make an HTTP request to the URL specified in the tool call
    resp = function_to_call(
      method=tool.function.arguments.get('method'),
      url=tool.function.arguments.get('url'),
    )
    print(resp.text)
  else:
    print('Function not found:', tool.function.name)

How it works: Generating JSON Schema from Functions

The Ollama Python library utilizes Pydantic and docstring parsing to automatically generate the JSON schema. For example, consider the add_two_numbers function defined earlier in this post. The following JSON schema is generated (which was previously required to be provided manually as a 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 integer number"
                },
                "b": {
                    "type": "integer",
                    "description": "The second integer number"
                }
            }
        }
    }
}

Additional Improvements to the Ollama Python Library

The 0.4 release of the Ollama Python library introduces several enhancements:

  • Examples have been updated on the Ollama Python GitHub.

  • Full typing support across the library, enabling direct object access while preserving existing functionality.

Press contact

Timon Harz

oneboardhq@outlook.com

The logo for Oneboard Blog

Discover recent post from the Oneboard team.

Notes, simplified.

Follow us

Company

About

Blog

Careers

Press

Legal

Privacy

Terms

Security