Skip to main content

A library to easily build LLM agents with tool-using capabilities.

Project description

LLM Tool Harness

PyPI version License: MIT Python Versions

A Python library to easily build LLM agents with tool-using capabilities, allowing them to interact with external systems and perform actions.

Key Features

  • Simple Agent Creation: Quickly set up an LLM-powered agent.
  • Easy Tool Definition: Define custom tools with clear schemas and connect them to your Python functions.
  • Pluggable LLM Providers: Start with Anthropic (Claude) and easily extend to other LLM providers.
  • Conversation Management: Automatically handles conversation history and the flow of tool calls and results.
  • Extensible: Designed to be a foundation for more complex agentic systems.

Installation

pip install llm-tool-harness

You will also need to set up API keys for the LLM providers you intend to use. For Anthropic:

export ANTHROPIC_API_KEY="your_anthropic_api_key_here"

Quick Start

Here's how to create a simple agent that can use a bash tool:

import os
import subprocess
from llm_tool_harness import LLMAgent, Tool, AnthropicProvider, ToolInputSchema

# 1. Define your tool implementation function
def execute_bash_command(command: str) -> str:
    """Execute a bash command and return a formatted string with the results."""
    print(f"Executing bash command: {command}")
    try:
        result = subprocess.run(
            ["bash", "-c", command],
            capture_output=True,
            text=True,
            timeout=30
        )
        output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}\nEXIT CODE: {result.returncode}"
        print(f"Bash output:\n{output}")
        return output
    except subprocess.TimeoutExpired:
        return f"Error: Command '{command}' timed out after 30 seconds."
    except Exception as e:
        return f"Error executing command '{command}': {str(e)}"

def main():
    # Ensure ANTHROPIC_API_KEY is set (or handle error)
    if "ANTHROPIC_API_KEY" not in os.environ:
        print("Error: ANTHROPIC_API_KEY environment variable not set.")
        return

    # 2. Create an LLM provider instance
    # Replace with your desired Claude model, e.g., "claude-3-opus-20240229", "claude-3-haiku-20240307"
    anthropic_llm = AnthropicProvider(model="claude-3-sonnet-20240229", max_tokens=2000)

    # 3. Create the LLM Agent
    agent = LLMAgent(
        llm_provider=anthropic_llm,
        system_prompt="""You are a helpful AI assistant with access to a bash terminal.
        You can help the user by executing commands and interpreting the results.
        Be careful with destructive commands and always ask for confirmation if unsure.
        You have access to the 'bash' tool which allows you to run shell commands."""
    )

    # 4. Define the tool schema and create a Tool object
    bash_input_schema: ToolInputSchema = {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "The bash command to execute (e.g., 'ls -l', 'echo hello')"
            }
        },
        "required": ["command"]
    }
    bash_tool = Tool(
        name="bash",
        description="Execute bash shell commands on the local system and return the output.",
        input_schema=bash_input_schema,
        implementation=execute_bash_command
    )

    # 5. Register the tool with the agent
    agent.register_tool(bash_tool)

    # 6. Start the interactive chat loop
    # Or, for programmatic use:
    # response = agent.process_message("What files are in the current directory?")
    # print(f"Agent: {response}")
    agent.chat_loop()

if __name__ == "__main__":
    main()

Core Concepts

LLMAgent: The main orchestrator. It manages the conversation with an LLM, handles tool calls, and invokes tool implementations.

Tool: Represents an external capability the LLM can use. It consists of:

name: A unique identifier.

description: A natural language description for the LLM to understand when to use the tool.

input_schema: A JSON schema defining the parameters the tool expects.

implementation: The Python function that executes the tool's logic.

LLMProvider: An abstraction for different LLM backends (e.g., AnthropicProvider). This allows the agent to be LLM-agnostic.

Developing Custom Tools

Write a Python function that performs the desired action. This function will be the tool's implementation. It should accept arguments as defined in your tool's input_schema and return a string or a serializable dictionary.

Define an input_schema (JSON schema) that describes the parameters your function takes.

Create a Tool object providing the name, description, schema, and implementation function.

Register the Tool with your LLMAgent instance using agent.register_tool(your_tool).

Contributing

Contributions are welcome! Please feel free to open an issue to discuss a new feature or bug, or submit a pull request.

(Consider adding a CONTRIBUTING.md file with more detailed guidelines if the project grows).

License

This project is licensed under the MIT License - see the LICENSE file for details.

Before you commit these files:

  1. Replace placeholders in LICENSE:
    • [Year] with the current year.
    • [Your Full Name or Organization Name] with your name or org name.
  2. Review placeholders in README.md:
    • The build status badge URL (yourusername/llm-tool-harness) should be updated if you set up CI on GitHub Actions or another service.
    • Check the pip install name if you decide to call your package something other than llm-tool-harness on PyPI.
    • Update model names in the example if newer ones become standard (e.g., "claude-3-sonnet-20240229" is current but models evolve).

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_tool_harness-0.1.0.tar.gz (13.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_tool_harness-0.1.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_tool_harness-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for llm_tool_harness-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cac7ee61f2cc49bfcc9c46a75efd6a0cadad75138bede33314c6add0c09d6a3c
MD5 6608ff4caa6372c4403badc48ec0700a
BLAKE2b-256 66290089d9c73f57f1a4962c0adbf0d9c88047ddef559ee441089ca9d8fe4785

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llm_tool_harness-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48704e71672de6cab6531990a59717bb5c6f99683bcfd1742721197f5aae9f99
MD5 bcb55bd604bae88739dd84eec270e7b9
BLAKE2b-256 6fee3c81904ce5121c21b40ec2eedbcdb393e7299d70ecd0e5558cd4aa383f60

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