Skip to main content

Function calling capabilities for LLMs that don't natively support them

Project description

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

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

tools4all-0.2.2.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

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

tools4all-0.2.2-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file tools4all-0.2.2.tar.gz.

File metadata

  • Download URL: tools4all-0.2.2.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tools4all-0.2.2.tar.gz
Algorithm Hash digest
SHA256 05a0a626760dd753d58e20270085c7bca8ca4e2b9bc528ac25883165e5d2dcb2
MD5 d73ec858fb991a593cf4297a59a54ba9
BLAKE2b-256 b2620f24aae6cfb90c54ab89f61ec43b18c390860b9d2dd02364af0ec1008970

See more details on using hashes here.

File details

Details for the file tools4all-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: tools4all-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tools4all-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c27ec8a2b2c60fdc410229a27d7bf15508c621e39f9655486bf5b23e78b8bbc
MD5 a450c6b21c6c8e072ea9039427a7e251
BLAKE2b-256 78077f0902ae27befd151419fbf6bb2863f58ed9d1b872edfbb88afd8470e4e8

See more details on using hashes here.

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