David commited on
Commit ·
b0c6c93
1
Parent(s): 7da5655
Agent passed.
Browse filesFiles changed (4)
- agent.py+21-63
- app.py+2-1
- gaia_system_prompt.py+11
- tools.py+26-14
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768695 unmodified lines75767778798081828384853 unmodified lines899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412544 unmodified lines170171172173174175176177178179180181from llama_index.llms.google_genai import GoogleGenAIfrom llama_index.llms.gemini import Geminifrom llama_index.tools.arxiv import ArxivToolSpecfrom llama_index.tools.wikipedia import WikipediaToolSpecfrom llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpecfrom llama_index.core.tools import FunctionToolfrom llama_index.core.agent.workflow import AgentWorkflow, ReActAgentfrom llama_index.llms.lmstudio import LMStudiofrom llama_index.core.agent.workflow import (AgentStream,AgentOutput)from gradio import ChatMessagefrom llama_index.core.base.llms.types import ChatMessage as llama_index_chat_messagefrom tools import interpret_python_math_code, image_understanding, convert_audio_to_text, video_understanding, read_csv_file, read_xlsx_filefrom gaia_system_prompt import GAIA_SYSTEM_PROMPT, CUSTOM_SYSTEM_PROMPTimport osimport asyncioTIMEOUT=180 # Timeout for agent execution in secondsGEMINI_API_KEY = os.getenv("GEMINI_TOKEN")GEMINI_OPENAI_API_DIR = "https://generativelanguage.googleapis.com/v1beta/openai/"GEMINI_MODEL_NAME = "gemini-2.0-flash"LMSTUDIO_MODEL_NAME = "gemma-3-12B-it-qat-GGUF"API_DIR = "http://host.docker.internal:1234/v1" # LM Studio API URLclass FinalAgent:def __init__(self):# LLM Initialization# self.llm = GoogleGenAI(model=GEMINI_MODEL_NAME, api_key=GEMINI_API_KEY)self.llm = Gemini(model=GEMINI_MODEL_NAME, api_key=GEMINI_API_KEY)# self.llm = LMStudio(model_name=LMSTUDIO_MODEL_NAME, base_url=API_DIR, request_timeout=180, temperature=0.1)# Tool Initializationself.tools = [FunctionTool.from_defaults(fn=interpret_python_math_code,name="InterpretPythonMathCode",description="Interprets Python code for mathematical expressions."),FunctionTool.from_defaults(fn=image_understanding,name="ImageUnderstanding",description="Analyzes an image and generates a response to a given question based on the image's content."),FunctionTool.from_defaults(fn=convert_audio_to_text,name="ConvertAudioToText",description="Converts audio files to text using a speech-to-text model."),FunctionTool.from_defaults(fn=video_understanding,name="VideoUnderstanding",description="Analyzes a video and generates a response to a given question based on the video's content."),FunctionTool.from_defaults(fn=read_csv_file,name="ReadCSVFile",description="Reads a CSV file and returns its content as a string."),FunctionTool.from_defaults(fn=read_xlsx_file,name="ReadXLSXFile",description="Reads an XLSX file and returns its content as a string.")]self.tools.extend(5 unmodified linesself.tools.extend(DuckDuckGoSearchToolSpec().to_tool_list())# Print the tools for debuggingprint("Tools initialized:")for tool in self.tools:print(f"- {tool._metadata}")# Agent Workflow Initializationself.agent = AgentWorkflow.from_tools_or_functions(3 unmodified linestimeout=TIMEOUT)# self.agent = ReActAgent(# llm=self.llm,# verbose=True,# max_iterations=5,# system_prompt=CUSTOM_SYSTEM_PROMPT,# tools=self.tools# )print("FinalAgent initialized.")# async def __call__(self, question: str) -> str:# # Example# print(f"Agent received question: {question}")# # fixed_answer = "This is a default answer."# # print(f"Agent returning fixed answer: {fixed_answer}")# # response = fixed_answer# # Implement agent logic here# response = ""# # Run the agent with the question# stream = await self.agent.run(question)# response = stream.response.content# # async for event in stream.stream_events():# # if isinstance(event, AgentStream):# # # Check if delta is empty# # if event.raw["choices"][0]["delta"] != {}:# # response += event.raw["choices"][0]["delta"]["content"]# print(f"Agent response: {response}")# return responseasync def __call__(self, question: str) -> str:print(f"Agent received question: {question}")44 unmodified linesreturn response_str# async def main():# # Example usage# agent = FinalAgent()# question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."# answer = await agent(question)# print(f"Final answer: {answer}")# if __name__ == "__main__":# asyncio.run(main())12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758595 unmodified lines656667686970713 unmodified lines757677787980818244 unmodified lines127128129130131132133134135136137138139from llama_index.llms.gemini import Geminifrom llama_index.tools.arxiv import ArxivToolSpecfrom llama_index.tools.wikipedia import WikipediaToolSpecfrom llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpecfrom llama_index.core.tools import FunctionToolfrom llama_index.core.agent.workflow import AgentWorkflowfrom gradio import ChatMessagefrom llama_index.core.base.llms.types import ChatMessage as llama_index_chat_messagefrom tools import interpret_python_math_code, image_understanding, convert_audio_to_text, video_understanding, read_csv_file, read_xlsx_filefrom gaia_system_prompt import CUSTOM_SYSTEM_PROMPTimport osimport asyncioTIMEOUT=180 # Timeout for agent execution in secondsGEMINI_API_KEY = os.getenv("GEMINI_TOKEN")GEMINI_MODEL_NAME = "gemini-2.5-flash-preview-04-17"# GEMINI_MODEL_NAME = "gemini-2.0-flash"class FinalAgent:def __init__(self):# LLM Initializationself.llm = Gemini(model=GEMINI_MODEL_NAME, api_key=GEMINI_API_KEY)# Tool Initializationself.tools = [FunctionTool.from_defaults(fn=interpret_python_math_code,name="InterpretPythonMathCode",description=interpret_python_math_code.__doc__),FunctionTool.from_defaults(fn=image_understanding,name="ImageUnderstanding",description=image_understanding.__doc__),FunctionTool.from_defaults(fn=convert_audio_to_text,name="ConvertAudioToText",description= convert_audio_to_text.__doc__),FunctionTool.from_defaults(fn=video_understanding,name="VideoUnderstanding",description= video_understanding.__doc__),FunctionTool.from_defaults(fn=read_csv_file,name="ReadCSVFile",description=read_csv_file.__doc__),FunctionTool.from_defaults(fn=read_xlsx_file,name="ReadXLSXFile",description= read_xlsx_file.__doc__)]self.tools.extend(5 unmodified linesself.tools.extend(DuckDuckGoSearchToolSpec().to_tool_list())# Agent Workflow Initializationself.agent = AgentWorkflow.from_tools_or_functions(3 unmodified linestimeout=TIMEOUT)print("FinalAgent initialized.")async def __call__(self, question: str) -> str:print(f"Agent received question: {question}")44 unmodified linesreturn response_strasync def main():# Example usageagent = FinalAgent()question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."question2 = "In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?"answer = await agent(question)print(f"Final answer: {answer}")if __name__ == "__main__":asyncio.run(main())
7 unmodified lines89101112131474 unmodified lines8990919293947 unmodified linesimport asyncioimport timeSLEEP_TIME_BETWEEN_QUESTIONS = 30 # Sleep time between questions to avoid rate limiting# (Keep Constants as is)# --- Constants ---74 unmodified linescontinuetry:# Run the agent on the questionsubmitted_answer = await agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})7 unmodified lines89101112131474 unmodified lines899091929394957 unmodified linesimport asyncioimport timeSLEEP_TIME_BETWEEN_QUESTIONS = 60 # Sleep time between questions to avoid rate limiting# (Keep Constants as is)# --- Constants ---74 unmodified linescontinuetry:# Run the agent on the questionprint(item)submitted_answer = await agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
12 unmodified lines13141516171812 unmodified lines3. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.4. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.The final answer should be written in the following format:<final_answer>YOUR FINAL ANSWER12 unmodified lines131415161718192021222324252627282912 unmodified lines3. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.4. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.To answer the questions, you should use the follwing tools:- DuckDuckGoSearchTool: Use this tool to search the web for information.- ArxivTool: Use this tool to search for academic papers on arXiv.- WikipediaTool: Use this tool to search for information on Wikipedia.- InterpretPythonCodeTool: Use this tool to execute Python code to perform math calculations and return the result.- ImageUnderstandingTool: Use this tool to analyze images and extract information.- ConvertAudioToTextTool: Use this tool to convert audio files to text.- VideoUnderstandingTool: Use this tool to analyze videos and extract information.- ReadCSVFileTool: Use this tool to read CSV files and extract information.- ReadXLSXFileTool: Use this tool to read XLSX files and extract information.The final answer should be written in the following format:<final_answer>YOUR FINAL ANSWER
10 unmodified lines111213141516109 unmodified lines1261271281291301311321331341351361375 unmodified lines1431441451461471481491501511521531542 unmodified lines1571581591601611621631641651661671681691701711721731741751761771782 unmodified lines18118218318418518618718818919019119219319419519632 unmodified lines22923023123210 unmodified linesimport base64from google import genaiALLOWED_MODULES = {"numpy", "pandas", "scipy"}GEMINI_API_KEY = os.getenv("GEMINI_TOKEN")109 unmodified lines"""Converts speech from an audio file into text.Args:path_to_audio (str): The path to the audio file to be transcribed.Returns:str: The transcribed text content of the audio file."""client = genai.Client(api_key="GOOGLE_API_KEY")myfile = client.files.upload(file=path_to_audio)5 unmodified linesreturn transcription.text# Analyze image tooldef image_understanding(path_to_image: str, question: str) -> str:"""Analyzes an image and generates a response to a given question based on the image's content.Args:path_to_image (str): The path to the image file to be analyzed.question (str): The question to be answered, based on the contents of the image.Returns:2 unmodified linesclient = genai.Client(api_key=GEMINI_API_KEY)my_file = client.files.upload(file=path_to_image)response = client.models.generate_content(model=GEMINI_MODEL_NAME,contents=[my_file, question],)return response.text# Analyze video tooldef video_understanding(path_to_video: str, question: str) -> str:"""Analyzes a video and generates a response to a given question based on the video's content.Args:path_to_video (str): The path to the video file to be analyzed.question (str): The question to be answered, based on the contents of the video.Returns:2 unmodified linesclient = genai.Client(api_key=GEMINI_API_KEY)my_file = client.files.upload(file=path_to_video)response = client.models.generate_content(model=GEMINI_MODEL_NAME,contents=[my_file, question],)return response.text## Read .csv file tooldef read_csv_file(path_to_csv: str) -> str:"""32 unmodified lines# Return df as plain tectreturn df.to_string(index=False)except Exception as e:return f"Error reading the XLSX file: {e}"10 unmodified lines11121314151617109 unmodified lines1271281291301311321331341351361371385 unmodified lines1441451461471481491501511521531541552 unmodified lines1581591601611621631641651661671681691701711721731741751761771781791802 unmodified lines18318418518618718818919019119219319419519619719819920020120232 unmodified lines23523623723823924024124224324410 unmodified linesimport base64from google import genaiimport requestsALLOWED_MODULES = {"numpy", "pandas", "scipy"}GEMINI_API_KEY = os.getenv("GEMINI_TOKEN")109 unmodified lines"""Converts speech from an audio file into text.Args:path_to_audio (str): The path to the audio file to be transcribed. An URL can also be used.Returns:str: The transcribed text content of the audio file."""client = genai.Client(api_key=GEMINI_API_KEY)myfile = client.files.upload(file=path_to_audio)5 unmodified linesreturn transcription.text# Analyze image tooldef image_understanding(url_to_image: str, question: str) -> str:"""Analyzes an image and generates a response to a given question based on the image's content. An URL needs to be used.Args:path_to_image (str): The URL to the image file to be analyzed.question (str): The question to be answered, based on the contents of the image.Returns:2 unmodified linesclient = genai.Client(api_key=GEMINI_API_KEY)image_bytes = requests.get(url_to_image).contentimage = genai.types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")response = client.models.generate_content(model=GEMINI_MODEL_NAME,contents=[question, image],)return response.text# Analyze video tooldef video_understanding(url_to_video: str, question: str) -> str:"""Analyzes a video and generates a response to a given question based on the video's content.Args:url_to_video (str): The URL to the video file to be analyzed (example:YouTube).question (str): The question to be answered, based on the contents of the video.Returns:2 unmodified linesclient = genai.Client(api_key=GEMINI_API_KEY)response = client.models.generate_content(model=GEMINI_MODEL_NAME,contents=genai.types.Content(parts=[genai.types.Part(file_data=genai.types.FileData(file_uri=url_to_video)),genai.types.Part(text=question)]))return response.text## Read .csv file tooldef read_csv_file(path_to_csv: str) -> str:"""32 unmodified lines# Return df as plain tectreturn df.to_string(index=False)except Exception as e:return f"Error reading the XLSX file: {e}"# Example usage of the toolsif __name__ == "__main__":# Example usage of the tools# print(video_understanding("https://www.youtube.com/watch?v=L1vXCYZAYYM", "What is happening in this video?"))print(image_understanding("https://i.etsystatic.com/28810262/r/il/2fc5e0/5785166966/il_1140xN.5785166966_nvy4.jpg", "What does this image represent?"))
agent.py+21-63
app.py+2-1
gaia_system_prompt.py+11
tools.py+26-14