File size: 10,355 Bytes
fe36046 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
"""Retrieval Agent - Handles information gathering and search tasks"""
import os
import requests
from typing import Dict, Any, List
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage, ToolMessage
from langchain_core.tools import tool
from langchain_groq import ChatGroq
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain.tools.retriever import create_retriever_tool
from src.memory import memory_manager
from src.tracing import get_langfuse_callback_handler
# Tool definitions (same as original)
@tool
def wiki_search(input: str) -> str:
"""Search Wikipedia for a query and return maximum 2 results.
Args:
input: The search query."""
try:
search_docs = WikipediaLoader(query=input, load_max_docs=2).load()
if not search_docs:
return "No Wikipedia results found for the query."
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata.get("source", "Unknown")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
for doc in search_docs
])
return formatted_search_docs
except Exception as e:
print(f"Error in wiki_search: {e}")
return f"Error searching Wikipedia: {e}"
@tool
def web_search(input: str) -> str:
"""Search Tavily for a query and return maximum 3 results.
Args:
input: The search query."""
try:
search_docs = TavilySearchResults(max_results=3).invoke(input)
if not search_docs:
return "No web search results found for the query."
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.get("url", "Unknown")}" />\n{doc.get("content", "No content")}\n</Document>'
for doc in search_docs
])
return formatted_search_docs
except Exception as e:
print(f"Error in web_search: {e}")
return f"Error searching web: {e}"
@tool
def arvix_search(input: str) -> str:
"""Search Arxiv for a query and return maximum 3 results.
Args:
input: The search query."""
try:
search_docs = ArxivLoader(query=input, load_max_docs=3).load()
if not search_docs:
return "No Arxiv results found for the query."
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata.get("source", "Unknown")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
for doc in search_docs
])
return formatted_search_docs
except Exception as e:
print(f"Error in arvix_search: {e}")
return f"Error searching Arxiv: {e}"
def load_retrieval_prompt() -> str:
"""Load the retrieval prompt from file"""
try:
with open("./prompts/retrieval_prompt.txt", "r", encoding="utf-8") as f:
return f.read().strip()
except FileNotFoundError:
return """You are a specialized retrieval agent. Use available tools to search for information and provide comprehensive answers."""
def get_retrieval_tools() -> List:
"""Get list of tools available to the retrieval agent"""
tools = [wiki_search, web_search, arvix_search]
# Add vector store retrieval tool if available
if memory_manager.vector_store:
try:
retrieval_tool = create_retriever_tool(
retriever=memory_manager.vector_store.as_retriever(),
name="question_search",
description="A tool to retrieve similar questions from a vector store.",
)
tools.append(retrieval_tool)
except Exception as e:
print(f"Could not create retrieval tool: {e}")
return tools
def execute_tool_calls(tool_calls: list, tools: list) -> list:
"""Execute tool calls and return results"""
tool_messages = []
# Create a mapping of tool names to tool functions
tool_map = {tool.name: tool for tool in tools}
for tool_call in tool_calls:
tool_name = tool_call['name']
tool_args = tool_call['args']
tool_call_id = tool_call['id']
if tool_name in tool_map:
try:
print(f"Retrieval Agent: Executing {tool_name} with args: {tool_args}")
result = tool_map[tool_name].invoke(tool_args)
tool_messages.append(
ToolMessage(
content=str(result),
tool_call_id=tool_call_id
)
)
except Exception as e:
print(f"Error executing {tool_name}: {e}")
tool_messages.append(
ToolMessage(
content=f"Error executing {tool_name}: {e}",
tool_call_id=tool_call_id
)
)
else:
tool_messages.append(
ToolMessage(
content=f"Unknown tool: {tool_name}",
tool_call_id=tool_call_id
)
)
return tool_messages
def fetch_attachment_if_needed(query: str) -> str:
"""Fetch attachment content if the query matches a known task"""
try:
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=30)
resp.raise_for_status()
questions = resp.json()
for q in questions:
if str(q.get("question")).strip() == str(query).strip():
task_id = str(q.get("task_id"))
print(f"Retrieval Agent: Downloading attachment for task {task_id}")
file_resp = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=60)
if file_resp.status_code == 200 and file_resp.content:
try:
file_text = file_resp.content.decode("utf-8", errors="replace")
except Exception:
file_text = "(binary or non-UTF8 file omitted)"
MAX_CHARS = 8000
if len(file_text) > MAX_CHARS:
file_text = file_text[:MAX_CHARS] + "\n… (truncated)"
return f"Attached file content for task {task_id}:\n```python\n{file_text}\n```"
else:
print(f"No attachment for task {task_id}")
return ""
return ""
except Exception as e:
print(f"Error fetching attachment: {e}")
return ""
def retrieval_agent(state: Dict[str, Any]) -> Dict[str, Any]:
"""
Retrieval agent that handles information gathering tasks
"""
print("Retrieval Agent: Processing information retrieval request")
try:
# Get retrieval prompt
retrieval_prompt = load_retrieval_prompt()
# Initialize LLM with tools
llm = ChatGroq(model="qwen-qwq-32b", temperature=0.3)
tools = get_retrieval_tools()
llm_with_tools = llm.bind_tools(tools)
# Get callback handler for tracing
callback_handler = get_langfuse_callback_handler()
callbacks = [callback_handler] if callback_handler else []
# Build messages
messages = state.get("messages", [])
# Add retrieval system prompt
retrieval_messages = [SystemMessage(content=retrieval_prompt)]
# Get user query for context and attachment fetching
user_query = None
for msg in reversed(messages):
if msg.type == "human":
user_query = msg.content
break
# Check for similar questions in memory
if user_query:
similar_qa = memory_manager.get_similar_qa(user_query)
if similar_qa:
context_msg = HumanMessage(
content=f"Here is a similar question and answer for reference:\n\n{similar_qa}"
)
retrieval_messages.append(context_msg)
# Fetch attachment if needed
attachment_content = fetch_attachment_if_needed(user_query)
if attachment_content:
attachment_msg = HumanMessage(content=attachment_content)
retrieval_messages.append(attachment_msg)
# Add original messages (excluding system messages to avoid duplicates)
for msg in messages:
if msg.type != "system":
retrieval_messages.append(msg)
# Get initial response from LLM and iterate tool calls if necessary
response = llm_with_tools.invoke(retrieval_messages, config={"callbacks": callbacks})
max_tool_iterations = 3 # safeguard to prevent infinite loops
iteration = 0
while response.tool_calls and iteration < max_tool_iterations:
iteration += 1
print(f"Retrieval Agent: LLM requested {len(response.tool_calls)} tool calls (iteration {iteration})")
# Execute the tool calls
tool_messages = execute_tool_calls(response.tool_calls, tools)
# Append the LLM response and tool results to the conversation
retrieval_messages.extend([response] + tool_messages)
# Ask the model again with the new information
response = llm_with_tools.invoke(retrieval_messages, config={"callbacks": callbacks})
# After iterating (or if no tool calls), we have our final response
retrieval_messages.append(response)
return {
**state,
"messages": retrieval_messages,
"agent_response": response,
"current_step": "verification"
}
except Exception as e:
print(f"Retrieval Agent Error: {e}")
error_response = AIMessage(content=f"I encountered an error while processing your request: {e}")
return {
**state,
"messages": state.get("messages", []) + [error_response],
"agent_response": error_response,
"current_step": "verification"
} |