From f83504dffb180b129a4f416c4822f28143744da8 Mon Sep 17 00:00:00 2001 From: "Xiang (Sean) Zhou" Date: Fri, 16 May 2025 14:01:22 -0700 Subject: [PATCH] chore: add toolbox sample agent PiperOrigin-RevId: 759746416 --- contributing/samples/toolbox_agent/README.md | 74 ++++++++++++++++ .../samples/toolbox_agent/__init__.py | 15 ++++ contributing/samples/toolbox_agent/agent.py | 28 ++++++ .../samples/toolbox_agent/tool_box.db | Bin 0 -> 8192 bytes contributing/samples/toolbox_agent/tools.yaml | 81 ++++++++++++++++++ 5 files changed, 198 insertions(+) create mode 100644 contributing/samples/toolbox_agent/README.md create mode 100644 contributing/samples/toolbox_agent/__init__.py create mode 100644 contributing/samples/toolbox_agent/agent.py create mode 100644 contributing/samples/toolbox_agent/tool_box.db create mode 100644 contributing/samples/toolbox_agent/tools.yaml diff --git a/contributing/samples/toolbox_agent/README.md b/contributing/samples/toolbox_agent/README.md new file mode 100644 index 0000000..98218f2 --- /dev/null +++ b/contributing/samples/toolbox_agent/README.md @@ -0,0 +1,74 @@ +# Toolbox Agent + +This agent is utilizing [mcp toolbox for database](https://googleapis.github.io/genai-toolbox/getting-started/introduction/) to assist end user based on the informaton stored in database. +Follow below steps to run this agent + +# Install toolbox + +* Run below command: + +```bash +export OS="linux/amd64" # one of linux/amd64, darwin/arm64, darwin/amd64, or windows/amd64 +curl -O https://storage.googleapis.com/genai-toolbox/v0.5.0/$OS/toolbox +chmod +x toolbox +``` + +# install SQLite + +* install sqlite from https://sqlite.org/ + + +# Create DB (optional. The db instance is already attached in the folder) + +* Run below command: + +```bash +sqlite3 tool_box.db +``` + +* Run below SQL: + +```sql +CREATE TABLE hotels( + id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR NOT NULL, + location VARCHAR NOT NULL, + price_tier VARCHAR NOT NULL, + checkin_date DATE NOT NULL, + checkout_date DATE NOT NULL, + booked BIT NOT NULL +); + + +INSERT INTO hotels(id, name, location, price_tier, checkin_date, checkout_date, booked) +VALUES + (1, 'Hilton Basel', 'Basel', 'Luxury', '2024-04-22', '2024-04-20', 0), + (2, 'Marriott Zurich', 'Zurich', 'Upscale', '2024-04-14', '2024-04-21', 0), + (3, 'Hyatt Regency Basel', 'Basel', 'Upper Upscale', '2024-04-02', '2024-04-20', 0), + (4, 'Radisson Blu Lucerne', 'Lucerne', 'Midscale', '2024-04-24', '2024-04-05', 0), + (5, 'Best Western Bern', 'Bern', 'Upper Midscale', '2024-04-23', '2024-04-01', 0), + (6, 'InterContinental Geneva', 'Geneva', 'Luxury', '2024-04-23', '2024-04-28', 0), + (7, 'Sheraton Zurich', 'Zurich', 'Upper Upscale', '2024-04-27', '2024-04-02', 0), + (8, 'Holiday Inn Basel', 'Basel', 'Upper Midscale', '2024-04-24', '2024-04-09', 0), + (9, 'Courtyard Zurich', 'Zurich', 'Upscale', '2024-04-03', '2024-04-13', 0), + (10, 'Comfort Inn Bern', 'Bern', 'Midscale', '2024-04-04', '2024-04-16', 0); +``` + +# create tools configurations + +* Create a yaml file named "tools.yaml", see its contents in the agent folder. + +# start toolbox server + +* Run below commands in the agent folder + +```bash +toolbox --tools-file "tools.yaml" +``` + +# start ADK web UI + +# send user query + +* query 1: what can you do for me ? +* query 2: could you let know the information about "Hilton Basel" hotel ? diff --git a/contributing/samples/toolbox_agent/__init__.py b/contributing/samples/toolbox_agent/__init__.py new file mode 100644 index 0000000..c48963c --- /dev/null +++ b/contributing/samples/toolbox_agent/__init__.py @@ -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 diff --git a/contributing/samples/toolbox_agent/agent.py b/contributing/samples/toolbox_agent/agent.py new file mode 100644 index 0000000..37a5997 --- /dev/null +++ b/contributing/samples/toolbox_agent/agent.py @@ -0,0 +1,28 @@ +# 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.tools import ToolboxToolset + +root_agent = Agent( + model="gemini-2.0-flash", + name="root_agent", + instruction="You are a helpful assistant", + # Add Toolbox tools to ADK agent + tools=[ + ToolboxToolset( + server_url="http://127.0.0.1:5000", toolset_name="my-toolset" + ) + ], +) diff --git a/contributing/samples/toolbox_agent/tool_box.db b/contributing/samples/toolbox_agent/tool_box.db new file mode 100644 index 0000000000000000000000000000000000000000..4be746cc905a0eda8e049297019ef226e73e588a GIT binary patch literal 8192 zcmeI1O>fgc5Qf*GP!o}aOBGoa()NI8RSJ$nAr%s-9ik?}4d5oAa;diVLaaLL$X+9I zLP8+%o4A0u@uQH~jT7fX0S<7Skv(f$@67Bx_GRRg=eEx&ejP>w!f_Q{M~ZUo5MzW; zZgPrcY>Mz}lWV!i`@dNZ-CDe}Z z=CqGZ=M+C_p57K}Mh0{ye_=X}mN`>PMG(4#`yoqJs}#e?cj+1TX(TnP6mFlo7d|`l zh?vUpW4Uc%(>=*5V?~(oRI*BO9)=e*>&W(l;XGI z`(nNDv+!L!1Ooy<00;m9AOHk_01yBIKmZ5;f&WgRmRHf<#*M8lwGj@)-&l^@jA4sL zOrFQS7rP{&hHl*3)8#R}s@y-gDk8Pbabyzl5s5tfDiMQbKlQ!gVrVrBtsbf>x?A26 zp{+3RJu*s*l9&dTbiLtFOsdPpjjXtS@Mv8{yV@<0;iOL^!o~2rSX(Ao+JW(4x&z&) zUs2IRO%w5L#%a_D8TT1woCNrYGWwRJ-cH^n(P$cGWGxuA`eo6=hHSy2F~={36TC8T zzv&=LpjYZ^D%#(a;2q-maV$Epf&|-%EAr6E@&^R_<|P$X%Mw9rL^#I|eM6Z$!efGS z7?&vMM%IpA%Snpz6&{l)@bJ|tYrL+N>x$O&9daz@&ieKR&|NlT)Fv=_ZN9f B30D9B literal 0 HcmV?d00001 diff --git a/contributing/samples/toolbox_agent/tools.yaml b/contributing/samples/toolbox_agent/tools.yaml new file mode 100644 index 0000000..d905035 --- /dev/null +++ b/contributing/samples/toolbox_agent/tools.yaml @@ -0,0 +1,81 @@ +# 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. +sources: + my-sqlite-db: + kind: "sqlite" + database: "tool_box.db" +tools: + search-hotels-by-name: + kind: sqlite-sql + source: my-sqlite-db + description: Search for hotels based on name. + parameters: + - name: name + type: string + description: The name of the hotel. + statement: SELECT * FROM hotels WHERE name LIKE '%' || $1 || '%'; + search-hotels-by-location: + kind: sqlite-sql + source: my-sqlite-db + description: Search for hotels based on location. + parameters: + - name: location + type: string + description: The location of the hotel. + statement: SELECT * FROM hotels WHERE location LIKE '%' || $1 || '%'; + book-hotel: + kind: sqlite-sql + source: my-sqlite-db + description: >- + Book a hotel by its ID. If the hotel is successfully booked, returns a NULL, raises an error if not. + parameters: + - name: hotel_id + type: string + description: The ID of the hotel to book. + statement: UPDATE hotels SET booked = 1 WHERE id = $1; + update-hotel: + kind: sqlite-sql + source: my-sqlite-db + description: >- + Update a hotel's check-in and check-out dates by its ID. Returns a message + indicating whether the hotel was successfully updated or not. + parameters: + - name: hotel_id + type: string + description: The ID of the hotel to update. + - name: checkin_date + type: string + description: The new check-in date of the hotel. + - name: checkout_date + type: string + description: The new check-out date of the hotel. + statement: >- + UPDATE hotels SET checkin_date = CAST($2 as date), checkout_date = CAST($3 + as date) WHERE id = $1; + cancel-hotel: + kind: sqlite-sql + source: my-sqlite-db + description: Cancel a hotel by its ID. + parameters: + - name: hotel_id + type: string + description: The ID of the hotel to cancel. + statement: UPDATE hotels SET booked = 0 WHERE id = $1; +toolsets: + my-toolset: + - search-hotels-by-name + - search-hotels-by-location + - book-hotel + - update-hotel + - cancel-hotel