|
from zoneinfo import ZoneInfo |
|
from google.adk.agents import Agent,BaseAgent,LlmAgent |
|
from google.adk.tools import google_search |
|
from google.adk.runners import Runner |
|
from google.adk.sessions import InMemorySessionService |
|
from google.genai import types |
|
import google.genai.types as types |
|
import requests |
|
from google.adk.events import Event, EventActions |
|
from google.adk.agents.invocation_context import InvocationContext |
|
from typing import AsyncGenerator |
|
from google.genai import types as genai_types |
|
from google.adk.tools import ToolContext, FunctionTool |
|
import logging |
|
|
|
from google.adk.tools import agent_tool |
|
|
|
logging.basicConfig(level=logging.ERROR) |
|
|
|
url = 'https://agents-course-unit4-scoring.hf.space/questions' |
|
headers = {'accept': 'application/json'} |
|
response = requests.get(url, headers=headers) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def answer_questions(): |
|
"""Fetch questions from the GAIA API and return them in a structured format""" |
|
url = 'https://agents-course-unit4-scoring.hf.space/questions' |
|
headers = {'accept': 'application/json'} |
|
response = requests.get(url, headers=headers) |
|
|
|
if response.status_code != 200: |
|
return f"Error fetching questions: {response.status_code}" |
|
|
|
questions_data = response.json() |
|
return questions_data |
|
|
|
from typing import Dict, Any |
|
def submit_questions(answers: list[str]) -> Dict[str, Any]: |
|
url = 'https://agents-course-unit4-scoring.hf.space/submit' |
|
payload = { |
|
"username": "ashishja", |
|
"agent_code": "https://huggingface.co/spaces/ashishja/Agents_Course_Final_Assignment_Ashish/tree/main", |
|
"answers": answers} |
|
headers = {'accept': 'application/json', "Content-Type": "application/json"} |
|
response = requests.post(url, headers=headers, json =payload) |
|
import json |
|
print(json.dumps(payload, indent=2)) |
|
if response.status_code == 200: |
|
return response.json() |
|
else: |
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
|
responses_api = FunctionTool(func= answer_questions) |
|
submit_api = FunctionTool(func=submit_questions) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_NAME="weather_sentiment_agent" |
|
USER_ID="user1234" |
|
SESSION_ID="1234" |
|
|
|
|
|
code_agent = LlmAgent( |
|
name='codegaiaAgent', |
|
model="gemini-2.5-pro-preview-05-06", |
|
description=( |
|
"You are a smart agent that can write and execute code and answer any questions provided access the given files and answer" |
|
), |
|
instruction = ( |
|
"if the question contains a file with .py ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. " |
|
"If the question contains a spreadsheet file like .xlsx and .csv among others, get the file and depending on the question and the file provided, execute the code and provide the final answer. " |
|
"use code like import pandas as pd , file = pd.read_csv('file.csv') and then use the file to answer the question. " |
|
"if the question contains a file with .txt ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. " |
|
"if the question contains a file with .json ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. " |
|
"If you are writing code or if you get a code file, use the code execution tool to run the code and provide the final answer. " |
|
) |
|
|
|
, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
search_agent = LlmAgent( |
|
name='searchgaiaAgent', |
|
model="gemini-2.5-pro-preview-05-06", |
|
description=( |
|
"You are a smart agent that can search the web and answer any questions provided access the given files and answer" |
|
), |
|
instruction = ( |
|
"Get the url associated perform a search and consolidate the information provided and answer the provided question " |
|
) |
|
|
|
, |
|
tools=[google_search], |
|
|
|
|
|
) |
|
|
|
image_agent = LlmAgent( |
|
name='imagegaiaAgent', |
|
model="gemini-2.5-pro-preview-05-06", |
|
description=( |
|
"You are a smart agent that can when given a image file and answer any questions related to it" |
|
), |
|
instruction = ( |
|
"Get the image file from the link associated in the prompt use Gemini to watch the video and answer the provided question ") |
|
|
|
, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
youtube_agent = LlmAgent( |
|
name='youtubegaiaAgent', |
|
model="gemini-2.5-pro-preview-05-06", |
|
description=( |
|
"You are a smart agent that can when given a youtube link watch it and answer any questions related to it" |
|
), |
|
instruction = ( |
|
"Get the youtube link associated use Gemini to watch the video and answer the provided question ") |
|
|
|
, |
|
|
|
|
|
|
|
) |
|
|
|
root_agent = LlmAgent( |
|
name='basegaiaAgent', |
|
model="gemini-2.5-pro-preview-05-06", |
|
description=( |
|
"You are a smart agent that can answer any questions provided access the given files and answer" |
|
), |
|
instruction = ( |
|
"You are a helpful agent. When the user asks to get the questions or makes a similar request, " |
|
"invoke your tool 'responses_api' to retrieve the questions data. " |
|
"The questions data will be a list of dictionaries, each containing 'task_id', 'question', and 'file_name' fields. " |
|
"For each question in the data: " |
|
"1. If file_name is not empty, the file can be accessed at https://agents-course-unit4-scoring.hf.space/files/{task_id} " |
|
"2. Use appropriate sub-agents based on question type (code_agent for coding, search_agent for web search, etc.) " |
|
"3. Provide a concise, direct answer for each question " |
|
"4. Return a dictionary with keys 'task_id' and 'submitted_answer' for each answer " |
|
"5. Collect all dictionaries in a list and pass to 'submit_api' tool to submit the answers. " |
|
"Always provide direct, factual answers without prefixes like 'The answer is:' or 'Final answer:'" |
|
) |
|
|
|
, |
|
tools=[responses_api,submit_api,agent_tool.AgentTool(agent = code_agent),\ |
|
agent_tool.AgentTool(agent = search_agent), agent_tool.AgentTool(youtube_agent), agent_tool.AgentTool(image_agent)], |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session_service = InMemorySessionService() |
|
|
|
try: |
|
session = session_service.create_session(app_name=APP_NAME, |
|
user_id=USER_ID, |
|
session_id=SESSION_ID) |
|
print(f"✅ Default session created: {SESSION_ID}") |
|
except Exception as e: |
|
print(f"⚠️ Error creating default session: {e}") |
|
session = None |
|
|
|
runner = Runner(agent=root_agent, app_name=APP_NAME, session_service=session_service) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|