Skip to main content

Tools4All

A drop-in replacement for the Ollama Python library that adds function calling capabilities to LLMs that don't natively support them.

🎯 Goal

Tools4All attempts to enables function calling to non capable Ollama served LLMs. It works by:

  1. Extending ollama.Client and overriding the chat method
  2. Detecting if the LLM is capable of function calling
  3. Injecting tool descriptions into the system prompt if the LLM is not capable of function calling
  4. Parsing the LLM's response to extract tool calls and inject them into ollama.ChatResponse
  5. Parsing role "tool" message items to extract tool results

This approach allows function calling with models that lack native tool support.

Installation

pip install tools4all

Use as you would use ollama python library

📦 Dependencies

  • ollama: Python client for Ollama
  • pydantic: Data validation and settings management
  • rich: For pretty printing (optional)

Usage

from tools4all import Client

# Actual function that may be called
def get_weather(location):
    # Your implementation here
    return f"The weather in {location} is snowy"

# Define the tool description
tool = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g., San Francisco, CA"
                }
            },
            "required": ["location"]
        }
    }
}

# Create a Tools4All client
client = Client(host='http://127.0.0.1:11434')

# Process a user prompt
prompt = "What's the weather like in San Francisco?"
model = "phi4:latest" # set the ollama model to use

# Initialize conversation history
messages=[
    {"role": "user", "content": prompt}
]

# Query the model
response = client.chat(
    messages=messages,
    tools=[tool],
    model=model
)

# handle tool calls
if response.message.tool_calls:
    for i, tool_call in enumerate(response.message.tool_calls):
        # Generate tool call id
        tool_call_id = f"tool_call_{i}"
        
        # Handle get_weather tool call
        if tool_call.function.name == "get_weather":
            location = tool_call.function.arguments["location"]
            weather = get_weather(location)
            
            # Add tool result to messages
            messages.append(
                {
                    "role": "tool",
                    "tool_call_id": tool_call_id,
                    "name": tool_call.function.name,
                    "content": weather
                }
            )

    # Query the model again with the updated messages
    response = client.chat(
        messages=messages,
        model=model
    )

# Print the response content
print(response.message.content)

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgements

Metadata

Release files for tools4all 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tools4all 0.2.4
File Size Uploaded
tools4all-0.2.4.tar.gz 54.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tools4all 0.2.4
File Interpreter ABI Platform
tools4all-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 67.5 kB

Release files / tools4all-0.2.4.tar.gz

Download URL tools4all-0.2.4.tar.gz
Size 54.9 kB
Tags Source
SHA-256 checksum
How to use checksums
358db036ea73c888a9e9edb0b4dd48d9a9443c9651bb7ae3139bac2a56d88017
BLAKE2b-256 checksum
How to use checksums
588ad014681c87908d9f2a1eb73065c87d60925f4f4a264372c0011cc0f7c6b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / tools4all-0.2.4-py3-none-any.whl

Download URL tools4all-0.2.4-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f05c0861eae4f9924117dcab4e200836be7ef59d4673bcb14fb4c74d4f0e5fb
BLAKE2b-256 checksum
How to use checksums
4fa3b6d8708da012567ddd5481214697cdf7e8163053962e59df9103f17c2bce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page