Github开源生信云平台 DEMO
curl --location "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header "Content-Type: application/json" \ --data '{ "model": "qwen-plus", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "你是谁?" } ], "stream":true, "stream_options": { "include_usage": true } }'
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"finish_reason":null,"logprobs":null,"delta":{"content":"你好"},"index":0}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"delta":{"content":"!我是通"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"delta":{"content":"义千问"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"delta":{"content":"有任何问题或需要帮助,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"delta":{"content":"欢迎随时告诉我!😊"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[{"finish_reason":"stop","delta":{"content":""},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":22,"completion_tokens":66,"total_tokens":88,"prompt_tokens_details":{"cached_tokens":0}},"created":1770343445,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-f37af769-6ca0-9b36-83a2-177bce0afe22"} data: [DONE]
好问题,这段 SSE 流式返回 里,确实混着 LLM 真正生成的内容 和 API 厂商为了协议/统计/控制加的字段。我给你按“是不是 LLM 输出”拆清楚 👇
👉 只有 choices[].delta.content 里的字符串,才是 LLM 真正“说的话”👉 其他字段(role、finish_reason、usage、id、created、model、object 等)全部是 API 厂商添加的协议/元数据
choices[].delta.content
role
finish_reason
usage
id
created
model
object
{ "choices":[{ "delta":{ "content":"", "role":"assistant" } }] }
含义:
role: "assistant"👉 API 协议字段,告诉客户端:
role: "assistant"
接下来是 assistant 在说话
content: ""👉 空字符串,不是 LLM 输出
content: ""
📌 结论:❌ 没有任何模型生成文本✅ 这是 API 协议初始化 chunk
"delta": { "content": "你好" } "delta": { "content": "!我是通" } "delta": { "content": "义千问" } "delta": { "content": "有任何问题或需要帮助," } "delta": { "content": "欢迎随时告诉我!😊" }
把它们拼起来:
你好!我是通义千问有任何问题或需要帮助,欢迎随时告诉我!😊
📌 结论:
👉 客户端正确做法:累加 delta.content
delta.content
"finish_reason": "stop", "delta": { "content": "" }
finish_reason: "stop"
📌 结论:❌ 不是 LLM 输出✅ 结束信号
{ "choices": [], "usage": { "prompt_tokens": 22, "completion_tokens": 66, "total_tokens": 88 } }
📌 结论:❌ 完全不是 LLM 输出✅ API 侧统计信息
choices[].delta.role
id / model / created / object
if chunk.Choices[i].Delta.Content != "" { fullText += chunk.Choices[i].Delta.Content }
如果你愿意,下一步我可以帮你:
chat.completions.stream
create(stream=true)
你这个问题已经是 写 SDK / 网关层的人 才会问的级别了 👍