Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions generative_ai/reasoning_engine/quickstart_adk_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import vertexai
import os

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION = os.getenv("GOOGLE_CLOUD_LOCATION")

client = vertexai.Client(project=PROJECT_ID, location=LOCATION)

# [START agentengine_quickstart_adk_tool]
def get_exchange_rate(
currency_from: str = "USD",
currency_to: str = "EUR",
currency_date: str = "latest",
) -> dict:
"""Retrieves the exchange rate between two currencies on a specified date."""
import requests

response = requests.get(
f"https://api.frankfurter.app/{currency_date}",
params={"from": currency_from, "to": currency_to},
)
return response.json()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The HTTP request to the Frankfurter API does not handle potential errors, such as network issues or non-200 status codes. This could lead to unexpected behavior or crashes if the API is unavailable or returns an error. It's good practice to check for a successful response by adding response.raise_for_status() before this line.

# [END agentengine_quickstart_adk_tool]

async def quickstart_adk_example(staging_bucket: str) -> list:

# [START agentengine_quickstart_adk_agent_init]
from google.adk.agents import Agent
from vertexai import agent_engines

agent = Agent(
model="gemini-2.0-flash",
name="currency_exchange_agent",
tools=[get_exchange_rate],
)

app = agent_engines.AdkApp(agent=agent)
# [END agentengine_quickstart_adk_agent_init]

# [START agentengine_quickstart_adk_testlocally]
async for event in app.async_stream_query(
user_id="USER_ID",
message="What is the exchange rate from US dollars to SEK today?",
):
print(event)
# [END agentengine_quickstart_adk_testlocally]

# [START agentengine_quickstart_adk_deploy]
remote_agent = client.agent_engines.create(
agent=app,
config={
"requirements": ["google-cloud-aiplatform[agent_engines,adk]"],
"staging_bucket": staging_bucket,
}
)
# [END agentengine_quickstart_adk_deploy]

# [START agentengine_quickstart_adk_testremotely]
events = []
async for event in remote_agent.async_stream_query(
user_id="USER_ID",
message="What is the exchange rate from US dollars to SEK today?",
):
print(event)
events.append(event)
# [END agentengine_quickstart_adk_testremotely]

# [START agentengine_quickstart_adk_cleanup]
remote_agent.delete(force=True)
# [END agentengine_quickstart_adk_cleanup]

return events