Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,14 +38,14 @@ llm = ChatOpenAI(model="gpt-4o", temperature=0, openai_api_key=OPENAI_API_KEY)
|
|
| 38 |
# === INPUT SCHEMAS ===
|
| 39 |
|
| 40 |
class Query(BaseModel):
|
| 41 |
-
|
| 42 |
|
| 43 |
class ProjectRequest(BaseModel):
|
| 44 |
userLoginId: int
|
| 45 |
orgId: int
|
| 46 |
|
| 47 |
class AgentQuery(BaseModel):
|
| 48 |
-
|
| 49 |
userLoginId: Optional[int] = None
|
| 50 |
orgId: Optional[int] = None
|
| 51 |
auth_token: Optional[str] = None
|
|
@@ -123,7 +123,7 @@ def search_documents(query: str) -> str:
|
|
| 123 |
"""Search through ingested documents and get relevant information.
|
| 124 |
|
| 125 |
Args:
|
| 126 |
-
query: The search query or
|
| 127 |
|
| 128 |
Returns:
|
| 129 |
Relevant information from the documents with sources
|
|
@@ -154,13 +154,13 @@ def search_documents(query: str) -> str:
|
|
| 154 |
context = "\n\n".join(context_texts)
|
| 155 |
unique_sources = list(set(sources))
|
| 156 |
|
| 157 |
-
# Use the LLM directly to answer the
|
| 158 |
-
prompt = f"""Based on the following context, answer the
|
| 159 |
|
| 160 |
Context:
|
| 161 |
{context}
|
| 162 |
|
| 163 |
-
Please provide a comprehensive answer based on the context above. If the context doesn't contain enough information to answer the
|
| 164 |
|
| 165 |
response = llm.invoke(prompt)
|
| 166 |
|
|
@@ -212,8 +212,8 @@ def get_user_projects(userLoginId: str) -> str:
|
|
| 212 |
document_search_tool = Tool(
|
| 213 |
name="document_search",
|
| 214 |
description="""Use this tool to search through ingested documents and get relevant information from the knowledge base.
|
| 215 |
-
Perfect for answering
|
| 216 |
-
Input should be a search query or
|
| 217 |
func=search_documents
|
| 218 |
)
|
| 219 |
|
|
@@ -234,7 +234,7 @@ agent_prompt = ChatPromptTemplate.from_messages([
|
|
| 234 |
2. **Project Management**: Get list of user projects and project information
|
| 235 |
|
| 236 |
Your capabilities:
|
| 237 |
-
- Answer
|
| 238 |
- Help users find their projects and project information
|
| 239 |
- Provide general assistance and information
|
| 240 |
- Use appropriate tools based on user queries
|
|
@@ -247,7 +247,7 @@ Guidelines:
|
|
| 247 |
- If you're unsure which tool to use, you can ask for clarification
|
| 248 |
- Provide helpful, accurate, and well-formatted responses
|
| 249 |
|
| 250 |
-
Remember: Always use the most appropriate tool based on the user's
|
| 251 |
("user", "{input}"),
|
| 252 |
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 253 |
])
|
|
@@ -268,7 +268,7 @@ def chat_with_agent(query: AgentQuery):
|
|
| 268 |
"""Main agent endpoint - handles both document search and project queries intelligently"""
|
| 269 |
try:
|
| 270 |
# Prepare the input for the agent
|
| 271 |
-
agent_input = query.
|
| 272 |
|
| 273 |
# If user provided credentials, add them to the context
|
| 274 |
|
|
@@ -282,14 +282,14 @@ def chat_with_agent(query: AgentQuery):
|
|
| 282 |
result = agent_executor.invoke({"input": agent_input})
|
| 283 |
|
| 284 |
return {
|
| 285 |
-
"
|
| 286 |
"answer": result["output"],
|
| 287 |
"agent_used": True
|
| 288 |
}
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
return {
|
| 292 |
-
"
|
| 293 |
"answer": f"An error occurred: {str(e)}",
|
| 294 |
"agent_used": True
|
| 295 |
}
|
|
@@ -298,15 +298,15 @@ def chat_with_agent(query: AgentQuery):
|
|
| 298 |
def chat_documents_only(query: Query):
|
| 299 |
"""Direct document search without agent"""
|
| 300 |
try:
|
| 301 |
-
result = search_documents(query.
|
| 302 |
return {
|
| 303 |
-
"
|
| 304 |
"answer": result,
|
| 305 |
"tool_used": "document_search"
|
| 306 |
}
|
| 307 |
except Exception as e:
|
| 308 |
return {
|
| 309 |
-
"
|
| 310 |
"answer": f"An error occurred: {str(e)}",
|
| 311 |
"tool_used": "document_search"
|
| 312 |
}
|
|
@@ -339,4 +339,3 @@ def list_projects(request: ProjectRequest):
|
|
| 339 |
@app.get("/health")
|
| 340 |
def health():
|
| 341 |
return {"status": "ok", "tools": ["document_search", "project_list"], "agent": "active"}
|
| 342 |
-
|
|
|
|
| 38 |
# === INPUT SCHEMAS ===
|
| 39 |
|
| 40 |
class Query(BaseModel):
|
| 41 |
+
message: str
|
| 42 |
|
| 43 |
class ProjectRequest(BaseModel):
|
| 44 |
userLoginId: int
|
| 45 |
orgId: int
|
| 46 |
|
| 47 |
class AgentQuery(BaseModel):
|
| 48 |
+
message: str
|
| 49 |
userLoginId: Optional[int] = None
|
| 50 |
orgId: Optional[int] = None
|
| 51 |
auth_token: Optional[str] = None
|
|
|
|
| 123 |
"""Search through ingested documents and get relevant information.
|
| 124 |
|
| 125 |
Args:
|
| 126 |
+
query: The search query or message about the documents
|
| 127 |
|
| 128 |
Returns:
|
| 129 |
Relevant information from the documents with sources
|
|
|
|
| 154 |
context = "\n\n".join(context_texts)
|
| 155 |
unique_sources = list(set(sources))
|
| 156 |
|
| 157 |
+
# Use the LLM directly to answer the message based on context
|
| 158 |
+
prompt = f"""Based on the following context, answer the message: {query}
|
| 159 |
|
| 160 |
Context:
|
| 161 |
{context}
|
| 162 |
|
| 163 |
+
Please provide a comprehensive answer based on the context above. If the context doesn't contain enough information to answer the message, say so clearly."""
|
| 164 |
|
| 165 |
response = llm.invoke(prompt)
|
| 166 |
|
|
|
|
| 212 |
document_search_tool = Tool(
|
| 213 |
name="document_search",
|
| 214 |
description="""Use this tool to search through ingested documents and get relevant information from the knowledge base.
|
| 215 |
+
Perfect for answering messages about uploaded documents, manuals, or any content that was previously stored.
|
| 216 |
+
Input should be a search query or message about the documents.""",
|
| 217 |
func=search_documents
|
| 218 |
)
|
| 219 |
|
|
|
|
| 234 |
2. **Project Management**: Get list of user projects and project information
|
| 235 |
|
| 236 |
Your capabilities:
|
| 237 |
+
- Answer messages about documents using the document search tool
|
| 238 |
- Help users find their projects and project information
|
| 239 |
- Provide general assistance and information
|
| 240 |
- Use appropriate tools based on user queries
|
|
|
|
| 247 |
- If you're unsure which tool to use, you can ask for clarification
|
| 248 |
- Provide helpful, accurate, and well-formatted responses
|
| 249 |
|
| 250 |
+
Remember: Always use the most appropriate tool based on the user's message to provide the best possible answer."""),
|
| 251 |
("user", "{input}"),
|
| 252 |
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 253 |
])
|
|
|
|
| 268 |
"""Main agent endpoint - handles both document search and project queries intelligently"""
|
| 269 |
try:
|
| 270 |
# Prepare the input for the agent
|
| 271 |
+
agent_input = query.message
|
| 272 |
|
| 273 |
# If user provided credentials, add them to the context
|
| 274 |
|
|
|
|
| 282 |
result = agent_executor.invoke({"input": agent_input})
|
| 283 |
|
| 284 |
return {
|
| 285 |
+
"message": query.message,
|
| 286 |
"answer": result["output"],
|
| 287 |
"agent_used": True
|
| 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 |
}
|
|
|
|
| 298 |
def chat_documents_only(query: Query):
|
| 299 |
"""Direct document search without agent"""
|
| 300 |
try:
|
| 301 |
+
result = search_documents(query.message)
|
| 302 |
return {
|
| 303 |
+
"message": query.message,
|
| 304 |
"answer": result,
|
| 305 |
"tool_used": "document_search"
|
| 306 |
}
|
| 307 |
except Exception as e:
|
| 308 |
return {
|
| 309 |
+
"message": query.message,
|
| 310 |
"answer": f"An error occurred: {str(e)}",
|
| 311 |
"tool_used": "document_search"
|
| 312 |
}
|
|
|
|
| 339 |
@app.get("/health")
|
| 340 |
def health():
|
| 341 |
return {"status": "ok", "tools": ["document_search", "project_list"], "agent": "active"}
|
|
|