Spaces:
Build error
Build error
Download agent.py from giqua/Giqua_Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/spaces/giqua/Giqua_Final_Assignment_Template/resolve/main/agent.py
- Command line
-
hf download hf://spaces/giqua/Giqua_Final_Assignment_Template/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/giqua/Giqua_Final_Assignment_Template/resolve/main/agent.py
5.55 kB
| import os | |
| from langchain_core.messages import HumanMessage | |
| from langchain_core.runnables.config import RunnableConfig | |
| from langgraph.checkpoint.memory import MemorySaver | |
| from langchain.globals import set_debug | |
| from langchain.globals import set_verbose | |
| from langgraph.prebuilt import create_react_agent | |
| from langgraph.prebuilt import ToolNode | |
| from langgraph.prebuilt.chat_agent_executor import AgentState | |
| from smolagents import DuckDuckGoSearchTool | |
| from smolagents import PythonInterpreterTool | |
| from tools import analyze_audio | |
| from tools import analyze_excel | |
| from tools import analyze_image | |
| from tools import analyze_video | |
| from tools import download_file_for_task | |
| from tools import read_file_contents | |
| from tools import search_arxiv | |
| from tools import search_tavily | |
| from tools import search_wikipedia | |
| from tools import SmolagentToolWrapper | |
| from tools import tavily_extract_tool | |
| from utils import get_llm | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "") | |
| if not GOOGLE_API_KEY: | |
| raise ValueError("GOOGLE_API_KEY environment variable is not set.") | |
| AGENT_MODEL_NAME = os.getenv("AGENT_MODEL_NAME", "gemini-2.0-flash") | |
| DEFAULT_GAIA_AGENT_PROMPT = """ | |
| You are an intelligent AI assistant that is able to solve complex, real-world tasks. | |
| Abide by the following principles: | |
| - Think step-by-step. | |
| - Be concise. The final answer *must* stricly match the required format with nothing else. Your answer should be a number or as few words as possible or a comma separated list of numbers and/or strings. Do not abbreviate, do not use units or complete sentences. If returning a number, don't use commas, measurement units or currency symbols. | |
| - Use tools. If a question involves external information, structured data, images, or audio, call the appropriate tool to retrieve or process it. Please pass the question verbatim when calling a tool. | |
| - No guessing. If you don't know, say you don't know. Always exhaust your list of tools. | |
| - Use code if necessary. For calculations, parsing, or transformations, generate Python code and execute it. | |
| - Never submit logic leading up to your answer - just the answer. Don't use complete sentences. | |
| - Please include the descriptive terms in your solution as indicated in the question. | |
| - Always return the answer to the question prefixed with 'FINAL ANSWER: '" | |
| - If the question ask to count something, return only the number of items, don't add any words. | |
| - If the question ask to list something, return only the list of items, don't add any words. | |
| - If the question ask to sort something, return only the sorted list of items, don't add any words. | |
| Examples | |
| QUESTION: How much is a hamburger in USD? ANSWER: 4.99 | |
| QUESTION: Put fresh fruit, apple, and fresh apple in alphabetical order. ANSWER: apple, fresh apple, fresh fruit | |
| QUESTION: What city is known as the 'Gateway to the West'? ANSWER: Saint Louis | |
| QUESTION: How many faces are there on Mount Rushmore? ANSWER: 4 | |
| QUESTION: How would you respond to 'Is it hot or cold?'? ANSWER: Hot | |
| QUESTION: What pages did the first chapter of the book have? ANSWER: 1, 2, 3 | |
| QUESTION: Who sang the song 'I Will Always Love You'? ANSWER: Whitney Houston | |
| """ | |
| #set_debug(True) | |
| #set_verbose(True) | |
| class State(AgentState): | |
| task_id: str | |
| question: str | |
| file_name: str | |
| class MultiModalAgent: | |
| def __init__(self, model_name: str | None = None): | |
| if model_name is None: | |
| model_name = AGENT_MODEL_NAME | |
| llm = self._get_llm(model_name) | |
| tools = self._get_tools() | |
| self.agent = create_react_agent( | |
| llm, | |
| tools=tools, | |
| state_schema=State, | |
| state_modifier=DEFAULT_GAIA_AGENT_PROMPT, | |
| checkpointer = MemorySaver() | |
| ) | |
| def _get_llm(self, model_name: str): | |
| return get_llm( | |
| llm_provider_api_key=GOOGLE_API_KEY, | |
| model_name=model_name, | |
| ) | |
| def _get_tools(self): | |
| tools = [ | |
| SmolagentToolWrapper(DuckDuckGoSearchTool()), | |
| SmolagentToolWrapper(PythonInterpreterTool()), | |
| download_file_for_task, | |
| read_file_contents, | |
| analyze_audio, | |
| analyze_image, | |
| analyze_excel, | |
| analyze_video, | |
| search_arxiv, | |
| search_tavily, | |
| search_wikipedia, | |
| tavily_extract_tool, | |
| ] | |
| return ToolNode(tools) | |
| async def __call__( | |
| self, task_id: str, question: str, file_name: str | None = None | |
| ) -> str: | |
| config = RunnableConfig( | |
| recursion_limit=64, | |
| configurable={ "thread_id": task_id } | |
| ) | |
| if not file_name: | |
| file_name = "None - no file present" | |
| message = HumanMessage( | |
| content= | |
| [ | |
| { | |
| "type": "text", | |
| "text": f"Task Id: {task_id}, Question: {question}, Filename: {file_name}. If a filename is present (and is not 'None'), download the file for the task that's referenced in the question. If there isn't a filename present, please use tools where applicable." | |
| } | |
| ] | |
| ) | |
| answer = await self.agent.ainvoke( | |
| { | |
| "messages": [message], | |
| "question": question, | |
| "task_id": task_id, | |
| "file_name": file_name | |
| }, config) | |
| final_answer = answer['messages'][-1].content | |
| if "FINAL ANSWER: " in final_answer: | |
| return final_answer.split("FINAL ANSWER: ", 1)[1].strip() | |
| else: | |
| return final_answer | |