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.2.tar.gz (647.5 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.2-py3-none-any.whl (708.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_function_tools-0.0.2.tar.gz
  • Upload date:
  • Size: 647.5 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.2.tar.gz
Algorithm Hash digest
SHA256 0d9908cee0409184671f18cfde08e690070aab2335e3f9beba05d6aff76b3fa7
MD5 0ac86b678f5e5a8a43f1e001aab10859
BLAKE2b-256 488f8bdb1fb2794d9abed6cc161ffbc901a4188a6e0899bf848e9696e74020c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llm_function_tools-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d71d94aec21466a65989c1c53d4b38abde4ffe478cf29e6c618f6604dac308f5
MD5 b0ed7378ed4cb13421c472cd6cb38cf9
BLAKE2b-256 7ee2dd8c4986c4a275df825f3fa11fe1ff31ebb4e7f8ff590601c062fa457e7e

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