【AI大模型】使用Python调用DeepSeek的API,原来SDK是调用这个,绝对的一分钟上手和使用
前言
在当今数字化时代,AI大模型技术迅速发展,DeepSeek作为其中的佼佼者,凭借其强大的语言理解和生成能力,受到了众多开发者的关注。作为一名对前沿技术充满热情的程序员,对接DeepSeek的API进行体验和开发是探索其潜力的重要一步。本文将为您提供一份详细且清晰的保姆级教程,帮助您快速上手并使用DeepSeek的API。
免费体验
DeepSeek为新用户提供了一个免费体验的机会,这对于初学者来说是一个绝佳的起点。截至2025年2月8日,注册DeepSeek账户会赠送10元,有效期为一个月。这10元可以支持您发起多次对话,具体能调用多少次,根据DeepSeek给出的答案,大概在1到5千次之间。您可以利用这个免费体验期,充分探索DeepSeek的功能和性能。
API-Key申请
申请API-Key是使用DeepSeek API的第一步。过程非常简单,只需按照以下步骤操作:
- 登录DeepSeek官网:www.deepseek.com/。
- 点击右上角的"API开放平台",如果没有登录则会自动跳转至登录或注册页面。
- 输入手机号后,点击发送验证码,勾选复选框,输入验证码即可登录。
- 登录成功后,进入API开发平台,点击左边菜单的"API keys",然后点击"创建API KEY"。
- 在弹出框中输入一个名称,点击"创建"按钮,生成API key后,一定要点击复制按钮进行复制并保存。
首次调用API
拿到API-Key后,就可以开始首次调用API了。以下是使用Python调用DeepSeek API的示例代码:
python
# Please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False
)
print(response.choices[0].message.content)
这段代码首先导入了OpenAI库,然后使用您的API-Key和DeepSeek的API基础URL创建了一个客户端。接着,通过调用chat.completions.create
方法,向模型发送了一个简单的对话请求,并打印出模型返回的回复内容。
基本概念
在深入了解DeepSeek API之前,了解一些基本概念是非常重要的。
最小单元
Token是模型用来表示自然语言文本的最小单位,可以是一个词、一个数字或一个标点符号等。DeepSeek会根据模型输入和输出的总Token数进行计量计费。
推理模型
DeepSeek推出了推理模型deepseek-reasoner
。在输出最终回答之前,模型会先输出一段思维链内容,以提升最终答案的准确性。API向用户开放了deepseek-reasoner
思维链的内容,供用户查看、展示、蒸馏使用。在每一轮对话过程中,模型会输出思维链内容(reasoning_content
)和最终回答(content
),但之前轮输出的思维链内容不会被拼接到上下文中。
智能体
代码层对话
通过代码调用DeepSeek API实现对话功能是一种常见的应用。您可以根据需求,将API调用封装成函数或类,方便在项目中重复使用。例如,您可以创建一个DeepSeekChat
类,用于管理与DeepSeek模型的对话。
自定义界面
为了更好地展示和使用DeepSeek的功能,您可以创建自定义的用户界面。以下是使用Vue.js创建的一个简单的对话界面示例:
vue
<template>
<div class="chat-interface">
<el-card shadow="hover">
<div class="chat-history">
<div v-for="(message, index) in messages" :key="index" class="chat-message">
<div :class="{'message-user': message.type === 'user', 'message-bot': message.type === 'bot'}">
<span class="avatar" :style="{ backgroundColor: message.type === 'user' ? '#409EFF' : '#F56C6C' }">
{{ message.type === 'user' ? '我' : '李白' }}
</span>
<div class="content">
{{ message.content }}
</div>
</div>
</div>
</div>
<div class="input-area">
<el-input v-model="userInput" placeholder="输入你的问题" class="input-box" clearable></el-input>
<el-button type="primary" @click="sendMessage" :loading="loadingFlag">发送</el-button>
</div>
</el-card>
</div>
</template>
<script setup lang="ts" name="batchPortfolio">
import { ref } from 'vue';
import axios from 'axios';
const loadingFlag=ref(false)
const userInput = ref('');
const messages: { type: 'user' | 'bot'; content: string }[]= ref([]);
const sendMessage = async () => {
if (userInput.value.trim()) {
messages.value.push({ type: 'user', content: `${userInput.value}` });
loadingFlag.value=true;
const response = await axios.get(`http://127.0.0.1:5000/generate_text/${userInput.value}`);
loadingFlag.value=false;
// 机器人回复
setTimeout(() => {
messages.value.push({ type: 'bot', content: `${response.data.generated_text}` });
}, 100);
// // 模拟机器人回复
// setTimeout(() => {
// messages.value.push({ type: 'bot', content: `机器人回复: ${userInput.value.split(' ').join(' ')} 的回复` });
// }, 1000);
userInput.value = '';
}
};
</script>
<style scoped lang="scss">
.chat-interface {
max-width: 600px;
margin: 0 auto;
padding: 20px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
border-radius: 8px;
background-color: #fff;
}
.chat-history {
padding: 16px;
overflow-y: auto;
max-height: 400px;
border-bottom: 1px solid #ebeef5;
}
.chat-message {
margin-bottom: 16px;
align-items: center;
}
.message-user {
display: flex;
align-items: center;
justify-content: flex-end;
line-height: 40px;
text-align: center;
color: #fff;
}
.message-bot {
display: flex;
align-items: center;
line-height: 40px;
text-align: center;
color: #fff;
}
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
margin-right: 12px;
}
.message-user .avatar {
background-color: #409EFF;
}
.message-bot .avatar {
background-color: #F56C6C;
}
.content {
max-width: calc(100% - 52px); /* 40px avatar + 12px margin */
padding: 8px 16px;
border-radius: 4px;
background-color: #f0f0f0;
color: #333;
}
.message-bot .content {
background-color: #fff3e0;
justify-content: flex-start;
}
.message-user .content {
background-color: #e6f7ff;
}
.input-area {
display: flex;
padding: 16px;
border-top: 1px solid #ebeef5;
}
.input-box {
flex: 1;
border-radius: 4px;
}
.el-button {
margin-left: 12px;
}
</style>
这个界面允许用户输入问题,并通过调用后端API获取DeepSeek模型的回复。
总结
DeepSeek的API对接过程非常简洁清晰,从创建API-Key到直接调用API并返回数据,整个过程不到一分钟就能完成。这使得开发者能够快速上手并开始探索其强大的功能。当然,在实际应用中,您可能还需要花费一些时间来优化代码、调整自定义界面以及封装API调用,但DeepSeek提供的强大支持和丰富的功能将为您的项目带来无限可能。