Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,17 +58,22 @@ def main():
|
|
| 58 |
max_retries=6, # LangChain's built-in retry logic
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
custom_prefix = """
|
| 62 |
You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
|
| 63 |
You should use the tools below to answer the question posed of you.
|
| 64 |
|
| 65 |
-
|
| 66 |
-
1.
|
| 67 |
-
2.
|
| 68 |
-
3.
|
| 69 |
-
4.
|
|
|
|
|
|
|
| 70 |
"""
|
| 71 |
|
|
|
|
|
|
|
| 72 |
agent = create_pandas_dataframe_agent(
|
| 73 |
llm,
|
| 74 |
df,
|
|
@@ -77,6 +82,9 @@ def main():
|
|
| 77 |
allow_dangerous_code=True,
|
| 78 |
prefix=custom_prefix,
|
| 79 |
handle_parsing_errors=True,
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
# --- 4. EXECUTION ---
|
|
@@ -105,7 +113,8 @@ def main():
|
|
| 105 |
else:
|
| 106 |
st.error("Rate limit exceeded consistently. Please try again in a minute.")
|
| 107 |
else:
|
| 108 |
-
|
|
|
|
| 109 |
with st.expander("Show Technical Error"):
|
| 110 |
st.code(str(e))
|
| 111 |
break
|
|
|
|
| 58 |
max_retries=6, # LangChain's built-in retry logic
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Updated Prefix to strictly separate Thought, Action, and Final Answer
|
| 62 |
custom_prefix = """
|
| 63 |
You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
|
| 64 |
You should use the tools below to answer the question posed of you.
|
| 65 |
|
| 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. Once you have the result, provide ONLY the 'Final Answer'.
|
| 70 |
+
4. Your 'Action Input' must be valid Python code and ONLY Python code. No markdown backticks.
|
| 71 |
+
5. Before creating a visualization for filtered data, always check if the filtered dataframe is empty.
|
| 72 |
+
6. For plots, use `plt.figure()` and end with `st.pyplot(plt.gcf())`.
|
| 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,
|
|
|
|
| 82 |
allow_dangerous_code=True,
|
| 83 |
prefix=custom_prefix,
|
| 84 |
handle_parsing_errors=True,
|
| 85 |
+
agent_executor_kwargs={
|
| 86 |
+
"handle_parsing_errors": True
|
| 87 |
+
}
|
| 88 |
)
|
| 89 |
|
| 90 |
# --- 4. EXECUTION ---
|
|
|
|
| 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))
|
| 120 |
break
|