Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +103 -62
src/streamlit_app.py
CHANGED
|
@@ -387,7 +387,7 @@ def initialize_agent():
|
|
| 387 |
base_retriever = vector_store.as_retriever(
|
| 388 |
search_type="similarity",
|
| 389 |
search_kwargs={
|
| 390 |
-
"k":
|
| 391 |
|
| 392 |
}
|
| 393 |
)
|
|
@@ -415,86 +415,127 @@ def initialize_agent():
|
|
| 415 |
except Exception as e:
|
| 416 |
return f"Error retrieving information: {str(e)}"
|
| 417 |
|
| 418 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
Retriver_tool = Tool(
|
| 420 |
-
name="
|
| 421 |
-
func=
|
| 422 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
)
|
| 424 |
-
|
| 425 |
-
#
|
| 426 |
-
def general_qa(query):
|
| 427 |
-
"""General question answering"""
|
| 428 |
-
try:
|
| 429 |
-
return llm.invoke(query).content
|
| 430 |
-
except Exception as e:
|
| 431 |
-
return f"Error: {str(e)}"
|
| 432 |
-
|
| 433 |
qa_tool = Tool(
|
| 434 |
-
name="
|
| 435 |
-
func=
|
| 436 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
)
|
| 438 |
-
|
| 439 |
# Summary tool
|
| 440 |
-
def summarize_text(text):
|
| 441 |
-
"""Summarize text"""
|
| 442 |
-
try:
|
| 443 |
-
prompt = f"Summarize the following concisely:\n\n{text}"
|
| 444 |
-
return llm.invoke(prompt).content
|
| 445 |
-
except Exception as e:
|
| 446 |
-
return f"Error: {str(e)}"
|
| 447 |
-
|
| 448 |
summary_tool = Tool(
|
| 449 |
-
name="
|
| 450 |
-
func=
|
| 451 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
)
|
| 453 |
-
|
| 454 |
# Explanation tool
|
| 455 |
-
def explain_concept(concept):
|
| 456 |
-
"""Explain concepts"""
|
| 457 |
-
try:
|
| 458 |
-
prompt = f"Explain clearly:\n\n{concept}"
|
| 459 |
-
return llm.invoke(prompt).content
|
| 460 |
-
except Exception as e:
|
| 461 |
-
return f"Error: {str(e)}"
|
| 462 |
-
|
| 463 |
explanation_tool = Tool(
|
| 464 |
-
name="
|
| 465 |
-
func=
|
| 466 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
)
|
| 468 |
-
|
| 469 |
-
|
|
|
|
| 470 |
tool_names = ", ".join([tool.name for tool in tools])
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
# Custom ReAct prompt
|
| 473 |
react_prompt = PromptTemplate.from_template(
|
| 474 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
-
|
| 477 |
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
|
|
|
| 487 |
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
2. Action Input should be the question/text only - no quotes or special formatting
|
| 491 |
-
3. Always provide a Final Answer
|
| 492 |
|
| 493 |
-
|
| 494 |
-
{chat_history}
|
| 495 |
|
| 496 |
-
|
| 497 |
-
|
| 498 |
).partial(
|
| 499 |
tools="\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
|
| 500 |
tool_names=tool_names
|
|
|
|
| 387 |
base_retriever = vector_store.as_retriever(
|
| 388 |
search_type="similarity",
|
| 389 |
search_kwargs={
|
| 390 |
+
"k": 3,
|
| 391 |
|
| 392 |
}
|
| 393 |
)
|
|
|
|
| 415 |
except Exception as e:
|
| 416 |
return f"Error retrieving information: {str(e)}"
|
| 417 |
|
| 418 |
+
# Retriever tool (core RAG function)
|
| 419 |
+
retriever_tool = create_retriever_tool(
|
| 420 |
+
retriever=base_retriever,
|
| 421 |
+
name="retriever",
|
| 422 |
+
description=(
|
| 423 |
+
"Use this tool to answer ANY question that might be related to or found in the uploaded or provided documents. "
|
| 424 |
+
"Always call this tool FIRST whenever the question could possibly require information from those documents. "
|
| 425 |
+
"If the question asks about facts, data, summaries, policies, reports, or anything that may come from the user's documents, "
|
| 426 |
+
"use this tool to retrieve the relevant content before answering."
|
| 427 |
+
),
|
| 428 |
+
)
|
| 429 |
+
|
| 430 |
Retriver_tool = Tool(
|
| 431 |
+
name="retriever",
|
| 432 |
+
func=retriever_tool,
|
| 433 |
+
description=(
|
| 434 |
+
"Retrieves relevant context from the user's uploaded or stored documents. "
|
| 435 |
+
"Use this tool for any question that might involve the content of the documents, "
|
| 436 |
+
"such as document summaries, factual answers, or topic-specific details."
|
| 437 |
+
),
|
| 438 |
)
|
| 439 |
+
|
| 440 |
+
# QA tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
qa_tool = Tool(
|
| 442 |
+
name="Question Answering",
|
| 443 |
+
func=llm.invoke,
|
| 444 |
+
description=(
|
| 445 |
+
"A general-purpose question answering tool. "
|
| 446 |
+
"Use this ONLY for casual or open-ended questions that are NOT related to the provided documents. "
|
| 447 |
+
"Examples: greetings, opinions, or general world knowledge questions (e.g., 'How are you?', 'What is AI?'). "
|
| 448 |
+
"Do NOT use this if the question might depend on the document contents."
|
| 449 |
+
),
|
| 450 |
)
|
| 451 |
+
|
| 452 |
# Summary tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
summary_tool = Tool(
|
| 454 |
+
name="Summary",
|
| 455 |
+
func=llm.invoke,
|
| 456 |
+
description="Summarizes long text passages into concise summaries using a structured summarization prompt.",
|
| 457 |
+
prompt=PromptTemplate(
|
| 458 |
+
input_variables=["input"],
|
| 459 |
+
template="""
|
| 460 |
+
You are a summarization assistant. Follow these steps to summarize the text:
|
| 461 |
+
1. Read the text carefully.
|
| 462 |
+
2. Identify the main points and key details.
|
| 463 |
+
3. Write a concise summary that captures the essence of the text.
|
| 464 |
+
|
| 465 |
+
Text: {input}
|
| 466 |
+
|
| 467 |
+
Summary:
|
| 468 |
+
""",
|
| 469 |
+
),
|
| 470 |
)
|
| 471 |
+
|
| 472 |
# Explanation tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
explanation_tool = Tool(
|
| 474 |
+
name="Explanation",
|
| 475 |
+
func=llm.invoke,
|
| 476 |
+
description="Explains complex concepts in simple, clear terms using examples or analogies when appropriate.",
|
| 477 |
+
prompt=PromptTemplate(
|
| 478 |
+
input_variables=["input"],
|
| 479 |
+
template="""
|
| 480 |
+
You are an explanation assistant. Follow these steps to explain the concept:
|
| 481 |
+
1. Understand the concept thoroughly.
|
| 482 |
+
2. Break down the concept into simpler parts.
|
| 483 |
+
3. Provide a clear and detailed explanation with examples.
|
| 484 |
+
|
| 485 |
+
Concept: {input}
|
| 486 |
+
|
| 487 |
+
Explanation:
|
| 488 |
+
""",
|
| 489 |
+
),
|
| 490 |
)
|
| 491 |
+
|
| 492 |
+
# Tool list (retriever first for prioritization)
|
| 493 |
+
tools = [Retriver_tool, summary_tool, explanation_tool, qa_tool]
|
| 494 |
tool_names = ", ".join([tool.name for tool in tools])
|
| 495 |
+
|
| 496 |
+
example = """
|
| 497 |
+
Example:
|
| 498 |
+
Thought: I should use the retriever tool to find relevant info.
|
| 499 |
+
Action: retriever
|
| 500 |
+
Action Input: current head of the American Red Cross
|
| 501 |
+
Observation: The documents do not mention the head of the American Red Cross.
|
| 502 |
+
Thought: The information is not in the documents.
|
| 503 |
+
Final Answer: I'm sorry, but I couldn’t find information about that in the provided documents.
|
| 504 |
+
"""
|
| 505 |
# Custom ReAct prompt
|
| 506 |
react_prompt = PromptTemplate.from_template(
|
| 507 |
+
example + """
|
| 508 |
+
You are a retrieval-augmented assistant that answers questions ONLY using the information
|
| 509 |
+
found in the user's provided documents.
|
| 510 |
+
|
| 511 |
+
You have access to the following tools:
|
| 512 |
+
{tools}
|
| 513 |
|
| 514 |
+
Follow this reasoning format:
|
| 515 |
|
| 516 |
+
Thought: Think about what the question is asking and whether you can find the answer in the user's documents.
|
| 517 |
+
Action: The action to take, must be one of [{tool_names}]
|
| 518 |
+
Action Input: The input to the action (be specific)
|
| 519 |
+
Observation: The result of the action
|
| 520 |
+
... (You may repeat this Thought/Action/Observation cycle as needed)
|
| 521 |
+
Final Answer: Your final grounded answer to the user's question.
|
| 522 |
|
| 523 |
+
### Important Grounding Rules:
|
| 524 |
+
- You MUST first use the 'retriever' tool to search for relevant information in the user's documents.
|
| 525 |
+
- Only use the information retrieved from the documents to answer the question.
|
| 526 |
+
- If the retrieved information does not contain a clear or relevant answer, respond with:
|
| 527 |
+
"I'm sorry, but I couldn’t find information about that in the provided documents."
|
| 528 |
+
- Do NOT use your own general knowledge or external world knowledge.
|
| 529 |
+
- Use the 'Question Answering' tool only for generic greetings (like 'hi', 'how are you') or clarification.
|
| 530 |
+
- You may use multiple tools in sequence before providing the final answer.
|
| 531 |
|
| 532 |
+
Previous conversation:
|
| 533 |
+
{chat_history}
|
|
|
|
|
|
|
| 534 |
|
| 535 |
+
Question: {input}
|
|
|
|
| 536 |
|
| 537 |
+
{agent_scratchpad}
|
| 538 |
+
"""
|
| 539 |
).partial(
|
| 540 |
tools="\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
|
| 541 |
tool_names=tool_names
|