Skip to main content

A lightweight Python library that simplifies the process of exposing functions as tools for Large Language Models

Project description

PyToolsmith

A lightweight Python library that simplifies the process of exposing functions as tools for Large Language Models.

Status

Tests PyPI - Version codecov License: MIT

What is this?

LLM Tooling (or function calling) is a powerful way to connect LLMs to the real world. However, defining tool definitions can be cumbersome, as it requires defining both the tool function, and a JSON schema that describes the tool. Additionally, in some cases, you may want to control certain parameters passed into tools rather than have the LLM decide what to pass in. PyToolsmith aims to solve this by providing a simple API to define tools from function definitions and automatically generate the JSON schema to pass to the LLM.

Features

  • Generates JSON schemas directly from your function definitions.
  • Parses Google-style docstrings to describe your tools in the schema.
  • Pass the same tools into different LLM providers with a simple method call.
  • Define custom type mappings to extend functionality.

Supported Provider Interfaces

  • Anthropic
  • AWS Bedrock
  • OpenAI

Included Type Support

Part of being able to define schemas is mapping certain types to a JSON-compatible format. As such, PyToolsmith allows you to define custom type maps to be used to generate the JSON schema. However, it comes out-of-the-box with support for:

  1. Standard based objects str, int, float, bool, etc.
  2. UUIDs

Usage

Simply define a tool definition as such:

from pytoolsmith import ToolDefinition


# 1. Define your function
def get_user_by_id(user_id: str, tenant_id: str) -> dict:
    """
    This a tool that gets a user by its ID.
   
    Args:
       user_id: The user to search for.
       tenant_id: The tenant to search inside of
       
    Returns: A dictionary representing the user.
    """
    # Your tool logic here.
    return {
        "user_id": user_id,
        "tenant_id": tenant_id,
        "user_name": "John Doe",
        "email": "john.doe@example.com",
        "phone": "123-456-7890"
    }


# 2. Make a tool definition, calling out the injected parameter.
tool_definition = ToolDefinition(
    function=get_user_by_id,
    injected_parameters=["tenant_id"],
    # You can also define a message that can be sent to the user here.
    # Use mustache template syntax to inject parameter values into the message.
    user_message="Looking up a user using id {{user_id}}."
)

# 3. Get a schema representing the tool automatically
schema = tool_definition.build_json_schema()

# 4. Get a tool definition ready to pass directly into LLM calls. 
# Note that the LLM does not have the context for the controlled parameter.
schema.to_openai()
schema.to_anthropic()
schema.to_bedrock()

# Bedrock Output:
# {
#     "name": "get_user_by_id",
#     "inputSchema": {
#         "json": {
#             "type": "object",
#             "properties": {
#                 "user_id": {
#                     "type": "string",
#                     "description": "The user to search for.",
#                 }
#             },
#             "required": ["my_param"],
#         }
#     },
#     "description": "This a tool that gets a user by its ID. "
#                    "Returns: A dictionary representing the user.",
# }

Additionally, you can use the ToolLibrary class to make it easy to pass in a list of tools directly to your LLM call.

# ^ continuing from above
from pytoolsmith import ToolLibrary

# Make a library:
tool_library = ToolLibrary()
tool_library.add_tool(tool_definition)
tool_library.add_tool(other_tool_definition)

# Get a tool list ready to pass directly into LLM calls.
tool_library.to_openai()
tool_library.to_anthropic()
tool_library.to_bedrock()
# All of these are a list that can be passed in directly to your LLM call.

When you implement your LLM call, you can use the tool library to get the tool list, and call it directly.

# ^ continuing from above
from anthropic import Anthropic
from anthropic.types import MessageParam, TextBlockParam

from pytoolsmith import ToolDefinition

client = Anthropic()

hardset_parameters = {"tenant_id": "1234"}

# Get the tool name and parameters from the LLM call
llm_result = client.messages.create(
    system="You are a helpful assistant who can look up users by their ID.",
    tools=tool_library.to_anthropic(),
    model="claude-3-7-sonnet-latest",
    messages=[
        MessageParam(
            role="user",
            content=[TextBlockParam(text="Can you help me look up my account? My id is 5678", type="text")],
        )
    ],
    max_tokens=100,
)

llm_set_params, tool_name = parse_llm_result(llm_result)
# `llm_set_params` would be like: {"user_id": "5678"}
# This was decided by the LLM and passed back as something to call.

tool: ToolDefinition = tool_library.get_tool_from_name(tool_name)

user_message = tool.format_message_for_call(llm_parameters=llm_set_params, hardset_parameters=hardset_parameters)
# The user message will be `Looking up a user using id 5678.`
tool_result = tool.call_tool(
    llm_parameters=llm_set_params,
    hardset_parameters=hardset_parameters,
)
# Result is ready to be passed back to the next LLM call.
# The user message will be `Looking up a user using id 5678.`

Additionally, you can control the serialization parameters:

from bson import ObjectId
from pytoolsmith import pytoolsmith_config

pytoolsmith_config.update_type_map({ObjectId: "string"})


# Now you can define the following function as a tool:
def get_object_by_id(object_id: ObjectId) -> dict:
    ...

Additional Configuration

Library Subsetting
Sometimes you may not want to pass all of your tools to the LLM. Subsetting allows you to select a smaller set of tools to pass in- either individually or by tagging them with a tool_group parameter on your ToolDefinitions.

To use, call the subset() method on a ToolLibrary instance to get a smaller library generated.

Vendor-Specific Options
If needed, additional OpenAPI spec can be passed into a ToolDefinition constructor with the

The following client-specific configuration options are available as options on the to_<provider> methods:

Future Plans

  • Support for tuples as fixed-length lists
  • Extendable serialization support (for LLM messages -> function inputs and vise versa)

A Note

I was heavily inspired by FastAPI's batteries-included ability to create automatic OpenAPI specs for web applications. Having a single source of truth for your API docs defined as code speeds up development and reduces the chance of errors - why not apply that to LLM interfaces? 🤠

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

pytoolsmith-0.1.5.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

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

pytoolsmith-0.1.5-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file pytoolsmith-0.1.5.tar.gz.

File metadata

  • Download URL: pytoolsmith-0.1.5.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.6

File hashes

Hashes for pytoolsmith-0.1.5.tar.gz
Algorithm Hash digest
SHA256 244a8592e1758f5c120096eedb5e5ad6b9756ec095c2cb5d7ef15cd626b295c8
MD5 cb308d8733acd4babd91f6d222f7af1e
BLAKE2b-256 bec4cef435c3948f7b63cb8d795d750d0249cd700673102e2ed9614658c202a5

See more details on using hashes here.

File details

Details for the file pytoolsmith-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pytoolsmith-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 38c738afa1c0895f2c0e3b4b6a915571d84efae0692ec595940ff4dd4620b128
MD5 883e19f5258e3d5acd79b9c59484b5ba
BLAKE2b-256 5110df72fe9f93b765ab3ad5bc953f92cbb99bb381091fd83b332256ae68c1df

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