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:
- Injecting tool descriptions into the system prompt
- Parsing the LLM's response to extract tool calls
- Executing the tools and providing results back to the LLM
- 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 Ollamapydantic: Data validation and settings managementrich: 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:
- LLMResponseParser: Parses LLM responses to extract code blocks, tool calls, and comments
- ToolRegistry: Manages tool registration and execution
- Tools4All: Main class that handles chat completions and adapts for models without native tool support
- Process Prompt Function: Orchestrates the entire workflow from user prompt to final answer
🔄 Workflow
- User sends a prompt
- The prompt is sent to the LLM with tool descriptions
- The LLM's response is parsed to extract tool calls
- Tools are executed with the provided arguments
- Tool results are sent back to the LLM
- 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
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 tools4all-0.1.2.tar.gz.
File metadata
- Download URL: tools4all-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48ee7929ad04192ef9716b9df0b2ff3ff98f74e027959008ec147f1d98f4d49f
|
|
| MD5 |
a65ecfdf74a50886851e1cbf670fdb50
|
|
| BLAKE2b-256 |
cedd9d3dadf0396568fbf6b16aee472e1028b277becf82443a1aa0544d8fb448
|
File details
Details for the file tools4all-0.1.2-py3-none-any.whl.
File metadata
- Download URL: tools4all-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45761b38ed81ddcd6885b0ae297c98164fe9665608b165e492e65b1211dbc0e9
|
|
| MD5 |
08f2f23817f958d6b0f6de4d1a250a51
|
|
| BLAKE2b-256 |
4da9d4fdd546c04c81c52a561be81fc4e76715d0f8624e7574cfd96eefde541e
|