Rajan Sharma commited on
Commit
da1ed24
·
verified ·
1 Parent(s): 14882eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
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 gives the LLM strict instructions to prevent parsing errors.
109
  AGENT_PREFIX = """
110
- You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
111
- You are a data analysis expert providing a structured response.
112
-
113
- You are given a user query and you must use the python_repl_ast tool to answer.
114
-
115
- RULES:
116
- 1. You MUST use the python_repl_ast tool to derive your answers.
117
- 2. You MUST NOT provide a "Final Answer" until you have used the tool and have an observation.
118
- 3. You MUST work step-by-step. First Think, then use the Action/Action Input, then get an Observation. Do not try to do everything in one step.
119
-
120
- This is an example of a valid response:
121
- Thought: I need to check the column names in the dataframe.
122
- Action: python_repl_ast
123
- Action Input: print(df.columns)
124
-
125
- Begin!
126
- """
127
-
128
- # Create the pandas DataFrame agent with our custom prefix
 
 
 
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 # <-- APPLYING THE PROMPT ENGINEERING FIX
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.