Markiian Tsalyk commited on
Commit
8eea4d3
·
1 Parent(s): 8a5e198

Added task files

Browse files
7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx ADDED
Binary file (5.29 kB). View file
 
__pycache__/f918266a-b3e0-4914-865d-4faa564f1aef.cpython-313.pyc ADDED
Binary file (1.92 kB). View file
 
__pycache__/tools.cpython-313.pyc CHANGED
Binary files a/__pycache__/tools.cpython-313.pyc and b/__pycache__/tools.cpython-313.pyc differ
 
f918266a-b3e0-4914-865d-4faa564f1aef.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)}")
llama_index_agent.py CHANGED
@@ -98,6 +98,11 @@ class LlamaIndexAgent:
98
  name="pandas_column_sum",
99
  description="Use this to compute sum on pandas dataframe column",
100
  )
 
 
 
 
 
101
 
102
  # Create the agent
103
  self.agent = ReActAgent.from_tools(
@@ -114,6 +119,7 @@ class LlamaIndexAgent:
114
  fetch_historical_event_data_tool,
115
  read_excel_tool,
116
  pandas_column_sum_tool,
 
117
  ],
118
  llm=self.llm,
119
  verbose=verbose,
@@ -180,6 +186,7 @@ if __name__ == "__main__":
180
  # """
181
  # On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?
182
  # """,
 
183
  ]
184
 
185
  for query in example_queries:
 
98
  name="pandas_column_sum",
99
  description="Use this to compute sum on pandas dataframe column",
100
  )
101
+ compute_sum_tool = FunctionTool.from_defaults(
102
+ fn=tools.compute_sum,
103
+ name="compute_sum",
104
+ description="Use this to compute sum of provided values",
105
+ )
106
 
107
  # Create the agent
108
  self.agent = ReActAgent.from_tools(
 
119
  fetch_historical_event_data_tool,
120
  read_excel_tool,
121
  pandas_column_sum_tool,
122
+ compute_sum_tool,
123
  ],
124
  llm=self.llm,
125
  verbose=verbose,
 
186
  # """
187
  # On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?
188
  # """,
189
+ # "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.\nAttached file: 7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx",
190
  ]
191
 
192
  for query in example_queries:
requirements.txt CHANGED
@@ -11,4 +11,5 @@ llama-index
11
  llama-index-llms-openrouter
12
  wikipedia
13
  youtube-transcript-api
14
- python-dotenv
 
 
11
  llama-index-llms-openrouter
12
  wikipedia
13
  youtube-transcript-api
14
+ python-dotenv
15
+ openpyxl
tools.py CHANGED
@@ -143,6 +143,18 @@ def multiply(a: float, b: float, **kwargs) -> float:
143
  return a * b
144
 
145
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def length(iterable: Any, **kwargs) -> int:
147
  """
148
  Return the length of an iterable.
@@ -162,7 +174,8 @@ def execute_python_file(file_path: str) -> Any:
162
 
163
  This function takes a path to a Python file, executes it by importing it as a module,
164
  and returns the result. The file should contain a function call that produces
165
- the result to be returned.
 
166
 
167
  Args:
168
  file_path (str): Path to the Python file to execute.
@@ -197,16 +210,13 @@ def execute_python_file(file_path: str) -> Any:
197
  stdout_capture = io.StringIO()
198
  stderr_capture = io.StringIO()
199
 
200
- # Store the original __main__ module
201
- original_main = sys.modules.get("__main__")
202
-
203
  try:
204
  spec = importlib.util.spec_from_file_location(module_name, file_path)
205
  if spec is None or spec.loader is None:
206
  raise ImportError(f"Could not load module spec from {file_path}")
207
 
208
  module = importlib.util.module_from_spec(spec)
209
-
210
  sys.modules[module_name] = module
211
 
212
  # Execute the module
@@ -215,11 +225,21 @@ def execute_python_file(file_path: str) -> Any:
215
  ):
216
  spec.loader.exec_module(module)
217
 
 
 
 
 
 
 
 
 
 
218
  if hasattr(module, "result"):
219
  return module.result
220
  else:
221
- print(f"RESULT PYTHON: {stdout_capture.getvalue().strip()}")
222
- return stdout_capture.getvalue().strip()
 
223
 
224
  except Exception as e:
225
  error_output = stderr_capture.getvalue()
 
143
  return a * b
144
 
145
 
146
+ def compute_sum(values: list[int | float], **kwargs) -> float:
147
+ """
148
+ Computes sum of provided values
149
+
150
+ Args:
151
+ values: list of integer or float values
152
+ Return:
153
+ Sum of the values
154
+ """
155
+ return sum(values)
156
+
157
+
158
  def length(iterable: Any, **kwargs) -> int:
159
  """
160
  Return the length of an iterable.
 
174
 
175
  This function takes a path to a Python file, executes it by importing it as a module,
176
  and returns the result. The file should contain a function call that produces
177
+ the result to be returned. This version also executes code under the
178
+ 'if __name__ == "__main__":' block.
179
 
180
  Args:
181
  file_path (str): Path to the Python file to execute.
 
210
  stdout_capture = io.StringIO()
211
  stderr_capture = io.StringIO()
212
 
213
+ # First approach: Import normally to get module definitions
 
 
214
  try:
215
  spec = importlib.util.spec_from_file_location(module_name, file_path)
216
  if spec is None or spec.loader is None:
217
  raise ImportError(f"Could not load module spec from {file_path}")
218
 
219
  module = importlib.util.module_from_spec(spec)
 
220
  sys.modules[module_name] = module
221
 
222
  # Execute the module
 
225
  ):
226
  spec.loader.exec_module(module)
227
 
228
+ # After module is loaded, directly execute the main block by reading the file
229
+ # and executing the content with __name__ = "__main__"
230
+ with open(file_path, "r") as f:
231
+ file_content = f.read()
232
+ # Create a namespace with everything from the module
233
+ namespace = {**module.__dict__}
234
+ namespace["__name__"] = "__main__"
235
+ exec(file_content, namespace)
236
+
237
  if hasattr(module, "result"):
238
  return module.result
239
  else:
240
+ output = stdout_capture.getvalue().strip()
241
+ print(f"RESULT PYTHON: {output}")
242
+ return output
243
 
244
  except Exception as e:
245
  error_output = stderr_capture.getvalue()