快速上手DeepSeek API:从零开始调用你的第一个AI大模型
本文详细介绍了如何快速上手DeepSeek API,包括基础配置、API密钥获取、模型升级信息,并提供了cURL、Python和Node.js三种语言的完整调用示例,帮助开发者从零开始调用AI大模型。
为什么选择DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.?
DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.采用了与OpenAI兼容的API格式,这意味着你可以轻松地将现有的OpenAI SDK或兼容OpenAI APIThe application programming interface specification developed by OpenAI for accessing their AI models.的软件迁移到DeepSeek平台。这种兼容性设计大大降低了开发者的学习成本和技术迁移难度。
基础配置:两步搞定
1. 设置基础URL
DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.的基础URL为:
https://api.deepseek.com
重要提示:
- 为了保持与OpenAI的兼容性,你也可以使用
https://api.deepseek.com/v1作为基础URL - 请注意,这里的
v1仅表示API版本,与模型版本没有任何关系
2. 获取API密钥
访问DeepSeek官方网站申请你的API密钥。这是调用API的通行证,请妥善保管。
模型升级通知
DeepSeek近期完成了重要升级:
- deepseek-chatA DeepSeek model optimized for fast responses in standard conversational scenarios without reasoning mode. 和 deepseek-reasonerA DeepSeek model designed for deep reasoning and complex problem analysis with thinking mode enabled. 现已升级为 DeepSeek-V3.2The underlying advanced architecture powering DeepSeek's current core language models.
- deepseek-chatA DeepSeek model optimized for fast responses in standard conversational scenarios without reasoning mode.:DeepSeek-V3.2The underlying advanced architecture powering DeepSeek's current core language models.的非思考模式
- deepseek-reasonerA DeepSeek model designed for deep reasoning and complex problem analysis with thinking mode enabled.:DeepSeek-V3.2The underlying advanced architecture powering DeepSeek's current core language models.的思考模式
实战:调用聊天API
获得API密钥后,你可以使用以下示例代码开始调用DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.。这是一个非流式响应的例子,你可以将 stream 参数设置为 true 来获取流式响应。
cURL示例
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
Python示例
import requests
import os
api_key = os.getenv("DEEPSEEK_API_KEY")
url = "https://api.deepseek.com/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": False
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
Node.js示例
const fetch = require('node-fetch');
const apiKey = process.env.DEEPSEEK_API_KEY;
const url = 'https://api.deepseek.com/chat/completions';
const data = {
model: 'deepseek-chat',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' }
],
stream: false
};
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
最佳实践建议
- 环境变量管理:始终将API密钥存储在环境变量中,避免硬编码
- 错误处理:添加适当的错误处理机制,处理网络异常和API限制
- 版本控制:关注DeepSeek官方文档,及时了解API和模型的更新
- 性能优化:根据需求选择合适的响应模式(流式/非流式)
进阶功能探索
DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.不仅支持基础的聊天功能,还提供:
- 流式响应(实时获取生成内容)
- 多种模型选择(聊天、推理等)
- 可配置的参数调整(温度、最大令牌数等)
- 系统角色定制
开始你的AI之旅
现在你已经掌握了DeepSeek APIAn AI model API service compatible with OpenAI's API format for accessing DeepSeek's language models.的基础调用方法。从简单的问候开始,逐步探索更复杂的应用场景,让DeepSeek的强大AI能力为你的项目赋能!
提示:在实际开发中,建议使用官方SDK或成熟的HTTP客户端库,以获得更好的稳定性和开发体验。
Data Analysis
| 模型名称 (API 参数) | 对应版本/模式 | 说明 |
|---|---|---|
deepseek-chat |
DeepSeek-V3.2The underlying advanced architecture powering DeepSeek's current core language models. (非思考模式) | 适用于通用对话和聊天场景。 |
deepseek-reasoner |
DeepSeek-V3.2The underlying advanced architecture powering DeepSeek's current core language models. (思考模式) | 启用模型的深度推理能力。 |
Source/Note: 根据文本中“模型升级通知”部分的信息整理。
版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。
文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。
若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。