Skip to content

Commit 7773a9c

Browse files
authored
chore(py): Add tools sample for OpenAI Compat plugin (#3684)
1 parent 4e7d00a commit 7773a9c

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

py/samples/compat-oai-hello/src/compat_oai_hello.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,28 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17-
"""OpenAI sample."""
17+
"""OpenAI hello sample.
18+
19+
Key features demonstrated in this sample:
20+
21+
| Feature Description | Example Function / Code Snippet |
22+
|----------------------------------------------------------|----------------------------------------|
23+
| Plugin Initialization | `ai = Genkit(plugins=[OpenAI()])` |
24+
| Default Model Configuration | `ai = Genkit(model=...)` |
25+
| Defining Flows | `@ai.flow()` decorator (multiple uses) |
26+
| Defining Tools | `@ai.tool()` decorator (multiple uses) |
27+
| Pydantic for Tool Input Schema | `GablorkenOutputSchema` |
28+
| Simple arithmetic addition(Input integers a,b) | `sum_two_numbers2` |
29+
| Simple Generation (Prompt String) | `say_hi` |
30+
| Generation with Messages (`Message`, `Role`, `TextPart`) | `say_hi_constrained` |
31+
| Generated response as stream (Prompt String) | `say_hi_stream` |
32+
| Generation with Tools | `calculate_gablorken` |
33+
| Generate current weather response using tools | `get_weather_flow` |
34+
| Weather response generated as stream | `get_weather_flow_stream` |
35+
| Tool Response Handling with context | `generate_character` |
36+
37+
38+
"""
1839

1940
from decimal import Decimal
2041

@@ -37,6 +58,18 @@ class MyInput(BaseModel):
3758
b: int = Field(description='b field')
3859

3960

61+
class HelloSchema(BaseModel):
62+
"""Hello schema.
63+
64+
Args:
65+
text: The text to say hello to.
66+
receiver: The receiver of the hello.
67+
"""
68+
69+
text: str
70+
receiver: str
71+
72+
4073
@ai.flow()
4174
def sum_two_numbers2(my_input: MyInput) -> int:
4275
"""Sum two numbers.
@@ -215,6 +248,42 @@ def gablorken_tool(input_: GablorkenInput) -> int:
215248
return input_.value * 3 - 5
216249

217250

251+
@ai.flow()
252+
async def calculate_gablorken(value: int):
253+
"""Generate a request to calculate gablorken according to gablorken_tool.
254+
255+
Args:
256+
value: Input data containing number
257+
258+
Returns:
259+
A GenerateRequest object with the evaluation output
260+
"""
261+
response = await ai.generate(
262+
prompt=f'what is the gablorken of {value}',
263+
model=openai_model('gpt-4'),
264+
tools=['gablorkenTool'],
265+
)
266+
267+
return response.message.content[0].root.text
268+
269+
270+
@ai.flow()
271+
async def say_hi_constrained(hi_input: str):
272+
"""Generate a request to greet a user with response following `HelloSchema` schema.
273+
274+
Args:
275+
hi_input: Input data containing user information.
276+
277+
Returns:
278+
A `HelloSchema` object with the greeting message.
279+
"""
280+
response = await ai.generate(
281+
prompt='hi ' + hi_input,
282+
output_schema=HelloSchema,
283+
)
284+
return response.output
285+
286+
218287
@ai.flow()
219288
async def generate_character(name: str, ctx: ActionRunContext):
220289
"""Generate an RPG character.
@@ -252,7 +321,10 @@ async def main() -> None:
252321
await logger.ainfo(sum_two_numbers2(MyInput(a=1, b=3)))
253322

254323
await logger.ainfo(await say_hi('John Doe'))
324+
await logger.ainfo(await say_hi_constrained('John Doe'))
255325
await logger.ainfo(await say_hi_stream('John Doe'))
326+
327+
await logger.ainfo(await calculate_gablorken(33))
256328
await logger.ainfo(await get_weather_flow('London and Paris'))
257329
await logger.ainfo(await get_weather_flow_stream('London and Paris'))
258330

0 commit comments

Comments
 (0)