能否加入 Azure OpenAI 的相关支持 #471 (#536)

* 能否加入 Azure OpenAI 的相关支持 #471

* Azure OpenAI 锅巴配置

* 增加Azure OpenAI依赖

* 动态加载Azure OpenAI依赖

---------

Co-authored-by: sigeisment <zvdfka@gmail.com>
Co-authored-by: ikechan8370 <geyinchibuaa@gmail.com>
This commit is contained in:
sigeisment 2023-08-24 22:35:29 +08:00 committed by GitHub
parent c2c6ea43de
commit 502cc810bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 0 deletions

View file

@ -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)

View file

@ -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: '必应',