-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathrun_workflow.py
More file actions
66 lines (53 loc) · 2.23 KB
/
Copy pathrun_workflow.py
File metadata and controls
66 lines (53 loc) · 2.23 KB
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
"""Start the streaming workflow and consume model chunks live."""
import asyncio
import os
from datetime import timedelta
from google.genai import types
from temporalio.client import Client
from temporalio.contrib.pydantic import pydantic_data_converter
from temporalio.contrib.workflow_streams import WorkflowStreamClient
from google_genai.streaming.workflow import StreamingWorkflow
# Only a chunk carrying finish_reason ends the subscribe loop, so bound the
# wait: if generation fails mid-stream, no such chunk ever arrives.
STREAM_TIMEOUT = 60.0
# @@@SNIPSTART python-google-genai-streaming-run-workflow
async def consume(client: Client, workflow_id: str) -> None:
"""Subscribe to the "gemini" topic and print chunks as the model produces them."""
stream = WorkflowStreamClient.create(client, workflow_id)
async for item in stream.subscribe(
["gemini"],
from_offset=0,
result_type=types.GenerateContentResponse,
poll_cooldown=timedelta(milliseconds=50),
):
chunk: types.GenerateContentResponse = item.data
if chunk.text:
print(chunk.text, end="", flush=True)
if chunk.candidates and chunk.candidates[0].finish_reason:
print()
return
async def main() -> None:
# The stream publishes Pydantic GenerateContentResponse chunks, so the
# consumer needs the Pydantic data converter to decode them.
client = await Client.connect(
os.environ.get("TEMPORAL_ADDRESS", "localhost:7233"),
data_converter=pydantic_data_converter,
)
# @@@SNIPEND
workflow_id = "google-genai-streaming"
handle = await client.start_workflow(
StreamingWorkflow.run,
"Count from 1 to 5, one number per sentence.",
id=workflow_id,
task_queue="google-genai-streaming",
)
try:
await asyncio.wait_for(consume(client, workflow_id), timeout=STREAM_TIMEOUT)
except asyncio.TimeoutError:
print(f"\nNo end-of-stream chunk after {STREAM_TIMEOUT}s; giving up.")
# Release the workflow now that we've consumed the stream.
await handle.signal(StreamingWorkflow.finish)
result = await handle.result()
print(f"Final result: {result}")
if __name__ == "__main__":
asyncio.run(main())