跳到正文
DeepSeek:API 更新日誌·· 4 小時前AI 評分49

如何呼叫 DeepSeek Chat API

Invoke The Chat API

AI 導讀

DeepSeek 提供取得 API key 後以 OpenAI API 格式呼叫 deepseek-flash 的 Chat API 範例,涵蓋 curl、Python 和 Node.js。範例預設為非串流回應;將 stream 設為 true 可取得串流回應,亦可參閲 Anthropic API 格式範例。

正文

Once you have obtained an API key, you can access the DeepSeek model using the following example scripts in the OpenAI API format. This is a non-stream example, you can set the stream parameter to true to get stream response.

For examples using the Anthropic API format, please refer to Anthropic API.

  • curl
  • python
  • nodejs
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
-d '{
"model": "deepseek-flash",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"thinking": {"type": "enabled"},
"reasoning_effort": "high",
"stream": false
}'
# Please install OpenAI SDK first: `pip3 install openai`
import os
from openai import OpenAI

client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url="https://api.deepseek.com",
)

response = client.chat.completions.create(
model="deepseek-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False,
reasoning_effort="high",
extra_body={"thinking": {"type": "enabled"}},
)

print(response.choices[0].message.content)
// Please install OpenAI SDK first: `npm install openai`

import OpenAI from "openai";

const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: process.env.DEEPSEEK_API_KEY,
});

async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "deepseek-flash",
thinking: {"type": "enabled"},
reasoning_effort: "high",
stream: false,
});

console.log(completion.choices[0].message.content);
}

main();

來源:DeepSeek:API 更新日誌 · api-docs.deepseek.com