Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ from langchain.document_loaders import TextLoader
|
|
| 20 |
from langchain.memory import ConversationBufferMemory
|
| 21 |
from langchain.agents import initialize_agent, Tool
|
| 22 |
from langchain.agents.agent_types import AgentType
|
|
|
|
| 23 |
|
| 24 |
class AbbyyVantage:
|
| 25 |
"""
|
|
@@ -248,8 +249,7 @@ def setup_agent(file):
|
|
| 248 |
agent_executor = initialize_agent(
|
| 249 |
tools=tools,
|
| 250 |
llm=ChatOpenAI(model="gpt-3.5-turbo"),
|
| 251 |
-
agent=AgentType.
|
| 252 |
-
#agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
|
| 253 |
memory=memory,
|
| 254 |
verbose=True
|
| 255 |
)
|
|
@@ -285,9 +285,6 @@ def ask_question(message, history):
|
|
| 285 |
"**💡 Advisor’s Practical Tip:**\n"
|
| 286 |
"- Give actionable tips to help the user get the most from their policy.\n\n"
|
| 287 |
|
| 288 |
-
"**⚠️ Caveats and Exclusions:**\n"
|
| 289 |
-
"- Mention any exclusions, limitations, or waiting periods.\n\n"
|
| 290 |
-
|
| 291 |
"Your tone should be empathetic and clear—like a smart, helpful insurance advisor.\n"
|
| 292 |
"Format everything using **Markdown**. Always include all four sections in the output.\n\n"
|
| 293 |
|
|
@@ -298,8 +295,6 @@ def ask_question(message, history):
|
|
| 298 |
"**🧠 Simplified Explanation:**\n"
|
| 299 |
"- ...\n\n"
|
| 300 |
"**💡 Advisor’s Practical Tip:**\n"
|
| 301 |
-
"- ...\n\n"
|
| 302 |
-
"**⚠️ Caveats and Exclusions:**\n"
|
| 303 |
"- ...\n"
|
| 304 |
"---"
|
| 305 |
"---\n"
|
|
@@ -307,7 +302,6 @@ def ask_question(message, history):
|
|
| 307 |
"**📄 Policy Details:**\n- The policy offers 24-month waiting period for pre-existing conditions.\n\n"
|
| 308 |
"**🧠 Simplified Explanation:**\n- You have to wait 2 years before claims related to past illnesses are covered.\n\n"
|
| 309 |
"**💡 Advisor’s Practical Tip:**\n- Consider a top-up or rider that waives this waiting period.\n\n"
|
| 310 |
-
"**⚠️ Caveats and Exclusions:**\n- Diabetes and hypertension are included in pre-existing illnesses list.\n\n"
|
| 311 |
)
|
| 312 |
|
| 313 |
|
|
@@ -315,8 +309,20 @@ def ask_question(message, history):
|
|
| 315 |
prompt = f"{advisory_prompt}\nQuestion: {message}"
|
| 316 |
|
| 317 |
try:
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
except Exception as e:
|
| 321 |
return f"❌ Error: {str(e)}"
|
| 322 |
|
|
|
|
| 20 |
from langchain.memory import ConversationBufferMemory
|
| 21 |
from langchain.agents import initialize_agent, Tool
|
| 22 |
from langchain.agents.agent_types import AgentType
|
| 23 |
+
from langchain.schema import messages_from_dict, messages_to_dict
|
| 24 |
|
| 25 |
class AbbyyVantage:
|
| 26 |
"""
|
|
|
|
| 249 |
agent_executor = initialize_agent(
|
| 250 |
tools=tools,
|
| 251 |
llm=ChatOpenAI(model="gpt-3.5-turbo"),
|
| 252 |
+
agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
|
|
|
|
| 253 |
memory=memory,
|
| 254 |
verbose=True
|
| 255 |
)
|
|
|
|
| 285 |
"**💡 Advisor’s Practical Tip:**\n"
|
| 286 |
"- Give actionable tips to help the user get the most from their policy.\n\n"
|
| 287 |
|
|
|
|
|
|
|
|
|
|
| 288 |
"Your tone should be empathetic and clear—like a smart, helpful insurance advisor.\n"
|
| 289 |
"Format everything using **Markdown**. Always include all four sections in the output.\n\n"
|
| 290 |
|
|
|
|
| 295 |
"**🧠 Simplified Explanation:**\n"
|
| 296 |
"- ...\n\n"
|
| 297 |
"**💡 Advisor’s Practical Tip:**\n"
|
|
|
|
|
|
|
| 298 |
"- ...\n"
|
| 299 |
"---"
|
| 300 |
"---\n"
|
|
|
|
| 302 |
"**📄 Policy Details:**\n- The policy offers 24-month waiting period for pre-existing conditions.\n\n"
|
| 303 |
"**🧠 Simplified Explanation:**\n- You have to wait 2 years before claims related to past illnesses are covered.\n\n"
|
| 304 |
"**💡 Advisor’s Practical Tip:**\n- Consider a top-up or rider that waives this waiting period.\n\n"
|
|
|
|
| 305 |
)
|
| 306 |
|
| 307 |
|
|
|
|
| 309 |
prompt = f"{advisory_prompt}\nQuestion: {message}"
|
| 310 |
|
| 311 |
try:
|
| 312 |
+
result = agent_executor.invoke({"input": prompt})
|
| 313 |
+
final_output = result.get("output", "No output.")
|
| 314 |
+
steps = result.get("intermediate_steps", [])
|
| 315 |
+
|
| 316 |
+
steps_md = "\n\n<details><summary>🧠 Agent Thought Process</summary>\n\n"
|
| 317 |
+
for i, (action, observation) in enumerate(steps, 1):
|
| 318 |
+
steps_md += f"**Step {i}**\n\n"
|
| 319 |
+
steps_md += f"- **Thought**: {action.log.strip()}\n"
|
| 320 |
+
steps_md += f"- **Tool Used**: `{action.tool}` with input `{action.tool_input}`\n"
|
| 321 |
+
steps_md += f"- **Observation**: {observation.strip()}\n\n"
|
| 322 |
+
steps_md += "</details>"
|
| 323 |
+
|
| 324 |
+
return final_output + "\n\n" + steps_md
|
| 325 |
+
|
| 326 |
except Exception as e:
|
| 327 |
return f"❌ Error: {str(e)}"
|
| 328 |
|