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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -2
app.py CHANGED
@@ -104,14 +104,36 @@ 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
- # Create the pandas DataFrame agent, powered by Cohere
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  agent = create_pandas_dataframe_agent(
109
  llm,
110
  df,
111
  agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
112
  verbose=True,
113
  allow_dangerous_code=True,
114
- handle_parsing_errors=True # <-- THE FINAL FIX IS HERE
 
115
  )
116
 
117
  # Run the agent with the user's scenario text.
 
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,
132
  agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
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.