Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
from __future__ import annotations
|
| 3 |
import os
|
| 4 |
import traceback
|
|
@@ -104,28 +105,31 @@ def handle(user_msg: str, history_messages: List[Dict[str, str]], files: list) -
|
|
| 104 |
# Initialize the Cohere Chat LLM for the agent
|
| 105 |
llm = ChatCohere(model=COHERE_MODEL_PRIMARY, temperature=0)
|
| 106 |
|
| 107 |
-
# --- PROMPT ENGINEERING FIX ---
|
| 108 |
-
# This prefix
|
| 109 |
AGENT_PREFIX = """
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
agent = create_pandas_dataframe_agent(
|
| 130 |
llm,
|
| 131 |
df,
|
|
@@ -133,7 +137,7 @@ def handle(user_msg: str, history_messages: List[Dict[str, str]], files: list) -
|
|
| 133 |
verbose=True,
|
| 134 |
allow_dangerous_code=True,
|
| 135 |
handle_parsing_errors=True,
|
| 136 |
-
prefix=AGENT_PREFIX #
|
| 137 |
)
|
| 138 |
|
| 139 |
# Run the agent with the user's scenario text.
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# app.py
|
| 3 |
from __future__ import annotations
|
| 4 |
import os
|
| 5 |
import traceback
|
|
|
|
| 105 |
# Initialize the Cohere Chat LLM for the agent
|
| 106 |
llm = ChatCohere(model=COHERE_MODEL_PRIMARY, temperature=0)
|
| 107 |
|
| 108 |
+
# --- AGGRESSIVE PROMPT ENGINEERING FIX ---
|
| 109 |
+
# This new prefix explicitly forbids the LLM from outputting an Action and a Final Answer simultaneously.
|
| 110 |
AGENT_PREFIX = """
|
| 111 |
+
You are a data analysis agent working with a pandas dataframe named `df`.
|
| 112 |
+
You must answer the user's query by executing Python code one step at a time.
|
| 113 |
+
|
| 114 |
+
Your response MUST strictly follow one of two formats:
|
| 115 |
+
|
| 116 |
+
FORMAT 1: To think and execute code.
|
| 117 |
+
Thought: Your reasoning for the next step. This is mandatory.
|
| 118 |
+
Action: The tool to use, which is always `python_repl_ast`.
|
| 119 |
+
Action Input: The single-line Python code to execute.
|
| 120 |
+
|
| 121 |
+
FORMAT 2: To give the final answer.
|
| 122 |
+
Thought: I have now gathered all the information needed to answer the user's request.
|
| 123 |
+
Final Answer: The final, comprehensive answer.
|
| 124 |
+
|
| 125 |
+
CRITICAL RULE: Your response must contain EITHER an "Action" OR a "Final Answer", but NEVER BOTH.
|
| 126 |
+
If you need to run code, use FORMAT 1. If you are finished, use FORMAT 2.
|
| 127 |
+
Do not combine them. Start by thinking about the first step.
|
| 128 |
+
|
| 129 |
+
Begin!
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
# Create the pandas DataFrame agent with our new, stricter prefix
|
| 133 |
agent = create_pandas_dataframe_agent(
|
| 134 |
llm,
|
| 135 |
df,
|
|
|
|
| 137 |
verbose=True,
|
| 138 |
allow_dangerous_code=True,
|
| 139 |
handle_parsing_errors=True,
|
| 140 |
+
prefix=AGENT_PREFIX # Applying the new aggressive prompt
|
| 141 |
)
|
| 142 |
|
| 143 |
# Run the agent with the user's scenario text.
|