Deepseek调用对话API

Deepseek调用对话API

环境准备

  1. 安装Git,基于其Git Bash终端搭建conda虚拟环境。官网:https://git-scm.com/install/windows
  2. Miniconda,使用其conda命令。官网:https://www.anaconda.com/download/success?reg=skipped

conda使用教程

调用对应API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
conda create -n ai_api_test python=3.10 -y
conda create -n ai_api_test python -m pip install openai

conda activate ai_api_test

export DEEPSEEK_API_KEY="***********"
# unset SSL_CERT_FILE

cd $(cygpath -u "D:\temp")
python deepseek_api_test.py

pyton

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Please install OpenAI SDK first: `pip3 install openai`
import os
from openai import OpenAI

# 清空SSL_CERT_FILE环境变量
if "SSL_CERT_FILE" in os.environ:
    del os.environ["SSL_CERT_FILE"]

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)