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
- Import the package
import ollama_tools
- 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=[])
'''
- 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.AsyncClientis different fromollama.AsyncClientwhenstream=Trueis enabled, the for-loopasync for part in AsyncClient()does not needawaitbeforeAsyncClient
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ollama_tools-1.0.1.tar.gz.
File metadata
- Download URL: ollama_tools-1.0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe1ca51357016d41039ded04757e58587b05100ddbab3e28ae4f0e6b46b6ab07
|
|
| MD5 |
923dc1673f47d7626844a411c6c26a7b
|
|
| BLAKE2b-256 |
416ef2b2f254b99b76a9589a2e273ae824d010a25552fd6fccd59410f72d2d98
|
Provenance
The following attestation bundles were made for ollama_tools-1.0.1.tar.gz:
Publisher:
workflow.yml on NewJerseyStyle/ollama-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ollama_tools-1.0.1.tar.gz -
Subject digest:
fe1ca51357016d41039ded04757e58587b05100ddbab3e28ae4f0e6b46b6ab07 - Sigstore transparency entry: 167874555
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/ollama-tools@28198ebf95dc99bc85d35de1f275cfe245e28548 -
Branch / Tag:
refs/tags/v1.0.1-hotfix - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@28198ebf95dc99bc85d35de1f275cfe245e28548 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ollama_tools-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ollama_tools-1.0.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6e5081998c8c33f98daed5bff63e4f32e8ba56a5ef3f1dd3f6a0ab81c90f202
|
|
| MD5 |
fd5b09a83c6248ad81c4aa305bf7a0cb
|
|
| BLAKE2b-256 |
71ff01a782b11d06fac45ce7ee938328701ad310a91ee0af6796997db2035f9e
|
Provenance
The following attestation bundles were made for ollama_tools-1.0.1-py3-none-any.whl:
Publisher:
workflow.yml on NewJerseyStyle/ollama-tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ollama_tools-1.0.1-py3-none-any.whl -
Subject digest:
d6e5081998c8c33f98daed5bff63e4f32e8ba56a5ef3f1dd3f6a0ab81c90f202 - Sigstore transparency entry: 167874559
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/ollama-tools@28198ebf95dc99bc85d35de1f275cfe245e28548 -
Branch / Tag:
refs/tags/v1.0.1-hotfix - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@28198ebf95dc99bc85d35de1f275cfe245e28548 -
Trigger Event:
release
-
Statement type: