-
Notifications
You must be signed in to change notification settings - Fork 4
/
chat.js
76 lines (64 loc) · 2.24 KB
/
chat.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
70
71
72
73
74
75
76
import ModelClient from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
import { getUserModelName, getUserTemperature, saveUserModelName, saveUserTemperature } from './user.js';
import dotenv from 'dotenv';
dotenv.config();
const token = process.env['GITHUB_TOKEN'];
const endpoint = 'https://models.inference.ai.azure.com';
let modelName = 'gpt-4o'; // 默认模型
let temperature = 0.7; // 默认温度
// 设置模型名称的函数
function setModelName(userId, name) {
modelName = name;
saveUserModelName(userId, name);
resetUserHistory(userId);
}
// 设置温度的函数
function setTemperature(userId, temp) {
temperature = temp;
saveUserTemperature(userId, temp);
}
// 使用 Map 来保存每个用户的对话历史
const userHistories = new Map();
// 重置或初始化特定用户的对话历史
function resetUserHistory(userId) {
userHistories.set(userId, [{ role: 'system', content: 'You are a helpful assistant.' }]);
}
// 获取特定用户的对话历史
function getUserHistory(userId) {
if (!userHistories.has(userId)) {
resetUserHistory(userId);
}
return userHistories.get(userId);
}
// 更新对话历史
function updateMessageHistory(userId, role, content) {
const history = getUserHistory(userId);
history.push({ role, content });
}
// 获取 AI 的响应
async function getAIResponse(userId, userInput) {
const client = new ModelClient(endpoint, new AzureKeyCredential(token));
// 更新用户消息到对话历史
updateMessageHistory(userId, 'user', userInput);
const messageHistory = getUserHistory(userId);
try {
const response = await client.path("/chat/completions").post({
body: {
messages: messageHistory,
model: modelName,
temperature: temperature,
}
});
if (response.status !== "200") {
throw new Error(response.body.error.message);
}
const content = response.body.choices[0].message.content;
updateMessageHistory(userId, 'assistant', content);
return content;
} catch (error) {
console.error('无法获取响应:', error);
return '我目前无法处理你的请求,因为遇到了错误:' + error.message;
}
}
export { setModelName, setTemperature, resetUserHistory, getAIResponse };