|
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():
|
|
url = 'https://agents-course-unit4-scoring.hf.space/questions'
|
|
headers = {'accept': 'application/json'}
|
|
response = requests.get(url, headers=headers)
|
|
prompts = []
|
|
for i in response.json():
|
|
task_id = i['task_id']
|
|
if i['file_name'] != '':
|
|
url_file = f"https://agents-course-unit4-scoring.hf.space/files/{i['task_id']}"
|
|
question = i['question']
|
|
prompt = f"{task_id}:{question} and the file is {url_file}, give the final answer only"
|
|
else:
|
|
question = i['question']
|
|
prompt = f"{task_id}:{question} give the final answer only"
|
|
prompts.append(prompt)
|
|
return prompts
|
|
|
|
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. "
|
|
"Once you receive the list of questions, loop over each question and provide a concise answer for each based on the question and any provided file. "
|
|
"For every answer, return a dictionary with the keys task_id and submitted_answer, for example: "
|
|
"{'task_id': 'the-task-id', 'submitted_answer': 'your answer'}. "
|
|
"Collect all such dictionaries in a list (do not include any backslashes), and pass this list to the 'submit_api' tool to submit the answers."
|
|
)
|
|
|
|
,
|
|
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()
|
|
session = session_service.create_session(app_name=APP_NAME, \
|
|
user_id=USER_ID,\
|
|
session_id=SESSION_ID)
|
|
|
|
runner = Runner(agent=root_agent, app_name=APP_NAME, session_service=session_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|