指南
流式处理响应
流式处理输出使用服务器发送的事件 (SSE),它允许服务器发回事件流中内容的增量。
流式响应有利于提供实时反馈,通过允许在生成文本时显示文本来增强用户交互。
要启用流式处理,您必须将"stream": true
在您的请求中:
import os
from openai import OpenAI
XAI_API_KEY = os.getenv("XAI_API_KEY")
client = OpenAI(
api_key=XAI_API_KEY,
base_url="https://api.x.ai/v1",
)
stream = client.chat.completions.create(
model="grok-2-latest",
messages=[
{"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."},
{"role": "user", "content": "What is the meaning of life, the universe, and everything?"},
],
stream=True # Set streaming here
)
for chunk in stream:
print(chunk.choices[0].delta.content, end="", flush=True)
您将获得如下事件流:
bash
data: {"id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,"model":"grok-2-latest","choices":[{"index":0,"delta":{"content":"Ah","role":"assistant"}}],"usage":{"prompt_tokens":41,"completion_tokens":1,"total_tokens":42,"prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},"system_fingerprint":"fp_xxxxxxxxxx"}
data: {"id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,"model":"grok-2-latest","choices":[{"index":0,"delta":{"content":",","role":"assistant"}}],"usage":{"prompt_tokens":41,"completion_tokens":2,"total_tokens":43,"prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},"system_fingerprint":"fp_xxxxxxxxxx"}
data: [DONE]
建议您使用客户端 SDK 解析事件流。
Python/Javascript 中的流响应示例:
bash
Ah, the ultimate question! According to Douglas Adams, the answer is **42**. However, the trick lies in figuring out what the actual question is. If you're looking for a bit more context or a different perspective:
- **Philosophically**: The meaning of life might be to seek purpose, happiness, or to fulfill one's potential.
- **Biologically**: It could be about survival, reproduction, and passing on genes.
- **Existentially**: You create your own meaning through your experiences and choices.
But let's not forget, the journey to find this meaning might just be as important as the answer itself! Keep exploring, questioning, and enjoying the ride through the universe. And remember, don't panic!