Skip to main content

ToolFuse

A common protocol for AI agent tools
Explore the docs »

View Demo · Report Bug · Request Feature


ToolFuse provides a common protocol for AI agent tools, use them with your favorite agent framework or model.

Installation

pip install toolfuse

Usage

A simple weather logger tool

from toolfuse import Tool, action, observation
from selenium import webdriver


class WeatherLogger(Tool):
  """A simple weather logger."""

    @action
    def log(self, message: str) -> None:
        """Logs a message to the log file."""

        with open("weather.txt", "a") as file:
            file.write("***\n" + message + "\n")

    @observation
    def weather(self, location: str) -> str:
        """Checks the current weather from the internet using wttr.in."""

        weather_api_url = f"http://wttr.in/{location}?format=%l:+%C+%t"
        response = requests.get(weather_api_url)
        response.raise_for_status()
        return response.text

The functions to be made available to the agent as

  • @action if they mutate the environment
  • @observation if they are read only.

Use the tool in a prompt

weather_logger = WeatherLogger()

msg = f"Please select the appropriate action from {weather_logger.json_schema()}"

Create a tool from an existing class

from toolfuse import tool

class Foo:
  def echo(self, txt: str) -> str:
    return txt


foo_tool = tool(Foo())

Create a tool from an existing function

from toolfuse import tool

def echo(txt: str) -> str:
  return txt

echo_tool = tool(echo)

Function Calling

Use a tool with OpenAI function calling

from openai import OpenAI

client = OpenAI()

weatherlogger = WeatherLogger()
schemas = weatherlogger.json_schema()

messages = []
messages.append({"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."})
messages.append({"role": "user", "content": "What is the weather in Paris?"})

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + openai.api_key,
}
json_data = {"model": model, "messages": messages, "tools": schemas}
response = requests.post(
    "https://api.openai.com/v1/chat/completions",
    headers=headers,
    json=json_data,
)

assistant_message = response.json()["choices"][0]["message"]
messages.append(assistant_message)
assistant_message
{
  "role": "assistant",
  "tool_calls": [
    {
      "id": "call_RYXaDjxpUCfWmpXU7BZEYVqS",
      "type": "function",
      "function": {
        "name": "weather",
        "arguments": "{\n  \"location\": \"Paris\"}"
      }
    }
  ]
}

Then to use this action

for tool in assistant_message["tool_calls"]:
    action = weatherlogger.find_action(tool["function"]["name"])
    args = json.loads(tool["function"]["arguments"])
    resp = weatherlogger.use(action, **args)

MultiTool

Combine tools with MultiTool

from toolfuse import Tool, MultiTool

class Chat(Tool):
  """A simple chat tool"""

    @action
    def send_message(self, message: str) -> None:
        """Logs a message to the log file."""

        with open("chat.txt", "a") as file:
            file.write("***\n" + message + "\n")


multitool = MultiTool(tools=[WeatherLogger(), Chat()])
multitool.json_schema()

Merge one tools actions into another

chat_tool = Chat()
chat_tool.merge(WeatherLogger())

Add an action to a tool

def echo(txt: str) -> str:
  return txt

weather_tool = WeatherLogger()
weather_tool.add_action(echo)

Available Tools

:computer: AgentDesk provides AI agents with a full GUI desktop locally or in the cloud.

:wrench: AgentUtils provides some basic utilities for agentic tool use

Roadmap

  • Integrate with langchain
  • Integrate with babyagi
  • Integrate with autogen
  • Integrate with llamaindex

Metadata

Release files for toolfuse 0.1.25

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toolfuse 0.1.25
File Size Uploaded
toolfuse-0.1.25.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for toolfuse 0.1.25
File Interpreter ABI Platform
toolfuse-0.1.25-py3-none-any.whl Python 3 none any Details

Total release size: 24.2 kB

Release files / toolfuse-0.1.25.tar.gz

Download URL toolfuse-0.1.25.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
54948d6b4faf2fd2837e33534629db8fc3bcd1fbeb2d643ceb691b6b776dad59
BLAKE2b-256 checksum
How to use checksums
0d5fcc186b5a6f7d21219c7e10cc9b36dc57dd64f9e885f332c1e4c33d53eb2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.1 Darwin/22.6.0

Release files / toolfuse-0.1.25-py3-none-any.whl

Download URL toolfuse-0.1.25-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
144d6b97441cb93cf9ca33c405732fdeba33cb2441dc1f8978e7c2e450497aee
BLAKE2b-256 checksum
How to use checksums
d7b209bc7b58930e0ea7a6e66e358a7af0bac0b2b261667ee513f2b146cff85c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.1 Darwin/22.6.0

Release history Release notifications | RSS feed

This release

0.1.25 This release

2 release files

0.1.24

2 release files

0.1.19

2 release files

0.1.16

2 release files

0.1.15

2 release files

0.1.12

2 release files

0.1.11

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page