openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.38.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/responses/background.py

46lines · modeblame

f588695fstainless-app[bot]1 years ago1from typing import List
2
3import rich
4from pydantic import BaseModel
5
6from openai import OpenAI
7
8
9class Step(BaseModel):
10explanation: str
11output: str
12
13
14class MathResponse(BaseModel):
15steps: List[Step]
16final_answer: str
17
18
19client = OpenAI()
20id = None
21
22with client.responses.create(
23input="solve 8x + 31 = 2",
24model="gpt-4o-2024-08-06",
25background=True,
26stream=True,
27) as stream:
28for event in stream:
29if event.type == "response.created":
30id = event.response.id
31if "output_text" in event.type:
32rich.print(event)
33if event.sequence_number == 10:
34break
35
36print("Interrupted. Continuing...")
37
38assert id is not None
39with client.responses.retrieve(
40response_id=id,
41stream=True,
42starting_after=10,
43) as stream:
44for event in stream:
45if "output_text" in event.type:
46rich.print(event)