Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,10 +52,10 @@ def main():
|
|
| 52 |
if st.button("Run Agent") and query:
|
| 53 |
# Initialize LLM with max_retries at the model level
|
| 54 |
llm = ChatGoogleGenerativeAI(
|
| 55 |
-
model="gemini-2.5-flash",
|
| 56 |
google_api_key=GEMINI_API_KEY,
|
| 57 |
temperature=0,
|
| 58 |
-
max_retries=6,
|
| 59 |
)
|
| 60 |
|
| 61 |
# Updated Prefix to strictly separate Thought, Action, and Final Answer
|
|
@@ -66,14 +66,21 @@ def main():
|
|
| 66 |
STRICT FORMATTING RULES:
|
| 67 |
1. NEVER provide a 'Final Answer' and an 'Action' in the same response.
|
| 68 |
2. If you need to run code, provide ONLY the 'Action' and 'Action Input'.
|
| 69 |
-
3.
|
| 70 |
-
4.
|
| 71 |
-
5.
|
| 72 |
-
6.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
|
| 75 |
# Create the Pandas Agent
|
| 76 |
-
# Using agent_executor_kwargs to pass handle_parsing_errors to the executor directly
|
| 77 |
agent = create_pandas_dataframe_agent(
|
| 78 |
llm,
|
| 79 |
df,
|
|
@@ -81,6 +88,7 @@ def main():
|
|
| 81 |
agent_type="zero-shot-react-description",
|
| 82 |
allow_dangerous_code=True,
|
| 83 |
prefix=custom_prefix,
|
|
|
|
| 84 |
handle_parsing_errors=True,
|
| 85 |
agent_executor_kwargs={
|
| 86 |
"handle_parsing_errors": True
|
|
@@ -93,7 +101,6 @@ def main():
|
|
| 93 |
st_callback = StreamlitCallbackHandler(thought_container)
|
| 94 |
|
| 95 |
with st.spinner("Agent is analyzing..."):
|
| 96 |
-
# Manual retry logic wrapper for the whole agentic loop
|
| 97 |
max_loop_retries = 3
|
| 98 |
for attempt in range(max_loop_retries):
|
| 99 |
try:
|
|
@@ -101,7 +108,7 @@ def main():
|
|
| 101 |
st.markdown("---")
|
| 102 |
st.subheader("✅ Final Analysis Result")
|
| 103 |
st.success(response)
|
| 104 |
-
break
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
if "429" in str(e) or "RESOURCE_EXHAUSTED" in str(e):
|
|
@@ -113,7 +120,6 @@ def main():
|
|
| 113 |
else:
|
| 114 |
st.error("Rate limit exceeded consistently. Please try again in a minute.")
|
| 115 |
else:
|
| 116 |
-
# Enhanced error display for parsing issues
|
| 117 |
st.error(f"Agent encountered a parsing or execution error.")
|
| 118 |
with st.expander("Show Technical Error"):
|
| 119 |
st.code(str(e))
|
|
|
|
| 52 |
if st.button("Run Agent") and query:
|
| 53 |
# Initialize LLM with max_retries at the model level
|
| 54 |
llm = ChatGoogleGenerativeAI(
|
| 55 |
+
model="gemini-2.5-flash-preview-09-2025",
|
| 56 |
google_api_key=GEMINI_API_KEY,
|
| 57 |
temperature=0,
|
| 58 |
+
max_retries=6,
|
| 59 |
)
|
| 60 |
|
| 61 |
# Updated Prefix to strictly separate Thought, Action, and Final Answer
|
|
|
|
| 66 |
STRICT FORMATTING RULES:
|
| 67 |
1. NEVER provide a 'Final Answer' and an 'Action' in the same response.
|
| 68 |
2. If you need to run code, provide ONLY the 'Action' and 'Action Input'.
|
| 69 |
+
3. Your 'Action Input' must be valid Python code and ONLY Python code. No markdown backticks.
|
| 70 |
+
4. Before creating a visualization for filtered data, always check if the filtered dataframe is empty.
|
| 71 |
+
5. For plots, use `plt.figure()` and end with `st.pyplot(plt.gcf())`.
|
| 72 |
+
6. The visuals must be clear and perfect .
|
| 73 |
+
7. Avoide over writing .
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
# The suffix reinforces the format right before the model generates text
|
| 77 |
+
custom_suffix = """
|
| 78 |
+
Begin!
|
| 79 |
+
Question: {input}
|
| 80 |
+
Thought: {agent_scratchpad}
|
| 81 |
"""
|
| 82 |
|
| 83 |
# Create the Pandas Agent
|
|
|
|
| 84 |
agent = create_pandas_dataframe_agent(
|
| 85 |
llm,
|
| 86 |
df,
|
|
|
|
| 88 |
agent_type="zero-shot-react-description",
|
| 89 |
allow_dangerous_code=True,
|
| 90 |
prefix=custom_prefix,
|
| 91 |
+
suffix=custom_suffix,
|
| 92 |
handle_parsing_errors=True,
|
| 93 |
agent_executor_kwargs={
|
| 94 |
"handle_parsing_errors": True
|
|
|
|
| 101 |
st_callback = StreamlitCallbackHandler(thought_container)
|
| 102 |
|
| 103 |
with st.spinner("Agent is analyzing..."):
|
|
|
|
| 104 |
max_loop_retries = 3
|
| 105 |
for attempt in range(max_loop_retries):
|
| 106 |
try:
|
|
|
|
| 108 |
st.markdown("---")
|
| 109 |
st.subheader("✅ Final Analysis Result")
|
| 110 |
st.success(response)
|
| 111 |
+
break
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
if "429" in str(e) or "RESOURCE_EXHAUSTED" in str(e):
|
|
|
|
| 120 |
else:
|
| 121 |
st.error("Rate limit exceeded consistently. Please try again in a minute.")
|
| 122 |
else:
|
|
|
|
| 123 |
st.error(f"Agent encountered a parsing or execution error.")
|
| 124 |
with st.expander("Show Technical Error"):
|
| 125 |
st.code(str(e))
|