keynes42 commited on
Commit
5d255b5
·
verified ·
1 Parent(s): 60ec0e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -77,6 +77,19 @@ class CachedWikiTool(WikipediaSearchTool):
77
  def run(self, page: str):
78
  return super().run(page)
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  # --- Basic Agent Definition ---
81
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
82
  class BasicAgent:
@@ -122,13 +135,17 @@ class BasicAgent:
122
  # Introduce tools
123
  wiki_tool = CachedWikiTool()
124
  search_tool = CachedWebSearchTool()
125
- python_tool = PythonInterpreterTool()
126
  html_parse_tool = VisitWebpageTool()
127
  # Initialize the agent
128
  self.agent = CodeAgent(model=self,
129
  tools=[wiki_tool, search_tool, python_tool, html_parse_tool],
130
  add_base_tools=True,
131
- additional_authorized_imports=["dateparser", "bs4", "regex"])
 
 
 
 
132
 
133
  def _serialize_messages(self, messages):
134
  prompt = []
 
77
  def run(self, page: str):
78
  return super().run(page)
79
 
80
+ class PreloadedPythonTool(PythonInterpreterTool):
81
+ """
82
+ A PythonInterpreterTool that automatically prepends the necessary imports
83
+ (bs4, BeautifulSoup, regex) so you never hit NameError inside your code blocks.
84
+ """
85
+ def run(self, code: str) -> str:
86
+ preamble = (
87
+ "import bs4\n"
88
+ "from bs4 import BeautifulSoup\n"
89
+ "import regex\n"
90
+ )
91
+ return super().run(preamble + code)
92
+
93
  # --- Basic Agent Definition ---
94
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
95
  class BasicAgent:
 
135
  # Introduce tools
136
  wiki_tool = CachedWikiTool()
137
  search_tool = CachedWebSearchTool()
138
+ python_tool = PreloadedPythonTool()
139
  html_parse_tool = VisitWebpageTool()
140
  # Initialize the agent
141
  self.agent = CodeAgent(model=self,
142
  tools=[wiki_tool, search_tool, python_tool, html_parse_tool],
143
  add_base_tools=True,
144
+ additional_authorized_imports=["dateparser", "bs4", "regex"],
145
+ system_message=("When writing Python code in the PythonInterpreterTool, you must "
146
+ "always import bs4 and regex (already preloaded), and *return* your "
147
+ "result by calling final_answer(...). Do not assign to a variable named final_answer.")
148
+ )
149
 
150
  def _serialize_messages(self, messages):
151
  prompt = []