Back to Open Interpreter

Add a Custom Tool to your Instance

examples/custom_tool.ipynb

0.4.22.0 KB
Original Source

Add a Custom Tool to your Instance

You can create custom tools for your instance of Open Interpreter. This is extremely helpful for adding new functionality in a reliable way.

First, create a profile and configure your instance:

python
# Configure Open Interpreter
from interpreter import interpreter

interpreter.llm.model = "claude-3-5-sonnet-20240620"
interpreter.computer.import_computer_api = True
interpreter.llm.supports_functions = True
interpreter.llm.supports_vision = True
interpreter.llm.context_window = 100000
interpreter.llm.max_tokens = 4096

Then you will define your custom tool by writing valid Python code within a comment. This example is for searching the AWS documentation using Perplexity:

python
custom_tool = """

import os
import requests

def search_aws_docs(query):

    url = "https://api.perplexity.ai/chat/completions"

    payload = {
        "model": "llama-3.1-sonar-small-128k-online",
        "messages": [
            {
                "role": "system",
                "content": "Be precise and concise."
            },
            {
                "role": "user",
                "content": query
            }
        ],
        "temperature": 0.2,
        "top_p": 0.9,
        "return_citations": True,
        "search_domain_filter": ["docs.aws.amazon.com"],
        "return_images": False,
        "return_related_questions": False,
        #"search_recency_filter": "month",
        "top_k": 0,
        "stream": False,
        "presence_penalty": 0,
        "frequency_penalty": 1
    }
    headers = {
        "Authorization": f"Bearer {os.environ.get('PPLX_API_KEY')}",
        "Content-Type": "application/json"
    }

    response = requests.request("POST", url, json=payload, headers=headers)

    print(response.text)

    return response.text

"""

Finally, you add the tool to the OI instance's computer:

python
interpreter.computer.run("python", custom_tool)

Note: You can define and set multiple tools in a single instance.