import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
result = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'Return only a JSON object with name="Heihuzi" and count=4.'}], response_format={'type': 'json_object'}, max_tokens=1024)
assert result.choices[0].finish_reason == 'stop'
value = json.loads(result.choices[0].message.content)
assert value == {'name': 'Heihuzi', 'count': 4}
print(value)
DeepSeek
DeepSeek Chat Completions
已实测的 JSON、普通文本和流式调用。
POST
/
v1
/
chat
/
completions
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
result = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'Return only a JSON object with name="Heihuzi" and count=4.'}], response_format={'type': 'json_object'}, max_tokens=1024)
assert result.choices[0].finish_reason == 'stop'
value = json.loads(result.choices[0].message.content)
assert value == {'name': 'Heihuzi', 'count': 4}
print(value)
调用
POST https://code.heihuzi.ai/v1/chat/completions。实测日期:2026-09-19。先准备DeepSeek Key 和 SDK。
请求参数
string
required
填写
deepseek-flash。本页普通文本、JSON、流式、工具和图片示例均使用此模型。object[]
required
消息历史,使用
role 和 content。工具结果还需 tool_call_id;图片输入使用内容数组,见对应专题。integer
本页成功示例使用
1024。另外使用 1024 请求长输出,实测输出在上限内结束,finish_reason 为 length;客户端应将它识别为截断。object
本次验证
{"type":"json_object"},并对返回内容执行 JSON 解析和字段断言。boolean
true 返回 SSE;完整示例核对最终 finish_reason。stream_options.include_usage: true 的测试收到了用量。非流式 JSON 示例
此完整示例已通过便利店执行,返回对象为{"name":"Heihuzi","count":4}。提示词明确要求 JSON,客户端仍需检查终止原因并解析内容。
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
result = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'Return only a JSON object with name="Heihuzi" and count=4.'}], response_format={'type': 'json_object'}, max_tokens=1024)
assert result.choices[0].finish_reason == 'stop'
value = json.loads(result.choices[0].message.content)
assert value == {'name': 'Heihuzi', 'count': 4}
print(value)
流式文本与思考内容
本次收到答案42、非空 reasoning_content、finish_reason: "stop" 和用量,原始事件流以 data: [DONE] 结束。最后的用量块可能没有 choices,代码按数组遍历处理。
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
events = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'What is 19 + 23? Reply with the number only.'}], max_tokens=1024, stream=True, stream_options={'include_usage': True})
(answer, reasoning) = ('', '')
(finish_reason, usage) = (None, None)
for event in events:
if event.usage:
usage = event.usage
for choice in event.choices:
answer += choice.delta.content or ''
reasoning += getattr(choice.delta, 'reasoning_content', None) or ''
finish_reason = choice.finish_reason or finish_reason
print(answer)
print('finish_reason', finish_reason, 'reasoning_chars', len(reasoning))
print('usage', usage)
assert answer.strip() == '42' and finish_reason == 'stop'
assert reasoning, 'No reasoning_content returned'
响应与限制
正文在choices[].message.content,思考内容可能在同级 reasoning_content;流式读取对应 delta。本次用量返回 prompt_tokens、completion_tokens、total_tokens,没有返回完整缓存及推理细分字段。
需要工具时见完整工具闭环,需要图片时见图片理解。本页只列出通过实测的请求配置,见实测范围。
格式参考:DeepSeek Chat Completions、JSON Output。本页承诺范围以便利店实测为准。