Spaces:
Sleeping
Sleeping
Update agent.py
Browse files1 unmodified line234567891034 unmodified lines454647484950514 unmodified lines5657585960616263646566671 unmodified line6970717273747576777879808126 unmodified lines10810911011111211311424 unmodified lines1391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831845 unmodified lines1901911921931941951961971981992002012022032042052062072082092102113 unmodified lines2152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153161 unmodified lineagent.pyThis file defines the core logic for a sophisticated AI agent using LangGraph.## MODIFICATION: This version has been refactored for an integrated vision model.The primary LLM now processes images directly, removing the need for a separate 'describe_image' tool.This allows for more direct and less "lossy" multimodal reasoning."""# ----------------------------------------------------------34 unmodified linesfrom dotenv import load_dotenvload_dotenv()# --- Configuration and Caching ---JSONL_PATH, FAISS_CACHE, EMBED_MODEL = Path("metadata.jsonl"), Path("faiss_index.pkl"), "sentence-transformers/all-mpnet-base-v2"RETRIEVER_K, CACHE_TTL = 5, 600API_CACHE = TTLCache(maxsize=256, ttl=CACHE_TTL)4 unmodified linesreturn val# ----------------------------------------------------------# Section 2: Standalone Tool Functions# ----------------------------------------------------------@tooldef python_repl(code: str) -> str:"""Executes a string of Python code and returns the stdout/stderr."""# (Implementation remains the same)code = textwrap.dedent(code).strip()try:result = subprocess.run(["python", "-c", code], capture_output=True, text=True, timeout=10, check=False)1 unmodified lineelse: return f"Execution failed.\nSTDOUT:\n```\n{result.stdout}\n```\nSTDERR:\n```\n{result.stderr}\n```"except subprocess.TimeoutExpired: return "Execution timed out (>10s)."## MODIFICATION: The 'describe_image_func' has been removed. Its functionality is now## handled by the 'preprocess_image_node' in the graph.@tooldef process_youtube_video(url: str) -> str:"""Downloads and processes a YouTube video, extracting audio and converting to text."""# (Implementation remains the same)try:print(f"Processing YouTube video: {url}")with tempfile.TemporaryDirectory() as temp_dir:26 unmodified lines@tooldef process_audio_file(file_url: str) -> str:"""Downloads and processes an audio file (MP3, WAV, etc.) and converts to text."""# (Implementation remains the same)try:print(f"Processing audio file: {file_url}")with tempfile.TemporaryDirectory() as temp_dir:24 unmodified linesdef web_search_func(query: str, cache_func) -> str:"""Performs a web search using Tavily and returns a compilation of results."""# (Implementation remains the same)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 remains the same)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 remains the same)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# ----------------------------------------------------------## MODIFICATION: The system prompt is updated to reflect the new workflow.## It no longer mentions 'describe_image' but instructs the model that it can## directly see and reason about images provided in the prompt.SYSTEM_PROMPT_TEMPLATE = ("""You are an expert-level multimodal research assistant. Your goal is to answer the user's question accurately using all available tools and your own vision capabilities.**CRITICAL INSTRUCTIONS:**1. **INTEGRATED VISION:** You can directly see and understand images provided in the user's prompt. Reason about the image content directly to answer questions.2. **MULTIMODAL TOOL USE:** When you encounter URLs for other media types, use the appropriate tool:- For YouTube URLs: Use the `process_youtube_video` tool- For audio files (mp3, wav, etc.): Use the `process_audio_file` tool3. **SEARCH & RETRIEVAL:** For information not in the prompt, use the search tools (`web_search`, `wiki_search`, `arxiv_search`) or retrieve past examples. Do not make up answers.4. **AVAILABLE TOOLS:** Here is the exact list of tools you have access to for non-image tasks:{tools}5. **REASONING:** Think step-by-step. First, analyze the user's question and any attached text or images. Second, if the answer requires external data, decide which tool is appropriate. Third, call the tools with correct parameters. Finally, synthesize all information into a final answer.6. **FINAL ANSWER FORMAT:** Your final response MUST strictly follow this format:`FINAL ANSWER: [Your comprehensive answer incorporating all tool results and image analysis]`""")# ----------------------------------------------------------5 unmodified lines"""print(f"Initializing agent with provider: {provider}")# Step 1: Build LLM## MODIFICATION: We now only need one primary, vision-capable LLM. The 'vision_llm' is removed.if provider == "google":llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro-latest", temperature=0)elif provider == "groq":# The model requested was 'llama-4-scout-17b-16e-instruct', but as of mid-2024,# the publicly available vision model on Groq is Llama 3.1. We'll use that.llm = ChatGroq(model_name="meta-llama/llama-4-maverick-17b-128e-instruct", temperature=0)else:raise ValueError(f"Provider '{provider}' not supported for integrated vision yet.")# Step 2: Build Retriever (remains the same)embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)if FAISS_CACHE.exists():with open(FAISS_CACHE, "rb") as f: vector_store = pickle.load(f)else: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"))]3 unmodified lineswith 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## MODIFICATION: The 'describe_image' tool has been removed from the list.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 the tool list and create the final system prompttool_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: New node to pre-process images before they reach the assistant.def preprocess_image_node(state: MessagesState):"""Checks the last human message for an image URL. If found, it downloadsthe image, converts it to base64, and reformats the message contentfor a multimodal LLM."""last_message = state["messages"][-1]if not isinstance(last_message, HumanMessage) or not isinstance(last_message.content, str):return state# Regex to find image URLsimage_url_match = re.search(r'(https?://[^\s]+\.(?:png|jpg|jpeg|gif|webp))', last_message.content)if image_url_match:image_url = image_url_match.group(0)print(f"--- Found image URL: {image_url} ---")try:# Download and process the imageresponse = requests.get(image_url, timeout=10)response.raise_for_status()img = Image.open(BytesIO(response.content))# Convert to base64buffered = BytesIO()img.convert("RGB").save(buffered, format="JPEG")b64_string = base64.b64encode(buffered.getvalue()).decode()# Create the multimodal message contentnew_content = [{"type": "text", "text": last_message.content},{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_string}"}}]# Replace the last message with the new multimodal onestate["messages"][-1] = HumanMessage(content=new_content)print("--- Image pre-processed and embedded into the message ---")except Exception as e:print(f"Error processing image URL: {e}")# Optional: You could modify the message to inform the LLM of the failure# For now, we just pass it along without the image.return statedef retriever_node(state: MessagesState):# (Implementation remains the same)user_query = state["messages"][-1].contentdocs = retriever.invoke(user_query)messages = [SystemMessage(content=final_system_prompt)]if docs:example_text = "\n\n---\n\n".join(d.page_content for d in docs)messages.append(AIMessage(content=f"I have found {len(docs)} similar solved examples:\n\n{example_text}", name="ExampleRetriever"))messages.extend(state["messages"])return {"messages": messages}def assistant_node(state: MessagesState):result = llm_with_tools.invoke(state["messages"])return {"messages": [result]}# Step 6: Build Graph## MODIFICATION: The graph flow is updated to include the new pre-processing node.builder = StateGraph(MessagesState)builder.add_node("retriever", retriever_node)builder.add_node("preprocess_image", preprocess_image_node) # New nodebuilder.add_node("assistant", assistant_node)builder.add_node("tools", ToolNode(tools_list))builder.add_edge(START, "retriever")builder.add_edge("retriever", "preprocess_image") # New edgebuilder.add_edge("preprocess_image", "assistant") # New edgebuilder.add_conditional_edges("assistant", tools_condition, {"tools": "tools", "__end__": "__end__"})builder.add_edge("tools", "assistant")agent_executor = builder.compile()print("Agent Executor with integrated vision created successfully.")return agent_executor1 unmodified line234567891034 unmodified lines454647484950514 unmodified lines5657585960616263646566671 unmodified line697071727374757677787926 unmodified lines10610710810911011111224 unmodified lines1371381391401411421431441451461471481491501511521531541551561571581591601611621631641651665 unmodified lines1721731741751761771781791801811821831841851861871881893 unmodified lines1931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003011 unmodified lineagent.pyThis 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."""# ----------------------------------------------------------34 unmodified linesfrom dotenv import load_dotenvload_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, 600API_CACHE = TTLCache(maxsize=256, ttl=CACHE_TTL)4 unmodified linesreturn val# ----------------------------------------------------------# Section 2: Standalone Tool Functions (remains the same)# ----------------------------------------------------------@tooldef 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)1 unmodified lineelse: return f"Execution failed.\nSTDOUT:\n```\n{result.stdout}\n```\nSTDERR:\n```\n{result.stderr}\n```"except subprocess.TimeoutExpired: return "Execution timed out (>10s)."@tooldef 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:26 unmodified lines@tooldef 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:24 unmodified linesdef 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)# ----------------------------------------------------------5 unmodified lines"""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"))]3 unmodified lineswith 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_nodeuser_query_text = ""if isinstance(last_message.content, str):user_query_text = last_message.contentelif isinstance(last_message.content, list): # For multimodal messagesuser_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 URLsurls = re.findall(r'(https?://[^\s]+)', user_query_text)image_processed = Falsefor url in urls:try:print(f"Routing URL: {url}")# Simple classification firstif "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 classificationheaders = requests.head(url, timeout=5, allow_redirects=True).headerscontent_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 messagenew_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 nowelif '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 statefinal_messages = system_messages + messagesreturn {"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 nodebuilder.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
agent.py+88-103