Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,8 @@ import pandas as pd
|
|
| 10 |
|
| 11 |
# New additions for data analysis agent
|
| 12 |
from langchain.agents.agent_types import AgentType
|
|
|
|
| 13 |
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
|
| 14 |
-
from langchain_cohere import ChatCohere # <-- NEW, CORRECT IMPORT
|
| 15 |
|
| 16 |
# ---- Local modules
|
| 17 |
from settings import (
|
|
@@ -47,11 +47,11 @@ You are an expert data analysis project manager. Your task is to read the user's
|
|
| 47 |
|
| 48 |
From the user's text, extract the following:
|
| 49 |
1. **Primary Objective:** A one-sentence summary of the user's main goal.
|
| 50 |
-
2. **Key Tasks:** A numbered list of the specific questions the user wants answered.
|
| 51 |
-
3. **Expert Guidelines & Assumptions:** A bulleted list of
|
| 52 |
-
4. **Required Output Format:** A description of how the user wants the final answer
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
--- USER'S SCENARIO ---
|
| 57 |
{user_scenario}
|
|
@@ -66,7 +66,7 @@ def is_healthcare_scenario(text: str, has_files: bool) -> bool:
|
|
| 66 |
Dynamic detection: require uploaded files AND either structured scenario sections
|
| 67 |
or healthcare keywords (configured in settings).
|
| 68 |
"""
|
| 69 |
-
t = (text or "").lower()
|
| 70 |
kws = HEALTHCARE_SETTINGS["healthcare_keywords"]
|
| 71 |
structured = any(s in t for s in ["background", "situation", "tasks", "deliverables"])
|
| 72 |
return has_files and (structured or any(k in t for k in kws))
|
|
@@ -113,7 +113,22 @@ def handle(user_msg: str, history_messages: List[Dict[str, str]], files: list) -
|
|
| 113 |
|
| 114 |
llm = ChatCohere(model=COHERE_MODEL_PRIMARY, temperature=0)
|
| 115 |
enhanced_prompt = _create_enhanced_prompt(safe_in)
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
agent = create_pandas_dataframe_agent(
|
| 119 |
llm,
|
|
@@ -121,10 +136,10 @@ def handle(user_msg: str, history_messages: List[Dict[str, str]], files: list) -
|
|
| 121 |
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 122 |
verbose=True,
|
| 123 |
allow_dangerous_code=True,
|
| 124 |
-
prefix=AGENT_PREFIX
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
-
# Use the new .invoke() method
|
| 128 |
result = agent.invoke({"input": enhanced_prompt})
|
| 129 |
reply = _sanitize_text(result.get("output", "No output generated."))
|
| 130 |
|
|
@@ -188,4 +203,4 @@ if __name__ == "__main__":
|
|
| 188 |
"open_fallbacks": USE_OPEN_FALLBACKS,
|
| 189 |
"timeout_s": COHERE_TIMEOUT_S
|
| 190 |
})
|
| 191 |
-
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|
|
|
|
| 10 |
|
| 11 |
# New additions for data analysis agent
|
| 12 |
from langchain.agents.agent_types import AgentType
|
| 13 |
+
from langchain_cohere import ChatCohere
|
| 14 |
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
|
|
|
|
| 15 |
|
| 16 |
# ---- Local modules
|
| 17 |
from settings import (
|
|
|
|
| 47 |
|
| 48 |
From the user's text, extract the following:
|
| 49 |
1. **Primary Objective:** A one-sentence summary of the user's main goal.
|
| 50 |
+
2. **Key Tasks:** A numbered list of ALL the specific questions the user wants answered.
|
| 51 |
+
3. **Expert Guidelines & Assumptions:** A bulleted list of any specific numbers, metrics, or calculation methods mentioned.
|
| 52 |
+
4. **Required Output Format:** A description of how the user wants the final answer structured.
|
| 53 |
|
| 54 |
+
CRITICAL INSTRUCTION: Tell the data analyst that it MUST answer ALL of the key tasks before providing its final answer.
|
| 55 |
|
| 56 |
--- USER'S SCENARIO ---
|
| 57 |
{user_scenario}
|
|
|
|
| 66 |
Dynamic detection: require uploaded files AND either structured scenario sections
|
| 67 |
or healthcare keywords (configured in settings).
|
| 68 |
"""
|
| 69 |
+
t = (text or "").lower()
|
| 70 |
kws = HEALTHCARE_SETTINGS["healthcare_keywords"]
|
| 71 |
structured = any(s in t for s in ["background", "situation", "tasks", "deliverables"])
|
| 72 |
return has_files and (structured or any(k in t for k in kws))
|
|
|
|
| 113 |
|
| 114 |
llm = ChatCohere(model=COHERE_MODEL_PRIMARY, temperature=0)
|
| 115 |
enhanced_prompt = _create_enhanced_prompt(safe_in)
|
| 116 |
+
|
| 117 |
+
AGENT_PREFIX = """
|
| 118 |
+
You are a data analysis agent. You have access to one or more pandas dataframes.
|
| 119 |
+
You MUST respond in one of two formats.
|
| 120 |
+
|
| 121 |
+
FORMAT 1: To perform a task. Your response must be a single block of text with ONLY these three sections:
|
| 122 |
+
Thought: Your step-by-step reasoning.
|
| 123 |
+
Action: python_repl_ast
|
| 124 |
+
Action Input: The Python code to run.
|
| 125 |
+
|
| 126 |
+
FORMAT 2: To give the final answer. Your response must be a single block of text with ONLY these two sections:
|
| 127 |
+
Thought: I have now answered all the user's questions and can provide the final report.
|
| 128 |
+
Final Answer: The complete answer, structured as the user requested.
|
| 129 |
+
|
| 130 |
+
CRITICAL RULE: NEVER combine `Action` and `Final Answer` in the same response. Choose one format.
|
| 131 |
+
"""
|
| 132 |
|
| 133 |
agent = create_pandas_dataframe_agent(
|
| 134 |
llm,
|
|
|
|
| 136 |
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 137 |
verbose=True,
|
| 138 |
allow_dangerous_code=True,
|
| 139 |
+
prefix=AGENT_PREFIX,
|
| 140 |
+
max_iterations=50 # <-- THE FINAL FIX IS HERE
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
result = agent.invoke({"input": enhanced_prompt})
|
| 144 |
reply = _sanitize_text(result.get("output", "No output generated."))
|
| 145 |
|
|
|
|
| 203 |
"open_fallbacks": USE_OPEN_FALLBACKS,
|
| 204 |
"timeout_s": COHERE_TIMEOUT_S
|
| 205 |
})
|
| 206 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|