File size: 13,322 Bytes
0f4f3d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
import time
import json
import logging
from dotenv import load_dotenv
from langgraph.graph import StateGraph, END
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_core.tools import tool
from typing import TypedDict, Annotated, Sequence
import operator
import random

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("GAIA_Agent")

# Load environment variables
load_dotenv()
google_api_key = os.getenv("GOOGLE_API_KEY") or os.environ.get("GOOGLE_API_KEY")
if not google_api_key:
    raise ValueError("Missing GOOGLE_API_KEY environment variable")

# --- Math Tools ---
@tool
def multiply(a: int, b: int) -> int:
    """Multiply two integers."""
    return a * b

@tool
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

@tool
def subtract(a: int, b: int) -> int:
    """Subtract b from a."""
    return a - b

@tool
def divide(a: int, b: int) -> float:
    """Divide a by b, error on zero."""
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

@tool
def modulus(a: int, b: int) -> int:
    """Compute a mod b."""
    return a % b

# --- Browser Tools ---
@tool
def wiki_search(query: str) -> str:
    """Search Wikipedia and return up to 3 relevant documents."""
    try:
        # Ensure query contains "discography" keyword
        if "discography" not in query.lower():
            query = f"{query} discography"
            
        docs = WikipediaLoader(query=query, load_max_docs=3).load()
        if not docs:
            return "No Wikipedia results found."
        
        results = []
        for doc in docs:
            title = doc.metadata.get('title', 'Unknown Title')
            content = doc.page_content[:2000]  # Limit content length
            results.append(f"Title: {title}\nContent: {content}")
        
        return "\n\n---\n\n".join(results)
    except Exception as e:
        return f"Wikipedia search error: {str(e)}"

@tool
def arxiv_search(query: str) -> str:
    """Search Arxiv and return up to 3 relevant papers."""
    try:
        docs = ArxivLoader(query=query, load_max_docs=3).load()
        if not docs:
            return "No arXiv papers found."
        
        results = []
        for doc in docs:
            title = doc.metadata.get('Title', 'Unknown Title')
            authors = ", ".join(doc.metadata.get('Authors', []))
            content = doc.page_content[:2000]  # Limit content length
            results.append(f"Title: {title}\nAuthors: {authors}\nContent: {content}")
        
        return "\n\n---\n\n".join(results)
    except Exception as e:
        return f"arXiv search error: {str(e)}"

@tool
def web_search(query: str) -> str:
    """Search the web using DuckDuckGo and return top results."""
    try:
        search = DuckDuckGoSearchRun()
        result = search.run(query)
        return f"Web search results for '{query}':\n{result[:2000]}"  # Limit content length
    except Exception as e:
        return f"Web search error: {str(e)}"

# --- Load system prompt ---
with open("system_prompt.txt", "r", encoding="utf-8") as f:
    system_prompt = f.read()

# --- Tool Setup ---
tools = [
    multiply,
    add,
    subtract,
    divide,
    modulus,
    wiki_search,
    arxiv_search,
    web_search,
]

# --- Graph Builder ---
def build_graph():
    # Initialize model with Gemini 2.5 Flash
    llm = ChatGoogleGenerativeAI(
        model="gemini-2.5-flash",
        temperature=0.3,
        google_api_key=google_api_key,
        max_retries=0,  # Disable internal retries
        request_timeout=30  # Keep timeout reasonable
    )
    
    # Bind tools to LLM
    llm_with_tools = llm.bind_tools(tools)
    
    # 1. Define state structure
    class AgentState(TypedDict):
        messages: Annotated[Sequence, operator.add]
        step_count: int
        start_time: float
        last_action: str
        api_errors: int  # Track consecutive API errors
    
    # 2. Create graph
    workflow = StateGraph(AgentState)
    
    # 3. Define node functions
    def agent_node(state: AgentState):
        """Main agent node with manual retry handling"""
        # Ensure state has required fields
        state.setdefault("start_time", time.time())
        state.setdefault("step_count", 0)
        state.setdefault("last_action", "start")
        state.setdefault("api_errors", 0)
        
        # Check global timeout (2 minutes)
        if time.time() - state["start_time"] > 120:
            return {
                "messages": [AIMessage(content="AGENT ERROR (GLOBAL_TIMEOUT): Execution exceeded 2-minute limit")],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "timeout",
                "api_errors": state["api_errors"]
            }
        
        # Check step limit (max 8 steps)
        if state["step_count"] >= 8:
            return {
                "messages": [AIMessage(content="AGENT ERROR (STEP_LIMIT): Exceeded maximum step count of 8")],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "step_limit",
                "api_errors": state["api_errors"]
            }
        
        # Check consecutive API errors
        if state["api_errors"] >= 3:
            return {
                "messages": [AIMessage(content="AGENT ERROR (API_LIMIT): Too many consecutive API errors")],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "api_limit",
                "api_errors": state["api_errors"]
            }
        
        try:
            # Add variable delay to avoid rate limiting
            delay = 2 + random.uniform(0, 3)  # 2-5 seconds
            time.sleep(delay)
            
            # Call API without automatic retries
            response = llm_with_tools.invoke(state["messages"])
            
            # Reset error counter on success
            return {
                "messages": [response],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "agent",
                "api_errors": 0  # Reset error counter
            }
        
        except Exception as e:
            # Detailed error logging
            error_details = f"Gemini API Error: {type(e).__name__}: {str(e)}"
            logger.error(error_details)
            
            error_type = "UNKNOWN"
            if "429" in str(e) or "ResourceExhausted" in str(e):
                error_type = "RESOURCE_EXHAUSTED"
            elif "400" in str(e):
                error_type = "INVALID_REQUEST"
            elif "503" in str(e):
                error_type = "SERVICE_UNAVAILABLE"
                
            error_msg = f"AGENT ERROR ({error_type}): {error_details[:300]}"
            
            return {
                "messages": [AIMessage(content=error_msg)],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "error",
                "api_errors": state["api_errors"] + 1  # Increment error counter
            }
    
    def tool_node(state: AgentState):
        """Tool execution node"""
        # Ensure state has required fields
        state.setdefault("start_time", time.time())
        state.setdefault("step_count", 0)
        state.setdefault("last_action", "start")
        state.setdefault("api_errors", 0)
            
        # Check global timeout (2 minutes)
        if time.time() - state["start_time"] > 120:
            return {
                "messages": [AIMessage(content="AGENT ERROR (GLOBAL_TIMEOUT): Execution exceeded 2-minute limit")],
                "step_count": state["step_count"] + 1,
                "start_time": state["start_time"],
                "last_action": "timeout",
                "api_errors": state["api_errors"]
            }
        
        last_msg = state["messages"][-1]
        tool_calls = last_msg.additional_kwargs.get("tool_calls", [])
        
        responses = []
        for call in tool_calls:
            tool_name = call["function"]["name"]
            tool_args = call["function"].get("arguments", {})
            
            tool_func = next((t for t in tools if t.name == tool_name), None)
            if not tool_func:
                responses.append(f"Tool {tool_name} not available")
                continue
            
            try:
                # Parse arguments
                if isinstance(tool_args, str):
                    try:
                        tool_args = json.loads(tool_args)
                    except json.JSONDecodeError:
                        if "query" in tool_args:
                            tool_args = {"query": tool_args}
                        else:
                            tool_args = {"query": tool_args}
                
                # Execute tool
                result = tool_func.invoke(tool_args)
                responses.append(f"{tool_name} result: {str(result)[:1000]}")
            except Exception as e:
                responses.append(f"{tool_name} error: {str(e)}")
        
        tool_response_content = "\n".join(responses)
        return {
            "messages": [AIMessage(content=tool_response_content)],
            "step_count": state["step_count"] + 1,
            "start_time": state["start_time"],
            "last_action": "tool",
            "api_errors": state["api_errors"]  # Preserve error count
        }
    
    # 4. Add nodes to workflow
    workflow.add_node("agent", agent_node)
    workflow.add_node("tools", tool_node)
    
    # 5. Set entry point
    workflow.set_entry_point("agent")
    
    # 6. Define conditional edges
    def should_continue(state: AgentState):
        last_msg = state["messages"][-1]
        
        # Handle timeout or step limit errors
        if "AGENT ERROR (GLOBAL_TIMEOUT)" in last_msg.content or "AGENT ERROR (STEP_LIMIT)" in last_msg.content or "AGENT ERROR (API_LIMIT)" in last_msg.content:
            return "end"
        
        # Handle all other errors
        if "AGENT ERROR" in last_msg.content:
            # For RESOURCE_EXHAUSTED errors, wait longer before retrying
            if "RESOURCE_EXHAUSTED" in last_msg.content:
                time.sleep(10 + random.uniform(0, 10))  # Wait 10-20 seconds
            return "agent"
        
        # Route to tools if tool calls exist
        if hasattr(last_msg, "tool_calls") and last_msg.tool_calls:
            return "tools"
        
        # End if final answer is present
        if "FINAL ANSWER" in last_msg.content:
            return "end"
        
        # Continue to agent otherwise
        return "agent"
    
    workflow.add_conditional_edges(
        "agent",
        should_continue,
        {
            "agent": "agent",
            "tools": "tools",
            "end": END
        }
    )
    
    # 7. Define flow after tool node
    workflow.add_edge("tools", "agent")
    
    # 8. Compile graph
    return workflow.compile()

# Initialize agent graph
agent_graph = build_graph()

# Wrapper function to ensure execution within time limits
def run_agent(question):
    # Create initial state with all required fields
    initial_state = {
        "messages": [
            SystemMessage(content=system_prompt),
            HumanMessage(content=question)
        ],
        "step_count": 0,
        "start_time": time.time(),
        "last_action": "start",
        "api_errors": 0
    }
    
    # Run with overall timeout
    start_time = time.time()
    result = None
    end_state_reached = False
    
    try:
        # Execute with 3-minute overall timeout
        for step in agent_graph.stream(initial_state):
            # Check overall timeout every step
            if time.time() - start_time > 180:  # 3 minutes
                return {"error": "Overall execution timeout (3 minutes)"}
            
            # Capture the final state when the graph completes
            if END in step:
                result = step[END]
                end_state_reached = True
                break
    except Exception as e:
        return {"error": f"Execution failed: {str(e)}"}
    
    # Extract final answer safely
    if end_state_reached and result is not None:
        if "messages" in result and result["messages"]:
            return {"answer": result["messages"][-1].content}
        else:
            return {"error": "Agent finished but produced no messages"}
    else:
        return {"error": "Agent did not complete execution"}

# 示例调用函数(在app.py中使用)
def process_question(question):
    # Add initial delay to avoid burst requests
    time.sleep(1 + random.uniform(0, 2))
    
    response = run_agent(question)
    if "answer" in response:
        return response["answer"]
    elif "error" in response:
        return f"Error: {response['error']}"
    else:
        return "Unexpected response format"