diff --git a/apps/chat.js b/apps/chat.js index 81dd6bd..14a1019 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -69,6 +69,12 @@ import { SendMessageToSpecificGroupOrUserTool } from '../utils/tools/SendMessage import { SetTitleTool } from '../utils/tools/SetTitleTool.js' import { createCaptcha, solveCaptcha, solveCaptchaOneShot } from '../utils/bingCaptcha.js' +try { + await import('@azure/openai') +} catch (err) { + logger.warn('【Azure-Openai】依赖@azure/openai未安装,Azure OpenAI不可用 请执行pnpm install @azure/openai安装') +} + try { await import('emoji-strip') } catch (err) { @@ -1034,6 +1040,10 @@ export class chatgpt extends plugin { key = `CHATGPT:CONVERSATIONS_BARD:${(e.isGroup && Config.groupMerge) ? e.group_id.toString() : e.sender.user_id}` break } + case 'azure': { + key = `CHATGPT:CONVERSATIONS_AZURE:${e.sender.user_id}` + break + } } let ctime = new Date() previousConversation = (key ? await redis.get(key) : null) || JSON.stringify({ @@ -1041,6 +1051,7 @@ export class chatgpt extends plugin { ctime, utime: ctime, num: 0, + messages: [{ role: 'system', content: 'You are an AI assistant that helps people find information.' }], conversation: {} }) previousConversation = JSON.parse(previousConversation) @@ -1048,6 +1059,7 @@ export class chatgpt extends plugin { logger.info({ previousConversation }) } conversation = { + messages: previousConversation.messages, conversationId: previousConversation.conversation?.conversationId, parentMessageId: previousConversation.parentMessageId, clientId: previousConversation.clientId, @@ -1093,6 +1105,11 @@ export class chatgpt extends plugin { } } else if (chatMessage.id) { previousConversation.parentMessageId = chatMessage.id + } else if (chatMessage.message) { + if (previousConversation.messages.length > 10) { + previousConversation.messages.shift() + } + previousConversation.messages.push(chatMessage.message) } if (use === 'bard' && !chatMessage.error) { previousConversation.parentMessageId = chatMessage.responseID @@ -1901,6 +1918,23 @@ export class chatgpt extends plugin { let response = await client.sendMessage(prompt, conversation?.conversationId, image ? image[0] : undefined) return response } + case 'azure': { + let azureModel + try { + azureModel = await import('@azure/openai') + } catch (error) { + throw new Error('未安装@azure/openai包,请执行pnpm install @azure/openai安装') + } + let OpenAIClient = azureModel.OpenAIClient + let AzureKeyCredential = azureModel.AzureKeyCredential + let msg = conversation.messages + let content = { role: 'user', content: prompt } + msg.push(content) + const client = new OpenAIClient(Config.azureUrl, new AzureKeyCredential(Config.apiKey)) + const deploymentName = Config.azureDeploymentName + const { choices } = await client.getChatCompletions(deploymentName, msg) + let completion = choices[0].message; + return {'text' : completion.content, 'message': completion} case 'bard': { // 处理cookie const matchesPSID = /__Secure-1PSID=([^;]+)/.exec(Config.bardPsid) diff --git a/apps/management.js b/apps/management.js index 763a2b5..4a7e070 100644 --- a/apps/management.js +++ b/apps/management.js @@ -108,6 +108,11 @@ export class ChatgptManagement extends plugin { fnc: 'useXinghuoBasedSolution', permission: 'master' }, + { + reg: '^#chatgpt切换azure$', + fnc: 'useAzureBasedSolution', + permission: 'master' + }, { reg: '^#chatgpt切换(Bard|bard)$', fnc: 'useBardBasedSolution', @@ -838,6 +843,15 @@ azure语音:Azure 语音是微软 Azure 平台提供的一项语音服务, await this.reply('当前已经是星火模式了') } } + async useAzureBasedSolution () { + let use = await redis.get('CHATGPT:USE') + if (use !== 'azure') { + await redis.set('CHATGPT:USE', 'azure') + await this.reply('已切换到基于Azure的解决方案') + } else { + await this.reply('当前已经是Azure模式了') + } + } async useBardBasedSolution () { let use = await redis.get('CHATGPT:USE') @@ -894,6 +908,7 @@ azure语音:Azure 语音是微软 Azure 平台提供的一项语音服务, let mode = await redis.get('CHATGPT:USE') const modeMap = { browser: '浏览器', + azure: 'Azure', // apiReverse: 'API2', api: 'API', bing: '必应', diff --git a/config/config.example.json b/config/config.example.json index 6d2d673..35dae41 100644 --- a/config/config.example.json +++ b/config/config.example.json @@ -19,6 +19,8 @@ "openAiBaseUrl": "https://mondstadt.d201.eu.org/v1", "OpenAiPlatformRefreshToken": "", "openAiForceUseReverse": false, + "azureDeploymentName":"", + "azureUrl":"", "drawCD": 30, "model": "", "temperature": 0.8, diff --git a/guoba.support.js b/guoba.support.js index 148406f..0bb7319 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -849,6 +849,28 @@ export function supportGuoba () { label: '合成emoji的API地址,默认谷歌厨房', component: 'Input' }, + { + label: '以下为Azure chatGPT的配置', + component: 'Divider' + }, + { + field: 'apiKey', + label: 'Azure API Key', + bottomHelpMessage: '管理密钥,用于访问Azure的API接口', + component: 'InputPassword' + }, + { + field: 'azureUrl', + label: '端点地址', + bottomHelpMessage: 'https://xxxx.openai.azure.com/', + component: 'Input' + }, + { + field: 'azureDeploymentName', + label: '部署名称', + bottomHelpMessage: '创建部署时输入的名称', + component: 'Input' + }, { label: '以下为后台与渲染相关配置', component: 'Divider' diff --git a/package.json b/package.json index 9171460..e6a60d7 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "type": "module", "author": "ikechan8370", "dependencies": { + "@azure/openai": "^1.0.0-beta.1", "@fastify/cookie": "^8.3.0", "@fastify/cors": "^8.2.0", "@fastify/static": "^6.9.0", diff --git a/utils/common.js b/utils/common.js index 403d100..b25e51d 100644 --- a/utils/common.js +++ b/utils/common.js @@ -717,6 +717,7 @@ export async function getUserData (user) { chat: [], mode: '', cast: { + azure: '', api: '', // API设定 bing: '', // 必应设定 bing_resource: '', // 必应扩展资料