Skip to main content

A minimal and intuitive library for working with OpenAI's function calling API

Project description

OpenAI Toolchain

PyPI Python Version Tests Documentation Code style: black License: MIT

A Python library for working with OpenAI's function calling API, making it easier to create and manage tools that can be used with OpenAI's chat models.

Features

  • 🛠️ Simple function registration with @tool decorator
  • 🤖 Automatic tool schema generation
  • 🔄 Seamless integration with OpenAI's API
  • ⚡ Support for both sync and async operations
  • 📚 Clean and intuitive API

Installation

pip install openai-toolchain

Quick Start

  1. Define your tools using the @tool decorator:
from openai_toolchain import tool, OpenAIClient

@tool
def get_weather(location: str, unit: str = "celsius") -> str:
    """Get the current weather in a given location.

    Args:
        location: The city to get the weather for
        unit: The unit of temperature (celsius or fahrenheit)
    """
    return f"The weather in {location} is 22 {unit}"

@tool("get_forecast")
def get_forecast_function(location: str, days: int = 1) -> str:
    """Get a weather forecast for a location.

    Args:
        location: The city to get the forecast for
        days: Number of days to forecast (1-5)
    """
    return f"{days}-day forecast for {location}: Sunny"
  1. Use the tools with OpenAI:
# Initialize the client with your API key
client = OpenAIClient(api_key="your-api-key")

# Use the client
response = client.chat_with_tools(
    messages=[{"role": "user", "content": "What's the weather in Toronto?"}]
)
print(response)

Documentation

For detailed documentation, including API reference and examples, please visit:

📚 Documentation

Or run the documentation locally:

pip install -e ".[docs]"
mkdocs serve

Contributing

Contributions are welcome! Please see our Contributing Guide for details.

Pre-commit Hooks

This project uses pre-commit to ensure code quality and style consistency. To set it up:

  1. Install pre-commit:

    pip install pre-commit
    
  2. Install the git hook scripts:

    pre-commit install
    
  3. (Optional) Run against all files:

    pre-commit run --all-files
    

The hooks will now run automatically on every commit. To skip the pre-commit checks, use:

git commit --no-verify -m "Your commit message"

License

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

Initialize the client with your API key

client = OpenAIClient(api_key="your-api-key")

Chat with automatic tool calling

response = client.chat_with_tools( messages=[{"role": "user", "content": "What's the weather in Toronto?"}], tools=["get_weather"] # Optional: specify which tools to use )

print(response)


## Features

### 1. Tool Registration

Use the `@tool` decorator to register functions as tools:

```python
from openai_toolchain import tool

@tool
def search_web(query: str) -> str:
    """Search the web for information."""
    return f"Search results for: {query}"

2. Chat with Automatic Tool Calling

The chat_with_tools method handles tool calls automatically:

client = OpenAIClient(api_key="your-api-key")

response = client.chat_with_tools(
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Search for the latest Python news"}
    ],
    tools=["search_web"],
    model="gpt-4"  # Optional: specify a different model
)

3. Accessing Registered Tools

You can access registered tools directly:

from openai_toolchain import tool_registry

# Get all registered tools
tools = tool_registry.get_openai_tools()

# Call a tool directly
result = tool_registry.call_tool("get_weather", {"location": "Paris", "unit": "fahrenheit"})

API Reference

@tool decorator

Register a function as a tool:

from openai_toolchain import tool

@tool
def my_function(param: str) -> str:
    """Function documentation."""
    return f"Result for {param}"

tool_registry

The global registry instance with these methods:

  • register(func, **kwargs): Register a function as a tool
  • get_tool(name): Get a registered tool by name
  • call_tool(name, arguments): Call a registered tool by name with arguments
  • get_openai_tools(): Get all tools in OpenAI format

Development

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

License

MIT

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

openai_toolchain-0.1.1.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

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

openai_toolchain-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file openai_toolchain-0.1.1.tar.gz.

File metadata

  • Download URL: openai_toolchain-0.1.1.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for openai_toolchain-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3b4d4f6f92a1a38fed0d9814cc25b5c4ff8a9fcc7d99083d0d774b1983830ed0
MD5 937fcbf885e469ce1630cbce7c31f066
BLAKE2b-256 a02edf1095a07bae6cf845bcdbf12dcbfab0bccd4dfaa249e53822f048110aa3

See more details on using hashes here.

File details

Details for the file openai_toolchain-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_toolchain-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f74884fc3b4d366eec5e0c86f73de1fa9dd91ccca5d6dc6e1df534c2197facd5
MD5 ad75103be9071fe6093f122e6ac4dfd4
BLAKE2b-256 60517008eef15f0924bf6374b3c03d690bf449b6b0c7f563a3a4cb1569517a07

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