Skip to main content

A workaround for models on Ollama that does not support tool calling

Project description

ollama-tools

A workaround for models on Ollama that does not support tool calling. This package provides a temporary solution to use those models with tools.

Also works as Ollama server wrapper with ollama-tools --host 0.0.0.0 --port 22434 to proxy the Ollama server with extra support for tool calling with models such as DeepSeek-R1.

Installation

Install Ollama and get a model

curl -fsSL https://ollama.com/install.sh | sh
ollama serve &
ollama pull deepseek-r1:1.5b

ℹ️ Above Bash commands shows installation on Linux, installing Ollama on other operating systems refer to https://ollama.com/download

Install the package for Python binding

pip install ollama-tools

Example Usage

Develop in Python

  1. Import the package
import ollama_tools
  1. Simple chat without tools
response = ollama_tools.chat(
    model='deepseek-r1:1.5b',
    messages=[{'role': 'user', 'content': 'how is weather in Paris?'}])

print(response.message.content)
'''
# Example output:
Message(role='assistant', content="I don't have access to the current weather data for Paris. To find out the latest weather in Paris, you can check official weather websites or apps specifically designed for Paris, such as METAR or AccuWeather.", tool_calls=[])
'''
  1. Chat with tools
tools=[
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The location to get the weather for, e.g. San Francisco, CA"
                    },
                    "format": {
                        "type": "string",
                        "description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
                        "enum": ["celsius", "fahrenheit"]
                    }
                },
                "required": ["location", "format"]
            }
        }
    }
]
  
response = ollama_tools.chat(
    model='deepseek-r1:1.5b',
    messages=[{'role': 'user', 'content': 'how is weather in Paris?'}],
    tools=tools)

print(response)
'''
# Example output:
ChatResponse(model='deepseek-r1:1.5b', created_at='2025-01-31T06:09:22.095306834Z', done=True, done_reason='stop', total_duration=11153340905, load_duration=33513150, prompt_eval_count=138, prompt_eval_duration=1300000000, eval_count=265, eval_duration=9285000000, message=Message(role='assistant', content="To determine the current weather in Paris, you can use the following information: Paris is a significant city known for its historical landmarks, vibrant nightlife, and modern architecture. The local climate is generally milder than that of many other cities, with temperatures typically ranging between 20°C to 35°C (68°F to 95°F). However, Paris experiences occasional weather changes due to the region's natural geography and urbanization. On average, the rainy season in Paris can last about six months from June through September, while the dry season occurs during November through April. These seasonal changes may influence local rainfall patterns, which could impact weather-related activities like agriculture and transportation. Additionally, Paris has a strong presence of international students and professionals, who bring a diverse set of experiences into the city's daily life. As for specific weather information, you might want to check the official weather websites or weather apps that provide the most up-to-date and accurate forecasts. They typically offer detailed hourly updates and even short-term projections, which can help you plan your activities accordingly.", tool_calls=[ToolCall(name='get_current_weather', arguments={'location': 'Paris'})])
'''

AsyncClient

⚠️ The behaviour of ollama_tools.AsyncClient is different from ollama.AsyncClient when stream=True is enabled, the for-loop async for part in AsyncClient() does not need await before AsyncClient

import asyncio
from ollama_tools import AsyncClient

async def chat():
  message = {'role': 'user', 'content': 'Why is the sky blue?'}
  async for part in AsyncClient().chat(model='deepseek-r1:0.5b', messages=[message], stream=True):
    #             ^^^ With `Ollama` it will be `await AsyncClient()`
    print(part['message']['content'], end='', flush=True)

asyncio.run(chat())

Ollama Wrapper (Server)

Install the package with Ollama wrapper

pip install ollama-tools[full]
ollama-tools --host 0.0.0.0 --port 22434

Now we can use this in llama-index or langchain

### llama_index ==================
from llama_index.llms.ollama import Ollama

llm = Ollama(
    model="deepseek-r1",
    base_url="http://localhost:22434", # as we started the server with `--port 22434`
    request_timeout=60.0
)

response = llm.complete("What is the capital of France?")
print(response)


### langchain_ollama ==================
from langchain_ollama import ChatOllama

llm = ChatOllama(
    model="deepseek-r1",
    temperature=0,
    base_url="http://localhost:22434"
)

from langchain_core.messages import AIMessage

messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
print(ai_msg)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ollama_tools-1.0.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ollama_tools-1.0.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file ollama_tools-1.0.2.tar.gz.

File metadata

  • Download URL: ollama_tools-1.0.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for ollama_tools-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d4f75c83f5a1d5cd873a391ff18ff077737fbe85c22ab83a190e449c68669c90
MD5 9355f93344b69328b2607d7ac2e00cf0
BLAKE2b-256 e9a66321d7805b001f9ad3acbc9bc8f4693182c85e8ebf2a6322d625362cfc96

See more details on using hashes here.

Provenance

The following attestation bundles were made for ollama_tools-1.0.2.tar.gz:

Publisher: workflow.yml on NewJerseyStyle/ollama-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ollama_tools-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: ollama_tools-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for ollama_tools-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 29c931d72cb62eb41b64c1d1abdc7379f7621f6b5357c6448fa319f8b0b72dfb
MD5 fb4c7af11bd0181fec0f9d16035e3591
BLAKE2b-256 2015d0a8b37891197dcf53cf7346c8cf5df2a6da3c71bac17da6e7f4442491e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for ollama_tools-1.0.2-py3-none-any.whl:

Publisher: workflow.yml on NewJerseyStyle/ollama-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page