Skip to main content

Reusable tool argument and base tool classes extracted from quantalogic.

Project description

Quantalogic Toolbox

The Quantalogic Toolbox is a Python package designed to simplify the creation of configurable tools for AI agents. Whether you're building intelligent systems or automating workflows, this toolbox provides a robust framework for defining tools with type safety, clear documentation, and flexible execution options.

Why Use Quantalogic Toolbox?

Imagine you’re teaching an AI agent to perform tasks, like a chef learning recipes. You need tools that are easy to define, safe to use (no mixing up ingredients!), and well-documented so the agent knows exactly what to do. That’s where Quantalogic Toolbox shines:

  • Type Safety: Prevents errors by validating argument types.
  • Clarity: Auto-generates detailed descriptions for tools and arguments.
  • Flexibility: Supports both synchronous and asynchronous execution.
  • Speed: Quickly converts Python functions into agent-ready tools.

What is It?

The toolbox revolves around a few key components in the tool.py module:

  • ToolArgument: Defines an argument (e.g., name, type, description) with metadata like defaults and examples.
  • ToolDefinition: Sets up a tool’s configuration—name, description, arguments, and return type.
  • Tool: Adds execution logic to ToolDefinition, supporting execute (sync) and async_execute (async).
  • create_tool: A magic wand that turns any Python function into a Tool instance, extracting metadata automatically.

Helper functions like type_hint_to_str, get_type_description, and get_type_schema make type handling a breeze.

How to Use It

Installation

pip install quantalogic-toolbox

Quick Start

Turn a function into a tool in seconds:

from quantalogic_toolbox import create_tool

def add_numbers(a: int, b: int) -> int:
    """Add two numbers together."""
    return a + b

tool = create_tool(add_numbers)
result = tool.execute(a=5, b=3)  # Returns 8

Detailed Usage

Creating Tools

Option 1: Use create_tool

def greet(name: str) -> str:
    """Say hello to someone."""
    return f"Hello, {name}!"

greet_tool = create_tool(greet)
print(greet_tool.execute(name="Alice"))  # Output: Hello, Alice!

Option 2: Subclass Tool

from quantalogic_toolbox import Tool, ToolArgument

class MultiplyTool(Tool):
    name = "multiply"
    description = "Multiply two numbers."
    arguments = [
        ToolArgument(name="x", arg_type="int", description="First number", required=True),
        ToolArgument(name="y", arg_type="int", description="Second number", required=True)
    ]
    return_type = "int"

    def execute(self, x: int, y: int) -> int:
        return x * y

mult_tool = MultiplyTool()
print(mult_tool.execute(x=4, y=5))  # Output: 20

Defining Arguments

Arguments are like ingredients in a recipe—specify them clearly:

arg = ToolArgument(
    name="count",
    arg_type="int",
    description="Number of items to process",
    required=True,
    example="10"
)

Executing Tools

  • Synchronous: result = tool.execute(arg1=value1, arg2=value2)
  • Asynchronous: result = await tool.async_execute(arg1=value1, arg2=value2)

Visualizing the Process

Here’s how a function becomes a tool:

graph TD
    A[Write Function] --> B[Call create_tool]
    B --> C[Tool Instance Created]
    C --> D[Execute Sync or Async]
    D --> E[Get Result]

Examples

Async Example:

import asyncio

async def async_wait(name: str) -> str:
    """Wait a second, then greet."""
    await asyncio.sleep(1)
    return f"Hi, {name}!"

tool = create_tool(async_wait)
result = asyncio.run(tool.async_execute(name="Bob"))  # Output: Hi, Bob!

Complex Type Example:

from typing import List

def sum_list(numbers: List[int]) -> int:
    """Sum a list of integers."""
    return sum(numbers)

tool = create_tool(sum_list)
print(tool.execute(numbers=[1, 2, 3]))  # Output: 6

Insider Secrets

  • Maximize Descriptions: Detailed argument descriptions (e.g., “a list of integers”) help AI agents use tools correctly.
  • Async Power: Use async_execute for tasks that need to wait (e.g., API calls)—it’s built-in and seamless.
  • Type Details: The type_details field auto-explains complex types (e.g., “a list of int”), saving you time.

Memorization Trick

Think “NAR” for ToolArgument essentials:

  • Name
  • Arg_type
  • Required

Understanding Complex Concepts

Ever wonder how create_tool works? Picture it like a chef inspecting a recipe: it reads the function (ingredients and steps), figures out what’s needed (arguments), and writes a clear guide (tool metadata). No magic—just clever parsing of your code!

FAQ

Q: Can I use this with any AI framework?
A: Yes! It’s pure Python and framework-agnostic.

Q: What if my tool needs user approval?
A: Set need_validation=True in ToolDefinition.

Contributing

Got ideas? Open a pull request or issue on GitHub—we’d love your input!


This toolbox is your shortcut to building AI-ready tools fast, with less hassle and more precision. Start crafting today!

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

quantalogic_toolbox-0.9.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

quantalogic_toolbox-0.9.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file quantalogic_toolbox-0.9.0.tar.gz.

File metadata

  • Download URL: quantalogic_toolbox-0.9.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.8 Darwin/24.4.0

File hashes

Hashes for quantalogic_toolbox-0.9.0.tar.gz
Algorithm Hash digest
SHA256 5d8a94aac307a2e9512157343e33f6b4596b8a7cbe1afb50987a88c699d5134f
MD5 c1da22f7ae48cdbb648e721b2dd8c218
BLAKE2b-256 06ed5c08ce799c2c51a9cb348b19f1cc6190e2604f25dcc2b24ebd6cf8d3a3bf

See more details on using hashes here.

File details

Details for the file quantalogic_toolbox-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for quantalogic_toolbox-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fedd1a548b9d9c0b23c84902117021b9139241aeafc7029951f4d74d25226739
MD5 322249a076d4fbfad93178072553402c
BLAKE2b-256 6c1d28b613fb736675473892e7259f591a3a74937cfcedeef3162a68be9e9bc4

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