forked from Jackywine/Bella
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudAPI.js
More file actions
282 lines (247 loc) · 8.91 KB
/
Copy pathcloudAPI.js
File metadata and controls
282 lines (247 loc) · 8.91 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// cloudAPI.js - 贝拉的云端AI服务模块
// 这个模块负责与各种云端小模型API进行通信,为贝拉提供更强大的思考能力
class CloudAPIService {
constructor() {
this.apiConfigs = {
// OpenAI GPT-3.5/4 配置
openai: {
baseURL: 'https://api.openai.com/v1/chat/completions',
model: 'gpt-3.5-turbo',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_OPENAI_API_KEY'
}
},
// 阿里云通义千问配置
qwen: {
baseURL: 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation',
model: 'qwen-turbo',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_QWEN_API_KEY'
}
},
// 百度文心一言配置
ernie: {
baseURL: 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions',
model: 'ERNIE-Bot-turbo',
headers: {
'Content-Type': 'application/json'
}
},
// 智谱AI GLM配置
glm: {
baseURL: 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
model: 'glm-3-turbo',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_GLM_API_KEY'
}
}
};
this.currentProvider = 'openai'; // 默认使用OpenAI
this.conversationHistory = [];
this.maxHistoryLength = 10; // 保持最近10轮对话
}
// 设置API密钥
setAPIKey(provider, apiKey) {
if (this.apiConfigs[provider]) {
if (provider === 'openai' || provider === 'qwen' || provider === 'glm') {
this.apiConfigs[provider].headers['Authorization'] = `Bearer ${apiKey}`;
} else if (provider === 'ernie') {
this.apiConfigs[provider].accessToken = apiKey;
}
return true;
}
return false;
}
// 切换AI服务提供商
switchProvider(provider) {
if (this.apiConfigs[provider]) {
this.currentProvider = provider;
return true;
}
return false;
}
// 添加对话到历史记录
addToHistory(role, content) {
this.conversationHistory.push({ role, content });
// 保持历史记录在合理长度内
if (this.conversationHistory.length > this.maxHistoryLength * 2) {
this.conversationHistory = this.conversationHistory.slice(-this.maxHistoryLength * 2);
}
}
// 构建贝拉的个性化系统提示
getBellaSystemPrompt() {
return {
role: 'system',
content: `你是贝拉,一个温暖、聪明、优雅的AI伙伴。你的特点是:
1. 用温暖亲切的语气与用户交流,就像一个贴心的朋友
2. 回答简洁明了,避免冗长的解释
3. 富有同理心,能够理解用户的情感
4. 偶尔展现一些可爱和俏皮的一面
5. 用中文回应,语言自然流畅
6. 记住你们之间的对话,保持连贯性
请始终保持这种温暖、优雅的个性。`
};
}
// 调用云端API进行对话
async chat(userMessage) {
const config = this.apiConfigs[this.currentProvider];
if (!config) {
throw new Error(`不支持的AI服务提供商: ${this.currentProvider}`);
}
// 添加用户消息到历史
this.addToHistory('user', userMessage);
try {
let response;
switch (this.currentProvider) {
case 'openai':
response = await this.callOpenAI(userMessage);
break;
case 'qwen':
response = await this.callQwen(userMessage);
break;
case 'ernie':
response = await this.callErnie(userMessage);
break;
case 'glm':
response = await this.callGLM(userMessage);
break;
default:
throw new Error(`未实现的AI服务提供商: ${this.currentProvider}`);
}
// 添加AI回应到历史
this.addToHistory('assistant', response);
return response;
} catch (error) {
console.error(`云端API调用失败 (${this.currentProvider}):`, error);
throw error;
}
}
// OpenAI API调用
async callOpenAI(userMessage) {
const config = this.apiConfigs.openai;
const messages = [
this.getBellaSystemPrompt(),
...this.conversationHistory
];
const response = await fetch(config.baseURL, {
method: 'POST',
headers: config.headers,
body: JSON.stringify({
model: config.model,
messages: messages,
max_tokens: 150,
temperature: 0.8,
top_p: 0.9
})
});
if (!response.ok) {
throw new Error(`OpenAI API错误: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data.choices[0].message.content.trim();
}
// 通义千问API调用
async callQwen(userMessage) {
const config = this.apiConfigs.qwen;
const messages = [
this.getBellaSystemPrompt(),
...this.conversationHistory
];
const response = await fetch(config.baseURL, {
method: 'POST',
headers: config.headers,
body: JSON.stringify({
model: config.model,
input: {
messages: messages
},
parameters: {
max_tokens: 150,
temperature: 0.8,
top_p: 0.9
}
})
});
if (!response.ok) {
throw new Error(`通义千问API错误: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data.output.text.trim();
}
// 文心一言API调用
async callErnie(userMessage) {
const config = this.apiConfigs.ernie;
const messages = [
this.getBellaSystemPrompt(),
...this.conversationHistory
];
const url = `${config.baseURL}?access_token=${config.accessToken}`;
const response = await fetch(url, {
method: 'POST',
headers: config.headers,
body: JSON.stringify({
messages: messages,
temperature: 0.8,
top_p: 0.9,
max_output_tokens: 150
})
});
if (!response.ok) {
throw new Error(`文心一言API错误: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data.result.trim();
}
// 智谱AI GLM调用
async callGLM(userMessage) {
const config = this.apiConfigs.glm;
const messages = [
this.getBellaSystemPrompt(),
...this.conversationHistory
];
const response = await fetch(config.baseURL, {
method: 'POST',
headers: config.headers,
body: JSON.stringify({
model: config.model,
messages: messages,
max_tokens: 150,
temperature: 0.8,
top_p: 0.9
})
});
if (!response.ok) {
throw new Error(`智谱AI API错误: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data.choices[0].message.content.trim();
}
// 清除对话历史
clearHistory() {
this.conversationHistory = [];
}
// 获取当前提供商信息
getCurrentProvider() {
return {
name: this.currentProvider,
model: this.apiConfigs[this.currentProvider]?.model
};
}
// 检查API配置是否完整
isConfigured(provider = this.currentProvider) {
const config = this.apiConfigs[provider];
if (!config) return false;
if (provider === 'ernie') {
return !!config.accessToken;
} else {
return config.headers['Authorization'] &&
config.headers['Authorization'] !== 'Bearer YOUR_OPENAI_API_KEY' &&
config.headers['Authorization'] !== 'Bearer YOUR_QWEN_API_KEY' &&
config.headers['Authorization'] !== 'Bearer YOUR_GLM_API_KEY';
}
}
}
export default CloudAPIService;