Update agent.py
Browse files
agent.py
CHANGED
@@ -18,11 +18,15 @@ from supabase.client import Client, create_client
|
|
18 |
from langchain_openai import ChatOpenAI
|
19 |
|
20 |
from langchain.tools import Tool
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
load_dotenv()
|
24 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
25 |
|
|
|
26 |
def multiply(a: int, b: int) -> int:
|
27 |
return a * b
|
28 |
|
@@ -70,6 +74,29 @@ modulus_tool = Tool(
|
|
70 |
description="Modulus two numbers. Args (a: first int, b: second int)"
|
71 |
)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
def wiki_search(query: str) -> str:
|
74 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
75 |
formatted_search_docs = "\n\n---\n\n".join(
|
@@ -120,6 +147,81 @@ arvix_search_tool = Tool(
|
|
120 |
)
|
121 |
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
# load the system prompt from the file
|
124 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
125 |
system_prompt = f.read()
|
@@ -133,6 +235,8 @@ tools = [
|
|
133 |
substract_tool,
|
134 |
divide_tool,
|
135 |
modulus_tool,
|
|
|
|
|
136 |
wiki_search_tool,
|
137 |
web_search_tool,
|
138 |
arvix_search_tool,
|
|
|
18 |
from langchain_openai import ChatOpenAI
|
19 |
|
20 |
from langchain.tools import Tool
|
21 |
+
from code_interpreter import CodeInterpreter
|
22 |
+
|
23 |
+
interpreter_instance = CodeInterpreter()
|
24 |
|
25 |
|
26 |
load_dotenv()
|
27 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
28 |
|
29 |
+
### ======================================== MATHEMATICAL TOOLS ======================================== ###
|
30 |
def multiply(a: int, b: int) -> int:
|
31 |
return a * b
|
32 |
|
|
|
74 |
description="Modulus two numbers. Args (a: first int, b: second int)"
|
75 |
)
|
76 |
|
77 |
+
def power(a: float, b: float) -> float:
|
78 |
+
return a**b
|
79 |
+
|
80 |
+
power_tool = Tool(
|
81 |
+
name="power",
|
82 |
+
func=power,
|
83 |
+
description="Power two numbers. Args (a: first float, b: second float)"
|
84 |
+
)
|
85 |
+
|
86 |
+
def square_root(a: float) -> float | complex:
|
87 |
+
if a >= 0:
|
88 |
+
return a**0.5
|
89 |
+
return cmath.sqrt(a)
|
90 |
+
|
91 |
+
square_root_power = Tool(
|
92 |
+
name="square_root",
|
93 |
+
func=square_root,
|
94 |
+
description="Square two numbers. Args (a: float)"
|
95 |
+
)
|
96 |
+
|
97 |
+
|
98 |
+
### ======================================== BROWSER TOOLS ======================================== ###
|
99 |
+
|
100 |
def wiki_search(query: str) -> str:
|
101 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
102 |
formatted_search_docs = "\n\n---\n\n".join(
|
|
|
147 |
)
|
148 |
|
149 |
|
150 |
+
### ======================================== CODE INTERPRETER TOOLS ======================================== ###
|
151 |
+
def execute_code_multilang(code: str, language: str = "python") -> str:
|
152 |
+
"""Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
|
153 |
+
Args:
|
154 |
+
code (str): The source code to execute.
|
155 |
+
language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
|
156 |
+
Returns:
|
157 |
+
A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any).
|
158 |
+
"""
|
159 |
+
supported_languages = ["python", "bash", "sql", "c", "java"]
|
160 |
+
language = language.lower()
|
161 |
+
|
162 |
+
if language not in supported_languages:
|
163 |
+
return f"❌ Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}"
|
164 |
+
|
165 |
+
result = interpreter_instance.execute_code(code, language=language)
|
166 |
+
|
167 |
+
response = []
|
168 |
+
|
169 |
+
if result["status"] == "success":
|
170 |
+
response.append(f"✅ Code executed successfully in **{language.upper()}**")
|
171 |
+
|
172 |
+
if result.get("stdout"):
|
173 |
+
response.append(
|
174 |
+
"\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```"
|
175 |
+
)
|
176 |
+
|
177 |
+
if result.get("stderr"):
|
178 |
+
response.append(
|
179 |
+
"\n**Standard Error (if any):**\n```\n"
|
180 |
+
+ result["stderr"].strip()
|
181 |
+
+ "\n```"
|
182 |
+
)
|
183 |
+
|
184 |
+
if result.get("result") is not None:
|
185 |
+
response.append(
|
186 |
+
"\n**Execution Result:**\n```\n"
|
187 |
+
+ str(result["result"]).strip()
|
188 |
+
+ "\n```"
|
189 |
+
)
|
190 |
+
|
191 |
+
if result.get("dataframes"):
|
192 |
+
for df_info in result["dataframes"]:
|
193 |
+
response.append(
|
194 |
+
f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**"
|
195 |
+
)
|
196 |
+
df_preview = pd.DataFrame(df_info["head"])
|
197 |
+
response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```")
|
198 |
+
|
199 |
+
if result.get("plots"):
|
200 |
+
response.append(
|
201 |
+
f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)"
|
202 |
+
)
|
203 |
+
|
204 |
+
else:
|
205 |
+
response.append(f"❌ Code execution failed in **{language.upper()}**")
|
206 |
+
if result.get("stderr"):
|
207 |
+
response.append(
|
208 |
+
"\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```"
|
209 |
+
)
|
210 |
+
|
211 |
+
return "\n".join(response)
|
212 |
+
|
213 |
+
execute_code_multilang_tool = Tool(
|
214 |
+
name="execute_code_multilang",
|
215 |
+
func=execute_code_multilang,
|
216 |
+
description="""Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
|
217 |
+
Args:
|
218 |
+
code (str): The source code to execute.
|
219 |
+
language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
|
220 |
+
"""
|
221 |
+
)
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
# load the system prompt from the file
|
226 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
227 |
system_prompt = f.read()
|
|
|
235 |
substract_tool,
|
236 |
divide_tool,
|
237 |
modulus_tool,
|
238 |
+
power_tool,
|
239 |
+
square_root,
|
240 |
wiki_search_tool,
|
241 |
web_search_tool,
|
242 |
arvix_search_tool,
|