Pycrolis commited on
Commit
64a08a8
·
1 Parent(s): f3b4b95

feat(tool): add tool to execute Python code from file

Browse files
ShrewdAgent.py CHANGED
@@ -14,6 +14,7 @@ from loguru import logger
14
  from pydantic import SecretStr
15
 
16
  from tools.excel_to_text import excel_to_text
 
17
  from tools.produce_classifier import produce_classifier
18
  from tools.sort_words_alphabetically import sort_words_alphabetically
19
  from tools.web_page_information_extractor import web_page_information_extractor
@@ -48,6 +49,7 @@ class ShrewdAgent:
48
  produce_classifier,
49
  sort_words_alphabetically,
50
  excel_to_text,
 
51
  ]
52
  self.llm = ChatOpenAI(
53
  model="gpt-4o-mini",
 
14
  from pydantic import SecretStr
15
 
16
  from tools.excel_to_text import excel_to_text
17
+ from tools.execute_python_code_from_file import execute_python_code_from_file
18
  from tools.produce_classifier import produce_classifier
19
  from tools.sort_words_alphabetically import sort_words_alphabetically
20
  from tools.web_page_information_extractor import web_page_information_extractor
 
49
  produce_classifier,
50
  sort_words_alphabetically,
51
  excel_to_text,
52
+ execute_python_code_from_file,
53
  ]
54
  self.llm = ChatOpenAI(
55
  model="gpt-4o-mini",
data/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:196b64ea8f72841cb0c4fc972ca82a28e74de926c402bb043305e12e05e012b7
3
+ size 5285
data/f918266a-b3e0-4914-865d-4faa564f1aef.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from random import randint
3
+
4
+
5
+ class UhOh(Exception):
6
+ pass
7
+
8
+ class Hmm:
9
+ def __init__(self):
10
+ self.value = randint(-100, 100)
11
+
12
+ def Yeah(self):
13
+ if self.value == 0:
14
+ return True
15
+ else:
16
+ raise UhOh()
17
+
18
+ def Okay():
19
+ while True:
20
+ yield Hmm()
21
+
22
+ def keep_trying(go, first_try=True):
23
+ maybe = next(go)
24
+ try:
25
+ if maybe.Yeah():
26
+ return maybe.value
27
+ except UhOh:
28
+ if first_try:
29
+ print("Working...")
30
+ print("Please wait patiently...")
31
+ time.sleep(0.1)
32
+ return keep_trying(go, first_try=False)
33
+
34
+ if __name__ == "__main__":
35
+ go = Okay()
36
+ print(f"{keep_trying(go)}")
requirements.txt CHANGED
@@ -11,4 +11,5 @@ beautifulsoup4~=4.13.4
11
  readability-lxml~=0.8.4.1
12
  youtube-transcript-api~=1.0.3
13
  wikipedia~=1.4.0
14
- langchain_tavily~=0.1.6
 
 
11
  readability-lxml~=0.8.4.1
12
  youtube-transcript-api~=1.0.3
13
  wikipedia~=1.4.0
14
+ langchain_tavily~=0.1.6
15
+ rizaio~=0.11.0
tools/excel_to_text.py CHANGED
@@ -27,6 +27,6 @@ def excel_to_text(path_or_url: str) -> str:
27
 
28
 
29
  if __name__ == "__main__":
30
- print(excel_to_text.invoke("../7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx"))
31
  print(
32
  excel_to_text.invoke("https://agents-course-unit4-scoring.hf.space/files/7bd855d8-463d-4ed5-93ca-5fe35145f733"))
 
27
 
28
 
29
  if __name__ == "__main__":
30
+ print(excel_to_text.invoke("../data/7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx"))
31
  print(
32
  excel_to_text.invoke("https://agents-course-unit4-scoring.hf.space/files/7bd855d8-463d-4ed5-93ca-5fe35145f733"))
tools/execute_python_code_from_file.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool, ToolException
2
+ from loguru import logger
3
+ from rizaio import Riza
4
+
5
+ from tools.load_file import load_file
6
+
7
+
8
+ @tool("execute_python_code_from_file_tool", parse_docstring=True)
9
+ def execute_python_code_from_file(path_or_url: str) -> str:
10
+ """
11
+ Executes a given Python code snippet.
12
+
13
+ Args:
14
+ path_or_url (str): A string containing the Python code to execute.
15
+
16
+ Returns:
17
+ str: The standard output resulting from the execution of the code.
18
+ """
19
+ logger.info(f"execute_python_code_from_file with param: {path_or_url}")
20
+ code = load_file(path_or_url).read().decode("utf-8")
21
+ logger.debug(f"Python code:\n{code}")
22
+ client = Riza()
23
+ output = client.command.exec(
24
+ language="python",
25
+ code=code,
26
+ )
27
+
28
+ if output.exit_code > 0:
29
+ raise ToolException(
30
+ f"Riza code execution returned a non-zero exit code. "
31
+ f"The output captured from stderr was:\n{output.stderr}"
32
+ )
33
+ return output.stdout
34
+
35
+
36
+ if __name__ == "__main__":
37
+ print(execute_python_code_from_file.invoke("../data/f918266a-b3e0-4914-865d-4faa564f1aef.py"))
38
+ print(execute_python_code_from_file.invoke("https://agents-course-unit4-scoring.hf.space/files/f918266a-b3e0-4914-865d-4faa564f1aef"))