Skip to main content

A protocol for AI agent tools

Project description


OpenTool

A common protocol for AI agent tools
Explore the docs »

View Demo · Report Bug · Request Feature


OpenTool provides a simple common potocol for AI agent tools, allowing use accross different model types and frameworks.

Installation

pip install opentool-ai

Usage

Let's define a simplified Selenium web browser tool

from opentool 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

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

opentool_ai-0.1.7.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

opentool_ai-0.1.7-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file opentool_ai-0.1.7.tar.gz.

File metadata

  • Download URL: opentool_ai-0.1.7.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.1 Darwin/22.6.0

File hashes

Hashes for opentool_ai-0.1.7.tar.gz
Algorithm Hash digest
SHA256 508f7cb82d3a2622b97ea8b28c3c56ef3d633758a9fab256d86b3c7ad0718f33
MD5 7baf72d180bd97ba1ff827f8e8ca5654
BLAKE2b-256 db2287c3546fd6c093386c22195d32eb2d4d58c9cf1295dbe5204a9e2aa9d0b3

See more details on using hashes here.

File details

Details for the file opentool_ai-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: opentool_ai-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.1 Darwin/22.6.0

File hashes

Hashes for opentool_ai-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 491465c44b606f2bc6b7c1ce1b0466955611459cfa5d462f9409e39ffe2da26e
MD5 30f4b31603deae0cf492264f3de8fd7b
BLAKE2b-256 102a51b968d4d4f64433974ae05f14adf6615e18b164d94a538c302374c121f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page