Update app.py
Browse files
app.py
CHANGED
|
@@ -38,45 +38,54 @@ tools = [
|
|
| 38 |
# ----------------------
|
| 39 |
# Define chatbot class
|
| 40 |
# ----------------------
|
|
|
|
|
|
|
| 41 |
class cbfs:
|
| 42 |
-
def __init__(self, tools, openai_key: str, tavily_key: str):
|
| 43 |
-
if not openai_key
|
| 44 |
-
raise ValueError("⚠️ Please provide
|
| 45 |
|
| 46 |
-
# Initialize OpenAI model
|
| 47 |
self.model = ChatOpenAI(temperature=0, openai_api_key=openai_key)
|
| 48 |
|
| 49 |
-
# Initialize Tavily
|
| 50 |
-
self.tavily = TavilyClient(api_key=tavily_key)
|
| 51 |
|
| 52 |
-
# Memory
|
| 53 |
-
self.memory = ConversationBufferMemory(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
MessagesPlaceholder(variable_name="chat_history"),
|
| 57 |
-
("user", "{input}"),
|
| 58 |
-
MessagesPlaceholder(variable_name="agent_scratchpad")
|
| 59 |
-
])
|
| 60 |
|
| 61 |
-
#
|
| 62 |
self.chain = initialize_agent(
|
| 63 |
tools=tools,
|
| 64 |
llm=self.model,
|
| 65 |
-
agent=
|
| 66 |
verbose=True,
|
| 67 |
-
memory=self.memory
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
def convchain(self, query: str) -> str:
|
| 71 |
-
"""Run a single query through the agent."""
|
| 72 |
if not query:
|
| 73 |
return "Please enter a query."
|
| 74 |
try:
|
| 75 |
result = self.chain.invoke({"input": query})
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
self.memory.save_context({"input": query}, {"output": response})
|
| 78 |
return response
|
| 79 |
except Exception as e:
|
|
|
|
| 80 |
return f"❌ Error: {str(e)}"
|
| 81 |
|
| 82 |
|
|
|
|
| 38 |
# ----------------------
|
| 39 |
# Define chatbot class
|
| 40 |
# ----------------------
|
| 41 |
+
from langchain.agents import initialize_agent, AgentType
|
| 42 |
+
|
| 43 |
class cbfs:
|
| 44 |
+
def __init__(self, tools, openai_key: str, tavily_key: str = None):
|
| 45 |
+
if not openai_key:
|
| 46 |
+
raise ValueError("⚠️ Please provide an OpenAI API key.")
|
| 47 |
|
| 48 |
+
# Initialize OpenAI model
|
| 49 |
self.model = ChatOpenAI(temperature=0, openai_api_key=openai_key)
|
| 50 |
|
| 51 |
+
# Initialize Tavily (optional)
|
| 52 |
+
self.tavily = TavilyClient(api_key=tavily_key) if tavily_key else None
|
| 53 |
|
| 54 |
+
# Memory
|
| 55 |
+
self.memory = ConversationBufferMemory(
|
| 56 |
+
return_messages=True, memory_key="chat_history", ai_prefix="Assistant"
|
| 57 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Agent
|
| 60 |
self.chain = initialize_agent(
|
| 61 |
tools=tools,
|
| 62 |
llm=self.model,
|
| 63 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, # ✅ correct way
|
| 64 |
verbose=True,
|
| 65 |
+
memory=self.memory,
|
| 66 |
+
handle_parsing_errors=True # ✅ prevents silent failure
|
| 67 |
)
|
| 68 |
|
| 69 |
def convchain(self, query: str) -> str:
|
|
|
|
| 70 |
if not query:
|
| 71 |
return "Please enter a query."
|
| 72 |
try:
|
| 73 |
result = self.chain.invoke({"input": query})
|
| 74 |
+
# Debugging: show full raw result
|
| 75 |
+
print("Agent raw result:", result)
|
| 76 |
+
|
| 77 |
+
# Try both possible output keys
|
| 78 |
+
response = (
|
| 79 |
+
result.get("output")
|
| 80 |
+
or result.get("output_text")
|
| 81 |
+
or "⚠️ No response generated."
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Save memory
|
| 85 |
self.memory.save_context({"input": query}, {"output": response})
|
| 86 |
return response
|
| 87 |
except Exception as e:
|
| 88 |
+
print("Execution Error:", str(e))
|
| 89 |
return f"❌ Error: {str(e)}"
|
| 90 |
|
| 91 |
|