Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from qdrant_client import QdrantClient
|
|
| 6 |
from langchain.agents import Tool, AgentExecutor, create_openai_tools_agent
|
| 7 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 8 |
from langchain.tools import BaseTool
|
|
|
|
| 9 |
from typing import Type, Optional
|
| 10 |
import os
|
| 11 |
import warnings
|
|
@@ -52,6 +53,19 @@ class AgentQuery(BaseModel):
|
|
| 52 |
|
| 53 |
# === UTILITY FUNCTIONS ===
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def get_encoded_auth_token(user: int, token: str) -> str:
|
| 56 |
auth_string = f"{user}:{token}"
|
| 57 |
return base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
|
|
@@ -263,35 +277,68 @@ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
|
| 263 |
|
| 264 |
# === API ENDPOINTS ===
|
| 265 |
|
| 266 |
-
@app.post("/bot")
|
| 267 |
-
def chat_with_agent(query: AgentQuery):
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
if query.orgId is not None:
|
| 278 |
-
agent_input += f", OrgId: {query.orgId}"
|
| 279 |
-
agent_input += "]"
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
return {
|
| 285 |
"message": query.message,
|
| 286 |
"answer": result["output"],
|
| 287 |
-
"
|
| 288 |
}
|
| 289 |
-
|
| 290 |
except Exception as e:
|
| 291 |
return {
|
| 292 |
"message": query.message,
|
| 293 |
-
"answer": f"An error occurred: {str(e)}"
|
| 294 |
-
"agent_used": True
|
| 295 |
}
|
| 296 |
|
| 297 |
@app.post("/chat-documents")
|
|
@@ -311,6 +358,26 @@ def chat_documents_only(query: Query):
|
|
| 311 |
"tool_used": "document_search"
|
| 312 |
}
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
@app.post("/list-projects")
|
| 315 |
def list_projects(request: ProjectRequest):
|
| 316 |
"""Direct project listing without agent"""
|
|
|
|
| 6 |
from langchain.agents import Tool, AgentExecutor, create_openai_tools_agent
|
| 7 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 8 |
from langchain.tools import BaseTool
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
from typing import Type, Optional
|
| 11 |
import os
|
| 12 |
import warnings
|
|
|
|
| 53 |
|
| 54 |
# === UTILITY FUNCTIONS ===
|
| 55 |
|
| 56 |
+
|
| 57 |
+
# Store chat histories keyed by user_id
|
| 58 |
+
user_memories = {}
|
| 59 |
+
|
| 60 |
+
def get_user_memory(user_id: str):
|
| 61 |
+
"""Fetch or create memory for a user."""
|
| 62 |
+
if user_id not in user_memories:
|
| 63 |
+
user_memories[user_id] = ConversationBufferMemory(
|
| 64 |
+
memory_key="chat_history",
|
| 65 |
+
return_messages=True
|
| 66 |
+
)
|
| 67 |
+
return user_memories[user_id]
|
| 68 |
+
|
| 69 |
def get_encoded_auth_token(user: int, token: str) -> str:
|
| 70 |
auth_string = f"{user}:{token}"
|
| 71 |
return base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
|
|
|
|
| 277 |
|
| 278 |
# === API ENDPOINTS ===
|
| 279 |
|
| 280 |
+
# @app.post("/bot")
|
| 281 |
+
# def chat_with_agent(query: AgentQuery):
|
| 282 |
+
# """Main agent endpoint - handles both document search and project queries intelligently"""
|
| 283 |
+
# try:
|
| 284 |
+
# # Prepare the input for the agent
|
| 285 |
+
# agent_input = query.message
|
| 286 |
+
|
| 287 |
+
# # If user provided credentials, add them to the context
|
| 288 |
|
| 289 |
+
# if query.userLoginId is not None:
|
| 290 |
+
# agent_input += f" [UserLoginId: {query.userLoginId}"
|
| 291 |
+
# if query.orgId is not None:
|
| 292 |
+
# agent_input += f", OrgId: {query.orgId}"
|
| 293 |
+
# agent_input += "]"
|
| 294 |
|
| 295 |
+
# # Use the agent to process the query
|
| 296 |
+
# result = agent_executor.invoke({"input": agent_input})
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
# return {
|
| 299 |
+
# "message": query.message,
|
| 300 |
+
# "answer": result["output"],
|
| 301 |
+
# "agent_used": True
|
| 302 |
+
# }
|
| 303 |
|
| 304 |
+
# except Exception as e:
|
| 305 |
+
# return {
|
| 306 |
+
# "message": query.message,
|
| 307 |
+
# "answer": f"An error occurred: {str(e)}",
|
| 308 |
+
# "agent_used": True
|
| 309 |
+
# }
|
| 310 |
+
@app.post("/bot")
|
| 311 |
+
def chat_with_agent(query: AgentQuery):
|
| 312 |
+
"""Main agent endpoint with per-user chat history."""
|
| 313 |
+
try:
|
| 314 |
+
user_id = query.userLoginId or "anonymous" # fallback if no ID
|
| 315 |
+
memory = get_user_memory(user_id)
|
| 316 |
+
|
| 317 |
+
agent = initialize_agent(
|
| 318 |
+
tools,
|
| 319 |
+
llm,
|
| 320 |
+
agent="chat-conversational-react-description",
|
| 321 |
+
memory=memory,
|
| 322 |
+
verbose=True
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
# Build input
|
| 326 |
+
agent_input = query.message
|
| 327 |
+
if query.orgId is not None:
|
| 328 |
+
agent_input += f" [OrgId: {query.orgId}]"
|
| 329 |
+
|
| 330 |
+
# Run agent
|
| 331 |
+
result = agent.invoke({"input": agent_input})
|
| 332 |
+
|
| 333 |
return {
|
| 334 |
"message": query.message,
|
| 335 |
"answer": result["output"],
|
| 336 |
+
"chat_history": memory.load_memory_variables({})["chat_history"]
|
| 337 |
}
|
|
|
|
| 338 |
except Exception as e:
|
| 339 |
return {
|
| 340 |
"message": query.message,
|
| 341 |
+
"answer": f"An error occurred: {str(e)}"
|
|
|
|
| 342 |
}
|
| 343 |
|
| 344 |
@app.post("/chat-documents")
|
|
|
|
| 358 |
"tool_used": "document_search"
|
| 359 |
}
|
| 360 |
|
| 361 |
+
@app.get("/get-chat-history/{user_id}")
|
| 362 |
+
def get_chat_history(user_id: str, n: int = 10):
|
| 363 |
+
"""
|
| 364 |
+
Fetch the last N chat messages for a user.
|
| 365 |
+
Default: 10
|
| 366 |
+
"""
|
| 367 |
+
memory = user_memories.get(user_id)
|
| 368 |
+
|
| 369 |
+
if not memory:
|
| 370 |
+
return {"user_id": user_id, "chat_history": []}
|
| 371 |
+
|
| 372 |
+
history = memory.load_memory_variables({})["chat_history"]
|
| 373 |
+
|
| 374 |
+
# Only return last N messages
|
| 375 |
+
return {
|
| 376 |
+
"user_id": user_id,
|
| 377 |
+
"chat_history": history[-n:]
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
|
| 381 |
@app.post("/list-projects")
|
| 382 |
def list_projects(request: ProjectRequest):
|
| 383 |
"""Direct project listing without agent"""
|