mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-07-13 07:04:51 -06:00
Create a developer folder and add samples.
PiperOrigin-RevId: 755885332
This commit is contained in:
parent
180c2a934b
commit
a4adb739c0
14
.github/workflows/pyink.yml
vendored
14
.github/workflows/pyink.yml
vendored
@ -1,3 +1,17 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
name: Check Pyink Formatting
|
||||
|
||||
on:
|
||||
|
14
.github/workflows/python-unit-tests.yml
vendored
14
.github/workflows/python-unit-tests.yml
vendored
@ -1,3 +1,17 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
name: Python Unit Tests
|
||||
|
||||
on:
|
||||
|
39
contributing/samples/application_integration_agent/README.md
Normal file
39
contributing/samples/application_integration_agent/README.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Application Integration Agent Sample
|
||||
|
||||
## Introduction
|
||||
|
||||
This sample demonstrates how to use the `ApplicationIntegrationToolset` within an ADK agent to interact with external applications, specifically Jira in this case. The agent (`agent.py`) is configured to manage Jira issues using a pre-configured Application Integration connection.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **Set up Integration Connection:**
|
||||
* You need an existing [Integration connection](https://cloud.google.com/integration-connectors/docs/overview) configured to interact with your Jira instance. Follow the [documentation](https://google.github.io/adk-docs/tools/google-cloud-tools/#use-integration-connectors) to provision the Integration Connector in Google Cloud and then use this [documentation](https://cloud.google.com/integration-connectors/docs/connectors/jiracloud/configure) to create an JIRA connection. Note the `Connection Name`, `Project ID`, and `Location` of your connection.
|
||||
*
|
||||
|
||||
2. **Configure Environment Variables:**
|
||||
* Create a `.env` file in the same directory as `agent.py` (or add to your existing one).
|
||||
* Add the following variables to the `.env` file, replacing the placeholder values with your actual connection details:
|
||||
|
||||
```dotenv
|
||||
CONNECTION_NAME=<YOUR_JIRA_CONNECTION_NAME>
|
||||
CONNECTION_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT_ID>
|
||||
CONNECTION_LOCATION=<YOUR_CONNECTION_LOCATION>
|
||||
```
|
||||
|
||||
## How to Use
|
||||
|
||||
1. **Install Dependencies:** Ensure you have the necessary libraries installed (e.g., `google-adk`, `python-dotenv`).
|
||||
2. **Run the Agent:** Execute the agent script from your terminal:
|
||||
```bash
|
||||
python agent.py
|
||||
```
|
||||
3. **Interact:** Once the agent starts, you can interact with it by typing prompts related to Jira issue management.
|
||||
|
||||
## Sample Prompts
|
||||
|
||||
Here are some examples of how you can interact with the agent:
|
||||
|
||||
* `Can you list me all the issues ?`
|
||||
* `Can you list me all the projects ?`
|
||||
* `Can you create an issue: "Bug in product XYZ" in project ABC ?`
|
||||
|
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
50
contributing/samples/application_integration_agent/agent.py
Normal file
50
contributing/samples/application_integration_agent/agent.py
Normal file
@ -0,0 +1,50 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Sample agent using Application Integration toolset."""
|
||||
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from google.adk.agents.llm_agent import LlmAgent
|
||||
from google.adk.tools.application_integration_tool import ApplicationIntegrationToolset
|
||||
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
connection_name = os.getenv("CONNECTION_NAME")
|
||||
connection_project = os.getenv("CONNECTION_PROJECT")
|
||||
connection_location = os.getenv("CONNECTION_LOCATION")
|
||||
|
||||
|
||||
jira_tool = ApplicationIntegrationToolset(
|
||||
project=connection_project,
|
||||
location=connection_location,
|
||||
connection=connection_name,
|
||||
entity_operations={"Issues": [], "Projects": []},
|
||||
tool_name="jira_issue_manager",
|
||||
)
|
||||
|
||||
root_agent = LlmAgent(
|
||||
model="gemini-2.0-flash",
|
||||
name="Issue_Management_Agent",
|
||||
instruction="""
|
||||
You are an agent that helps manage issues in a JIRA instance.
|
||||
Be accurate in your responses based on the tool response. You can perform any formatting in the response that is appropriate or if asked by the user.
|
||||
If there is an error in the tool response, understand the error and try and see if you can fix the error and then and execute the tool again. For example if a variable or parameter is missing, try and see if you can find it in the request or user query or default it and then execute the tool again or check for other tools that could give you the details.
|
||||
If there are any math operations like count or max, min in the user request, call the tool to get the data and perform the math operations and then return the result in the response. For example for maximum, fetch the list and then do the math operation.
|
||||
""",
|
||||
tools=jira_tool.get_tools(),
|
||||
)
|
15
contributing/samples/artifact_save_text/__init__.py
Executable file
15
contributing/samples/artifact_save_text/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
41
contributing/samples/artifact_save_text/agent.py
Executable file
41
contributing/samples/artifact_save_text/agent.py
Executable file
@ -0,0 +1,41 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.tools.tool_context import ToolContext
|
||||
from google.genai import types
|
||||
|
||||
|
||||
async def log_query(tool_context: ToolContext, query: str):
|
||||
"""Roll a die with the specified number of sides."""
|
||||
await tool_context.save_artifact('query', types.Part(text=query))
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.0-flash-exp',
|
||||
name='log_agent',
|
||||
description='Log user query.',
|
||||
instruction="""Always log the user query and reploy "kk, I've logged."
|
||||
""",
|
||||
tools=[log_query],
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
51
contributing/samples/bigquery_agent/README.md
Normal file
51
contributing/samples/bigquery_agent/README.md
Normal file
@ -0,0 +1,51 @@
|
||||
# OAuth Sample
|
||||
|
||||
## Introduction
|
||||
|
||||
This sample tests and demos the OAuth support in ADK via two tools:
|
||||
|
||||
* 1. bigquery_datasets_list:
|
||||
|
||||
List user's datasets.
|
||||
|
||||
* 2. bigquery_datasets_get:
|
||||
Get a dataset's details.
|
||||
|
||||
* 3. bigquery_datasets_insert:
|
||||
Create a new dataset.
|
||||
|
||||
* 4. bigquery_tables_list:
|
||||
List all tables in a dataset.
|
||||
|
||||
* 5. bigquery_tables_get:
|
||||
Get a table's details.
|
||||
|
||||
* 6. bigquery_tables_insert:
|
||||
Insert a new table into a dataset.
|
||||
|
||||
## How to use
|
||||
|
||||
* 1. Follow https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name. to get your client id and client secret.
|
||||
Be sure to choose "web" as your client type.
|
||||
|
||||
* 2. Configure your .env file to add two variables:
|
||||
|
||||
* GOOGLE_CLIENT_ID={your client id}
|
||||
* GOOGLE_CLIENT_SECRET={your client secret}
|
||||
|
||||
Note: done't create a separate .env , instead put it to the same .env file that stores your Vertex AI or Dev ML credentials
|
||||
|
||||
* 3. Follow https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred to add http://localhost/dev-ui to "Authorized redirect URIs".
|
||||
|
||||
Note: localhost here is just a hostname that you use to access the dev ui, replace it with the actual hostname you use to access the dev ui.
|
||||
|
||||
* 4. For 1st run, allow popup for localhost in Chrome.
|
||||
|
||||
## Sample prompt
|
||||
|
||||
* `Do I have any datasets in project sean-dev-agent ?`
|
||||
* `Do I have any tables under it ?`
|
||||
* `could you get me the details of this table ?`
|
||||
* `Can you help to create a new dataset in the same project? id : sean_test , location: us`
|
||||
* `could you show me the details of this new dataset ?`
|
||||
* `could you create a new table under this dataset ? table name : sean_test_table. column1 : name is id , type is integer, required. column2 : name is info , type is string, required. column3 : name is backup , type is string, optional.`
|
15
contributing/samples/bigquery_agent/__init__.py
Normal file
15
contributing/samples/bigquery_agent/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
84
contributing/samples/bigquery_agent/agent.py
Normal file
84
contributing/samples/bigquery_agent/agent.py
Normal file
@ -0,0 +1,84 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from google.adk import Agent
|
||||
from google.adk.tools.google_api_tool import bigquery_tool_set
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
# Access the variable
|
||||
oauth_client_id = os.getenv("OAUTH_CLIENT_ID")
|
||||
oauth_client_secret = os.getenv("OAUTH_CLIENT_SECRET")
|
||||
bigquery_tool_set.configure_auth(oauth_client_id, oauth_client_secret)
|
||||
|
||||
bigquery_datasets_list = bigquery_tool_set.get_tool("bigquery_datasets_list")
|
||||
bigquery_datasets_get = bigquery_tool_set.get_tool("bigquery_datasets_get")
|
||||
bigquery_datasets_insert = bigquery_tool_set.get_tool(
|
||||
"bigquery_datasets_insert"
|
||||
)
|
||||
|
||||
bigquery_tables_list = bigquery_tool_set.get_tool("bigquery_tables_list")
|
||||
bigquery_tables_get = bigquery_tool_set.get_tool("bigquery_tables_get")
|
||||
bigquery_tables_insert = bigquery_tool_set.get_tool("bigquery_tables_insert")
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model="gemini-2.0-flash",
|
||||
name="bigquery_agent",
|
||||
instruction="""
|
||||
You are a helpful Google BigQuery agent that help to manage users' data on Goolge BigQuery.
|
||||
Use the provided tools to conduct various operations on users' data in Google BigQuery.
|
||||
|
||||
Scenario 1:
|
||||
The user wants to query their biguqery datasets
|
||||
Use bigquery_datasets_list to query user's datasets
|
||||
|
||||
Scenario 2:
|
||||
The user wants to query the details of a specific dataset
|
||||
Use bigquery_datasets_get to get a dataset's details
|
||||
|
||||
Scenario 3:
|
||||
The user wants to create a new dataset
|
||||
Use bigquery_datasets_insert to create a new dataset
|
||||
|
||||
Scenario 4:
|
||||
The user wants to query their tables in a specific dataset
|
||||
Use bigquery_tables_list to list all tables in a dataset
|
||||
|
||||
Scenario 5:
|
||||
The user wants to query the details of a specific table
|
||||
Use bigquery_tables_get to get a table's details
|
||||
|
||||
Scenario 6:
|
||||
The user wants to insert a new table into a dataset
|
||||
Use bigquery_tables_insert to insert a new table into a dataset
|
||||
|
||||
Current user:
|
||||
<User>
|
||||
{userInfo?}
|
||||
</User>
|
||||
""",
|
||||
tools=[
|
||||
bigquery_datasets_list,
|
||||
bigquery_datasets_get,
|
||||
bigquery_datasets_insert,
|
||||
bigquery_tables_list,
|
||||
bigquery_tables_get,
|
||||
bigquery_tables_insert,
|
||||
],
|
||||
)
|
15
contributing/samples/code_execution/__init__.py
Normal file
15
contributing/samples/code_execution/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
100
contributing/samples/code_execution/agent.py
Normal file
100
contributing/samples/code_execution/agent.py
Normal file
@ -0,0 +1,100 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Data science agent."""
|
||||
|
||||
from google.adk.agents.llm_agent import Agent
|
||||
from google.adk.tools import built_in_code_execution
|
||||
|
||||
|
||||
def base_system_instruction():
|
||||
"""Returns: data science agent system instruction."""
|
||||
|
||||
return """
|
||||
# Guidelines
|
||||
|
||||
**Objective:** Assist the user in achieving their data analysis goals within the context of a Python Colab notebook, **with emphasis on avoiding assumptions and ensuring accuracy.** Reaching that goal can involve multiple steps. When you need to generate code, you **don't** need to solve the goal in one go. Only generate the next step at a time.
|
||||
|
||||
**Code Execution:** All code snippets provided will be executed within the Colab environment.
|
||||
|
||||
**Statefulness:** All code snippets are executed and the variables stays in the environment. You NEVER need to re-initialize variables. You NEVER need to reload files. You NEVER need to re-import libraries.
|
||||
|
||||
**Imported Libraries:** The following libraries are ALREADY imported and should NEVER be imported again:
|
||||
|
||||
```tool_code
|
||||
import io
|
||||
import math
|
||||
import re
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import scipy
|
||||
```
|
||||
|
||||
**Output Visibility:** Always print the output of code execution to visualize results, especially for data exploration and analysis. For example:
|
||||
- To look a the shape of a pandas.DataFrame do:
|
||||
```tool_code
|
||||
print(df.shape)
|
||||
```
|
||||
The output will be presented to you as:
|
||||
```tool_outputs
|
||||
(49, 7)
|
||||
|
||||
```
|
||||
- To display the result of a numerical computation:
|
||||
```tool_code
|
||||
x = 10 ** 9 - 12 ** 5
|
||||
print(f'{{x=}}')
|
||||
```
|
||||
The output will be presented to you as:
|
||||
```tool_outputs
|
||||
x=999751168
|
||||
|
||||
```
|
||||
- You **never** generate ```tool_outputs yourself.
|
||||
- You can then use this output to decide on next steps.
|
||||
- Print just variables (e.g., `print(f'{{variable=}}')`.
|
||||
|
||||
**No Assumptions:** **Crucially, avoid making assumptions about the nature of the data or column names.** Base findings solely on the data itself. Always use the information obtained from `explore_df` to guide your analysis.
|
||||
|
||||
**Available files:** Only use the files that are available as specified in the list of available files.
|
||||
|
||||
**Data in prompt:** Some queries contain the input data directly in the prompt. You have to parse that data into a pandas DataFrame. ALWAYS parse all the data. NEVER edit the data that are given to you.
|
||||
|
||||
**Answerability:** Some queries may not be answerable with the available data. In those cases, inform the user why you cannot process their query and suggest what type of data would be needed to fulfill their request.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model="gemini-2.0-flash-001",
|
||||
name="data_science_agent",
|
||||
instruction=base_system_instruction() + """
|
||||
|
||||
|
||||
You need to assist the user with their queries by looking at the data and the context in the conversation.
|
||||
You final answer should summarize the code and code execution relavant to the user query.
|
||||
|
||||
You should include all pieces of data to answer the user query, such as the table from code execution results.
|
||||
If you cannot answer the question directly, you should follow the guidelines above to generate the next step.
|
||||
If the question can be answered directly with writing any code, you should do that.
|
||||
If you doesn't have enough data to answer the question, you should ask for clarification from the user.
|
||||
|
||||
You should NEVER install any package on your own like `pip install ...`.
|
||||
When plotting trends, you should make sure to sort and order the data by the x-axis.
|
||||
|
||||
|
||||
""",
|
||||
tools=[built_in_code_execution],
|
||||
)
|
15
contributing/samples/fields_output_schema/__init__.py
Normal file
15
contributing/samples/fields_output_schema/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
48
contributing/samples/fields_output_schema/agent.py
Normal file
48
contributing/samples/fields_output_schema/agent.py
Normal file
@ -0,0 +1,48 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.adk import Agent
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class WeahterData(BaseModel):
|
||||
temperature: str
|
||||
humidity: str
|
||||
wind_speed: str
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
name='root_agent',
|
||||
model='gemini-2.0-flash',
|
||||
instruction="""\
|
||||
Answer user's questions based on the data you have.
|
||||
|
||||
If you don't have the data, you can just say you don't know.
|
||||
|
||||
Here are the data you have for San Jose
|
||||
|
||||
* temperature: 26 C
|
||||
* humidity: 20%
|
||||
* wind_speed: 29 mph
|
||||
|
||||
Here are the data you have for Cupertino
|
||||
|
||||
* temperature: 16 C
|
||||
* humidity: 10%
|
||||
* wind_speed: 13 mph
|
||||
|
||||
""",
|
||||
output_schema=WeahterData,
|
||||
output_key='weather_data',
|
||||
)
|
15
contributing/samples/fields_planner/__init__.py
Executable file
15
contributing/samples/fields_planner/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
112
contributing/samples/fields_planner/agent.py
Executable file
112
contributing/samples/fields_planner/agent.py
Executable file
@ -0,0 +1,112 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.planners import BuiltInPlanner
|
||||
from google.adk.planners import PlanReActPlanner
|
||||
from google.adk.tools.tool_context import ToolContext
|
||||
from google.genai import types
|
||||
|
||||
|
||||
def roll_die(sides: int, tool_context: ToolContext) -> int:
|
||||
"""Roll a die and return the rolled result.
|
||||
|
||||
Args:
|
||||
sides: The integer number of sides the die has.
|
||||
|
||||
Returns:
|
||||
An integer of the result of rolling the die.
|
||||
"""
|
||||
result = random.randint(1, sides)
|
||||
if not 'rolls' in tool_context.state:
|
||||
tool_context.state['rolls'] = []
|
||||
|
||||
tool_context.state['rolls'] = tool_context.state['rolls'] + [result]
|
||||
return result
|
||||
|
||||
|
||||
async def check_prime(nums: list[int]) -> str:
|
||||
"""Check if a given list of numbers are prime.
|
||||
|
||||
Args:
|
||||
nums: The list of numbers to check.
|
||||
|
||||
Returns:
|
||||
A str indicating which number is prime.
|
||||
"""
|
||||
primes = set()
|
||||
for number in nums:
|
||||
number = int(number)
|
||||
if number <= 1:
|
||||
continue
|
||||
is_prime = True
|
||||
for i in range(2, int(number**0.5) + 1):
|
||||
if number % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.add(number)
|
||||
return (
|
||||
'No prime numbers found.'
|
||||
if not primes
|
||||
else f"{', '.join(str(num) for num in primes)} are prime numbers."
|
||||
)
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.5-pro-preview-03-25',
|
||||
# model='gemini-2.0-flash',
|
||||
name='data_processing_agent',
|
||||
description=(
|
||||
'hello world agent that can roll a dice of 8 sides and check prime'
|
||||
' numbers.'
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
You can roll dice of different sizes.
|
||||
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
|
||||
It is ok to discuss previous dice roles, and comment on the dice rolls.
|
||||
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
|
||||
You should never roll a die on your own.
|
||||
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
|
||||
You should not check prime numbers before calling the tool.
|
||||
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
|
||||
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
|
||||
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
|
||||
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
|
||||
3. When you respond, you must include the roll_die result from step 1.
|
||||
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
|
||||
You should not rely on the previous history on prime results.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
planner=BuiltInPlanner(
|
||||
thinking_config=types.ThinkingConfig(
|
||||
include_thoughts=True,
|
||||
),
|
||||
),
|
||||
# planner=PlanReActPlanner(),
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
112
contributing/samples/fields_planner/asyncio_run.py
Executable file
112
contributing/samples/fields_planner/asyncio_run.py
Executable file
@ -0,0 +1,112 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
import warnings
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk import Runner
|
||||
from google.adk.artifacts import InMemoryArtifactService
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.adk.sessions import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(app_name, user_id_1)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
await run_prompt(session_11, 'Hi')
|
||||
await run_prompt(session_11, 'Roll a die.')
|
||||
await run_prompt(session_11, 'Roll a die again.')
|
||||
await run_prompt(session_11, 'What numbers did I got?')
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
def main_sync():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(app_name, user_id_1)
|
||||
|
||||
def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
for event in runner.run_sync(
|
||||
session=session,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
run_prompt(session_11, 'Hi')
|
||||
run_prompt(session_11, 'Roll a die.')
|
||||
run_prompt(session_11, 'Roll a die again.')
|
||||
run_prompt(session_11, 'What numbers did I got?')
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
main_sync()
|
15
contributing/samples/generate_image/__init__.py
Normal file
15
contributing/samples/generate_image/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
54
contributing/samples/generate_image/agent.py
Normal file
54
contributing/samples/generate_image/agent.py
Normal file
@ -0,0 +1,54 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.genai import Client
|
||||
from google.genai import types
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.tools import load_artifacts
|
||||
from google.adk.tools import ToolContext
|
||||
|
||||
# Only Vertex AI supports image generation for now.
|
||||
client = Client()
|
||||
|
||||
|
||||
async def generate_image(prompt: str, tool_context: 'ToolContext'):
|
||||
"""Generates an image based on the prompt."""
|
||||
response = client.models.generate_images(
|
||||
model='imagen-3.0-generate-002',
|
||||
prompt=prompt,
|
||||
config={'number_of_images': 1},
|
||||
)
|
||||
if not response.generated_images:
|
||||
return {'status': 'failed'}
|
||||
image_bytes = response.generated_images[0].image.image_bytes
|
||||
await tool_context.save_artifact(
|
||||
'image.png',
|
||||
types.Part.from_bytes(data=image_bytes, mime_type='image/png'),
|
||||
)
|
||||
return {
|
||||
'status': 'success',
|
||||
'detail': 'Image generated successfully and stored in artifacts.',
|
||||
'filename': 'image.png',
|
||||
}
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.0-flash-001',
|
||||
name='root_agent',
|
||||
description="""An agent that generates images and answer questions about the images.""",
|
||||
instruction="""You are an agent whose job is to generate or edit an image based on the user's prompt.
|
||||
""",
|
||||
tools=[generate_image, load_artifacts],
|
||||
)
|
698
contributing/samples/generate_image/sample.session.json
Normal file
698
contributing/samples/generate_image/sample.session.json
Normal file
File diff suppressed because one or more lines are too long
15
contributing/samples/hello_world/__init__.py
Executable file
15
contributing/samples/hello_world/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
134
contributing/samples/hello_world/agent.py
Executable file
134
contributing/samples/hello_world/agent.py
Executable file
@ -0,0 +1,134 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.planners import BuiltInPlanner
|
||||
from google.adk.planners import PlanReActPlanner
|
||||
from google.adk.tools.tool_context import ToolContext
|
||||
from google.genai import types
|
||||
|
||||
|
||||
def roll_die(sides: int, tool_context: ToolContext) -> int:
|
||||
"""Roll a die and return the rolled result.
|
||||
|
||||
Args:
|
||||
sides: The integer number of sides the die has.
|
||||
|
||||
Returns:
|
||||
An integer of the result of rolling the die.
|
||||
"""
|
||||
result = random.randint(1, sides)
|
||||
if not 'rolls' in tool_context.state:
|
||||
tool_context.state['rolls'] = []
|
||||
|
||||
tool_context.state['rolls'] = tool_context.state['rolls'] + [result]
|
||||
return result
|
||||
|
||||
|
||||
async def check_prime(nums: list[int]) -> str:
|
||||
"""Check if a given list of numbers are prime.
|
||||
|
||||
Args:
|
||||
nums: The list of numbers to check.
|
||||
|
||||
Returns:
|
||||
A str indicating which number is prime.
|
||||
"""
|
||||
primes = set()
|
||||
for number in nums:
|
||||
number = int(number)
|
||||
if number <= 1:
|
||||
continue
|
||||
is_prime = True
|
||||
for i in range(2, int(number**0.5) + 1):
|
||||
if number % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.add(number)
|
||||
return (
|
||||
'No prime numbers found.'
|
||||
if not primes
|
||||
else f"{', '.join(str(num) for num in primes)} are prime numbers."
|
||||
)
|
||||
|
||||
|
||||
async def before_agent_callback(callback_context):
|
||||
print('@before_agent_callback')
|
||||
return None
|
||||
|
||||
|
||||
async def after_agent_callback(callback_context):
|
||||
print('@after_agent_callback')
|
||||
return None
|
||||
|
||||
|
||||
async def before_model_callback(callback_context, llm_request):
|
||||
print('@before_model_callback')
|
||||
return None
|
||||
|
||||
|
||||
async def after_model_callback(callback_context, llm_response):
|
||||
print('@after_model_callback')
|
||||
return None
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.0-flash-exp',
|
||||
name='data_processing_agent',
|
||||
description=(
|
||||
'hello world agent that can roll a dice of 8 sides and check prime'
|
||||
' numbers.'
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
You can roll dice of different sizes.
|
||||
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
|
||||
It is ok to discuss previous dice roles, and comment on the dice rolls.
|
||||
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
|
||||
You should never roll a die on your own.
|
||||
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
|
||||
You should not check prime numbers before calling the tool.
|
||||
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
|
||||
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
|
||||
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
|
||||
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
|
||||
3. When you respond, you must include the roll_die result from step 1.
|
||||
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
|
||||
You should not rely on the previous history on prime results.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
# planner=BuiltInPlanner(
|
||||
# thinking_config=types.ThinkingConfig(
|
||||
# include_thoughts=True,
|
||||
# ),
|
||||
# ),
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
before_agent_callback=before_agent_callback,
|
||||
after_agent_callback=after_agent_callback,
|
||||
before_model_callback=before_model_callback,
|
||||
after_model_callback=after_model_callback,
|
||||
)
|
145
contributing/samples/hello_world/asyncio_run.py
Executable file
145
contributing/samples/hello_world/asyncio_run.py
Executable file
@ -0,0 +1,145 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
import warnings
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk import Runner
|
||||
from google.adk.agents.run_config import RunConfig
|
||||
from google.adk.artifacts import InMemoryArtifactService
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.adk.sessions import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
async def run_prompt_bytes(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user',
|
||||
parts=[
|
||||
types.Part.from_bytes(
|
||||
data=str.encode(new_message), mime_type='text/plain'
|
||||
)
|
||||
],
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
run_config=RunConfig(save_input_blobs_as_artifacts=True),
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
await run_prompt(session_11, 'Hi')
|
||||
await run_prompt(session_11, 'Roll a die with 100 sides')
|
||||
await run_prompt(session_11, 'Roll a die again.')
|
||||
await run_prompt(session_11, 'What numbers did I got?')
|
||||
await run_prompt_bytes(session_11, 'Hi bytes')
|
||||
print(
|
||||
await artifact_service.list_artifact_keys(
|
||||
app_name=app_name, user_id=user_id_1, session_id=session_11.id
|
||||
)
|
||||
)
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
def main_sync():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
|
||||
def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
for event in runner.run(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
run_prompt(session_11, 'Hi')
|
||||
run_prompt(session_11, 'Roll a die with 100 sides.')
|
||||
run_prompt(session_11, 'Roll a die again.')
|
||||
run_prompt(session_11, 'What numbers did I got?')
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('--------------ASYNC--------------------')
|
||||
asyncio.run(main())
|
||||
print('--------------SYNC--------------------')
|
||||
main_sync()
|
16
contributing/samples/hello_world_litellm/__init__.py
Normal file
16
contributing/samples/hello_world_litellm/__init__.py
Normal file
@ -0,0 +1,16 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from . import agent
|
94
contributing/samples/hello_world_litellm/agent.py
Normal file
94
contributing/samples/hello_world_litellm/agent.py
Normal file
@ -0,0 +1,94 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import random
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.models.lite_llm import LiteLlm
|
||||
|
||||
|
||||
def roll_die(sides: int) -> int:
|
||||
"""Roll a die and return the rolled result.
|
||||
|
||||
Args:
|
||||
sides: The integer number of sides the die has.
|
||||
|
||||
Returns:
|
||||
An integer of the result of rolling the die.
|
||||
"""
|
||||
return random.randint(1, sides)
|
||||
|
||||
|
||||
async def check_prime(nums: list[int]) -> str:
|
||||
"""Check if a given list of numbers are prime.
|
||||
|
||||
Args:
|
||||
nums: The list of numbers to check.
|
||||
|
||||
Returns:
|
||||
A str indicating which number is prime.
|
||||
"""
|
||||
primes = set()
|
||||
for number in nums:
|
||||
number = int(number)
|
||||
if number <= 1:
|
||||
continue
|
||||
is_prime = True
|
||||
for i in range(2, int(number**0.5) + 1):
|
||||
if number % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.add(number)
|
||||
return (
|
||||
"No prime numbers found."
|
||||
if not primes
|
||||
else f"{', '.join(str(num) for num in primes)} are prime numbers."
|
||||
)
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
# model=LiteLlm(model="gemini/gemini-2.5-pro-exp-03-25"),
|
||||
# model=LiteLlm(model="vertex_ai/gemini-2.5-pro-exp-03-25"),
|
||||
# model=LiteLlm(model="vertex_ai/claude-3-5-haiku"),
|
||||
model=LiteLlm(model="openai/gpt-4o"),
|
||||
# model=LiteLlm(model="anthropic/claude-3-sonnet-20240229"),
|
||||
name="data_processing_agent",
|
||||
description=(
|
||||
"hello world agent that can roll a dice of 8 sides and check prime"
|
||||
" numbers."
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
You can roll dice of different sizes.
|
||||
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
|
||||
It is ok to discuss previous dice roles, and comment on the dice rolls.
|
||||
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
|
||||
You should never roll a die on your own.
|
||||
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
|
||||
You should not check prime numbers before calling the tool.
|
||||
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
|
||||
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
|
||||
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
|
||||
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
|
||||
3. When you respond, you must include the roll_die result from step 1.
|
||||
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
|
||||
You should not rely on the previous history on prime results.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
)
|
78
contributing/samples/hello_world_litellm/asyncio_run.py
Normal file
78
contributing/samples/hello_world_litellm/asyncio_run.py
Normal file
@ -0,0 +1,78 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
import warnings
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk import Runner
|
||||
from google.adk.artifacts import InMemoryArtifactService
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.adk.sessions import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
await run_prompt(session_11, 'Hi, introduce yourself.')
|
||||
await run_prompt(
|
||||
session_11, 'Roll a die with 100 sides and check if it is prime'
|
||||
)
|
||||
await run_prompt(session_11, 'Roll it again.')
|
||||
await run_prompt(session_11, 'What numbers did I got?')
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
15
contributing/samples/hello_world_ma/__init__.py
Executable file
15
contributing/samples/hello_world_ma/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
153
contributing/samples/hello_world_ma/agent.py
Executable file
153
contributing/samples/hello_world_ma/agent.py
Executable file
@ -0,0 +1,153 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from google.adk.agents import Agent
|
||||
from google.adk.tools.example_tool import ExampleTool
|
||||
from google.genai import types
|
||||
|
||||
|
||||
# --- Roll Die Sub-Agent ---
|
||||
def roll_die(sides: int) -> int:
|
||||
"""Roll a die and return the rolled result."""
|
||||
return random.randint(1, sides)
|
||||
|
||||
|
||||
roll_agent = Agent(
|
||||
name="roll_agent",
|
||||
description="Handles rolling dice of different sizes.",
|
||||
instruction="""
|
||||
You are responsible for rolling dice based on the user's request.
|
||||
When asked to roll a die, you must call the roll_die tool with the number of sides as an integer.
|
||||
""",
|
||||
tools=[roll_die],
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
# --- Prime Check Sub-Agent ---
|
||||
def check_prime(nums: list[int]) -> str:
|
||||
"""Check if a given list of numbers are prime."""
|
||||
primes = set()
|
||||
for number in nums:
|
||||
number = int(number)
|
||||
if number <= 1:
|
||||
continue
|
||||
is_prime = True
|
||||
for i in range(2, int(number**0.5) + 1):
|
||||
if number % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.add(number)
|
||||
return (
|
||||
"No prime numbers found."
|
||||
if not primes
|
||||
else f"{', '.join(str(num) for num in primes)} are prime numbers."
|
||||
)
|
||||
|
||||
|
||||
example_tool = ExampleTool([
|
||||
{
|
||||
"input": {
|
||||
"role": "user",
|
||||
"parts": [{"text": "Roll a 6-sided die."}],
|
||||
},
|
||||
"output": [
|
||||
{"role": "model", "parts": [{"text": "I rolled a 4 for you."}]}
|
||||
],
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"role": "user",
|
||||
"parts": [{"text": "Is 7 a prime number?"}],
|
||||
},
|
||||
"output": [{
|
||||
"role": "model",
|
||||
"parts": [{"text": "Yes, 7 is a prime number."}],
|
||||
}],
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"role": "user",
|
||||
"parts": [{"text": "Roll a 10-sided die and check if it's prime."}],
|
||||
},
|
||||
"output": [
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [{"text": "I rolled an 8 for you."}],
|
||||
},
|
||||
{
|
||||
"role": "model",
|
||||
"parts": [{"text": "8 is not a prime number."}],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
prime_agent = Agent(
|
||||
name="prime_agent",
|
||||
description="Handles checking if numbers are prime.",
|
||||
instruction="""
|
||||
You are responsible for checking whether numbers are prime.
|
||||
When asked to check primes, you must call the check_prime tool with a list of integers.
|
||||
Never attempt to determine prime numbers manually.
|
||||
Return the prime number results to the root agent.
|
||||
""",
|
||||
tools=[check_prime],
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model="gemini-1.5-flash",
|
||||
name="root_agent",
|
||||
instruction="""
|
||||
You are a helpful assistant that can roll dice and check if numbers are prime.
|
||||
You delegate rolling dice tasks to the roll_agent and prime checking tasks to the prime_agent.
|
||||
Follow these steps:
|
||||
1. If the user asks to roll a die, delegate to the roll_agent.
|
||||
2. If the user asks to check primes, delegate to the prime_agent.
|
||||
3. If the user asks to roll a die and then check if the result is prime, call roll_agent first, then pass the result to prime_agent.
|
||||
Always clarify the results before proceeding.
|
||||
""",
|
||||
global_instruction=(
|
||||
"You are DicePrimeBot, ready to roll dice and check prime numbers."
|
||||
),
|
||||
sub_agents=[roll_agent, prime_agent],
|
||||
tools=[example_tool],
|
||||
generate_content_config=types.GenerateContentConfig(
|
||||
safety_settings=[
|
||||
types.SafetySetting( # avoid false alarm about rolling dice.
|
||||
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold=types.HarmBlockThreshold.OFF,
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
115
contributing/samples/hello_world_ollama/README.md
Normal file
115
contributing/samples/hello_world_ollama/README.md
Normal file
@ -0,0 +1,115 @@
|
||||
# Using ollama models with ADK
|
||||
|
||||
## Model choice
|
||||
|
||||
If your agent is relying on tools, please make sure that you select a model with tool support from [ollama website](https://ollama.com/search?c=tools).
|
||||
|
||||
For reliable results, we recommend using a decent size model with tool support.
|
||||
|
||||
The tool support for the model can be checked with the following command:
|
||||
|
||||
```bash
|
||||
ollama show mistral-small3.1
|
||||
Model
|
||||
architecture mistral3
|
||||
parameters 24.0B
|
||||
context length 131072
|
||||
embedding length 5120
|
||||
quantization Q4_K_M
|
||||
|
||||
Capabilities
|
||||
completion
|
||||
vision
|
||||
tools
|
||||
```
|
||||
|
||||
You are supposed to see `tools` listed under capabilities.
|
||||
|
||||
You can also look at the template the model is using and tweak it based on your needs.
|
||||
|
||||
```bash
|
||||
ollama show --modelfile llama3.1 > model_file_to_modify
|
||||
```
|
||||
|
||||
Then you can create a model with the following command:
|
||||
|
||||
```bash
|
||||
ollama create llama3.1-modified -f model_file_to_modify
|
||||
```
|
||||
|
||||
## Using ollama_chat provider
|
||||
|
||||
Our LiteLlm wrapper can be used to create agents with ollama models.
|
||||
|
||||
```py
|
||||
root_agent = Agent(
|
||||
model=LiteLlm(model="ollama_chat/mistral-small3.1"),
|
||||
name="dice_agent",
|
||||
description=(
|
||||
"hello world agent that can roll a dice of 8 sides and check prime"
|
||||
" numbers."
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
**It is important to set the provider `ollama_chat` instead of `ollama`. Using `ollama` will result in unexpected behaviors such as infinite tool call loops and ignoring previous context.**
|
||||
|
||||
While `api_base` can be provided inside litellm for generation, litellm library is calling other APIs relying on the env variable instead as of v1.65.5 after completion. So at this time, we recommend setting the env variable `OLLAMA_API_BASE` to point to the ollama server.
|
||||
|
||||
```bash
|
||||
export OLLAMA_API_BASE="http://localhost:11434"
|
||||
adk web
|
||||
```
|
||||
|
||||
## Using openai provider
|
||||
|
||||
Alternatively, `openai` can be used as the provider name. But this will also require setting the `OPENAI_API_BASE=http://localhost:11434/v1` and `OPENAI_API_KEY=anything` env variables instead of `OLLAMA_API_BASE`. **Please notice that api base now has `/v1` at the end.**
|
||||
|
||||
```py
|
||||
root_agent = Agent(
|
||||
model=LiteLlm(model="openai/mistral-small3.1"),
|
||||
name="dice_agent",
|
||||
description=(
|
||||
"hello world agent that can roll a dice of 8 sides and check prime"
|
||||
" numbers."
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
```bash
|
||||
export OPENAI_API_BASE=http://localhost:11434/v1
|
||||
export OPENAI_API_KEY=anything
|
||||
adk web
|
||||
```
|
||||
|
||||
## Debugging
|
||||
|
||||
You can see the request sent to the ollama server by adding the following in your agent code just after imports.
|
||||
|
||||
```py
|
||||
import litellm
|
||||
litellm._turn_on_debug()
|
||||
```
|
||||
|
||||
Look for a line like the following:
|
||||
|
||||
```bash
|
||||
quest Sent from LiteLLM:
|
||||
curl -X POST \
|
||||
http://localhost:11434/api/chat \
|
||||
-d '{'model': 'mistral-small3.1', 'messages': [{'role': 'system', 'content': ...
|
||||
```
|
15
contributing/samples/hello_world_ollama/__init__.py
Executable file
15
contributing/samples/hello_world_ollama/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
89
contributing/samples/hello_world_ollama/agent.py
Executable file
89
contributing/samples/hello_world_ollama/agent.py
Executable file
@ -0,0 +1,89 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from google.adk.agents import Agent
|
||||
from google.adk.models.lite_llm import LiteLlm
|
||||
|
||||
|
||||
def roll_die(sides: int) -> int:
|
||||
"""Roll a die and return the rolled result.
|
||||
|
||||
Args:
|
||||
sides: The integer number of sides the die has.
|
||||
|
||||
Returns:
|
||||
An integer of the result of rolling the die.
|
||||
"""
|
||||
return random.randint(1, sides)
|
||||
|
||||
|
||||
def check_prime(numbers: list[int]) -> str:
|
||||
"""Check if a given list of numbers are prime.
|
||||
|
||||
Args:
|
||||
numbers: The list of numbers to check.
|
||||
|
||||
Returns:
|
||||
A str indicating which number is prime.
|
||||
"""
|
||||
primes = set()
|
||||
for number in numbers:
|
||||
number = int(number)
|
||||
if number <= 1:
|
||||
continue
|
||||
is_prime = True
|
||||
for i in range(2, int(number**0.5) + 1):
|
||||
if number % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.add(number)
|
||||
return (
|
||||
"No prime numbers found."
|
||||
if not primes
|
||||
else f"{', '.join(str(num) for num in primes)} are prime numbers."
|
||||
)
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model=LiteLlm(model="ollama_chat/mistral-small3.1"),
|
||||
name="dice_roll_agent",
|
||||
description=(
|
||||
"hello world agent that can roll a dice of any number of sides and"
|
||||
" check prime numbers."
|
||||
),
|
||||
instruction="""
|
||||
You roll dice and answer questions about the outcome of the dice rolls.
|
||||
You can roll dice of different sizes.
|
||||
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
|
||||
It is ok to discuss previous dice roles, and comment on the dice rolls.
|
||||
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
|
||||
You should never roll a die on your own.
|
||||
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
|
||||
You should not check prime numbers before calling the tool.
|
||||
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
|
||||
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
|
||||
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
|
||||
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
|
||||
3. When you respond, you must include the roll_die result from step 1.
|
||||
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
|
||||
You should not rely on the previous history on prime results.
|
||||
""",
|
||||
tools=[
|
||||
roll_die,
|
||||
check_prime,
|
||||
],
|
||||
)
|
77
contributing/samples/hello_world_ollama/asyncio_run.py
Executable file
77
contributing/samples/hello_world_ollama/asyncio_run.py
Executable file
@ -0,0 +1,77 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
import warnings
|
||||
|
||||
import agent
|
||||
from dotenv import load_dotenv
|
||||
from google.adk import Runner
|
||||
from google.adk.artifacts import InMemoryArtifactService
|
||||
from google.adk.cli.utils import logs
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.adk.sessions import Session
|
||||
from google.genai import types
|
||||
|
||||
load_dotenv(override=True)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
logs.log_to_tmp_folder()
|
||||
|
||||
|
||||
async def main():
|
||||
app_name = 'my_app'
|
||||
user_id_1 = 'user1'
|
||||
session_service = InMemorySessionService()
|
||||
artifact_service = InMemoryArtifactService()
|
||||
runner = Runner(
|
||||
app_name=app_name,
|
||||
agent=agent.root_agent,
|
||||
artifact_service=artifact_service,
|
||||
session_service=session_service,
|
||||
)
|
||||
session_11 = session_service.create_session(
|
||||
app_name=app_name, user_id=user_id_1
|
||||
)
|
||||
|
||||
async def run_prompt(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
print('------------------------------------')
|
||||
await run_prompt(session_11, 'Hi, introduce yourself.')
|
||||
await run_prompt(
|
||||
session_11, 'Roll a die with 100 sides and check if it is prime'
|
||||
)
|
||||
await run_prompt(session_11, 'Roll it again.')
|
||||
await run_prompt(session_11, 'What numbers did I got?')
|
||||
end_time = time.time()
|
||||
print('------------------------------------')
|
||||
print('End time:', end_time)
|
||||
print('Total time:', end_time - start_time)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
15
contributing/samples/human_in_loop/__init__.py
Normal file
15
contributing/samples/human_in_loop/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
50
contributing/samples/human_in_loop/agent.py
Normal file
50
contributing/samples/human_in_loop/agent.py
Normal file
@ -0,0 +1,50 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from typing import Any
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.tools import ToolContext
|
||||
from google.adk.tools.long_running_tool import LongRunningFunctionTool
|
||||
from google.genai import types
|
||||
|
||||
|
||||
def reimburse(purpose: str, amount: float) -> str:
|
||||
"""Reimburse the amount of money to the employee."""
|
||||
return {'status': 'ok'}
|
||||
|
||||
|
||||
def ask_for_approval(
|
||||
purpose: str, amount: float, tool_context: ToolContext
|
||||
) -> dict[str, Any]:
|
||||
"""Ask for approval for the reimbursement."""
|
||||
return {'status': 'pending'}
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-1.5-flash',
|
||||
name='reimbursement_agent',
|
||||
instruction="""
|
||||
You are an agent whose job is to handle the reimbursement process for
|
||||
the employees. If the amount is less than $100, you will automatically
|
||||
approve the reimbursement.
|
||||
|
||||
If the amount is greater than $100, you will
|
||||
ask for approval from the manager. If the manager approves, you will
|
||||
call reimburse() to reimburse the amount to the employee. If the manager
|
||||
rejects, you will inform the employee of the rejection.
|
||||
""",
|
||||
tools=[reimburse, LongRunningFunctionTool(func=ask_for_approval)],
|
||||
generate_content_config=types.GenerateContentConfig(temperature=0.1),
|
||||
)
|
15
contributing/samples/mcp_agent/__init__.py
Executable file
15
contributing/samples/mcp_agent/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
44
contributing/samples/mcp_agent/agent.py
Executable file
44
contributing/samples/mcp_agent/agent.py
Executable file
@ -0,0 +1,44 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import os
|
||||
|
||||
from google.adk.agents.llm_agent import LlmAgent
|
||||
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters
|
||||
|
||||
|
||||
async def create_agent():
|
||||
"""Gets tools from MCP Server."""
|
||||
tools, exit_stack = await MCPToolset.from_server(
|
||||
connection_params=StdioServerParameters(
|
||||
command='npx',
|
||||
args=[
|
||||
'-y', # Arguments for the command
|
||||
'@modelcontextprotocol/server-filesystem',
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
agent = LlmAgent(
|
||||
model='gemini-2.0-flash',
|
||||
name='enterprise_assistant',
|
||||
instruction='Help user accessing their file systems',
|
||||
tools=tools,
|
||||
)
|
||||
return agent, exit_stack
|
||||
|
||||
|
||||
root_agent = create_agent()
|
15
contributing/samples/memory/__init__.py
Executable file
15
contributing/samples/memory/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
31
contributing/samples/memory/agent.py
Executable file
31
contributing/samples/memory/agent.py
Executable file
@ -0,0 +1,31 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import random
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk.tools.load_memory_tool import load_memory_tool
|
||||
from google.adk.tools.preload_memory_tool import preload_memory_tool
|
||||
from google.genai import types
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model='gemini-2.0-flash-exp',
|
||||
name='memory_agent',
|
||||
description='agent that have access to memory tools.',
|
||||
instruction="""
|
||||
You are an agent that help user answer questions.
|
||||
""",
|
||||
tools=[load_memory_tool, preload_memory_tool],
|
||||
)
|
15
contributing/samples/non_llm_sequential/__init__.py
Executable file
15
contributing/samples/non_llm_sequential/__init__.py
Executable file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
37
contributing/samples/non_llm_sequential/agent.py
Executable file
37
contributing/samples/non_llm_sequential/agent.py
Executable file
@ -0,0 +1,37 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from google.adk.agents import Agent
|
||||
from google.adk.agents import SequentialAgent
|
||||
|
||||
sub_agent_1 = Agent(
|
||||
name='sub_agent_1',
|
||||
description='No.1 sub agent.',
|
||||
model='gemini-2.0-flash-001',
|
||||
instruction='JUST SAY 1.',
|
||||
)
|
||||
|
||||
sub_agent_2 = Agent(
|
||||
name='sub_agent_2',
|
||||
description='No.2 sub agent.',
|
||||
model='gemini-2.0-flash-001',
|
||||
instruction='JUST SAY 2.',
|
||||
)
|
||||
sequential_agent = SequentialAgent(
|
||||
name='sequential_agent',
|
||||
sub_agents=[sub_agent_1, sub_agent_2],
|
||||
)
|
||||
|
||||
root_agent = sequential_agent
|
40
contributing/samples/oauth_calendar_agent/README.md
Normal file
40
contributing/samples/oauth_calendar_agent/README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# OAuth Sample
|
||||
|
||||
## Introduction
|
||||
|
||||
This sample tests and demos the OAuth support in ADK via two tools:
|
||||
|
||||
* 1. list_calendar_events
|
||||
|
||||
This is a customized tool that calls Google Calendar API to list calendar events.
|
||||
It pass in the client id and client secrete to ADK and then get back the access token from ADK.
|
||||
And then it uses the access token to call calendar api.
|
||||
|
||||
* 2. get_calendar_events
|
||||
|
||||
This is an google calendar tool that calls Google Calendar API to get the details of a specific calendar.
|
||||
This tool is from the ADK built-in Google Calendar ToolSet.
|
||||
Everything is wrapped and the tool user just needs to pass in the client id and client secret.
|
||||
|
||||
## How to use
|
||||
|
||||
* 1. Follow https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name. to get your client id and client secret.
|
||||
Be sure to choose "web" as your client type.
|
||||
|
||||
* 2. Configure your .env file to add two variables:
|
||||
|
||||
* GOOGLE_CLIENT_ID={your client id}
|
||||
* GOOGLE_CLIENT_SECRET={your client secret}
|
||||
|
||||
Note: done't create a separate .env , instead put it to the same .env file that stores your Vertex AI or Dev ML credentials
|
||||
|
||||
* 3. Follow https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred to add http://localhost/dev-ui to "Authorized redirect URIs".
|
||||
|
||||
Note: localhost here is just a hostname that you use to access the dev ui, replace it with the actual hostname you use to access the dev ui.
|
||||
|
||||
* 4. For 1st run, allow popup for localhost in Chrome.
|
||||
|
||||
## Sample prompt
|
||||
|
||||
* `List all my today's meeting from 7am to 7pm.`
|
||||
* `Get the details of the first event.`
|
15
contributing/samples/oauth_calendar_agent/__init__.py
Normal file
15
contributing/samples/oauth_calendar_agent/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
215
contributing/samples/oauth_calendar_agent/agent.py
Normal file
215
contributing/samples/oauth_calendar_agent/agent.py
Normal file
@ -0,0 +1,215 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from fastapi.openapi.models import OAuth2
|
||||
from fastapi.openapi.models import OAuthFlowAuthorizationCode
|
||||
from fastapi.openapi.models import OAuthFlows
|
||||
from google.adk import Agent
|
||||
from google.adk.agents.callback_context import CallbackContext
|
||||
from google.adk.auth import AuthConfig
|
||||
from google.adk.auth import AuthCredential
|
||||
from google.adk.auth import AuthCredentialTypes
|
||||
from google.adk.auth import OAuth2Auth
|
||||
from google.adk.tools import ToolContext
|
||||
from google.adk.tools.google_api_tool import calendar_tool_set
|
||||
from google.auth.transport.requests import Request
|
||||
from google.oauth2.credentials import Credentials
|
||||
from googleapiclient.discovery import build
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
# Access the variable
|
||||
oauth_client_id = os.getenv("OAUTH_CLIENT_ID")
|
||||
oauth_client_secret = os.getenv("OAUTH_CLIENT_SECRET")
|
||||
|
||||
|
||||
SCOPES = ["https://www.googleapis.com/auth/calendar"]
|
||||
|
||||
calendar_tool_set.configure_auth(
|
||||
client_id=oauth_client_id, client_secret=oauth_client_secret
|
||||
)
|
||||
|
||||
get_calendar_events = calendar_tool_set.get_tool("calendar_events_get")
|
||||
# list_calendar_events = calendar_tool_set.get_tool("calendar_events_list")
|
||||
# you can replace below customized list_calendar_events tool with above ADK
|
||||
# build-in google calendar tool which is commented for now to acheive same
|
||||
# effect.
|
||||
|
||||
|
||||
def list_calendar_events(
|
||||
start_time: str,
|
||||
end_time: str,
|
||||
limit: int,
|
||||
tool_context: ToolContext,
|
||||
) -> list[dict]:
|
||||
"""Search for calendar events.
|
||||
|
||||
Example:
|
||||
|
||||
flights = get_calendar_events(
|
||||
calendar_id='joedoe@gmail.com',
|
||||
start_time='2024-09-17T06:00:00',
|
||||
end_time='2024-09-17T12:00:00',
|
||||
limit=10
|
||||
)
|
||||
# Returns up to 10 calendar events between 6:00 AM and 12:00 PM on
|
||||
September 17, 2024.
|
||||
|
||||
Args:
|
||||
calendar_id (str): the calendar ID to search for events.
|
||||
start_time (str): The start of the time range (format is
|
||||
YYYY-MM-DDTHH:MM:SS).
|
||||
end_time (str): The end of the time range (format is YYYY-MM-DDTHH:MM:SS).
|
||||
limit (int): The maximum number of results to return.
|
||||
|
||||
Returns:
|
||||
list[dict]: A list of events that match the search criteria.
|
||||
"""
|
||||
creds = None
|
||||
|
||||
# Check if the tokes were already in the session state, which means the user
|
||||
# has already gone through the OAuth flow and successfully authenticated and
|
||||
# authorized the tool to access their calendar.
|
||||
if "calendar_tool_tokens" in tool_context.state:
|
||||
creds = Credentials.from_authorized_user_info(
|
||||
tool_context.state["calendar_tool_tokens"], SCOPES
|
||||
)
|
||||
if not creds or not creds.valid:
|
||||
# If the access token is expired, refresh it with the refresh token.
|
||||
if creds and creds.expired and creds.refresh_token:
|
||||
creds.refresh(Request())
|
||||
else:
|
||||
auth_scheme = OAuth2(
|
||||
flows=OAuthFlows(
|
||||
authorizationCode=OAuthFlowAuthorizationCode(
|
||||
authorizationUrl="https://accounts.google.com/o/oauth2/auth",
|
||||
tokenUrl="https://oauth2.googleapis.com/token",
|
||||
scopes={
|
||||
"https://www.googleapis.com/auth/calendar": (
|
||||
"See, edit, share, and permanently delete all the"
|
||||
" calendars you can access using Google Calendar"
|
||||
)
|
||||
},
|
||||
)
|
||||
)
|
||||
)
|
||||
auth_credential = AuthCredential(
|
||||
auth_type=AuthCredentialTypes.OAUTH2,
|
||||
oauth2=OAuth2Auth(
|
||||
client_id=oauth_client_id, client_secret=oauth_client_secret
|
||||
),
|
||||
)
|
||||
# If the user has not gone through the OAuth flow before, or the refresh
|
||||
# token also expired, we need to ask users to go through the OAuth flow.
|
||||
# First we check whether the user has just gone through the OAuth flow and
|
||||
# Oauth response is just passed back.
|
||||
auth_response = tool_context.get_auth_response(
|
||||
AuthConfig(
|
||||
auth_scheme=auth_scheme, raw_auth_credential=auth_credential
|
||||
)
|
||||
)
|
||||
if auth_response:
|
||||
# ADK exchanged the access token already for us
|
||||
access_token = auth_response.oauth2.access_token
|
||||
refresh_token = auth_response.oauth2.refresh_token
|
||||
|
||||
creds = Credentials(
|
||||
token=access_token,
|
||||
refresh_token=refresh_token,
|
||||
token_uri=auth_scheme.flows.authorizationCode.tokenUrl,
|
||||
client_id=oauth_client_id,
|
||||
client_secret=oauth_client_secret,
|
||||
scopes=list(auth_scheme.flows.authorizationCode.scopes.keys()),
|
||||
)
|
||||
else:
|
||||
# If there are no auth response which means the user has not gone
|
||||
# through the OAuth flow yet, we need to ask users to go through the
|
||||
# OAuth flow.
|
||||
tool_context.request_credential(
|
||||
AuthConfig(
|
||||
auth_scheme=auth_scheme,
|
||||
raw_auth_credential=auth_credential,
|
||||
)
|
||||
)
|
||||
# The return value is optional and could be any dict object. It will be
|
||||
# wrapped in a dict with key as 'result' and value as the return value
|
||||
# if the object returned is not a dict. This response will be passed
|
||||
# to LLM to generate a user friendly message. e.g. LLM will tell user:
|
||||
# "I need your authorization to access your calendar. Please authorize
|
||||
# me so I can check your meetings for today."
|
||||
return "Need User Authorization to access their calendar."
|
||||
# We store the access token and refresh token in the session state for the
|
||||
# next runs. This is just an example. On production, a tool should store
|
||||
# those credentials in some secure store or properly encrypt it before store
|
||||
# it in the session state.
|
||||
tool_context.state["calendar_tool_tokens"] = json.loads(creds.to_json())
|
||||
|
||||
service = build("calendar", "v3", credentials=creds)
|
||||
events_result = (
|
||||
service.events()
|
||||
.list(
|
||||
calendarId="primary",
|
||||
timeMin=start_time + "Z" if start_time else None,
|
||||
timeMax=end_time + "Z" if end_time else None,
|
||||
maxResults=limit,
|
||||
singleEvents=True,
|
||||
orderBy="startTime",
|
||||
)
|
||||
.execute()
|
||||
)
|
||||
events = events_result.get("items", [])
|
||||
return events
|
||||
|
||||
|
||||
def update_time(callback_context: CallbackContext):
|
||||
# get current date time
|
||||
now = datetime.now()
|
||||
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
callback_context.state["_time"] = formatted_time
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
model="gemini-2.0-flash",
|
||||
name="calendar_agent",
|
||||
instruction="""
|
||||
You are a helpful personal calendar assistant.
|
||||
Use the provided tools to search for calendar events (use 10 as limit if user does't specify), and update them.
|
||||
Use "primary" as the calendarId if users don't specify.
|
||||
|
||||
Scenario1:
|
||||
The user want to query the calendar events.
|
||||
Use list_calendar_events to search for calendar events.
|
||||
|
||||
|
||||
Scenario2:
|
||||
User want to know the details of one of the listed calendar events.
|
||||
Use get_calendar_event to get the details of a calendar event.
|
||||
|
||||
|
||||
Current user:
|
||||
<User>
|
||||
{userInfo?}
|
||||
</User>
|
||||
|
||||
Currnet time: {_time}
|
||||
""",
|
||||
tools=[list_calendar_events, get_calendar_events],
|
||||
before_agent_callback=update_time,
|
||||
)
|
15
contributing/samples/quickstart/__init__.py
Normal file
15
contributing/samples/quickstart/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
82
contributing/samples/quickstart/agent.py
Normal file
82
contributing/samples/quickstart/agent.py
Normal file
@ -0,0 +1,82 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.adk.agents import Agent
|
||||
|
||||
|
||||
def get_weather(city: str) -> dict:
|
||||
"""Retrieves the current weather report for a specified city.
|
||||
|
||||
Args:
|
||||
city (str): The name of the city for which to retrieve the weather report.
|
||||
|
||||
Returns:
|
||||
dict: status and result or error msg.
|
||||
"""
|
||||
if city.lower() == "new york":
|
||||
return {
|
||||
"status": "success",
|
||||
"report": (
|
||||
"The weather in New York is sunny with a temperature of 25 degrees"
|
||||
" Celsius (41 degrees Fahrenheit)."
|
||||
),
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"status": "error",
|
||||
"error_message": f"Weather information for '{city}' is not available.",
|
||||
}
|
||||
|
||||
|
||||
def get_current_time(city: str) -> dict:
|
||||
"""Returns the current time in a specified city.
|
||||
|
||||
Args:
|
||||
city (str): The name of the city for which to retrieve the current time.
|
||||
|
||||
Returns:
|
||||
dict: status and result or error msg.
|
||||
"""
|
||||
import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
if city.lower() == "new york":
|
||||
tz_identifier = "America/New_York"
|
||||
else:
|
||||
return {
|
||||
"status": "error",
|
||||
"error_message": (
|
||||
f"Sorry, I don't have timezone information for {city}."
|
||||
),
|
||||
}
|
||||
|
||||
tz = ZoneInfo(tz_identifier)
|
||||
now = datetime.datetime.now(tz)
|
||||
report = (
|
||||
f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
|
||||
)
|
||||
return {"status": "success", "report": report}
|
||||
|
||||
|
||||
root_agent = Agent(
|
||||
name="weather_time_agent",
|
||||
model="gemini-2.0-flash",
|
||||
description=(
|
||||
"Agent to answer questions about the time and weather in a city."
|
||||
),
|
||||
instruction=(
|
||||
"I can answer your questions about the time and weather in a city."
|
||||
),
|
||||
tools=[get_weather, get_current_time],
|
||||
)
|
15
contributing/samples/workflow_agent_seq/__init__.py
Normal file
15
contributing/samples/workflow_agent_seq/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from . import agent
|
94
contributing/samples/workflow_agent_seq/agent.py
Normal file
94
contributing/samples/workflow_agent_seq/agent.py
Normal file
@ -0,0 +1,94 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.adk.agents.llm_agent import LlmAgent
|
||||
from google.adk.agents.sequential_agent import SequentialAgent
|
||||
|
||||
# --- 1. Define Sub-Agents for Each Pipeline Stage ---
|
||||
|
||||
# Code Writer Agent
|
||||
# Takes the initial specification (from user query) and writes code.
|
||||
code_writer_agent = LlmAgent(
|
||||
name="code_writer_agent",
|
||||
model="gemini-1.5-flash-001",
|
||||
instruction="""You are a Code Writer AI.
|
||||
Based on the user's request, write the initial Python code.
|
||||
Output *only* the raw code block.
|
||||
""",
|
||||
description="Writes initial code based on a specification.",
|
||||
# Stores its output (the generated code) into the session state
|
||||
# under the key 'generated_code'.
|
||||
output_key="generated_code",
|
||||
)
|
||||
|
||||
# Code Reviewer Agent
|
||||
# Takes the code generated by the previous agent (read from state) and provides feedback.
|
||||
code_reviewer_agent = LlmAgent(
|
||||
name="code_reviewer_agent",
|
||||
model="gemini-2.0-flash-001",
|
||||
instruction="""You are a Code Reviewer AI.
|
||||
|
||||
Review the below Python code.
|
||||
|
||||
```
|
||||
{generated_code}
|
||||
```
|
||||
|
||||
Provide constructive feedback on potential errors, style issues, or improvements.
|
||||
Focus on clarity and correctness.
|
||||
Output only the review comments.
|
||||
|
||||
""",
|
||||
description="Reviews code and provides feedback.",
|
||||
# Stores its output (the review comments) into the session state
|
||||
# under the key 'review_comments'.
|
||||
output_key="review_comments",
|
||||
)
|
||||
|
||||
# Code Refactorer Agent
|
||||
# Takes the original code and the review comments (read from state) and refactors the code.
|
||||
code_refactorer_agent = LlmAgent(
|
||||
name="code_refactorer_agent",
|
||||
model="gemini-2.0-flash-001",
|
||||
instruction="""You are a Code Refactorer AI.
|
||||
|
||||
Below is the original Python code:
|
||||
|
||||
```
|
||||
{generated_code}
|
||||
```
|
||||
|
||||
Below are the review comments:
|
||||
|
||||
{review_comments}
|
||||
|
||||
Refactor the code based on the provided feedback.
|
||||
|
||||
Output *only* the final, refactored code block.
|
||||
""",
|
||||
description="Refactors code based on review comments.",
|
||||
# Stores its output (the refactored code) into the session state
|
||||
# under the key 'refactored_code'.
|
||||
output_key="refactored_code",
|
||||
)
|
||||
|
||||
# --- 2. Create the SequentialAgent ---
|
||||
# This agent orchestrates the pipeline by running the sub_agents in order.
|
||||
code_pipeline_agent = SequentialAgent(
|
||||
name="code_pipeline_agent",
|
||||
sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent],
|
||||
# The agents will run in the order provided: Writer -> Reviewer -> Refactorer
|
||||
)
|
||||
|
||||
root_agent = code_pipeline_agent
|
@ -1,3 +1,17 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Utility functions for session service."""
|
||||
|
||||
import base64
|
||||
|
@ -1,3 +1,17 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from contextlib import AsyncExitStack
|
||||
import functools
|
||||
import sys
|
||||
|
Loading…
Reference in New Issue
Block a user