Spaces:
Sleeping
Sleeping
File size: 13,938 Bytes
a3e120b bf3da7a a3e120b e5bb694 bd93d23 e5bb694 6a1d4f7 e5bb694 a3e120b 6a1d4f7 bf3da7a 6a1d4f7 e5bb694 6a1d4f7 e5bb694 6a1d4f7 e5bb694 d5ef142 e5bb694 a3e120b e5bb694 bf3da7a 6a1d4f7 e5bb694 6a1d4f7 1376719 a3e120b e5bb694 bf3da7a d5ef142 bf3da7a d5ef142 e5bb694 bf3da7a e5bb694 d5ef142 bf3da7a d5ef142 bf3da7a 1376719 bf3da7a 1376719 bf3da7a 1376719 bf3da7a bd93d23 bf3da7a bd93d23 bf3da7a 1376719 bf3da7a bd93d23 bf3da7a bd93d23 bf3da7a bd93d23 bf3da7a 1376719 bf3da7a bd93d23 bf3da7a a3e120b bf3da7a a3e120b bf3da7a a3e120b bf3da7a 6a1d4f7 bf3da7a 6a1d4f7 bf3da7a 1376719 bf3da7a 0e1e580 bf3da7a 1376719 6a1d4f7 bd93d23 e5bb694 d5ef142 bf3da7a d4a60c1 bf3da7a 1376719 bf3da7a d5ef142 bf3da7a d5ef142 bd93d23 bf3da7a 1376719 bf3da7a 1376719 bf3da7a bd93d23 1376719 bf3da7a 1376719 d5ef142 bf3da7a d5ef142 bf3da7a 1376719 bf3da7a d5ef142 bf3da7a e5bb694 bf3da7a e5bb694 bf3da7a e5bb694 bf3da7a e5bb694 bf3da7a e5bb694 bf3da7a e5bb694 bf3da7a 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
"""
agent.py
This file defines the core logic for a sophisticated AI agent using LangGraph.
This version uses Groq's vision-capable models and includes proper reasoning steps.
"""
# ----------------------------------------------------------
# Section 0: Imports and Configuration
# ----------------------------------------------------------
import json
import os
import pickle
import re
import subprocess
import textwrap
import functools
from pathlib import Path
from typing import Dict, Any
import requests
from cachetools import TTLCache
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_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 ---
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: Tool Functions
# ----------------------------------------------------------
@tool
def python_repl(code: str) -> str:
"""Executes a string of Python code and returns the stdout/stderr."""
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)."
def web_search_func(query: str, cache_func) -> str:
"""Performs a web search using Tavily and returns a compilation of results."""
if not query or not query.strip():
return "Error: Empty search query"
key = f"web:{query}"
try:
results = cache_func(key, lambda: TavilySearchResults(max_results=5).invoke(query))
if not results:
return "No search results found"
formatted_results = []
for res in results:
if isinstance(res, dict) and 'url' in res and 'content' in res:
formatted_results.append(f"Source: {res['url']}\nContent: {res['content']}")
return "\n\n---\n\n".join(formatted_results) if formatted_results else "No valid results found"
except Exception as e:
return f"Search error: {e}"
def wiki_search_func(query: str, cache_func) -> str:
"""Searches Wikipedia and returns the top 2 results."""
if not query or not query.strip():
return "Error: Empty search query"
key = f"wiki:{query}"
try:
docs = cache_func(key, lambda: WikipediaLoader(
query=query,
load_max_docs=2,
doc_content_chars_max=2000
).load())
if not docs:
return "No Wikipedia articles found"
return "\n\n---\n\n".join([
f"Source: {d.metadata.get('source', 'Unknown')}\n\n{d.page_content}"
for d in docs
])
except Exception as e:
return f"Wikipedia search error: {e}"
def arxiv_search_func(query: str, cache_func) -> str:
"""Searches Arxiv for scientific papers and returns the top 2 results."""
if not query or not query.strip():
return "Error: Empty search query"
key = f"arxiv:{query}"
try:
docs = cache_func(key, lambda: ArxivLoader(query=query, load_max_docs=2).load())
if not docs:
return "No Arxiv papers found"
return "\n\n---\n\n".join([
f"Source: {d.metadata.get('source', 'Unknown')}\n"
f"Published: {d.metadata.get('Published', 'Unknown')}\n"
f"Title: {d.metadata.get('Title', 'Unknown')}\n\n"
f"Summary:\n{d.page_content}"
for d in docs
])
except Exception as e:
return f"Arxiv search error: {e}"
@tool
def analyze_task_and_reason(task_description: str) -> str:
"""
Analyzes the task and provides reasoning about what approach to take.
This tool helps determine what other tools might be needed.
"""
analysis = {
"task_type": "unknown",
"has_image": False,
"needs_search": False,
"needs_computation": False,
"approach": "Direct answer"
}
task_lower = task_description.lower()
# Check for image-related content
if any(keyword in task_lower for keyword in [
'image', 'picture', 'photo', 'visual', 'see in', 'shown in',
'attachment analysis', 'url:', 'http', '.jpg', '.png', '.gif'
]):
analysis["has_image"] = True
analysis["task_type"] = "image_analysis"
analysis["approach"] = "Process image with vision model, then analyze content"
# Check for search needs
if any(keyword in task_lower for keyword in [
'current', 'recent', 'latest', 'news', 'today', 'what is',
'who is', 'when did', 'research', 'find information'
]):
analysis["needs_search"] = True
if analysis["task_type"] == "unknown":
analysis["task_type"] = "information_search"
analysis["approach"] = "Search for current information"
# Check for computation needs
if any(keyword in task_lower for keyword in [
'calculate', 'compute', 'math', 'formula', 'equation',
'algorithm', 'code', 'program', 'python'
]):
analysis["needs_computation"] = True
if analysis["task_type"] == "unknown":
analysis["task_type"] = "computation"
analysis["approach"] = "Use Python for calculations"
reasoning = f"""TASK ANALYSIS COMPLETE:
Task Type: {analysis['task_type']}
Has Image: {analysis['has_image']}
Needs Search: {analysis['needs_search']}
Needs Computation: {analysis['needs_computation']}
RECOMMENDED APPROACH: {analysis['approach']}
REASONING:
- If this involves an image, I should process it directly with my vision capabilities
- If this needs current information, I should use web search or Wikipedia
- If this needs calculations, I should use the Python tool
- I should always provide a comprehensive final answer
NEXT STEPS: Proceed with the identified approach and use appropriate tools."""
return reasoning
# ----------------------------------------------------------
# Section 3: SYSTEM PROMPT
# ----------------------------------------------------------
SYSTEM_PROMPT = """You are an expert multimodal AI assistant with vision capabilities and access to various tools.
**CORE CAPABILITIES:**
1. **Vision Processing**: You can directly process and analyze images from URLs
2. **Web Search**: Access current information via web search and Wikipedia
3. **Computation**: Execute Python code for calculations and data processing
4. **Research**: Search academic papers and retrieve similar examples
**CRITICAL WORKFLOW:**
1. **ANALYZE FIRST**: Always start by using the 'analyze_task_and_reason' tool to understand what you're being asked to do
2. **PROCESS IMAGES DIRECTLY**: When you encounter image URLs, process them directly with your vision model - DO NOT use separate image tools
3. **USE TOOLS STRATEGICALLY**: Based on your analysis, use appropriate tools (web search, Python, etc.)
4. **VALIDATE PARAMETERS**: Always check that you're passing correct parameters to tools
5. **SYNTHESIZE**: Combine all information into a comprehensive answer
**IMAGE HANDLING:**
- You have native vision capabilities - process image URLs directly
- Look for image URLs in the task description
- When you see an image URL, examine it carefully and describe what you see
- Relate your visual observations to the question being asked
**TOOL USAGE RULES:**
- Always use 'analyze_task_and_reason' first to plan your approach
- Use web_search for current events, factual information, or research
- Use python_repl for calculations, data processing, or code execution
- Use wiki_search for encyclopedic information
- Use arxiv_search for academic/scientific papers
- Use retrieve_examples for similar solved problems
**OUTPUT FORMAT:**
Always end your response with your answer clearly stated on the last line.
**PARAMETER VALIDATION:**
- Check that search queries are meaningful and specific
- Ensure Python code is safe and well-formed
- Verify image URLs are accessible before processing
"""
# ----------------------------------------------------------
# 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: Initialize LLM with vision capabilities
if provider == "groq":
# Use Groq's vision-capable model
try:
llm = ChatGroq(
model_name="meta-llama/llama-4-maverick-17b-128e-instruct" # Vision-capable model
)
print("Initialized Groq LLM with vision capabilities")
except Exception as e:
print(f"Error initializing Groq: {e}")
raise
else:
raise ValueError(f"Provider '{provider}' not supported in this version")
# Step 2: Build Retriever (if metadata exists)
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
if FAISS_CACHE.exists():
with open(FAISS_CACHE, "rb") as f:
vector_store = pickle.load(f)
print("Loaded existing FAISS index")
else:
if JSONL_PATH.exists():
docs = []
with open(JSONL_PATH, "rt", encoding="utf-8") as f:
for line in f:
rec = json.loads(line)
docs.append(Document(
page_content=f"Question: {rec['Question']}\n\nFinal answer: {rec['Final answer']}",
metadata={"source": rec["task_id"]}
))
vector_store = FAISS.from_documents(docs, embeddings)
with open(FAISS_CACHE, "wb") as f:
pickle.dump(vector_store, f)
print(f"Created new FAISS index with {len(docs)} documents")
else:
# Create minimal vector store
docs = [Document(page_content="Sample document", metadata={"source": "sample"})]
vector_store = FAISS.from_documents(docs, embeddings)
print("Created minimal FAISS index")
retriever = vector_store.as_retriever(search_kwargs={"k": RETRIEVER_K})
# Step 3: Create tools list
tools_list = [
analyze_task_and_reason,
Tool(
name="web_search",
func=functools.partial(web_search_func, cache_func=cached_get),
description="Search the web for current information. Use specific, focused queries."
),
Tool(
name="wiki_search",
func=functools.partial(wiki_search_func, cache_func=cached_get),
description="Search Wikipedia for encyclopedic information."
),
Tool(
name="arxiv_search",
func=functools.partial(arxiv_search_func, cache_func=cached_get),
description="Search Arxiv for academic papers and research."
),
python_repl,
create_retriever_tool(
retriever=retriever,
name="retrieve_examples",
description="Retrieve similar solved examples from the knowledge base."
),
]
llm_with_tools = llm.bind_tools(tools_list)
# Step 4: Define Graph Nodes
def assistant_node(state: MessagesState):
"""Main assistant node that processes user input and tool responses."""
messages = [SystemMessage(content=SYSTEM_PROMPT)] + state["messages"]
try:
result = llm_with_tools.invoke(messages)
return {"messages": [result]}
except Exception as e:
error_msg = f"LLM Error: {e}"
print(error_msg)
return {"messages": [AIMessage(content=f"I encountered an error: {error_msg}")]}
def tools_node_wrapper(state: MessagesState):
"""Wrapper for tool execution with error handling."""
try:
tool_node = ToolNode(tools_list)
return tool_node.invoke(state)
except Exception as e:
error_msg = f"Tool execution error: {e}"
print(error_msg)
return {"messages": [AIMessage(content=error_msg)]}
# Step 5: Build Graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant_node)
builder.add_node("tools", tools_node_wrapper)
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
tools_condition,
{"tools": "tools", "__end__": "__end__"}
)
builder.add_edge("tools", "assistant")
agent_executor = builder.compile()
print("Agent Executor created successfully with vision capabilities")
return agent_executor |