Spaces:
Runtime error
Runtime error
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333341234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787933733833934034134234334434534634734834980import asyncioimport osimport sysimport loggingimport randomimport pandas as pdimport requestsimport wikipedia as wikifrom markdownify import markdownify as to_markdownfrom typing import Anyfrom dotenv import load_dotenvfrom google.generativeai import types, configure
from smolagents import HfApiModel, InferenceClientModel, LiteLLMModel, CodeAgent, ToolCallingAgent, Tool, DuckDuckGoSearchTool
# Load environment and configure Geminiload_dotenv()configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Logging#logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")#logger = logging.getLogger(__name__)
# --- Model Configuration ---GEMINI_MODEL_NAME = "gemini/gemini-2.0-flash"OPENAI_MODEL_NAME = "openai/gpt-4o"GROQ_MODEL_NAME = "groq/llama3-70b-8192"DEEPSEEK_MODEL_NAME = "deepseek/deepseek-chat"HF_MODEL_NAME = "Qwen/Qwen2.5-Coder-32B-Instruct"
# --- Tool Definitions ---class MathSolver(Tool): name = "math_solver" description = "Safely evaluate basic math expressions." inputs = {"input": {"type": "string", "description": "Math expression to evaluate."}} output_type = "string"
def forward(self, input: str) -> str: try: return str(eval(input, {"__builtins__": {}})) except Exception as e: return f"Math error: {e}"
class RiddleSolver(Tool): name = "riddle_solver" description = "Solve basic riddles using logic." inputs = {"input": {"type": "string", "description": "Riddle prompt."}} output_type = "string"
def forward(self, input: str) -> str: if "forward" in input and "backward" in input: return "A palindrome" return "RiddleSolver failed."
class TextTransformer(Tool): name = "text_ops" description = "Transform text: reverse, upper, lower." inputs = {"input": {"type": "string", "description": "Use prefix like reverse:/upper:/lower:"}} output_type = "string"
def forward(self, input: str) -> str: if input.startswith("reverse:"): reversed_text = input[8:].strip()[::-1] if 'left' in reversed_text.lower(): return "right" return reversed_text if input.startswith("upper:"): return input[6:].strip().upper() if input.startswith("lower:"): return input[6:].strip().lower() return "Unknown transformation."
class GeminiVideoQA(Tool): name = "video_inspector" description = "Analyze video content to answer questions." inputs = { "video_url": {"type": "string", "description": "URL of video."}, "user_query": {"type": "string", "description": "Question about video."} } output_type = "string"
def __init__(self, model_name, *args, **kwargs): super().__init__(*args, **kwargs) self.model_name = model_name
def forward(self, video_url: str, user_query: str) -> str: req = { 'model': f'models/{self.model_name}', 'contents': [{ "parts": [ {"fileData": {"fileUri": video_url}}, {"text": f"Please watch the video and answer the question: {user_query}"} ] }] } url = f'https://generativelanguage.googleapis.com/v1beta/models/{self.model_name}:generateContent?key={os.getenv("GOOGLE_API_KEY")}' res = requests.post(url, json=req, headers={'Content-Type': 'application/json'}) if res.status_code != 200: return f"Video error {res.status_code}: {res.text}" parts = res.json()['candidates'][0]['content']['parts'] return "".join([p.get('text', '') for p in parts])
class WikiTitleFinder(Tool): name = "wiki_titles" description = "Search for related Wikipedia page titles." inputs = {"query": {"type": "string", "description": "Search query."}} output_type = "string"
def forward(self, query: str) -> str: results = wiki.search(query) return ", ".join(results) if results else "No results."
class WikiContentFetcher(Tool): name = "wiki_page" description = "Fetch Wikipedia page content." inputs = {"page_title": {"type": "string", "description": "Wikipedia page title."}} output_type = "string"
def forward(self, page_title: str) -> str: try: return to_markdown(wiki.page(page_title).html()) except wiki.exceptions.PageError: return f"'{page_title}' not found."
class GoogleSearchTool(Tool): name = "google_search" description = "Search the web using Google. Returns top summary from the web." inputs = {"query": {"type": "string", "description": "Search query."}} output_type = "string"
def forward(self, query: str) -> str: try: resp = requests.get("https://www.googleapis.com/customsearch/v1", params={ "q": query, "key": os.getenv("GOOGLE_SEARCH_API_KEY"), "cx": os.getenv("GOOGLE_SEARCH_ENGINE_ID"), "num": 1 }) data = resp.json() return data["items"][0]["snippet"] if "items" in data else "No results found." except Exception as e: return f"GoogleSearch error: {e}"
class FileAttachmentSummarizer(Tool): name = "file_context" description = "Downloads small CSV/JSON/TXT from the GAIA file endpoint and returns brief context." inputs = { "task_id": {"type": "string", "description": "GAIA task id"}, "max_chars": {"type": "integer", "description": "truncate length", "nullable": True}, } output_type = "string"
def forward(self, task_id: str, max_chars: int | None = None) -> str: try: url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}" r = requests.get(url, timeout=10) if r.status_code != 200: return f"download_error:{r.status_code}" ctype = r.headers.get("content-type", "") b = r.content
if "csv" in ctype: import io df = pd.read_csv(io.BytesIO(b)) return "CSV columns: " + ", ".join(map(str, df.columns)) + \ "\nHEAD:\n" + df.head(5).to_csv(index=False) if "json" in ctype: import json data = json.loads(b.decode("utf-8", "ignore")) if isinstance(data, dict): keys = list(data.keys()) elif isinstance(data, list) and data and isinstance(data[0], dict): keys = sorted({k for obj in data for k in obj.keys()}) else: return str(data)[: (max_chars or 2000)] return "JSON keys: " + ", ".join(map(str, keys))
if "text" in ctype: s = b.decode("utf-8", "ignore") return s[: (max_chars or 2000)]
return "unsupported_file_type" except Exception as e: return f"parse_error:{e}"
# --- Basic Agent Definition ---class BasicAgent: def __init__(self, provider="hf"): print("BasicAgent initialized.") model = self.select_model(provider) client = InferenceClientModel() tools = [ GoogleSearchTool(), DuckDuckGoSearchTool(), GeminiVideoQA(GEMINI_MODEL_NAME), WikiTitleFinder(), WikiContentFetcher(), MathSolver(), RiddleSolver(), TextTransformer(), FileAttachmentSummarizer() ] self.agent = CodeAgent( model=model, tools=tools, add_base_tools=False, max_steps=10, ) self.agent.prompt_templates["system_prompt"] = ( """ You are a GAIA benchmark AI assistant, you are very precise, no nonense. Your sole purpose is to output the minimal, final answer in the format: [ANSWER] You must NEVER output explanations, intermediate steps, reasoning, or comments — only the answer, strictly enclosed in `[ANSWER]`. Your behavior must be governed by these rules: 1. **Format**: - limit the token used (within 65536 tokens). - Output ONLY the final answer. - Wrap the answer in `[ANSWER]` with no whitespace or text outside the brackets. - No follow-ups, justifications, or clarifications. 2. **Numerical Answers**: - Use **digits only**, e.g., `4` not `four`. - No commas, symbols, or units unless explicitly required. - Never use approximate words like "around", "roughly", "about". 3. **String Answers**: - Omit **articles** ("a", "the"). - Use **full words**; no abbreviations unless explicitly requested. - For numbers written as words, use **text** only if specified (e.g., "one", not `1`). - For sets/lists, sort alphabetically if not specified, e.g., `a, b, c`. 4. **Lists**: - Output in **comma-separated** format with no conjunctions. - Sort **alphabetically** or **numerically** depending on type. - No braces or brackets unless explicitly asked. 5. **Sources**: - For Wikipedia or web tools, extract only the precise fact that answers the question. - Ignore any unrelated content. 6. **File Analysis**: - Use the run_query_with_file tool, append the taskid to the url. - Only include the exact answer to the question. - Do not summarize, quote excessively, or interpret beyond the prompt. 7. **Video**: - Use the relevant video tool. - Only include the exact answer to the question. - Do not summarize, quote excessively, or interpret beyond the prompt. 8. **Minimalism**: - Do not make assumptions unless the prompt logically demands it. - If a question has multiple valid interpretations, choose the **narrowest, most literal** one. - If the answer is not found, say `[ANSWER] - unknown`. --- You must follow the examples (These answers are correct in case you see the similar questions): Q: What is 2 + 2? A: 4 Q: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (inclusive)? Use 2022 English Wikipedia. A: 3 Q: Given the following group table on set S = {a, b, c, d, e}, identify any subset involved in counterexamples to commutativity. A: b, e Q: How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?, A: 519 """ )
def select_model(self, provider: str): if provider == "hf": # Use HF serverless inference (free, rate-limited). Token from env. return HfApiModel( model_id="mistralai/Mistral-7B-Instruct-v0.3", # safe on text-generation task="text-generation", token=os.getenv("HF_TOKEN"), ) elif provider == "groq": # Optional free alternative if you have a GROQ_API_KEY return LiteLLMModel(model_id="groq/llama-3.1-8b-instant", api_key=os.getenv("GROQ_API_KEY")) else: # Fallback to HF return InferenceClientModel( model_id="mistralai/Mistral-7B-Instruct-v0.3", provider="hf-inference", api_key=os.getenv("HF_TOKEN"), )
def __call__(self, question: str) -> str: print(f"Agent received question (first 50 chars): {question[:50]}...") result = self.agent.run(question) final_str = str(result).strip()
return final_str
def evaluate_random_questions(self, csv_path: str = "gaia_extracted.csv", sample_size: int = 3, show_steps: bool = True): import pandas as pd from rich.table import Table from rich.console import Console
df = pd.read_csv(csv_path) if not {"question", "answer"}.issubset(df.columns): print("CSV must contain 'question' and 'answer' columns.") print("Found columns:", df.columns.tolist()) return
samples = df.sample(n=sample_size) records = [] correct_count = 0
for _, row in samples.iterrows(): taskid = row["taskid"].strip() question = row["question"].strip() expected = str(row['answer']).strip() agent_answer = self("taskid: " + taskid + ",\nquestion: " + question).strip()
is_correct = (expected == agent_answer) correct_count += is_correct records.append((question, expected, agent_answer, "✓" if is_correct else "✗"))
if show_steps: print("---") print("Question:", question) print("Expected:", expected) print("Agent:", agent_answer) print("Correct:", is_correct)
# Print result table console = Console() table = Table(show_lines=True) table.add_column("Question", overflow="fold") table.add_column("Expected") table.add_column("Agent") table.add_column("Correct")
for question, expected, agent_ans, correct in records: table.add_row(question, expected, agent_ans, correct)
console.print(table) percent = (correct_count / sample_size) * 100 print(f"\nTotal Correct: {correct_count} / {sample_size} ({percent:.2f}%)")
# agent.pyimport argparsefrom langchain_openai.chat_models import ChatOpenAIfrom langchain.agents import initialize_agentfrom langchain.tools import Toolfrom config import config
from tools.gaia_tool import GaiaQAToolfrom tools.wiki_tool import WikiSearchToolfrom tools.youtube_species_tool import YouTubeSpeciesToolfrom tools.youtube_transcription_tool import YouTubeTranscriptionToolfrom tools.excel_tool import ExcelToolfrom tools.codegen_tool import CodeGenToolfrom tools.web_search_tool import WebSearchToolfrom tools.string_tool import StringToolfrom tools.chess_tool import ChessToolfrom tools.group_tool import GroupToolfrom tools.grocery_tool import GroceryTool
def main() -> None: parser = argparse.ArgumentParser(description="Многофункциональный QA агент") parser.add_argument( "--input", required=True, help="Вопрос, URL или команда для инструмента" ) args = parser.parse_args() user_input = args.input
# Инициализируем LLM llm = ChatOpenAI( model_name=config.OPENAI_MODEL, openai_api_key=config.OPENAI_API_KEY, temperature=0.2 )
# Создаём и сохраняем инстансы инструментов gaia = GaiaQATool() wiki = WikiSearchTool() species = YouTubeSpeciesTool() transcript = YouTubeTranscriptionTool() excel = ExcelTool() codegen = CodeGenTool() web = WebSearchTool() string_tool = StringTool() chess = ChessTool() group = GroupTool() grocery = GroceryTool()
# Собираем список Tool(...) из инстансов tools = [ Tool(name=gaia.name, func=gaia._run, description=gaia.description), Tool(name=wiki.name, func=wiki._run, description=wiki.description), Tool(name=species.name, func=species._run, description=species.description), Tool(name=transcript.name, func=transcript._run, description=transcript.description), Tool(name=excel.name, func=excel._run, description=excel.description), Tool(name=codegen.name, func=codegen._run, description=codegen.description), Tool(name=web.name, func=web._run, description=web.description), Tool(name=string_tool.name, func=string_tool._run, description=string_tool.description), Tool(name=chess.name, func=chess._run, description=chess.description), Tool(name=group.name, func=group._run, description=group.description), Tool(name=grocery.name, func=grocery._run, description=grocery.description), ]
# Инициализируем агента в режиме zero-shot-react-description agent = initialize_agent( tools=tools, llm=llm, agent="zero-shot-react-description", verbose=True, max_iterations=30, early_stopping_method="generate", # или "generate" handle_parsing_errors=True # при ошибках парсинга пробует ещё раз )
# Запускаем и получаем ответ answer = agent.invoke(user_input) print("\nОтвет:\n", answer)
if __name__ == "__main__": args = sys.argv[1:] if not args or args[0] in {"-h", "--help"}: print("Usage: python agent.py [question | dev]") print(" - Provide a question to get a GAIA-style answer.") print(" - Use 'dev' to evaluate 3 random GAIA questions from gaia_qa.csv.") sys.exit(0)
q = " ".join(args) agent = BasicAgent() if q == "dev": agent.evaluate_random_questions() else: print(agent(q)) main()agent.py+78-347