Spaces:
Sleeping
Sleeping
File size: 15,029 Bytes
a3e120b 6cdf45e a3e120b e5bb694 bd93d23 e5bb694 6a1d4f7 e5bb694 a3e120b 6a1d4f7 bd93d23 6a1d4f7 e5bb694 6a1d4f7 e5bb694 6a1d4f7 e5bb694 d5ef142 e5bb694 a3e120b 6a1d4f7 e5bb694 a3e36f8 6a1d4f7 e5bb694 6a1d4f7 6cdf45e a3e120b e5bb694 d5ef142 e5bb694 6cdf45e e5bb694 d5ef142 6cdf45e d5ef142 bd93d23 6cdf45e bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 6cdf45e bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 a3e36f8 bd93d23 a3e120b a3e36f8 d5ef142 6cdf45e d5ef142 a3e120b d5ef142 6cdf45e d5ef142 a3e120b d5ef142 6cdf45e d5ef142 6a1d4f7 6cdf45e 6a1d4f7 a3e120b 6cdf45e 6a1d4f7 bd93d23 e5bb694 d5ef142 6cdf45e bd93d23 6cdf45e d5ef142 6cdf45e a3e36f8 bd93d23 a3e36f8 bd93d23 a3e36f8 d5ef142 6cdf45e d5ef142 6cdf45e d5ef142 6cdf45e a3e120b a3e36f8 e5bb694 bd93d23 a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e a3e36f8 6cdf45e e5bb694 bd93d23 6cdf45e e5bb694 6cdf45e e5bb694 6cdf45e e5bb694 6cdf45e bd93d23 |
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
"""
agent.py
This file defines the core logic for a sophisticated AI agent using LangGraph.
## MODIFICATION: This version introduces a 'multimodal_router' node.
This node intelligently inspects user input to identify, classify (using HEAD requests),
and pre-process URLs for images, audio, and video before the main LLM reasoning step.
"""
# ----------------------------------------------------------
# Section 0: Imports and Configuration
# ----------------------------------------------------------
import json
import os
import pickle
import re
import subprocess
import textwrap
import base64
import functools
from io import BytesIO
from pathlib import Path
import tempfile
import yt_dlp
from pydub import AudioSegment
import speech_recognition as sr
import requests
from cachetools import TTLCache
from PIL import Image
from langchain.schema import Document
from langchain.tools.retriever import create_retriever_tool
from langchain_community.vectorstores import FAISS
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.tools import Tool, tool
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_groq import ChatGroq
from langchain_huggingface import HuggingFaceEmbeddings
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode, tools_condition
from dotenv import load_dotenv
load_dotenv()
# --- Configuration and Caching (remains the same) ---
JSONL_PATH, FAISS_CACHE, EMBED_MODEL = Path("metadata.jsonl"), Path("faiss_index.pkl"), "sentence-transformers/all-mpnet-base-v2"
RETRIEVER_K, CACHE_TTL = 5, 600
API_CACHE = TTLCache(maxsize=256, ttl=CACHE_TTL)
def cached_get(key: str, fetch_fn):
if key in API_CACHE: return API_CACHE[key]
val = fetch_fn()
API_CACHE[key] = val
return val
# ----------------------------------------------------------
# Section 2: Standalone Tool Functions (remains the same)
# ----------------------------------------------------------
@tool
def python_repl(code: str) -> str:
"""Executes a string of Python code and returns the stdout/stderr."""
# ... (implementation unchanged)
code = textwrap.dedent(code).strip()
try:
result = subprocess.run(["python", "-c", code], capture_output=True, text=True, timeout=10, check=False)
if result.returncode == 0: return f"Execution successful.\nSTDOUT:\n```\n{result.stdout}\n```"
else: return f"Execution failed.\nSTDOUT:\n```\n{result.stdout}\n```\nSTDERR:\n```\n{result.stderr}\n```"
except subprocess.TimeoutExpired: return "Execution timed out (>10s)."
@tool
def process_youtube_video(url: str) -> str:
"""Downloads and processes a YouTube video, extracting audio and converting to text."""
# ... (implementation unchanged)
try:
print(f"Processing YouTube video: {url}")
with tempfile.TemporaryDirectory() as temp_dir:
ydl_opts = {
'format': 'bestaudio/best', 'outtmpl': f'{temp_dir}/%(title)s.%(ext)s',
'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav'}],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
title = info.get('title', 'Unknown')
audio_files = list(Path(temp_dir).glob("*.wav"))
if not audio_files: return "Error: Could not download audio from YouTube video"
r, transcript_parts = sr.Recognizer(), []
audio = AudioSegment.from_wav(str(audio_files[0])).set_channels(1).set_frame_rate(16000)
chunks = [audio[i:i + 30000] for i in range(0, len(audio), 30000)]
for i, chunk in enumerate(chunks[:10]):
chunk_file = Path(temp_dir) / f"chunk_{i}.wav"
chunk.export(chunk_file, format="wav")
try:
with sr.AudioFile(str(chunk_file)) as source:
text = r.recognize_google(r.record(source))
transcript_parts.append(text)
except (sr.UnknownValueError, sr.RequestError) as e:
transcript_parts.append(f"[Speech recognition error or unintelligible audio: {e}]")
return f"YouTube Video: {title}\n\nTranscript (first 5 minutes):\n{' '.join(transcript_parts)}"
except Exception as e:
print(f"Error processing YouTube video: {e}")
return f"Error processing YouTube video: {e}"
@tool
def process_audio_file(file_url: str) -> str:
"""Downloads and processes an audio file (MP3, WAV, etc.) and converts to text."""
# ... (implementation unchanged)
try:
print(f"Processing audio file: {file_url}")
with tempfile.TemporaryDirectory() as temp_dir:
response = requests.get(file_url, timeout=30)
response.raise_for_status()
ext = os.path.splitext(file_url)[1][1:] or 'mp3'
audio_file = Path(temp_dir) / f"audio.{ext}"
with open(audio_file, 'wb') as f: f.write(response.content)
wav_file = Path(temp_dir) / "audio.wav"
AudioSegment.from_file(str(audio_file)).export(wav_file, format="wav")
r, transcript_parts = sr.Recognizer(), []
audio = AudioSegment.from_wav(str(wav_file)).set_channels(1).set_frame_rate(16000)
chunks = [audio[i:i + 30000] for i in range(0, len(audio), 30000)]
for i, chunk in enumerate(chunks[:20]):
chunk_file = Path(temp_dir) / f"chunk_{i}.wav"
chunk.export(chunk_file, format="wav")
try:
with sr.AudioFile(str(chunk_file)) as source:
text = r.recognize_google(r.record(source))
transcript_parts.append(text)
except (sr.UnknownValueError, sr.RequestError) as e:
transcript_parts.append(f"[Speech recognition error or unintelligible audio: {e}]")
return f"Audio file transcript:\n{' '.join(transcript_parts)}"
except Exception as e:
print(f"Error processing audio file: {e}")
return f"Error processing audio file: {e}"
def web_search_func(query: str, cache_func) -> str:
"""Performs a web search using Tavily and returns a compilation of results."""
# ... (implementation unchanged)
key = f"web:{query}"
results = cache_func(key, lambda: TavilySearchResults(max_results=5).invoke(query))
return "\n\n---\n\n".join([f"Source: {res['url']}\nContent: {res['content']}" for res in results])
def wiki_search_func(query: str, cache_func) -> str:
"""Searches Wikipedia and returns the top 2 results."""
# ... (implementation unchanged)
key = f"wiki:{query}"
docs = cache_func(key, lambda: WikipediaLoader(query=query, load_max_docs=2, doc_content_chars_max=2000).load())
return "\n\n---\n\n".join([f"Source: {d.metadata['source']}\n\n{d.page_content}" for d in docs])
def arxiv_search_func(query: str, cache_func) -> str:
"""Searches Arxiv for scientific papers and returns the top 2 results."""
# ... (implementation unchanged)
key = f"arxiv:{query}"
docs = cache_func(key, lambda: ArxivLoader(query=query, load_max_docs=2).load())
return "\n\n---\n\n".join([f"Source: {d.metadata['source']}\nPublished: {d.metadata['Published']}\nTitle: {d.metadata['Title']}\n\nSummary:\n{d.page_content}" for d in docs])
# ----------------------------------------------------------
# Section 3: DYNAMIC SYSTEM PROMPT (remains the same)
# ----------------------------------------------------------
SYSTEM_PROMPT_TEMPLATE = (
"""You are an expert-level multimodal research assistant...""" # Unchanged
)
# ----------------------------------------------------------
# Section 4: Factory Function for Agent Executor
# ----------------------------------------------------------
def create_agent_executor(provider: str = "groq"):
"""
Factory function to create and compile the LangGraph agent executor.
"""
print(f"Initializing agent with provider: {provider}")
# Step 1: Build LLM (remains the same)
if provider == "groq":
llm = ChatGroq(model_name="llama-3.1-70b-vision-preview", temperature=0)
else:
raise ValueError(f"Provider '{provider}' not currently configured for this router.")
# Step 2: Build Retriever (remains the same, but will be called inside the router)
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
if FAISS_CACHE.exists():
with open(FAISS_CACHE, "rb") as f: vector_store = pickle.load(f)
else:
# ... logic to build vector_store from JSONL or create empty ...
docs = []
if JSONL_PATH.exists():
docs = [Document(page_content=f"Question: {rec['Question']}\n\nFinal answer: {rec['Final answer']}", metadata={"source": rec["task_id"]}) for rec in (json.loads(line) for line in open(JSONL_PATH, "rt", encoding="utf-8"))]
if not docs:
docs = [Document(page_content="Sample document", metadata={"source": "sample"})]
vector_store = FAISS.from_documents(docs, embeddings)
with open(FAISS_CACHE, "wb") as f: pickle.dump(vector_store, f)
retriever = vector_store.as_retriever(search_kwargs={"k": RETRIEVER_K})
# Step 3: Create the final list of tools (remains the same)
tools_list = [
python_repl, process_youtube_video, process_audio_file,
Tool(name="web_search", func=functools.partial(web_search_func, cache_func=cached_get), description="Performs a web search using Tavily."),
Tool(name="wiki_search", func=functools.partial(wiki_search_func, cache_func=cached_get), description="Searches Wikipedia."),
Tool(name="arxiv_search", func=functools.partial(arxiv_search_func, cache_func=cached_get), description="Searches Arxiv for scientific papers."),
create_retriever_tool(retriever=retriever, name="retrieve_examples", description="Retrieve solved questions similar to the user's query."),
]
# Step 4: Format prompt and bind tools (remains the same)
tool_definitions = "\n".join([f"- `{tool.name}`: {tool.description}" for tool in tools_list])
final_system_prompt = SYSTEM_PROMPT_TEMPLATE.format(tools=tool_definitions)
llm_with_tools = llm.bind_tools(tools_list)
# Step 5: Define Graph Nodes
## MODIFICATION: A new, powerful router node that replaces the previous pre-processing.
def multimodal_router(state: MessagesState):
"""
Inspects the user's message, classifies URLs, and prepares the state for the LLM.
This node acts as a central dispatcher.
"""
print("--- Entering Multimodal Router ---")
messages = state["messages"]
last_message = messages[-1]
# 1. Perform knowledge base retrieval first
# We consolidate this logic here from the old retriever_node
user_query_text = ""
if isinstance(last_message.content, str):
user_query_text = last_message.content
elif isinstance(last_message.content, list): # For multimodal messages
user_query_text = " ".join(item['text'] for item in last_message.content if item['type'] == 'text')
docs = retriever.invoke(user_query_text)
system_messages = [SystemMessage(content=final_system_prompt)]
if docs:
example_text = "\n\n---\n\n".join(d.page_content for d in docs)
system_messages.append(AIMessage(content=f"I have found {len(docs)} similar solved examples:\n\n{example_text}", name="ExampleRetriever"))
# 2. Extract and classify URLs
urls = re.findall(r'(https?://[^\s]+)', user_query_text)
image_processed = False
for url in urls:
try:
print(f"Routing URL: {url}")
# Simple classification first
if "youtube.com" in url or "youtu.be" in url:
system_messages.append(SystemMessage(content=f"[System Note: A YouTube URL has been detected. Use the 'process_youtube_video' tool if the user asks about it.]"))
continue
# Use a HEAD request for robust classification
headers = requests.head(url, timeout=5, allow_redirects=True).headers
content_type = headers.get('Content-Type', '')
if 'image/' in content_type and not image_processed:
print(f" -> Classified as Image. Processing for vision model.")
response = requests.get(url, timeout=10)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
buffered = BytesIO()
img.convert("RGB").save(buffered, format="JPEG")
b64_string = base64.b64encode(buffered.getvalue()).decode()
# Embed the image into the last message
new_content = [
{"type": "text", "text": user_query_text},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_string}"}}
]
messages[-1] = HumanMessage(content=new_content)
image_processed = True # Process only the first image for now
elif 'audio/' in content_type:
print(f" -> Classified as Audio.")
system_messages.append(SystemMessage(content=f"[System Note: An audio URL has been detected. Use the 'process_audio_file' tool if the user asks about it.]"))
else:
print(f" -> Classified as Web Page/Other.")
except Exception as e:
print(f" -> Could not process URL {url}: {e}")
# Rebuild the final state
final_messages = system_messages + messages
return {"messages": final_messages}
def assistant_node(state: MessagesState):
result = llm_with_tools.invoke(state["messages"])
return {"messages": [result]}
# Step 6: Build Graph
## MODIFICATION: The graph is now simpler and more robust.
builder = StateGraph(MessagesState)
builder.add_node("multimodal_router", multimodal_router) # The new, powerful starting node
builder.add_node("assistant", assistant_node)
builder.add_node("tools", ToolNode(tools_list))
builder.add_edge(START, "multimodal_router")
builder.add_edge("multimodal_router", "assistant")
builder.add_conditional_edges("assistant", tools_condition, {"tools": "tools", "__end__": "__end__"})
builder.add_edge("tools", "assistant")
agent_executor = builder.compile()
print("Agent Executor with Multimodal Router created successfully.")
return agent_executor |