Andrei Sferdian commited on
Commit
1c7a9a0
·
1 Parent(s): 8b65802

Add python code execution tool

Browse files
Files changed (6) hide show
  1. agent.py +24 -11
  2. app.py +5 -0
  3. python_code.py +35 -0
  4. tools/__init__.py +4 -1
  5. tools/execute_code.py +27 -0
  6. tools/files.py +30 -1
agent.py CHANGED
@@ -1,18 +1,21 @@
1
  from typing import Annotated, TypedDict
2
 
3
- from langchain_community.tools import DuckDuckGoSearchRun
4
  from langchain_core.messages import AnyMessage, HumanMessage
5
  from langchain_together import ChatTogether
6
  from langgraph.graph import START, StateGraph
7
  from langgraph.graph.message import add_messages
8
  from langgraph.prebuilt import create_react_agent
9
 
 
10
  from tools import (
11
  GetYouTubeTranscript,
12
  add,
13
  divide,
 
14
  find_wiki_page,
15
  floor_divide,
 
16
  get_wiki_page_section_by_title,
17
  load_excel_file,
18
  modulus,
@@ -22,11 +25,13 @@ from tools import (
22
  )
23
 
24
  tools = [
25
- DuckDuckGoSearchRun(),
26
  GetYouTubeTranscript(),
 
 
27
  find_wiki_page,
28
  get_wiki_page_section_by_title,
29
  load_excel_file,
 
30
  add,
31
  subtract,
32
  multiply,
@@ -46,12 +51,14 @@ model = ChatTogether(
46
  prompt = (
47
  "You are an agent that will undergo the GAIA test.\n"
48
  "You will be given a question and you will have to answer it using the tools provided.\n"
49
- "Respond in the requested format, if specified, otherwise use the short answer styl. I.E. Question: What is the capital of France? Answer: Paris.\n"
50
- "When searching for Wikipedia content, always follow this specific sequence:\n"
51
- " a. Use find_wiki_page tool to locate the relevant Wikipedia page.\n"
 
 
 
52
  " The response will be a JSON string with the page's summary and sections.\n"
53
- " b. If the page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
54
- "If the user asks for a YouTube transcript, use the GetYouTubeTranscript tool.\n"
55
  "If the user asks for an Excel file, use the load_excel_file tool.\n"
56
  "When you need to perform calculations, use the provided calculator tools (add, subtract, multiply, divide).\n"
57
  "Do not performa calculations by yourself!\n"
@@ -103,12 +110,11 @@ def prepare(state: GraphState):
103
  print("Question is in reverse order, reversing it...")
104
  question = question[::-1]
105
 
106
- if state.get("file_name"):
 
107
  return {
108
  "messages": [
109
- HumanMessage(
110
- content=f"Question: {question}\nFile: {state['file_name']}"
111
- )
112
  ],
113
  }
114
 
@@ -134,3 +140,10 @@ graph.add_edge("prepare", "react")
134
  graph.add_edge("prepare", "react")
135
 
136
  agent = graph.compile()
 
 
 
 
 
 
 
 
1
  from typing import Annotated, TypedDict
2
 
3
+ from langchain_community.tools import GoogleSerperResults
4
  from langchain_core.messages import AnyMessage, HumanMessage
5
  from langchain_together import ChatTogether
6
  from langgraph.graph import START, StateGraph
7
  from langgraph.graph.message import add_messages
8
  from langgraph.prebuilt import create_react_agent
9
 
10
+ import questions
11
  from tools import (
12
  GetYouTubeTranscript,
13
  add,
14
  divide,
15
+ execute_python_code,
16
  find_wiki_page,
17
  floor_divide,
18
+ get_text_file,
19
  get_wiki_page_section_by_title,
20
  load_excel_file,
21
  modulus,
 
25
  )
26
 
27
  tools = [
 
28
  GetYouTubeTranscript(),
29
+ GoogleSerperResults(),
30
+ execute_python_code,
31
  find_wiki_page,
32
  get_wiki_page_section_by_title,
33
  load_excel_file,
34
+ get_text_file,
35
  add,
36
  subtract,
37
  multiply,
 
51
  prompt = (
52
  "You are an agent that will undergo the GAIA test.\n"
53
  "You will be given a question and you will have to answer it using the tools provided.\n"
54
+ "Respond in the requested format, if specified, otherwise use the short answer style. I.E. Question: What is the capital of France? Answer: Paris.\n"
55
+ "When searching for information, always follow this specific sequence:\n"
56
+ " a. Use the 'google_serper_results_json' tool to search for relevant information.\n"
57
+ " Provide the query as input to the tool.\n"
58
+ " The tool will return a JSON string with the search results.\n"
59
+ " b. If the required information is not found in the search results, use the find_wiki_page tool to locate the relevant Wikipedia page.\n"
60
  " The response will be a JSON string with the page's summary and sections.\n"
61
+ " c. If the Wikipedia page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
 
62
  "If the user asks for an Excel file, use the load_excel_file tool.\n"
63
  "When you need to perform calculations, use the provided calculator tools (add, subtract, multiply, divide).\n"
64
  "Do not performa calculations by yourself!\n"
 
110
  print("Question is in reverse order, reversing it...")
111
  question = question[::-1]
112
 
113
+ file_name = state.get("file_name")
114
+ if file_name:
115
  return {
116
  "messages": [
117
+ HumanMessage(content=f"Question: {question}\nFile name: {file_name}")
 
 
118
  ],
119
  }
120
 
 
140
  graph.add_edge("prepare", "react")
141
 
142
  agent = graph.compile()
143
+
144
+ if __name__ == "__main__":
145
+ import questions
146
+
147
+ file_bytes = questions.get_file("f918266a-b3e0-4914-865d-4faa564f1aef")
148
+ file_content = file_bytes.decode("utf-8")
149
+ exec(file_content)
app.py CHANGED
@@ -288,6 +288,11 @@ def run_locally():
288
  "question": "The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.",
289
  "file_name": "7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx",
290
  },
 
 
 
 
 
291
  ]
292
 
293
  max_concurrency = int(os.getenv("MAX_CONCURRENCY", 1))
 
288
  "question": "The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.",
289
  "file_name": "7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx",
290
  },
291
+ # {
292
+ # "task_id": "f918266a-b3e0-4914-865d-4faa564f1aef",
293
+ # "question": "What is the final numeric output from the attached Python code?",
294
+ # "file_name": "f918266a-b3e0-4914-865d-4faa564f1aef.py",
295
+ # },
296
  ]
297
 
298
  max_concurrency = int(os.getenv("MAX_CONCURRENCY", 1))
python_code.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from random import randint
2
+ import time
3
+
4
+ class UhOh(Exception):
5
+ pass
6
+
7
+ class Hmm:
8
+ def __init__(self):
9
+ self.value = randint(-100, 100)
10
+
11
+ def Yeah(self):
12
+ if self.value == 0:
13
+ return True
14
+ else:
15
+ raise UhOh()
16
+
17
+ def Okay():
18
+ while True:
19
+ yield Hmm()
20
+
21
+ def keep_trying(go, first_try=True):
22
+ maybe = next(go)
23
+ try:
24
+ if maybe.Yeah():
25
+ return maybe.value
26
+ except UhOh:
27
+ if first_try:
28
+ print("Working...")
29
+ print("Please wait patiently...")
30
+ time.sleep(0.1)
31
+ return keep_trying(go, first_try=False)
32
+
33
+ if __name__ == "__main__":
34
+ go = Okay()
35
+ print(f"{keep_trying(go)}")
tools/__init__.py CHANGED
@@ -7,7 +7,8 @@ from .calculator import (
7
  power,
8
  subtract,
9
  )
10
- from .files import load_excel_file
 
11
  from .get_youtube_transcript import GetYouTubeTranscript
12
  from .wikipedia import (
13
  find_wiki_page,
@@ -19,6 +20,7 @@ __all__ = [
19
  "find_wiki_page",
20
  "get_wiki_page_section_by_title",
21
  "load_excel_file",
 
22
  "add",
23
  "subtract",
24
  "multiply",
@@ -26,4 +28,5 @@ __all__ = [
26
  "power",
27
  "modulus",
28
  "floor_divide",
 
29
  ]
 
7
  power,
8
  subtract,
9
  )
10
+ from .execute_code import execute_python_code
11
+ from .files import get_text_file, load_excel_file
12
  from .get_youtube_transcript import GetYouTubeTranscript
13
  from .wikipedia import (
14
  find_wiki_page,
 
20
  "find_wiki_page",
21
  "get_wiki_page_section_by_title",
22
  "load_excel_file",
23
+ "get_text_file",
24
  "add",
25
  "subtract",
26
  "multiply",
 
28
  "power",
29
  "modulus",
30
  "floor_divide",
31
+ "execute_python_code",
32
  ]
tools/execute_code.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+
3
+ from langchain.tools import tool
4
+
5
+
6
+ @tool(parse_docstring=True)
7
+ def execute_python_code(code: str) -> str:
8
+ """
9
+ Executes a given Python code snippet in a subprocess and returns its output.
10
+
11
+ Args:
12
+ code (str): The Python code to execute as a string.
13
+
14
+ Returns:
15
+ str: The standard output of the executed code if successful, or an error message
16
+ containing the standard error output if the execution fails.
17
+ Raises:
18
+ None: Any exceptions during execution are caught and handled internally.
19
+ """
20
+
21
+ try:
22
+ process = subprocess.run(
23
+ ["python3", "-c", code], text=True, capture_output=True, check=True
24
+ )
25
+ return process.stdout
26
+ except subprocess.CalledProcessError as e:
27
+ return f"Error: {e.stderr}"
tools/files.py CHANGED
@@ -1,7 +1,36 @@
1
  from langchain_core.tools import tool
2
 
3
 
4
- @tool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def load_excel_file(file_name: str) -> str:
6
  """
7
  Loads an Excel file by its name and returns its content as a string.
 
1
  from langchain_core.tools import tool
2
 
3
 
4
+ @tool(parse_docstring=True)
5
+ def get_text_file(file_name: str) -> str:
6
+ """
7
+ Loads a text file by its name and returns its content as a string.
8
+
9
+ Args:
10
+ file_name (str): The name of the text file to load. The file name should include
11
+ a task ID followed by a file extension (e.g., "12345.txt").
12
+
13
+ Returns:
14
+ str: The content of the text file as a string.
15
+ """
16
+ print(f"Loading text file: {file_name}")
17
+ try:
18
+ import questions
19
+
20
+ task_id, ext = file_name.rsplit(".", 1)
21
+ if not task_id or not ext:
22
+ return "Invalid file name format. Please provide a valid file name with task ID and extension."
23
+
24
+ file_content = questions.get_file(task_id)
25
+ if not file_content:
26
+ return f"File with task ID {task_id} not found."
27
+
28
+ return file_content.decode("utf-8")
29
+ except Exception as e:
30
+ return f"Error loading text file: {e}"
31
+
32
+
33
+ @tool(parse_docstring=True)
34
  def load_excel_file(file_name: str) -> str:
35
  """
36
  Loads an Excel file by its name and returns its content as a string.