-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathDeepSeek.js
69 lines (61 loc) · 2.47 KB
/
DeepSeek.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import axios from 'axios';
// https://platform.deepseek.com/usage
// https://platform.deepseek.com/api_keys
class DeepSeek {
constructor({apiKey, baseURL}) {
if (!apiKey) {
throw new Error('Missing required configuration parameters.');
}
this.apiKey = apiKey;
this.baseURL = baseURL || 'https://api.deepseek.com'; // 默认的API基础URL
this.userContexts = {}; // 存储每个用户的上下文
}
// 初始化用户上下文
initUserContext(userId) {
if (!this.userContexts[userId]) {
this.userContexts[userId] = [
{role: "system", content: "你是一名优秀的AI助手,知道最新的互联网内容,善用搜索引擎和github并总结最贴切的结论来回答我提出的每一个问题"}
];
}
}
// 更新用户上下文
updateUserContext(userId, message) {
this.userContexts[userId].push(message);
// 控制上下文长度,保留最新的20条消息
if (this.userContexts[userId].length > 20) {
this.userContexts[userId] = this.userContexts[userId].slice(-20);
}
}
async ask(userId, prompt, options = {}) {
this.initUserContext(userId); // 初始化用户上下文
const payload = {
model: 'deepseek-chat', // 使用的模型名称
messages: this.userContexts[userId].concat([{
role: 'user',
content: prompt
}]),
...options, // 其他选项,如 temperature、max_tokens 等
};
try {
const response = await axios.post(`${this.baseURL}/chat/completions`, payload, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`, // 使用鉴权信息
},
});
if (response.data && response.data.choices) {
const assistantMessage = response.data.choices[0].message;
this.updateUserContext(userId, assistantMessage); // 更新用户上下文
return assistantMessage.content;
} else {
throw new Error(
`Error from DeepSeek AI: ${response.data.error || 'Unknown error'}`
);
}
} catch (error) {
console.error('Error while communicating with DeepSeek AI:', error.message);
throw error;
}
}
}
export default DeepSeek;