Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -125,9 +125,20 @@ model = LiteLLMModel(
|
|
125 |
api_key=os.getenv("OPENAI_API_KEY"),
|
126 |
api_base=os.getenv("CUSTOM_OPENAI_API_BASE"),
|
127 |
temperature=0.1,
|
128 |
-
frequency_penalty=0.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
)
|
130 |
|
|
|
131 |
text_limit = 20000
|
132 |
ti_tool = TextInspectorTool(model, text_limit)
|
133 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
@@ -179,18 +190,37 @@ WEB_TOOLS = [
|
|
179 |
ArchiveSearchTool(browser),
|
180 |
TextInspectorTool(model, text_limit),
|
181 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
# Agent creation in a factory function
|
183 |
def create_agent():
|
184 |
-
"""Creates a fresh agent instance for each session"""
|
185 |
return CodeAgent(
|
186 |
model=model,
|
187 |
tools=[visualizer] + WEB_TOOLS,
|
188 |
-
max_steps=5
|
189 |
-
verbosity_level=
|
190 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
191 |
-
planning_interval=
|
|
|
|
|
|
|
|
|
192 |
)
|
193 |
-
|
194 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
195 |
|
196 |
def stream_to_gradio(
|
|
|
125 |
api_key=os.getenv("OPENAI_API_KEY"),
|
126 |
api_base=os.getenv("CUSTOM_OPENAI_API_BASE"),
|
127 |
temperature=0.1,
|
128 |
+
frequency_penalty=0.2,
|
129 |
+
messages=[
|
130 |
+
{
|
131 |
+
"role": "system",
|
132 |
+
"content": """ALWAYS format code responses with:
|
133 |
+
```py
|
134 |
+
# Your code
|
135 |
+
```
|
136 |
+
Use markdown for text and strict triple-backtick for code blocks"""
|
137 |
+
}
|
138 |
+
]
|
139 |
)
|
140 |
|
141 |
+
|
142 |
text_limit = 20000
|
143 |
ti_tool = TextInspectorTool(model, text_limit)
|
144 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
|
|
190 |
ArchiveSearchTool(browser),
|
191 |
TextInspectorTool(model, text_limit),
|
192 |
]
|
193 |
+
|
194 |
+
from smolagents.parsers import CodeParser
|
195 |
+
import re
|
196 |
+
|
197 |
+
class RobustCodeParser(CodeParser):
|
198 |
+
def extract_code(self, response: str) -> str:
|
199 |
+
try:
|
200 |
+
return super().extract_code(response)
|
201 |
+
except ValueError:
|
202 |
+
# Fallback pattern matching
|
203 |
+
code_match = re.search(r"```(?:python|py)?\n(.*?)\n```", response, re.DOTALL)
|
204 |
+
if code_match:
|
205 |
+
return code_match.group(1).strip()
|
206 |
+
raise ValueError(f"Invalid code format in response:\n{response}")
|
207 |
+
|
208 |
+
# Replace in agent creation:
|
209 |
+
|
210 |
# Agent creation in a factory function
|
211 |
def create_agent():
|
|
|
212 |
return CodeAgent(
|
213 |
model=model,
|
214 |
tools=[visualizer] + WEB_TOOLS,
|
215 |
+
max_steps=7, # Increased from 5
|
216 |
+
verbosity_level=3, # Higher debug info
|
217 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
218 |
+
planning_interval=3,
|
219 |
+
code_block_delimiters=("```py", "```"), # Explicit code formatting [github.com]
|
220 |
+
code_clean_pattern=r"^[\s\S]*?(```py\n[\s\S]*?\n```)", # Improved regex
|
221 |
+
enforce_code_format=True,
|
222 |
+
parser=RobustCodeParser() # Explicitly use RobustCodeParser here!
|
223 |
)
|
|
|
224 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
225 |
|
226 |
def stream_to_gradio(
|