Skip to content

Commit f544e67

Browse files
rapid-killer-9AnmolShukla2002twishabansal
authored
chore: modify quickstart structure to support testing (#1186)
Co-authored-by: Anmol Shukla <[email protected]> Co-authored-by: Twisha Bansal <[email protected]>
1 parent a21e680 commit f544e67

File tree

5 files changed

+302
-291
lines changed

5 files changed

+302
-291
lines changed

docs/en/getting-started/local_quickstart.md

Lines changed: 9 additions & 291 deletions
Original file line numberDiff line numberDiff line change
@@ -94,305 +94,23 @@ pip install google-genai
9494
code to create an agent:
9595
{{< tabpane persist=header >}}
9696
{{< tab header="ADK" lang="python" >}}
97-
from google.adk.agents import Agent
98-
from google.adk.runners import Runner
99-
from google.adk.sessions import InMemorySessionService
100-
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
101-
from google.genai import types
102-
from toolbox_core import ToolboxSyncClient
103-
104-
import asyncio
105-
import os
106-
107-
# TODO(developer): replace this with your Google API key
108-
109-
os.environ['GOOGLE_API_KEY'] = 'your-api-key'
110-
111-
async def main():
112-
with ToolboxSyncClient("<http://127.0.0.1:5000>") as toolbox_client:
113-
114-
prompt = """
115-
You're a helpful hotel assistant. You handle hotel searching, booking and
116-
cancellations. When the user searches for a hotel, mention it's name, id,
117-
location and price tier. Always mention hotel ids while performing any
118-
searches. This is very important for any operations. For any bookings or
119-
cancellations, please provide the appropriate confirmation. Be sure to
120-
update checkin or checkout dates if mentioned by the user.
121-
Don't ask for confirmations from the user.
122-
"""
123-
124-
root_agent = Agent(
125-
model='gemini-2.0-flash-001',
126-
name='hotel_agent',
127-
description='A helpful AI assistant.',
128-
instruction=prompt,
129-
tools=toolbox_client.load_toolset("my-toolset"),
130-
)
131-
132-
session_service = InMemorySessionService()
133-
artifacts_service = InMemoryArtifactService()
134-
session = await session_service.create_session(
135-
state={}, app_name='hotel_agent', user_id='123'
136-
)
137-
runner = Runner(
138-
app_name='hotel_agent',
139-
agent=root_agent,
140-
artifact_service=artifacts_service,
141-
session_service=session_service,
142-
)
143-
144-
queries = [
145-
"Find hotels in Basel with Basel in its name.",
146-
"Can you book the Hilton Basel for me?",
147-
"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.",
148-
"My check in dates would be from April 10, 2024 to April 19, 2024.",
149-
]
150-
151-
for query in queries:
152-
content = types.Content(role='user', parts=[types.Part(text=query)])
153-
events = runner.run(session_id=session.id,
154-
user_id='123', new_message=content)
155-
156-
responses = (
157-
part.text
158-
for event in events
159-
for part in event.content.parts
160-
if part.text is not None
161-
)
162-
163-
for text in responses:
164-
print(text)
165-
166-
asyncio.run(main())
167-
{{< /tab >}}
168-
{{< tab header="LangChain" lang="python" >}}
169-
import asyncio
170-
171-
from langgraph.prebuilt import create_react_agent
172-
173-
# TODO(developer): replace this with another import if needed
174-
175-
from langchain_google_vertexai import ChatVertexAI
176-
177-
# from langchain_google_genai import ChatGoogleGenerativeAI
178-
179-
# from langchain_anthropic import ChatAnthropic
180-
181-
from langgraph.checkpoint.memory import MemorySaver
182-
183-
from toolbox_langchain import ToolboxClient
18497

185-
prompt = """
186-
You're a helpful hotel assistant. You handle hotel searching, booking and
187-
cancellations. When the user searches for a hotel, mention it's name, id,
188-
location and price tier. Always mention hotel ids while performing any
189-
searches. This is very important for any operations. For any bookings or
190-
cancellations, please provide the appropriate confirmation. Be sure to
191-
update checkin or checkout dates if mentioned by the user.
192-
Don't ask for confirmations from the user.
193-
"""
98+
{{< include "quickstart/python/adk/quickstart.py" >}}
19499

195-
queries = [
196-
"Find hotels in Basel with Basel in its name.",
197-
"Can you book the Hilton Basel for me?",
198-
"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.",
199-
"My check in dates would be from April 10, 2024 to April 19, 2024.",
200-
]
201-
202-
async def main():
203-
# TODO(developer): replace this with another model if needed
204-
model = ChatVertexAI(model_name="gemini-2.0-flash-001")
205-
# model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001")
206-
# model = ChatAnthropic(model="claude-3-5-sonnet-20240620")
207-
208-
# Load the tools from the Toolbox server
209-
async with ToolboxClient("http://127.0.0.1:5000") as client:
210-
tools = await client.aload_toolset()
211-
212-
agent = create_react_agent(model, tools, checkpointer=MemorySaver())
100+
{{< /tab >}}
101+
{{< tab header="LangChain" lang="python" >}}
213102

214-
config = {"configurable": {"thread_id": "thread-1"}}
215-
for query in queries:
216-
inputs = {"messages": [("user", prompt + query)]}
217-
response = agent.invoke(inputs, stream_mode="values", config=config)
218-
print(response["messages"][-1].content)
103+
{{< include "quickstart/python/langchain/quickstart.py" >}}
219104

220-
asyncio.run(main())
221105
{{< /tab >}}
222106
{{< tab header="LlamaIndex" lang="python" >}}
223-
import asyncio
224-
import os
225-
226-
from llama_index.core.agent.workflow import AgentWorkflow
227-
228-
from llama_index.core.workflow import Context
229-
230-
# TODO(developer): replace this with another import if needed
231-
232-
from llama_index.llms.google_genai import GoogleGenAI
233-
234-
# from llama_index.llms.anthropic import Anthropic
235-
236-
from toolbox_llamaindex import ToolboxClient
237-
238-
prompt = """
239-
You're a helpful hotel assistant. You handle hotel searching, booking and
240-
cancellations. When the user searches for a hotel, mention it's name, id,
241-
location and price tier. Always mention hotel ids while performing any
242-
searches. This is very important for any operations. For any bookings or
243-
cancellations, please provide the appropriate confirmation. Be sure to
244-
update checkin or checkout dates if mentioned by the user.
245-
Don't ask for confirmations from the user.
246-
"""
247-
248-
queries = [
249-
"Find hotels in Basel with Basel in its name.",
250-
"Can you book the Hilton Basel for me?",
251-
"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.",
252-
"My check in dates would be from April 10, 2024 to April 19, 2024.",
253-
]
254-
255-
async def main():
256-
# TODO(developer): replace this with another model if needed
257-
llm = GoogleGenAI(
258-
model="gemini-2.0-flash-001",
259-
vertexai_config={"project": "project-id", "location": "us-central1"},
260-
)
261-
# llm = GoogleGenAI(
262-
# api_key=os.getenv("GOOGLE_API_KEY"),
263-
# model="gemini-2.0-flash-001",
264-
# )
265-
# llm = Anthropic(
266-
# model="claude-3-7-sonnet-latest",
267-
# api_key=os.getenv("ANTHROPIC_API_KEY")
268-
# )
269-
270-
# Load the tools from the Toolbox server
271-
async with ToolboxClient("http://127.0.0.1:5000") as client:
272-
tools = await client.aload_toolset()
273-
274-
agent = AgentWorkflow.from_tools_or_functions(
275-
tools,
276-
llm=llm,
277-
system_prompt=prompt,
278-
)
279-
ctx = Context(agent)
280-
for query in queries:
281-
response = await agent.run(user_msg=query, ctx=ctx)
282-
print(f"---- {query} ----")
283-
print(str(response))
284-
285-
asyncio.run(main())
107+
108+
{{< include "quickstart/python/llamaindex/quickstart.py" >}}
109+
286110
{{< /tab >}}
287111
{{< tab header="Core" lang="python" >}}
288-
import asyncio
289-
290-
from google import genai
291-
from google.genai.types import (
292-
Content,
293-
FunctionDeclaration,
294-
GenerateContentConfig,
295-
Part,
296-
Tool,
297-
)
298-
299-
from toolbox_core import ToolboxClient
300-
301-
prompt = """
302-
You're a helpful hotel assistant. You handle hotel searching, booking and
303-
cancellations. When the user searches for a hotel, mention it's name, id,
304-
location and price tier. Always mention hotel id while performing any
305-
searches. This is very important for any operations. For any bookings or
306-
cancellations, please provide the appropriate confirmation. Be sure to
307-
update checkin or checkout dates if mentioned by the user.
308-
Don't ask for confirmations from the user.
309-
"""
310-
311-
queries = [
312-
"Find hotels in Basel with Basel in its name.",
313-
"Please book the hotel Hilton Basel for me.",
314-
"This is too expensive. Please cancel it.",
315-
"Please book Hyatt Regency for me",
316-
"My check in dates for my booking would be from April 10, 2024 to April 19, 2024.",
317-
]
318-
319-
async def main():
320-
async with ToolboxClient("<http://127.0.0.1:5000>") as toolbox_client:
321-
322-
# The toolbox_tools list contains Python callables (functions/methods) designed for LLM tool-use
323-
# integration. While this example uses Google's genai client, these callables can be adapted for
324-
# various function-calling or agent frameworks. For easier integration with supported frameworks
325-
# (https://github.com/googleapis/mcp-toolbox-python-sdk/tree/main/packages), use the
326-
# provided wrapper packages, which handle framework-specific boilerplate.
327-
toolbox_tools = await toolbox_client.load_toolset("my-toolset")
328-
genai_client = genai.Client(
329-
vertexai=True, project="project-id", location="us-central1"
330-
)
331-
332-
genai_tools = [
333-
Tool(
334-
function_declarations=[
335-
FunctionDeclaration.from_callable_with_api_option(callable=tool)
336-
]
337-
)
338-
for tool in toolbox_tools
339-
]
340-
history = []
341-
for query in queries:
342-
user_prompt_content = Content(
343-
role="user",
344-
parts=[Part.from_text(text=query)],
345-
)
346-
history.append(user_prompt_content)
347-
348-
response = genai_client.models.generate_content(
349-
model="gemini-2.0-flash-001",
350-
contents=history,
351-
config=GenerateContentConfig(
352-
system_instruction=prompt,
353-
tools=genai_tools,
354-
),
355-
)
356-
history.append(response.candidates[0].content)
357-
function_response_parts = []
358-
for function_call in response.function_calls:
359-
fn_name = function_call.name
360-
# The tools are sorted alphabetically
361-
if fn_name == "search-hotels-by-name":
362-
function_result = await toolbox_tools[3](**function_call.args)
363-
elif fn_name == "search-hotels-by-location":
364-
function_result = await toolbox_tools[2](**function_call.args)
365-
elif fn_name == "book-hotel":
366-
function_result = await toolbox_tools[0](**function_call.args)
367-
elif fn_name == "update-hotel":
368-
function_result = await toolbox_tools[4](**function_call.args)
369-
elif fn_name == "cancel-hotel":
370-
function_result = await toolbox_tools[1](**function_call.args)
371-
else:
372-
raise ValueError("Function name not present.")
373-
function_response = {"result": function_result}
374-
function_response_part = Part.from_function_response(
375-
name=function_call.name,
376-
response=function_response,
377-
)
378-
function_response_parts.append(function_response_part)
379-
380-
if function_response_parts:
381-
tool_response_content = Content(role="tool", parts=function_response_parts)
382-
history.append(tool_response_content)
383-
384-
response2 = genai_client.models.generate_content(
385-
model="gemini-2.0-flash-001",
386-
contents=history,
387-
config=GenerateContentConfig(
388-
tools=genai_tools,
389-
),
390-
)
391-
final_model_response_content = response2.candidates[0].content
392-
history.append(final_model_response_content)
393-
print(response2.text)
394-
395-
asyncio.run(main())
112+
113+
{{< include "quickstart/python/core/quickstart.py" >}}
396114

397115
{{< /tab >}}
398116
{{< /tabpane >}}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from google.adk.agents import Agent
2+
from google.adk.runners import Runner
3+
from google.adk.sessions import InMemorySessionService
4+
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
5+
from google.genai import types
6+
from toolbox_core import ToolboxSyncClient
7+
8+
import asyncio
9+
import os
10+
11+
# TODO(developer): replace this with your Google API key
12+
13+
os.environ['GOOGLE_API_KEY'] = 'your-api-key'
14+
15+
async def main():
16+
with ToolboxSyncClient("http://127.0.0.1:5000") as toolbox_client:
17+
18+
prompt = """
19+
You're a helpful hotel assistant. You handle hotel searching, booking and
20+
cancellations. When the user searches for a hotel, mention it's name, id,
21+
location and price tier. Always mention hotel ids while performing any
22+
searches. This is very important for any operations. For any bookings or
23+
cancellations, please provide the appropriate confirmation. Be sure to
24+
update checkin or checkout dates if mentioned by the user.
25+
Don't ask for confirmations from the user.
26+
"""
27+
28+
root_agent = Agent(
29+
model='gemini-2.0-flash-001',
30+
name='hotel_agent',
31+
description='A helpful AI assistant.',
32+
instruction=prompt,
33+
tools=toolbox_client.load_toolset("my-toolset"),
34+
)
35+
36+
session_service = InMemorySessionService()
37+
artifacts_service = InMemoryArtifactService()
38+
session = await session_service.create_session(
39+
state={}, app_name='hotel_agent', user_id='123'
40+
)
41+
runner = Runner(
42+
app_name='hotel_agent',
43+
agent=root_agent,
44+
artifact_service=artifacts_service,
45+
session_service=session_service,
46+
)
47+
48+
queries = [
49+
"Find hotels in Basel with Basel in its name.",
50+
"Can you book the Hilton Basel for me?",
51+
"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.",
52+
"My check in dates would be from April 10, 2024 to April 19, 2024.",
53+
]
54+
55+
for query in queries:
56+
content = types.Content(role='user', parts=[types.Part(text=query)])
57+
events = runner.run(session_id=session.id,
58+
user_id='123', new_message=content)
59+
60+
responses = (
61+
part.text
62+
for event in events
63+
for part in event.content.parts
64+
if part.text is not None
65+
)
66+
67+
for text in responses:
68+
print(text)
69+
70+
asyncio.run(main())

0 commit comments

Comments
 (0)