Skip to main content

Agent Tools

A common protocol for AI agent tools

Installation

pip install agent-tool

Usage

Let's define a simplified Selenium web browser tool

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


class SeleniumBrowser(Tool):
    """Selenium browser as a tool"""

    def __init__(self, headless: bool = True) -> None:
        super().__init__()
        options = webdriver.ChromeOptions()
        if headless:
            options.add_argument("--headless")
        self.driver = webdriver.Chrome(options=options)

    @action
    def open_url(self, url: str) -> None:
        """Open a URL in the browser

        Args:
            url (str): URL to open
        """
        self.driver.get(url)

    @action
    def click_element(self, selector: str, selector_type: str = "css_selector") -> None:
        """Click an element identified by a CSS selector

        Args:
            selector (str): CSS selector
            selector_type (str, optional): Selector type. Defaults to "css_selector".
        """
        element = self.driver.find_element(selector_type, selector)
        element.click()

    @observation
    def get_html(self) -> str:
        """Get the entire HTML of the current page.

        Returns:
            str: Page HTML
        """
        return self.driver.page_source

    def close(self) -> None:
        """Close the tool"""
        self.driver.quit()

We mark the functions to be made available to the agent as @action if they mutate the environment, and @observation if they are read only.

Now we can use this tool with an agent such as openai function calling

from openai import OpenAI

client = OpenAI()

browser = SeleniumBrowser()
schemas = browser.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": "Get the HTML for the front page of wikipedia"})

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": "open_url",
        "arguments": "{\n  \"url\": \"https://wikipedia.org\"}"
      }
    },
    {
      "id": "call_TJIWlknwdoinfWEMFNss",
      "type": "function",
      "function": {
        "name": "get_html",
        "arguments": ""
      }
    }
  ]
}

Then to use this action

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

Tools can be used locally or spun up on a server and used remotely (In progress)

Share (In progress)

Register a tool with the AgentSea hub so others can find and use it.

pip install agentsea

Create a repo to publish

agentsea create tool

Add your tool to the tool.py in the repo, fill in the README.md and add your dependencies using Poetry

Publish to the hub

agentsea publish .

Roadmap

  • Integrate with langchain, babyagi, autogpt, etc

Metadata

Release files for agent-tool 0.1.6

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

Source distribution (sdist)

Source distribution for agent-tool 0.1.6
File Size Uploaded
agent_tool-0.1.6.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agent-tool 0.1.6
File Interpreter ABI Platform
agent_tool-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / agent_tool-0.1.6.tar.gz

Download URL agent_tool-0.1.6.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4449d11a6a53462bc05848878b547f46f4d991c045e599211d7d7effb64c9f6b
BLAKE2b-256 checksum
How to use checksums
be0d3d7bca58ae5c8b54462afcb74ff984bf5710785ad7d01fda275c413db7ed
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 / agent_tool-0.1.6-py3-none-any.whl

Download URL agent_tool-0.1.6-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0e3dedde2c4ddaa2a114b067315e24ea50a66c5f18123e96ffbac75e5128d26
BLAKE2b-256 checksum
How to use checksums
499d6039b3c0d2abf9316c4f52d6b261ef1b89f6227755dcbbc1077af7831768
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.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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