Skip to content

Commit 8982285

Browse files
committed
Created a custom tool notebook that demonstrates how to add a custom tool to an Open Interpreter instance, including configuring the instance and defining a custom tool using Python.
1 parent 33ca1eb commit 8982285

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

examples/custom_tool.ipynb

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Add a Custom Tool to your Instance\n",
8+
"\n",
9+
"You can create custom tools for your instance of Open Interpreter. This is extremely helpful for adding new functionality in a reliable way.\n"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"First, create a profile and configure your instance:"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"# Configure Open Interpreter\n",
26+
"from interpreter import interpreter\n",
27+
"\n",
28+
"interpreter.llm.model = \"claude-3-5-sonnet-20240620\"\n",
29+
"interpreter.computer.import_computer_api = True\n",
30+
"interpreter.llm.supports_functions = True\n",
31+
"interpreter.llm.supports_vision = True\n",
32+
"interpreter.llm.context_window = 100000\n",
33+
"interpreter.llm.max_tokens = 4096"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"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:"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"custom_tool = \"\"\"\n",
50+
"\n",
51+
"import os\n",
52+
"import requests\n",
53+
"\n",
54+
"def search_aws_docs(query):\n",
55+
"\n",
56+
" url = \"https://api.perplexity.ai/chat/completions\"\n",
57+
"\n",
58+
" payload = {\n",
59+
" \"model\": \"llama-3.1-sonar-small-128k-online\",\n",
60+
" \"messages\": [\n",
61+
" {\n",
62+
" \"role\": \"system\",\n",
63+
" \"content\": \"Be precise and concise.\"\n",
64+
" },\n",
65+
" {\n",
66+
" \"role\": \"user\",\n",
67+
" \"content\": query\n",
68+
" }\n",
69+
" ],\n",
70+
" \"temperature\": 0.2,\n",
71+
" \"top_p\": 0.9,\n",
72+
" \"return_citations\": True,\n",
73+
" \"search_domain_filter\": [\"docs.aws.amazon.com\"],\n",
74+
" \"return_images\": False,\n",
75+
" \"return_related_questions\": False,\n",
76+
" #\"search_recency_filter\": \"month\",\n",
77+
" \"top_k\": 0,\n",
78+
" \"stream\": False,\n",
79+
" \"presence_penalty\": 0,\n",
80+
" \"frequency_penalty\": 1\n",
81+
" }\n",
82+
" headers = {\n",
83+
" \"Authorization\": f\"Bearer {os.environ.get('PPLX_API_KEY')}\",\n",
84+
" \"Content-Type\": \"application/json\"\n",
85+
" }\n",
86+
"\n",
87+
" response = requests.request(\"POST\", url, json=payload, headers=headers)\n",
88+
"\n",
89+
" print(response.text)\n",
90+
"\n",
91+
" return response.text\n",
92+
"\n",
93+
"\"\"\"\n"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"Finally, you add the tool to the OI instance's computer:"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"interpreter.computer.run(\"python\", custom_tool)"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"> Note: You can define and set multiple tools in a single instance."
117+
]
118+
}
119+
],
120+
"metadata": {
121+
"language_info": {
122+
"name": "python"
123+
}
124+
},
125+
"nbformat": 4,
126+
"nbformat_minor": 2
127+
}

0 commit comments

Comments
 (0)