Skip to main content

Minimal metadata and discovery helpers for typed Python tools used by llm_function runtimes.

Project description

Llm function tools

llm_function_tools is an extension component for llm_function.

It provides small helpers for defining tools used by reusable LLM functions, including tools stored in standalone .py files.

Currently intended usage pattern for llm_function_tools:

  • define typed tools with @llm_tool
  • inspect the attached ToolSpec
  • discover tools from a Python module
  • load tools from a standalone .py file
from pathlib import Path
from tempfile import TemporaryDirectory

from pydantic import BaseModel, Field

from llm_function_tools import (
    discover_tools_in_module,
    get_tool_spec,
    llm_tool,
    load_tools_from_python_file,
    tool_from_callable,
)

Define typed tools

A tool is just a function with one BaseModel input and one BaseModel output. @llm_tool attaches a normalized ToolSpec to the function.

class WeatherInput(BaseModel):
    city: str = Field(..., description="City name.")


class WeatherOutput(BaseModel):
    forecast: str = Field(..., description="Weather forecast.")


@llm_tool(tags=["weather"], metadata={"scope": "demo"})
def get_weather(inputs: WeatherInput) -> WeatherOutput:
    """Get current weather for a city."""
    return WeatherOutput(forecast=f"Sunny in {inputs.city}")


tool_spec = get_tool_spec(get_weather)
tool_spec
ToolSpec(func=<function get_weather at 0x7e0640de5360>, name='get_weather', description='Get current weather for a city.', input_model=<class '__main__.WeatherInput'>, output_model=<class '__main__.WeatherOutput'>, metadata={'scope': 'demo'}, tags=['weather'])

Create a ToolSpec from a plain callable

You can also build ToolSpec directly without decorating the function.

class SearchInput(BaseModel):
    query: str


class SearchOutput(BaseModel):
    result: str


def search_notes(inputs: SearchInput) -> SearchOutput:
    """Search local notes."""
    return SearchOutput(result=f"Found: {inputs.query}")


tool_from_callable(search_notes)
ToolSpec(func=<function search_notes at 0x7e0640de5090>, name='search_notes', description='Search local notes.', input_model=<class '__main__.SearchInput'>, output_model=<class '__main__.SearchOutput'>, metadata={}, tags=[])

Discover tools from a module

In normal usage, loaders will inspect a Python module or file and collect decorated tools from it.

In a notebook, __main__ behaves as the current module, so we can demonstrate the same discovery flow here.

import __main__

discovered = discover_tools_in_module(__main__)
[(tool.name, tool.tags, tool.metadata) for tool in discovered]
[('get_weather', ['weather'], {'scope': 'demo'})]

Load tools from a standalone .py file

This is closer to the future llm_function use case where tools live outside the runtime module.

tmp_dir = TemporaryDirectory()
tool_file = Path(tmp_dir.name) / "sample_tools.py"
tool_file.write_text(
    """
from pydantic import BaseModel
from llm_function_tools import llm_tool


class MathInput(BaseModel):
    x: int


class MathOutput(BaseModel):
    y: int


@llm_tool(tags=['math'])
def add_one(inputs: MathInput) -> MathOutput:
    '''Add one to the input value.'''
    return MathOutput(y=inputs.x + 1)
""".strip()
)

file_tools = load_tools_from_python_file(str(tool_file))
[(tool.name, tool.description, tool.tags) for tool in file_tools]
[('add_one', 'Add one to the input value.', ['math'])]
file_tools
[ToolSpec(func=<function add_one at 0x7e06361ae830>, name='add_one', description='Add one to the input value.', input_model=<class '_llm_function_tools_sample_tools_8306dd8f7421.MathInput'>, output_model=<class '_llm_function_tools_sample_tools_8306dd8f7421.MathOutput'>, metadata={}, tags=['math'])]

The resulting ToolSpec objects are the input that a higher-level runtime such as llm_function can later resolve into runnable tool registries with provenance metadata.

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

llm_function_tools-0.0.1.tar.gz (643.8 kB view details)

Uploaded Source

Built Distribution

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

llm_function_tools-0.0.1-py3-none-any.whl (704.6 kB view details)

Uploaded Python 3

File details

Details for the file llm_function_tools-0.0.1.tar.gz.

File metadata

  • Download URL: llm_function_tools-0.0.1.tar.gz
  • Upload date:
  • Size: 643.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for llm_function_tools-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a7a9098ee2df300e903473bca92ceefd9ebf6ab8517e18273e98983643339db9
MD5 884284240d0780661ba3e15c6c503eea
BLAKE2b-256 4f5d8ef48e4f660d7303b005d6952f3ad0af46c62b714c3bbf3b55b7f6d42077

See more details on using hashes here.

File details

Details for the file llm_function_tools-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_function_tools-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e466d90816d2f7ce311bec63b5cd2f4a956dfbeeed209b18f4d314665d75434b
MD5 09fa0535d0e5612f3e93db2903a82910
BLAKE2b-256 9b6b506ea1e49251cc155979347bc2990befae4cedace77bedb3b91666bc5b08

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