-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add code snippets for Agent Engine #13735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yeesian
wants to merge
5
commits into
GoogleCloudPlatform:main
Choose a base branch
from
yeesian:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d91e47
Create quickstart_adk_example.py
yeesian 9c654f5
Update quickstart_adk_example.py with region tags
yeesian dfcecdb
Apply suggestions from code review
yeesian b52e0a5
Update quickstart_adk_example.py
yeesian 6c75f28
Update quickstart_adk_example.py
yeesian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| # [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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.