Skip to main content

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

Project description

Tools4All

A Python library that adds function calling capabilities to LLMs that don't natively support them, with a focus on Ollama models.

🎯 Goal

Tools4All enables function calling for any LLM, even those that don't natively support tools or function calling. It works by:

  1. Injecting tool descriptions into the system prompt
  2. Parsing the LLM's response to extract tool calls
  3. Executing the tools and providing results back to the LLM
  4. Generating a final answer based on the tool results

This approach allows you to use function calling with models like Gemma 3 (27B) through Ollama, even though they don't have native tool support.

🚀 Features

  • Universal Tool Support: Add function calling to any LLM through Ollama
  • Tool Registry: Easy registration and management of tools
  • Response Parsing: Intelligent parsing of LLM responses to extract tool calls
  • Final Answer Generation: Avoid hallucinations by using actual tool results
  • Flexible Model Configuration: Works with any model available through Ollama

📋 Installation

# Clone the repository
git clone https://github.com/yourusername/tools4all.git
cd tools4all

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

📦 Dependencies

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

🔧 Usage

Basic Usage

from tools4all import Tools4All, ToolRegistry

# Create a tool registry
registry = ToolRegistry()

# Define and register a tool
def get_weather(location):
    # Your implementation here
    return f"The weather in {location} is sunny."

registry.register_tool(
    "get_weather",
    get_weather,
    "Get the current weather",
    {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
            }
        },
        "required": ["location"]
    }
)

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

# Process a user prompt
prompt = "What's the weather like in San Francisco?"
model = "gemma3:27b"  # or any other model available through Ollama

client.process_prompt(prompt, registry, model)

Creating Custom Tools

You can create custom tools by defining functions and registering them with the ToolRegistry:

def calculate_mortgage(principal, interest_rate, years):
    monthly_rate = interest_rate / 100 / 12
    months = years * 12
    payment = principal * (monthly_rate * (1 + monthly_rate) ** months) / ((1 + monthly_rate) ** months - 1)
    return f"Monthly payment: ${payment:.2f}"

registry.register_tool(
    "calculate_mortgage",
    calculate_mortgage,
    "Calculate monthly mortgage payment",
    {
        "type": "object",
        "properties": {
            "principal": {
                "type": "number",
                "description": "Loan amount in dollars"
            },
            "interest_rate": {
                "type": "number",
                "description": "Annual interest rate in percent"
            },
            "years": {
                "type": "integer",
                "description": "Loan term in years"
            }
        },
        "required": ["principal", "interest_rate", "years"]
    }
)

🧩 Architecture

Tools4All consists of several key components:

  1. LLMResponseParser: Parses LLM responses to extract code blocks, tool calls, and comments
  2. ToolRegistry: Manages tool registration and execution
  3. Tools4All: Main class that handles chat completions and adapts for models without native tool support
  4. Process Prompt Function: Orchestrates the entire workflow from user prompt to final answer

🔄 Workflow

  1. User sends a prompt
  2. The prompt is sent to the LLM with tool descriptions
  3. The LLM's response is parsed to extract tool calls
  4. Tools are executed with the provided arguments
  5. Tool results are sent back to the LLM
  6. A final answer is generated based on the tool results

📝 Example

User: What's the weather like in San Francisco, CA?

Model: I'll check the weather for you.

Tool Call: get_temperature(location="San Francisco, CA", format="fahrenheit")
Tool Result: Current temperature in San Francisco, CA is 65°F

Tool Call: get_humidity(location="San Francisco, CA")
Tool Result: Current humidity in San Francisco, CA is 75%

Final Answer:
Based on the information I gathered:

Current temperature in San Francisco, CA is 65°F
Current humidity in San Francisco, CA is 75%

This is the current weather information for your requested location.

🛠️ Advanced Configuration

Using Different Models

You can specify which model to use when processing prompts:

# Use Gemma 3 (27B)
client.process_prompt(prompt, registry, model="gemma3:27b")

# Use Qwen 2.5 (3B)
client.process_prompt(prompt, registry, model="qwen2.5:3b")

Custom Host

You can specify a custom Ollama host:

client = Tools4All(host='http://your-ollama-server:11434')

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 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.1.0.tar.gz (24.5 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.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tools4all-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d9af0d844e134968ad4103f05513ec9007ce6570216e52af037271d672a81006
MD5 7cae0123d63a90b8bd8c83806542c71d
BLAKE2b-256 6ea14426520fc4d1e87beb93c59f69c537b2c9f2de45b6835686ae900711baf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tools4all-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3de58c7ae0e2431550fdc7d25ddff798f5d0e6a555ea892482b208d182e0dea
MD5 3d08acfa660cb28e4fc5a80b843f0efe
BLAKE2b-256 f468d477d1f05f80c82fb3261601cc00e8fbb6fcc2d3ab391232027dcc448480

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