-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathhello_google_genai.py
313 lines (247 loc) · 9.31 KB
/
hello_google_genai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# 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.
#
# SPDX-License-Identifier: Apache-2.0
"""Hello Google GenAI sample.
Key features demonstrated in this sample:
| Feature Description | Example Function / Code Snippet |
|----------------------------------------------------------|----------------------------------------|
| Plugin Initialization | `ai = Genkit(plugins=[GoogleGenai()])` |
| Default Model Configuration | `ai = Genkit(model=...)` |
| Defining Flows | `@ai.flow()` decorator (multiple uses) |
| Defining Tools | `@ai.tool()` decorator (multiple uses) |
| Pydantic for Tool Input Schema | `GablorkenInput` |
| Simple Generation (Prompt String) | `say_hi` |
| Generation with Messages (`Message`, `Role`, `TextPart`) | `simple_generate_with_tools_flow` |
| Generation with Tools | `simple_generate_with_tools_flow` |
| Tool Response Handling | `simple_generate_with_interrupts` |
| Tool Interruption (`ctx.interrupt`) | `gablorken_tool2` |
| Embedding (`ai.embed`, `Document`) | `embed_docs` |
| Generation Configuration (`temperature`, etc.) | `say_hi_with_configured_temperature` |
| Streaming Generation (`ai.generate_stream`) | `say_hi_stream` |
| Streaming Chunk Handling (`ctx.send_chunk`) | `say_hi_stream`, `generate_character` |
| Structured Output (Schema) | `generate_character` |
| Pydantic for Structured Output Schema | `RpgCharacter` |
| Unconstrained Structured Output | `generate_character_unconstrained` |
| Multi-modal Output Configuration | `generate_images` |
"""
import asyncio
import structlog
from pydantic import BaseModel, Field
from genkit.ai import Document, Genkit, ToolRunContext, tool_response
from genkit.plugins.google_ai.models import gemini
from genkit.plugins.google_genai import (
EmbeddingTaskType,
GeminiConfigSchema,
GeminiEmbeddingModels,
GoogleGenai,
google_genai_name,
)
from genkit.types import (
GenerationCommonConfig,
Message,
Role,
TextPart,
)
logger = structlog.get_logger(__name__)
ai = Genkit(
plugins=[GoogleGenai()],
model=google_genai_name('gemini-2.0-flash-exp'),
)
class GablorkenInput(BaseModel):
"""The Pydantic model for tools."""
value: int = Field(description='value to calculate gablorken for')
@ai.tool('calculates a gablorken', name='gablorkenTool')
def gablorken_tool(input_: GablorkenInput) -> int:
"""Calculate a gablorken.
Args:
input_: The input to calculate gablorken for.
Returns:
The calculated gablorken.
"""
return input_.value * 3 - 5
@ai.flow()
async def simple_generate_with_tools_flow(value: int) -> str:
"""Generate a greeting for the given name.
Args:
value: the integer to send to test function
Returns:
The generated response with a function.
"""
response = await ai.agenerate(
model=google_genai_name(gemini.GoogleAiVersion.GEMINI_2_0_FLASH),
messages=[
Message(
role=Role.USER,
content=[TextPart(text=f'what is a gablorken of {value}')],
),
],
tools=['gablorkenTool'],
)
return response.text
@ai.tool('calculates a gablorken', name='gablorkenTool2')
def gablorken_tool2(input_: GablorkenInput, ctx: ToolRunContext):
"""The user-defined tool function.
Args:
input_: the input to the tool
ctx: the tool run context
Returns:
The calculated gablorken.
"""
ctx.interrupt()
@ai.flow()
async def simple_generate_with_interrupts(value: int) -> str:
response1 = await ai.agenerate(
model=google_genai_name(gemini.GoogleAiVersion.GEMINI_2_0_FLASH),
messages=[
Message(
role=Role.USER,
content=[TextPart(text=f'what is a gablorken of {value}')],
),
],
tools=['gablorkenTool2'],
)
await logger.ainfo(f'len(response.tool_requests)={len(response1.tool_requests)}')
if len(response1.interrupts) == 0:
return response1.text
tr = tool_response(response1.interrupts[0], 178)
response = await ai.agenerate(
model=google_genai_name(gemini.GoogleAiVersion.GEMINI_2_0_FLASH),
messages=response1.messages,
tool_responses=[tr],
tools=['gablorkenTool'],
)
return response
@ai.flow()
async def say_hi(name: str):
"""Generate a greeting for the given name.
Args:
name: the name to send to test function
Returns:
The generated response with a function.
"""
resp = await ai.agenerate(
prompt=f'hi {name}',
)
return resp.text
@ai.flow()
async def embed_docs(docs: list[str]):
"""Generate an embedding for the words in a list.
Args:
docs: list of texts (string)
Returns:
The generated embedding.
"""
options = {'task_type': EmbeddingTaskType.CLUSTERING}
return await ai.aembed(
model=google_genai_name(GeminiEmbeddingModels.TEXT_EMBEDDING_004),
documents=[Document.from_text(doc) for doc in docs],
options=options,
)
@ai.flow()
async def say_hi_with_configured_temperature(data: str):
"""Generate a greeting for the given name.
Args:
data: the name to send to test function
Returns:
The generated response with a function.
"""
return await ai.agenerate(
messages=[Message(role=Role.USER, content=[TextPart(text=f'hi {data}')])],
config=GenerationCommonConfig(temperature=0.1),
)
@ai.flow()
async def say_hi_stream(name: str, ctx):
"""Generate a greeting for the given name.
Args:
name: the name to send to test function
ctx: the context of the tool
Returns:
The generated response with a function.
"""
stream, _ = ai.generate_stream(prompt=f'hi {name}')
result = ''
async for data in stream:
ctx.send_chunk(data.text)
for part in data.content:
result += part.root.text
return result
class Skills(BaseModel):
strength: int = Field(description='strength (0-100)')
charisma: int = Field(description='charisma (0-100)')
endurance: int = Field(description='endurance (0-100)')
class RpgCharacter(BaseModel):
"""An RPG character."""
name: str = Field(description='name of the character')
back_story: str = Field(description='back story', alias='backStory')
abilities: list[str] = Field(description='list of abilities (3-4)')
skills: Skills
@ai.flow()
async def generate_character(name: str, ctx):
"""Generate an RPG character.
Args:
name: the name of the character
ctx: the context of the tool
Returns:
The generated RPG character.
"""
if ctx.is_streaming:
stream, result = ai.generate_stream(
prompt=f'generate an RPG character named {name}',
output_schema=RpgCharacter,
)
async for data in stream:
ctx.send_chunk(data.output)
return (await result).output
else:
result = await ai.agenerate(
prompt=f'generate an RPG character named {name}',
output_schema=RpgCharacter,
)
return result.output
@ai.flow()
async def generate_character_unconstrained(name: str, ctx):
"""Generate an unconstrained RPG character.
Args:
name: the name of the character
ctx: the context of the tool
Returns:
The generated RPG character.
"""
result = await ai.agenerate(
prompt=f'generate an RPG character named {name}',
output_schema=RpgCharacter,
output_constrained=False,
output_instructions=True,
)
return result.output
@ai.flow()
async def generate_images(name: str, ctx):
"""Generate images for the given name.
Args:
name: the name to send to test function
ctx: the context of the tool
Returns:
The generated response with a function.
"""
result = await ai.agenerate(
prompt='tell me a about the Eifel Tower with photos',
config=GeminiConfigSchema(response_modalities=['text', 'image']),
)
return result
async def main() -> None:
"""Main function."""
await logger.ainfo(await say_hi(', tell me a joke'))
if __name__ == '__main__':
ai.run_main(main())