Skip to main content

Type-safe function registration and validation for LLM function calls

Project description

llmtk (LLM Toolkit)

Stop writing JSON schemas for your AI functions. Let Python types do it for you. ⚡️

License: MIT Python 3.9+

Features

  • 🎯 Zero Schema Maintenance: Your Python types become your OpenAI schemas
  • Type Safety: Catch invalid AI responses before they break your code
  • 📝 Rich Types: Support for Pydantic models, lists, and custom types
  • 🚀 Quick Setup: One decorator is all you need

Without llmtk, you write this:

# Define your function
def get_weather(city: str) -> str:
    return f"Weather in {city}: Sunny"

# Manually maintain OpenAI function schema
weather_schema = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get weather information for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "Name of the city"
                }
            },
            "required": ["city"]
        }
    }
}

# Hope the schema stays in sync with your function
tools = [weather_schema]

With llmtk, just write this:

from llmtk import register_function, get_openai_tools

@register_function
def get_weather(city: str) -> str:
    """Get weather information for a city"""
    return f"Weather in {city}: Sunny"

# Schema automatically generated from your Python types
tools = get_openai_tools()

Quick Start

pip install llmtk
from llmtk import register_function, call_function, get_openai_tools

@register_function
def calculate_price(quantity: int, unit_price: float) -> float:
    """Calculate total price for items"""
    return quantity * unit_price

# Get schema for OpenAI
tools = get_openai_tools()

# Use with OpenAI
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Calculate price for 5 items at $10 each"}],
    tools=tools,
    tool_choice="auto"
)

# Safe function execution with validation
result = call_function(
    response.choices[0].message.tool_calls[0].function.name,
    response.choices[0].message.tool_calls[0].function.arguments
)

if isinstance(result, tuple):
    print(f"Validation error: {result[0]}")
else:
    print(f"Total: ${result:.2f}")  # Total: $50.00

License

MIT License - feel free to use in your projects!

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

llmtk-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

llmtk-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for llmtk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9bd0d476a9c8bc77a793afb59f61a7d4d424350277abe17d7e31f0e8cefa5a0e
MD5 8cd08f70a93eb2dbf2225060e9aa8917
BLAKE2b-256 6f34d93d5a08f287dc85f346b453849ac7b22fce415554e81c0be101497dbdc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llmtk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for llmtk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e6409984f0d22b0e3c22a02159853ebdc04c8b387626c658dbcc07466fca36a
MD5 26281540a64ab8195e37cf292106f7b2
BLAKE2b-256 1f08e7213a179989f79f6385a2c9a9c7f1a07bcb17b9f9d25a96b634da225e4d

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