Commit Β·
2ca006c
1
Parent(s): 2604671
converted list to string
Browse files
README.md
CHANGED
|
@@ -1,15 +1,3 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Agentic RAG Knowledge Search
|
| 3 |
-
emoji: π
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.44.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
short_description: Agentic RAG β routes queries to internal docs (FAISS) or live web search
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
# Agentic RAG Knowledge Search
|
| 14 |
|
| 15 |
An Autonomous AI Microservice with Hybrid Retrieval & Self-Evaluation. Built with FastAPI, LangChain, Docker, and Google Gemini.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Agentic RAG Knowledge Search
|
| 2 |
|
| 3 |
An Autonomous AI Microservice with Hybrid Retrieval & Self-Evaluation. Built with FastAPI, LangChain, Docker, and Google Gemini.
|
app.py
CHANGED
|
@@ -18,12 +18,16 @@ except Exception as e:
|
|
| 18 |
logger.error(f"Failed to initialize agent: {e}")
|
| 19 |
agent_executor = None
|
| 20 |
|
| 21 |
-
def determine_source(response_text
|
| 22 |
"""
|
| 23 |
Attempt to determine if the response came from RAG or Web Search.
|
| 24 |
Looks for keywords or source indicators in the response.
|
| 25 |
"""
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Check for web search indicators
|
| 29 |
if any(keyword in response_lower for keyword in ["search", "web", "duckduckgo", "internet", "online"]):
|
|
@@ -52,13 +56,9 @@ def process_query(message: str, chat_history: list) -> tuple[list, str]:
|
|
| 52 |
"""
|
| 53 |
if not agent_executor:
|
| 54 |
error_msg = "Agent not initialized. Please check GOOGLE_API_KEY."
|
| 55 |
-
chat_history.append(
|
| 56 |
-
chat_history.append({"role": "assistant", "content": f"ERROR: {error_msg}"})
|
| 57 |
return chat_history, error_msg
|
| 58 |
|
| 59 |
-
# Add user message to history
|
| 60 |
-
chat_history.append({"role": "user", "content": message})
|
| 61 |
-
|
| 62 |
try:
|
| 63 |
logger.info(f"Processing query: {message}")
|
| 64 |
|
|
@@ -72,14 +72,20 @@ def process_query(message: str, chat_history: list) -> tuple[list, str]:
|
|
| 72 |
last_message = result["messages"][-1]
|
| 73 |
response_text = last_message.content
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Determine source
|
| 76 |
source = determine_source(response_text)
|
| 77 |
|
| 78 |
# Format response with source indicator
|
| 79 |
full_response = f"{response_text}\n\n--- {source} ---"
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
chat_history.append(
|
| 83 |
|
| 84 |
logger.info(f"Response generated successfully")
|
| 85 |
return chat_history, f"Query processed. {source}"
|
|
@@ -87,21 +93,20 @@ def process_query(message: str, chat_history: list) -> tuple[list, str]:
|
|
| 87 |
except Exception as e:
|
| 88 |
error_msg = f"Error processing query: {str(e)}"
|
| 89 |
logger.error(error_msg)
|
| 90 |
-
chat_history.append(
|
| 91 |
return chat_history, error_msg
|
| 92 |
|
| 93 |
def clear_chat() -> tuple[list, str]:
|
| 94 |
"""Clear chat history."""
|
| 95 |
-
return [], "
|
| 96 |
|
| 97 |
# Build Gradio Interface
|
| 98 |
-
with gr.Blocks(title="Agentic RAG Knowledge Search"
|
| 99 |
gr.Markdown("# π Agentic RAG Knowledge Search")
|
| 100 |
gr.Markdown("Ask questions answered from internal documents (RAG) or the live web β the agent decides.")
|
| 101 |
|
| 102 |
with gr.Group():
|
| 103 |
chatbot = gr.Chatbot(
|
| 104 |
-
type="messages",
|
| 105 |
label="Conversation History",
|
| 106 |
show_label=True,
|
| 107 |
height=400
|
|
@@ -130,7 +135,7 @@ with gr.Blocks(title="Agentic RAG Knowledge Search", theme=gr.themes.Soft()) as
|
|
| 130 |
inputs=[user_input, chatbot],
|
| 131 |
outputs=[chatbot, status_output]
|
| 132 |
).then(
|
| 133 |
-
fn=lambda:
|
| 134 |
inputs=[],
|
| 135 |
outputs=[user_input]
|
| 136 |
)
|
|
@@ -147,10 +152,10 @@ with gr.Blocks(title="Agentic RAG Knowledge Search", theme=gr.themes.Soft()) as
|
|
| 147 |
inputs=[user_input, chatbot],
|
| 148 |
outputs=[chatbot, status_output]
|
| 149 |
).then(
|
| 150 |
-
fn=lambda:
|
| 151 |
inputs=[],
|
| 152 |
outputs=[user_input]
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 18 |
logger.error(f"Failed to initialize agent: {e}")
|
| 19 |
agent_executor = None
|
| 20 |
|
| 21 |
+
def determine_source(response_text) -> str:
|
| 22 |
"""
|
| 23 |
Attempt to determine if the response came from RAG or Web Search.
|
| 24 |
Looks for keywords or source indicators in the response.
|
| 25 |
"""
|
| 26 |
+
# Convert list to string if needed
|
| 27 |
+
if isinstance(response_text, list):
|
| 28 |
+
response_text = " ".join(str(item) for item in response_text)
|
| 29 |
+
|
| 30 |
+
response_lower = str(response_text).lower()
|
| 31 |
|
| 32 |
# Check for web search indicators
|
| 33 |
if any(keyword in response_lower for keyword in ["search", "web", "duckduckgo", "internet", "online"]):
|
|
|
|
| 56 |
"""
|
| 57 |
if not agent_executor:
|
| 58 |
error_msg = "Agent not initialized. Please check GOOGLE_API_KEY."
|
| 59 |
+
chat_history.append([message, f"ERROR: {error_msg}"])
|
|
|
|
| 60 |
return chat_history, error_msg
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
logger.info(f"Processing query: {message}")
|
| 64 |
|
|
|
|
| 72 |
last_message = result["messages"][-1]
|
| 73 |
response_text = last_message.content
|
| 74 |
|
| 75 |
+
# Convert to string if it's a list
|
| 76 |
+
if isinstance(response_text, list):
|
| 77 |
+
response_text = "\n".join(str(item) for item in response_text)
|
| 78 |
+
else:
|
| 79 |
+
response_text = str(response_text)
|
| 80 |
+
|
| 81 |
# Determine source
|
| 82 |
source = determine_source(response_text)
|
| 83 |
|
| 84 |
# Format response with source indicator
|
| 85 |
full_response = f"{response_text}\n\n--- {source} ---"
|
| 86 |
|
| 87 |
+
# Append to chat history as list [user, assistant]
|
| 88 |
+
chat_history.append([message, full_response])
|
| 89 |
|
| 90 |
logger.info(f"Response generated successfully")
|
| 91 |
return chat_history, f"Query processed. {source}"
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
error_msg = f"Error processing query: {str(e)}"
|
| 95 |
logger.error(error_msg)
|
| 96 |
+
chat_history.append([message, f"ERROR: {error_msg}"])
|
| 97 |
return chat_history, error_msg
|
| 98 |
|
| 99 |
def clear_chat() -> tuple[list, str]:
|
| 100 |
"""Clear chat history."""
|
| 101 |
+
return [], ""
|
| 102 |
|
| 103 |
# Build Gradio Interface
|
| 104 |
+
with gr.Blocks(title="Agentic RAG Knowledge Search") as demo:
|
| 105 |
gr.Markdown("# π Agentic RAG Knowledge Search")
|
| 106 |
gr.Markdown("Ask questions answered from internal documents (RAG) or the live web β the agent decides.")
|
| 107 |
|
| 108 |
with gr.Group():
|
| 109 |
chatbot = gr.Chatbot(
|
|
|
|
| 110 |
label="Conversation History",
|
| 111 |
show_label=True,
|
| 112 |
height=400
|
|
|
|
| 135 |
inputs=[user_input, chatbot],
|
| 136 |
outputs=[chatbot, status_output]
|
| 137 |
).then(
|
| 138 |
+
fn=lambda: "",
|
| 139 |
inputs=[],
|
| 140 |
outputs=[user_input]
|
| 141 |
)
|
|
|
|
| 152 |
inputs=[user_input, chatbot],
|
| 153 |
outputs=[chatbot, status_output]
|
| 154 |
).then(
|
| 155 |
+
fn=lambda: "",
|
| 156 |
inputs=[],
|
| 157 |
outputs=[user_input]
|
| 158 |
)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, theme=gr.themes.Soft())
|