forked from temporalio/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivities.py
More file actions
33 lines (27 loc) · 1.09 KB
/
Copy pathactivities.py
File metadata and controls
33 lines (27 loc) · 1.09 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
"""Basic LLM activity with LangSmith tracing."""
from dataclasses import dataclass
from langsmith import traceable
from langsmith.wrappers import wrap_openai
from openai import AsyncOpenAI
from temporalio import activity
@dataclass
class OpenAIRequest:
model: str
input: str
instructions: str = "You are a helpful assistant."
@traceable(name="Call OpenAI", run_type="llm")
@activity.defn
async def call_openai(request: OpenAIRequest) -> str:
"""Call OpenAI Responses API. Retries handled by Temporal, not the OpenAI client."""
# wrap_openai patches the client so each API call (e.g. responses.create)
# creates its own child span with model parameters and token usage.
# max_retries=0 disables OpenAI's built-in retries — Temporal's activity
# retry policy handles retries instead, with full visibility in the UI.
client = wrap_openai(AsyncOpenAI(max_retries=0))
response = await client.responses.create(
model=request.model,
instructions=request.instructions,
input=request.input,
timeout=30,
)
return response.output_text