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
11234578910111267891017111213141516172425262718192021222332243425262728293041313233343546363738394051414243444556464748495061515253545566565758595 unmodified lines6566677879808182686970713 unmodified lines7576779293949596979899781011021031041051061077910810911011111211311411511611711811912012112280818244 unmodified lines127128129173174175176177178130131132133134135136137180181138139from 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 llama_index.core.agent.workflow import AgentWorkflowfrom gradio import ChatMessagefrom llama_index.core.base.llms.types import ChatMessage as llama_index_chat_message
from 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_PROMPTfrom gaia_system_prompt import CUSTOM_SYSTEM_PROMPT
import osimport asyncio
TIMEOUT=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 URLGEMINI_MODEL_NAME = "gemini-2.5-flash-preview-04-17"# GEMINI_MODEL_NAME = "gemini-2.0-flash"
class 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 Initialization self.tools = [ FunctionTool.from_defaults( fn=interpret_python_math_code, name="InterpretPythonMathCode", description="Interprets Python code for mathematical expressions." description=interpret_python_math_code.__doc__ ), 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." description=image_understanding.__doc__ ), FunctionTool.from_defaults( fn=convert_audio_to_text, name="ConvertAudioToText", description="Converts audio files to text using a speech-to-text model." description= convert_audio_to_text.__doc__ ), 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." description= video_understanding.__doc__ ), FunctionTool.from_defaults( fn=read_csv_file, name="ReadCSVFile", description="Reads a CSV file and returns its content as a string." description=read_csv_file.__doc__ ), FunctionTool.from_defaults( fn=read_xlsx_file, name="ReadXLSXFile", description="Reads an XLSX file and returns its content as a string." description= read_xlsx_file.__doc__ ) ] self.tools.extend(5 unmodified lines self.tools.extend( DuckDuckGoSearchToolSpec().to_tool_list() )
# Print the tools for debugging print("Tools initialized:") for tool in self.tools: print(f"- {tool._metadata}")
# Agent Workflow Initialization self.agent = AgentWorkflow.from_tools_or_functions(3 unmodified lines timeout=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 response
async def __call__(self, question: str) -> str: print(f"Agent received question: {question}") 44 unmodified lines return 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}")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." 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())if __name__ == "__main__": asyncio.run(main())7 unmodified lines8910111112131474 unmodified lines899091929394957 unmodified linesimport asyncioimport time
SLEEP_TIME_BETWEEN_QUESTIONS = 30 # Sleep time between questions to avoid rate limitingSLEEP_TIME_BETWEEN_QUESTIONS = 60 # Sleep time between questions to avoid rate limiting
# (Keep Constants as is)# --- Constants ---74 unmodified lines continue try: # Run the agent on the question print(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 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 ANSWER10 unmodified lines11121314151617109 unmodified lines1271281291291301311321331341341351361371385 unmodified lines1441451461461471481481491501511511521531541552 unmodified lines1581591601601611621631641651641661671681691701711701721731741751761751771781791802 unmodified lines18318418518418518618718818818919019119219319419519619719819919320020120232 unmodified lines23523623723223823924024124224324410 unmodified linesimport base64
from google import genaiimport requests
ALLOWED_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. 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="GOOGLE_API_KEY") client = genai.Client(api_key=GEMINI_API_KEY)
myfile = client.files.upload(file=path_to_audio)
5 unmodified lines return transcription.text
# Analyze image tooldef image_understanding(path_to_image: str, question: str) -> str:def 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. 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 path to the image file to be analyzed. 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 lines
client = genai.Client(api_key=GEMINI_API_KEY)
my_file = client.files.upload(file=path_to_image) image_bytes = requests.get(url_to_image).content image = genai.types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
response = client.models.generate_content( model=GEMINI_MODEL_NAME, contents=[my_file, question], contents=[question, image], )
return response.text
# Analyze video tooldef video_understanding(path_to_video: str, question: str) -> str:def 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: path_to_video (str): The path to the video file to be analyzed. 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 lines
client = 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], 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 tect return df.to_string(index=False) except Exception as e: return f"Error reading the XLSX file: {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