2. 使用下面源码,替换你申请的实际API 密钥,保存文件 deepseek_v3.py
[Python] 纯文本查看 复制代码# Please install OpenAI SDK first: `pip3 install openai`
# 初始化 OpenAI 客户端
from openai import OpenAI
text = input("请输入对话:\n")
print(" 正在AI对话... 请稍等.....")
# 请替换为你的 API 密钥
client = OpenAI(api_key="sk-a9123API 密钥4569", base_url="https://api.deepseek.com")
# 使用 stream=True 来逐行获取响应
response = client.chat.completions.create(
model="deepseek-chat", # 请确保模型名称正确
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": text},
],
stream=True # 启用流式响应
)
# 逐行显示响应内容
print("AI回复:")
for chunk in response:
if chunk.choices[0].delta.content: # 检查是否有内容
print(chunk.choices[0].delta.content, end="", flush=True) # 逐行显示
print() # 换行
# 获取完整响应内容 (非流式响应)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": text},
],
stream=False
)
# 获取完整响应内容
full_response = response.model_dump_json()
print("---------------------完整响应内容:---------------------------")
print(full_response)
3. 安装 OpenAI API 库
pip install openai -i https://pypi.tuna.tsinghua.edu.cn/simple
4. 参考视频 使用命令 运行代码
python.exe .\deepseek_v3.py