Spaces:
Sleeping
Sleeping
Download agent.py from SlipStream33/Course_Check: direct link, hf CLI and curl.
- Browser
- Download file 6.51 kB
-
https://huggingface.co/spaces/SlipStream33/Course_Check/resolve/main/agent.py
- Command line
-
hf download hf://spaces/SlipStream33/Course_Check/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/SlipStream33/Course_Check/resolve/main/agent.py
6.51 kB
| """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 | |