메인 콘텐츠로 건너뛰기
때때로 모델이 응답을 생성하는 데 시간이 걸릴 수 있습니다. stream 옵션을 true로 설정하면 응답을 여러 청크로 이루어진 스트림 형태로 받을 수 있어, 전체 응답이 생성될 때까지 기다리지 않고 결과를 점진적으로 표시할 수 있습니다. 스트리밍 출력은 호스팅되는 모든 모델에서 지원됩니다. 특히 reasoning models와 함께 사용할 것을 권장합니다. 모델이 출력을 시작하기 전에 오랜 시간 생각하는 경우, 스트리밍을 사용하지 않는 요청은 타임아웃될 수 있습니다.
import openai

client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="<your-api-key>",  # https://wandb.ai/settings 에서 API 키를 생성하세요
)

stream = client.chat.completions.create(
    model="openai/gpt-oss-120b",
    messages=[
        {"role": "user", "content": "Tell me a rambling joke"}
    ],
    stream=True,
)

for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
    else:
        print(chunk) # CompletionUsage 객체를 출력