Skip to main content

Dynamically generate JSON Schema from Python code.

Project description

llmfuncs

llmfuncs is a Python package for documenting, managing, and validating Python functions based on their signature and docstrings. It automatically generates JSON Schema for functions, and provides a way to validate function arguments against the schema.

Features

  • Automatic generation of JSON Schema from Python function signature and docstrings.
  • Support for Python built-in types and many typing module types.
  • Use and validate functions with JSON arguments against their schema.
  • Management of multiple functions through a ToolCollection object.
  • Bulk addition of functions from a module, a package, or a glob pattern.
  • Simple and intuitive API.

Installation

Install llmfuncs using pip:

pip install llmfuncs

Usage

Tool

The Tool class encapsulates a function and its associated schema.

from llmfuncs.tool import Tool

def greet(name: str) -> str:
    """Greet someone.

    Args:
        name: Name of the person to greet.

    Returns:
        A greeting message.
    """
    return f"Hello, {name}!"

tool = Tool(greet)
print(tool.name())  # "greet"
print(tool.schema())

ToolCollection

The ToolCollection class is a container for multiple Tool objects. It provides methods to add tools and use them.

from llmfuncs.tool import Tool, ToolCollection

tool_collection = ToolCollection()
tool_collection.add_tool(tool)
print(len(tool_collection))  # 1

You can also add tools in bulk from a module, a package, or a glob pattern.

tool_collection.add_tools_from_module('some_module')
tool_collection.add_tools_from_package('some_package')
tool_collection.add_tools_from_glob('*.py')

And here's how to use a tool:

json_args = '{"name": "World"}'
result = tool_collection.use_tool("greet", json_args)
print(result)  # "Hello, World!"

For more detailed usage and examples, please check the API documentation and the example scripts in the examples folder.

Creating New Tools

If you have an existing module that you want to work with llmfuncs, you can try passing it through an LLM to generate the type hints and docstrings for any functions missing them. For example:

Please update this module to add type hints to all function parameters and Google style docstrings to each function.

Contribute

We welcome contributions to llmfuncs!

License

This project is licensed under the terms of the MIT license. For more details, see the LICENSE file.

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

llmfuncs-0.2.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

llmfuncs-0.2.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file llmfuncs-0.2.2.tar.gz.

File metadata

  • Download URL: llmfuncs-0.2.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for llmfuncs-0.2.2.tar.gz
Algorithm Hash digest
SHA256 5fb68926ae5db4336663d1395d64a62f571706fce14936d9bb3f332fa3f68a65
MD5 09b15272b6c37b12db634b97959951cb
BLAKE2b-256 8bccd69e8ea79a51a6188a7a72d16cd57bb4b32898b6cc807f907ec849162b79

See more details on using hashes here.

File details

Details for the file llmfuncs-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: llmfuncs-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for llmfuncs-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 627d72d8fc6d01c87877171e30436522b653755bbf6b90d41b854c839d895606
MD5 456b9d202f7f27c87561f1efb8af1fca
BLAKE2b-256 5b72498a0a7c41a592f8daa31b59a9363b88ce9ecc88f5be4c635c9b5dc270e4

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