Appearance
Chat Completions
端点:
POST https://oneapi.zaiwenai.com/v1/chat/completions
与 OpenAI Chat Completions 接口完全兼容,支持所有 OpenAI API 参数。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 消息列表 |
stream | boolean | 否 | 是否启用流式响应,默认 false |
temperature | number | 否 | 采样温度,0-2 之间 |
max_tokens | integer | 否 | 最大输出 Token 数 |
tools | array | 否 | 工具定义列表,用于函数调用 |
tool_choice | string | object | 否 | 工具调用策略 |
其他参数请参考 OpenAI Chat Completions 文档。
Curl 示例
bash
curl https://oneapi.zaiwenai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d '{
"model": "deepseek-v4-flash",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'OpenAI Python SDK 示例
python
from openai import OpenAI
client = OpenAI(
base_url="https://oneapi.zaiwenai.com/v1",
api_key="$YOUR_API_KEY",
)
completion = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "hello!",
},
],
)
print(completion.choices[0].message.content)流式响应
python
stream = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{
"role": "user",
"content": "Tell me a short story",
},
],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")工具调用示例
python
import json
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称",
},
},
"required": ["city"],
},
},
},
]
completion = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{
"role": "user",
"content": "北京今天天气怎么样?",
},
],
tools=tools,
tool_choice="auto",
)
message = completion.choices[0].message
if message.tool_calls:
for tool_call in message.tool_calls:
print(f"调用工具: {tool_call.function.name}")
print(f"参数: {tool_call.function.arguments}")LangChain 集成示例
python
from pprint import pprint
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
base_url="https://oneapi.zaiwenai.com/v1",
api_key="$YOUR_API_KEY",
model="deepseek-v4-flash",
)
_weather_db = {
"Beijing": "Hazy, 28°C",
}
def get_user_city() -> str:
"""
Asks the user for their current city via command-line input.
"""
print("+++++ TOOL USER ++++: try to get user's city")
return "Beijing"
def get_weather(city: str) -> str:
"""
get weather for a given city from a mock database.
"""
print(f"+++++ TOOL USER ++++: try to get weather for {city}")
return _weather_db.get(
city, f"Sorry, I don't have weather information for {city}."
)
agent = create_agent(
model=model,
tools=[get_weather, get_user_city],
system_prompt="You are a helpful assistant.",
)
r = agent.invoke(
{"messages": [{"role": "user", "content": "What's the weather like?"}]}
)
pprint(r)计费说明
按 Token 用量计费,支持缓存命中优惠。请求前会预估费用,余额不足时返回 402。