akrstova commited on
Commit
71e070f
·
1 Parent(s): ec639fe

Add Python code tool

Browse files
Files changed (2) hide show
  1. system_prompt.txt +5 -1
  2. tools/file_tools.py +29 -1
system_prompt.txt CHANGED
@@ -1 +1,5 @@
1
- You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
 
 
 
 
 
1
+ You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. Even if you're uncertain, provide the best guess in this format.
2
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
3
+ If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
4
+ If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
5
+ If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
tools/file_tools.py CHANGED
@@ -1,6 +1,9 @@
1
  import pandas as pd
 
 
2
  from langchain_core.tools import tool
3
 
 
4
  @tool
5
  def analyze_excel_file(file_path: str, query: str) -> str:
6
  """
@@ -26,4 +29,29 @@ def analyze_excel_file(file_path: str, query: str) -> str:
26
  return result
27
 
28
  except Exception as e:
29
- return f"Error analyzing Excel file: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
+ import io
3
+ import contextlib
4
  from langchain_core.tools import tool
5
 
6
+
7
  @tool
8
  def analyze_excel_file(file_path: str, query: str) -> str:
9
  """
 
29
  return result
30
 
31
  except Exception as e:
32
+ return f"Error analyzing Excel file: {str(e)}"
33
+
34
+
35
+ @tool
36
+ def execute_python_code(code: str) -> str:
37
+ """
38
+ Executes a Python code string and returns the output or error.
39
+ Args:
40
+ code (str): The Python code to execute.
41
+ Returns:
42
+ str: The output or error message.
43
+ """
44
+ local_vars = {}
45
+ stdout = io.StringIO()
46
+ try:
47
+ with contextlib.redirect_stdout(stdout):
48
+ exec(code, {}, local_vars)
49
+ output = stdout.getvalue()
50
+ if output.strip():
51
+ return output.strip()
52
+ # If code defines a variable named 'result', return its value
53
+ if "result" in local_vars:
54
+ return str(local_vars["result"])
55
+ return "Code executed successfully, but produced no output."
56
+ except Exception as e:
57
+ return f"Error executing code: {e}"