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

codecov

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.

Supported Providers

  • Anthropic
  • AWS Bedrock
  • OpenAI (coming soon)

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
  3. Pydantic models

Usage

Simply define a tool definition as such:

from pytoolsmith import ToolDefinition


# 1. Define your function
def my_tool(my_param: str | None, my_controlled_param: str = "hello") -> str:
    """
    This a tool that formats a specific string with parameters.
   
    Args:
       my_param: A parameter controlled by the LLM
       my_controlled_param: A parameter controlled by the application.
       
    Returns: A formatted string.
    """
    return f"I did a search for {my_param} with controlled parameter {my_controlled_param}!"


# 2. Make a tool definition, calling out the injected parameter.
tool_definition = ToolDefinition(function=my_tool, injected_parameters=["my_controlled_param"])

# 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": "my_tool",
#     "inputSchema": {
#         "json": {
#             "type": "object",
#             "properties": {
#                 "my_param": {
#                     "anyOf": [{"type": "string"}, {"type": "null"}],
#                     "description": "A parameter controlled by the LLM",
#                 }
#             },
#             "required": ["my_param"],
#         }
#     },
#     "description": "This a tool that formats a specific string with parameters. Returns: A formatted string.",
# }

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.

Additionally, you can control the serialization parameters:

from bson import ObjectId
from pytoolsmith import pytoolsmith_config

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

Future Work

  • Live LLM test for OpenAI
  • Add support for calling tools & returning results directly from the library
    • Include serialization of LLM inputs to function input types
  • Support installations requiring pydantic v1
  • Publish on PyPI 🤠

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.0.tar.gz (35.0 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.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytoolsmith-0.1.0.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.31

File hashes

Hashes for pytoolsmith-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1f05714bb21e6962e0b9fab661752c044f7ef30dc8fecce1575992676aa451c8
MD5 cc75df0718751d1648f9b7401952915a
BLAKE2b-256 41f763f6f8d23ba5d214edaa4f1f898ee8397baa7499be5ac5b573b64293b63f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytoolsmith-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb10794490ca5c6f0e6c5ab9c26749cfa4b7dd7b81089e8bddd5e01d3c8b7979
MD5 6ca76af1cbd3c622361e5eb1884666fa
BLAKE2b-256 1e5de98173e6daab6d33750f9ff511b3e350446347b65f3e5c1c88476ed2b8e8

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