docs: add ADK to colab quickstart (#459)

This commit is contained in:
Twisha Bansal
2025-04-21 19:44:59 +05:30
committed by GitHub
parent 2c3f00c0cd
commit ba1d52aa04

View File

@@ -43,8 +43,8 @@
"This guide demonstrates how to quickly run\n",
"[Toolbox](https://github.com/googleapis/genai-toolbox) end-to-end in Google\n",
"Colab using Python, PostgreSQL, and either [Google\n",
"GenAI](https://pypi.org/project/google-genai/),\n",
"[Langgraph](https://www.langchain.com/langgraph) \n",
"GenAI](https://pypi.org/project/google-genai/), [ADK](https://google.github.io/adk-docs/),\n",
"[Langgraph](https://www.langchain.com/langgraph)\n",
"or [LlamaIndex](https://www.llamaindex.ai/).\n",
"\n",
"Within this Colab environment, you'll\n",
@@ -473,14 +473,17 @@
"source": [
"> You can either use LangGraph or LlamaIndex to develop a Toolbox based\n",
"> application. Run one of the sections below\n",
"> - [Connect using Google GenAI](#scrollTo=Rw6CivhFFlbq)\n",
"> - [Connect using Google GenAI](#scrollTo=Rwgv1LDdNKSn)\n",
"> - [Connect using ADK](#scrollTo=QqRlWqvYNKSo)\n",
"> - [Connect Using LangGraph](#scrollTo=pbapNMhhL33S)\n",
"> - [Connect using LlamaIndex](#scrollTo=04iysrm_L_7v)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "Rwgv1LDdNKSn"
},
"source": [
"### Connect Using Google GenAI"
]
@@ -488,19 +491,23 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "HY23RMk4NKSn"
},
"outputs": [],
"source": [
"# Install the Toolbox Core package\n",
"!pip install toolbox-core --quiet\n",
"\n",
"# Install the Google GenAI package\n",
"!pip install google-genai"
"!pip install google-genai --quiet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "9F1u566sNKSn"
},
"source": [
"Create a Google GenAI Application which can Search, Book and Cancel hotels."
]
@@ -508,7 +515,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "LAuBIOXvNKSn"
},
"outputs": [],
"source": [
"import asyncio\n",
@@ -545,7 +554,7 @@
"\n",
"async def run_application():\n",
" toolbox_client = ToolboxClient(\"http://127.0.0.1:5000\")\n",
" \n",
"\n",
" # The toolbox_tools list contains Python callables (functions/methods) designed for LLM tool-use\n",
" # integration. While this example uses Google's genai client, these callables can be adapted for\n",
" # various function-calling or agent frameworks. For easier integration with supported frameworks\n",
@@ -623,6 +632,101 @@
"asyncio.run(run_application())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QqRlWqvYNKSo"
},
"source": [
"### Connect Using ADK"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dhQTKlpVNKSo"
},
"outputs": [],
"source": [
"! pip install toolbox-langchain --quiet\n",
"! pip install google-adk langchain --quiet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tSLO_0vKNKSo"
},
"outputs": [],
"source": [
"from google.adk.agents import Agent\n",
"from google.adk.tools.toolbox_tool import ToolboxTool\n",
"from google.adk.runners import Runner\n",
"from google.adk.sessions import InMemorySessionService\n",
"from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService\n",
"from google.genai import types\n",
"\n",
"import os\n",
"# TODO(developer): replace this with your Google API key\n",
"os.environ['GOOGLE_API_KEY'] = \"<GOOGLE_API_KEY>\"\n",
"\n",
"toolbox_tools = ToolboxTool(\"http://127.0.0.1:5000\")\n",
"\n",
"prompt = \"\"\"\n",
" You're a helpful hotel assistant. You handle hotel searching, booking and\n",
" cancellations. When the user searches for a hotel, mention it's name, id,\n",
" location and price tier. Always mention hotel ids while performing any\n",
" searches. This is very important for any operations. For any bookings or\n",
" cancellations, please provide the appropriate confirmation. Be sure to\n",
" update checkin or checkout dates if mentioned by the user.\n",
" Don't ask for confirmations from the user.\n",
"\"\"\"\n",
"\n",
"root_agent = Agent(\n",
" model='gemini-2.0-flash',\n",
" name='hotel_agent',\n",
" description='A helpful AI assistant.',\n",
" instruction=prompt,\n",
" tools=toolbox_tools.get_toolset(\"my-toolset\"),\n",
")\n",
"\n",
"session_service = InMemorySessionService()\n",
"artifacts_service = InMemoryArtifactService()\n",
"session = session_service.create_session(\n",
" state={}, app_name='hotel_agent', user_id='123'\n",
")\n",
"runner = Runner(\n",
" app_name='hotel_agent',\n",
" agent=root_agent,\n",
" artifact_service=artifacts_service,\n",
" session_service=session_service,\n",
")\n",
"\n",
"queries = [\n",
" \"Find hotels in Basel with Basel in it's name.\",\n",
" \"Can you book the Hilton Basel for me?\",\n",
" \"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.\",\n",
" \"My check in dates would be from April 10, 2024 to April 19, 2024.\",\n",
"]\n",
"\n",
"for query in queries:\n",
" content = types.Content(role='user', parts=[types.Part(text=query)])\n",
" events = runner.run(session_id=session.id,\n",
" user_id='123', new_message=content)\n",
"\n",
" responses = (\n",
" part.text\n",
" for event in events\n",
" for part in event.content.parts\n",
" if part.text is not None\n",
" )\n",
"\n",
" for text in responses:\n",
" print(text)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -900,6 +1004,7 @@
"metadata": {
"colab": {
"collapsed_sections": [
"Rwgv1LDdNKSn",
"pbapNMhhL33S",
"04iysrm_L_7v"
],