A Python library for Auto LLM tool calling with decorator-based function registration
Project description
Toolflow
A Python library that makes LLM tool calling as simple as decorating a function. Just wrap your OpenAI client and pass decorated functions directly to the tools parameter - no complex setup required.
import toolflow
from openai import OpenAI
client = toolflow.from_openai(OpenAI())
@toolflow.tool
def get_weather(city: str) -> str:
return f"Weather in {city}: Sunny, 72°F"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Weather in NYC?"}],
tools=[get_weather], # Just pass your function! We'll handle the rest.
max_tool_calls=5, # Maximum number of tool calls to execute as a safety measure.
)
print(response.choices[0].message.content)
Features
- 🎯 Simple decorator-based tool registration - Just use
@toolto register functions - 🔧 Multiple LLM provider support(Coming soon) - OpenAI, Anthropic (Claude), and extensible for others
- 📝 Automatic schema generation - Function signatures are automatically converted to JSON schemas
- ⚡ Automatic execution - Tools can be automatically executed when called by the LLM
- 🔄 Asynchronous support(Coming soon) - Tools can be called asynchronously
- 🔄 Streaming support(Coming soon) - Tools can be streamed
Installation
pip install toolflow
Quick Start
import os
import toolflow
from openai import OpenAI
# Create a toolflow wrapped client
client = toolflow.from_openai(OpenAI(api_key=os.getenv("OPENAI_API_KEY")))
# Define tools using the @tool decorator
@toolflow.tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"Weather in {city}: Sunny, 72°F"
@toolflow.tool
def add_numbers(a: int, b: int) -> int:
"""Add two numbers and return the result."""
return a + b
# Use the familiar OpenAI API with your functions as tools
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "What's the weather in San Francisco? Also, what's 15 + 27?"}
],
tools=[get_weather, add_numbers]
)
print("Response:", response.choices[0].message.content)
Core Concepts
1. Decorating Functions with @tool
The @tool decorator adds metadata to functions so they can be used as LLM tools:
import toolflow
@toolflow.tool
def search_database(query: str, limit: int = 10) -> list:
"""Search the database for matching records."""
# Your implementation here
return ["result1", "result2"]
# The function can now be passed directly to the tools parameter
2. Custom Tool Names and Descriptions
@toolflow.tool(name="db_search", description="Search our product database")
def search_products(query: str) -> list:
"""Original docstring."""
return search_results
3. Multiple LLM Providers
# OpenAI
openai_client = toolflow.from_openai(OpenAI(api_key="your-openai-key"))
# Anthropic Claude
claude_client = toolflow.from_anthropic(anthropic.Anthropic(api_key="your-anthropic-key"))
4. Direct Function Calls
# You can still call your functions directly
@toolflow.tool
def get_weather(city: str) -> str:
return f"Weather in {city}: Sunny"
# Direct call
result = get_weather("New York")
print(result) # "Weather in New York: Sunny"
API Reference
@tool decorator
@toolflow.tool(name=None, description=None)
name: Custom tool name (defaults to function name)description: Tool description (defaults to docstring)
from_openai(client)
Wraps an OpenAI client to support toolflow functions:
import openai
import toolflow
client = toolflow.from_openai(openai.OpenAI())
Enhanced chat.completions.create()
When using a wrapped client, the create method gains additional parameters:
tools: List of toolflow decorated functions or regular tool dictsmax_tool_calls: Maximum number of tool calls to execute (default: 5)
Tool Function Methods
Every @tool decorated function gets these methods:
_tool_metadata- JSON schema for the tool
Development
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black toolflow/
isort toolflow/
# Type checking
mypy toolflow/
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT License - see LICENSE file for details.
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 toolflow-0.1.1.tar.gz.
File metadata
- Download URL: toolflow-0.1.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f607ea9dc6f3cee52a3d1c163533c64ad1c4c2dbad7fd82b2d4bcef1d16ffb2
|
|
| MD5 |
3905152c23f231a80a3c4c1613eec5b7
|
|
| BLAKE2b-256 |
5b4aae824edf8dc38540c4453b87c77b0bb93daf4a18b03434d35fbb11d43550
|
File details
Details for the file toolflow-0.1.1-py3-none-any.whl.
File metadata
- Download URL: toolflow-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d1cdd26b0e62594f796726c77d892098acc6217d799c0aa666c511e1117804e
|
|
| MD5 |
e93c2b22f54b9aa4d4e3ac7b937ce20c
|
|
| BLAKE2b-256 |
b01230f534460f8e03750d580d9557f6d7159942fb43ba6f3df58f7c8d7e5be9
|