File size: 6,512 Bytes
caf8f7e
 
 
68d780e
caf8f7e
68d780e
 
 
 
 
 
caf8f7e
 
 
 
0c74cb3
caf8f7e
 
 
 
 
 
b025756
dc9a2d8
68d780e
0c74cb3
 
 
 
 
 
 
 
 
caf8f7e
 
68d780e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf8f7e
68d780e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf8f7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c74cb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""LangGraph ReAct agent for the GAIA final assignment.

Builds a tool-calling agent graph (langgraph.prebuilt.create_react_agent)
with automatic LLM provider fallback:

1. GROQ_API_KEY  -> Groq (free tier, e.g. llama-3.1-8b-instant)  [preferred if HF credits depleted]
2. GOOGLE_API_KEY / GEMINI_API_KEY -> Gemini 2.0 Flash (free tier)
3. HF_TOKEN -> Hugging Face Inference Providers (Qwen2.5-72B)

Set one of these as a Space Secret. The agent picks the first available.
Optional overrides: GROQ_MODEL, GOOGLE_MODEL, HF_MODEL, HF_VLM_MODEL.
"""

import os
import re
import time

from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.prebuilt import create_react_agent

from tools import ALL_TOOLS

DEFAULT_HF_MODEL = "Qwen/Qwen3-32B"
DEFAULT_GROQ_MODEL = "groq/compound"
DEFAULT_GEMINI_MODEL = "gemini-2.0-flash"
MAX_NEW_TOKENS = 512
RECURSION_LIMIT = 35

GAIA_SYSTEM_PROMPT = (
    "You solve GAIA questions scored by EXACT MATCH. Use tools for every fact; never guess.\n"
    "Prefer wikipedia_lookup, web_search+visit_webpage, analyze_file, calculate, run_python.\n"
    "Finish by outputting ONLY the answer, no preamble, no 'FINAL ANSWER', no quotes.\n"
    "Numbers: digits only, no units/commas unless asked. Keep proper-noun casing."
)


def _build_llm():
    # 1) Groq (free, fast) β€” set GROQ_API_KEY secret
    groq_key = os.getenv("GROQ_API_KEY")
    if groq_key:
        try:
            from langchain_groq import ChatGroq

            model = os.getenv("GROQ_MODEL", DEFAULT_GROQ_MODEL)
            print(f"Using Groq model: {model}")
            return ChatGroq(
                model=model,
                api_key=groq_key,
                temperature=0.1,
                max_tokens=MAX_NEW_TOKENS,
            )
        except Exception as e:
            print(f"Groq init failed, falling back: {e}")

    # 2) Google Gemini (free tier) β€” set GOOGLE_API_KEY or GEMINI_API_KEY
    google_key = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
    if google_key:
        try:
            from langchain_google_genai import ChatGoogleGenerativeAI

            model = os.getenv("GOOGLE_MODEL", DEFAULT_GEMINI_MODEL)
            print(f"Using Gemini model: {model}")
            return ChatGoogleGenerativeAI(
                model=model,
                google_api_key=google_key,
                temperature=0.1,
                max_output_tokens=MAX_NEW_TOKENS,
            )
        except Exception as e:
            print(f"Gemini init failed, falling back: {e}")

    # 3) Hugging Face Inference Providers
    token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
    if token:
        try:
            from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint

            model_id = os.getenv("HF_MODEL", DEFAULT_HF_MODEL)
            print(f"Using HF model: {model_id}")
            endpoint = HuggingFaceEndpoint(
                repo_id=model_id,
                task="conversational",
                provider="auto",
                huggingfacehub_api_token=token,
                max_new_tokens=MAX_NEW_TOKENS,
                temperature=0.1,
                timeout=120,
            )
            return ChatHuggingFace(llm=endpoint)
        except Exception as e:
            print(f"HF init failed: {e}")

    raise RuntimeError(
        "No LLM credentials found. Set one of these Space Secrets:\n"
        "- GROQ_API_KEY (recommended free): get at https://console.groq.com/keys\n"
        "- GOOGLE_API_KEY (Gemini free): get at https://aistudio.google.com/app/apikey\n"
        "- HF_TOKEN (HF Inference Providers): https://huggingface.co/settings/tokens\n"
        "After adding, restart the Space."
    )


def build_gaia_graph():
    llm = _build_llm()
    return create_react_agent(
        model=llm,
        tools=ALL_TOOLS,
        prompt=SystemMessage(content=GAIA_SYSTEM_PROMPT),
    )


def clean_answer(raw: str) -> str:
    """Normalize the model's final message into a bare exact-match answer."""
    text = raw.strip()
    # Drop any FINAL ANSWER-style prefix the model may still emit.
    text = re.sub(r"^final\s+answer\s*[:\-]?\s*", "", text, flags=re.I)
    # Collapse internal newlines so the answer is a single line.
    text = re.sub(r"\s*\n+\s*", " ", text).strip()
    # Strip one layer of wrapping quotes.
    if len(text) >= 2 and text[0] == text[-1] and text[0] in "\"'":
        text = text[1:-1].strip()
    return text


class GaiaAgent:
    """Same call signature as the template's BasicAgent, backed by LangGraph."""

    def __init__(self):
        self.graph = build_gaia_graph()
        print("GaiaAgent initialized.")

    def __call__(self, question: str, file_path: str | None = None) -> str:
        content = question.strip()
        if file_path:
            content += (
                f"\n\nA file relevant to this question has been downloaded "
                f"to the local path: {file_path}\n"
                f"Start by calling analyze_file on this path."
            )
        # Retry on 413/rate-limit (Groq free tier 8000 TPM) with backoff + shorter history
        for attempt in range(3):
            try:
                result = self.graph.invoke(
                    {"messages": [HumanMessage(content=content)]},
                    config={"recursion_limit": RECURSION_LIMIT},
                )
                raw = result["messages"][-1].content
                if isinstance(raw, list):
                    raw = " ".join(
                        b.get("text", "") for b in raw if isinstance(b, dict)
                    )
                answer = clean_answer(str(raw))
                print(f"Agent answer: {answer[:100]}")
                return answer
            except Exception as e:
                msg = str(e)
                is_rate = "413" in msg or "rate_limit" in msg.lower() or "RateLimit" in msg or "tokens per minute" in msg.lower()
                if is_rate and attempt < 2:
                    wait = 12 * (attempt + 1)
                    print(f"Rate limited (attempt {attempt+1}/3): {msg[:200]} β€” waiting {wait}s then retrying with trimmed context")
                    time.sleep(wait)
                    # On retry, shorten the question a bit to reduce tokens
                    if len(content) > 2000:
                        content = content[:2000] + "..."
                    continue
                # Non-rate-limit or final failure: surface as AGENT ERROR but don't crash the whole run
                raise