deepseek api如何node调用
时间: 2025-02-14 07:56:30 浏览: 177
### 如何在 Node.js 中调用 DeepSeek API
为了在 Node.js 中调用 DeepSeek API,可以使用 `axios` 库来发送 HTTP 请求。以下是具体实现方法:
#### 安装依赖库
首先安装必要的 npm 包:
```bash
npm install axios
```
#### 初始化客户端并设置参数
创建一个新的 JavaScript 文件,并初始化 Axios 实例以配置基础 URL 和其他选项。
```javascript
const axios = require('axios');
// 配置 Axios 实例
const client = axios.create({
baseURL: 'https://api.deepseek.com',
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`, // 将 API 密钥存储于环境变量中更安全
'Content-Type': 'application/json'
}
});
```
#### 发送请求到 DeepSeek Chat 模型
构建消息体并向指定端点发起 POST 请求。
```javascript
async function callDeepSeekChat() {
try {
const response = await client.post('/v1/chat/completions', {
model: "deepseek-chat",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "你好,DeepSeek!" }
],
stream: false
});
console.log(response.data.choices[0].message.content);
} catch (error) {
console.error(`Error calling DeepSeek API: ${error.message}`);
}
}
```
此代码片段展示了如何通过 Node.js 调用 DeepSeek 的聊天完成接口[^1]。请注意,在实际应用中应妥善管理 API 密钥的安全性,例如将其保存在环境变量或专用的秘密管理系统内而不是硬编码在源码里。
阅读全文
相关推荐


















