Skip to content

Commit

Permalink
✨ Feature(custom): add support for azure openai api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jun 9, 2023
1 parent 9525c5e commit 61226d1
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 53 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"release": "bump-version"
},
"dependencies": {
"@azure/openai": "^1.0.0-beta.2",
"@element-plus/icons-vue": "^2.1.0",
"axios": "^1.4.0",
"chatgpt": "^5.2.5",
Expand Down
45 changes: 45 additions & 0 deletions src/api/azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { OpenAIClient, AzureKeyCredential, GetChatCompletionsOptions, ChatCompletions } from '@azure/openai'
import { Ref } from 'vue'

async function createChatCompletionStream (
azureAPIKey: string,
azureAPIEndpoint: string,
azureDeploymentName: string,
messages: any[],
result: Ref<string>,
historyDialog: Ref<any[]>,
errorIssue: Ref<boolean>,
loading: Ref<boolean>,
maxTokens?: number,
temperature?: number
): Promise<void> {
const client = new OpenAIClient(
azureAPIEndpoint,
new AzureKeyCredential(azureAPIKey)
)
const requestConfig: GetChatCompletionsOptions = {
maxTokens: maxTokens ?? 800,
temperature: temperature ?? 0.7,
stream: false
}
let response
let data
try {
response = await client.getChatCompletions(azureDeploymentName, messages, requestConfig) as ChatCompletions
data = response
result.value = data.choices[0].message?.content?.replace(/\\n/g, '\n') ?? ''
historyDialog.value.push({
role: 'assistant',
content: result.value
})
} catch (error) {
result.value = String(error)
errorIssue.value = true
console.error(error)
}
loading.value = false
}

export default {
createChatCompletionStream
}
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import webapi from './webapi'
import official from './official'
import azure from './azure'
import common from './common'

export default {
webapi,
official,
azure,
common
}
4 changes: 4 additions & 0 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const messages = {
cancel: 'Cancel',
settingOpen: 'Open',
settingClose: 'Close',
settingAzureDeploymentName: 'Deploy name',
settingAzureEndpoint: 'endpoint',
settingChooseLanguage: 'Language',
settingTemperature: 'Temperature',
settingMaxTokens: 'Max tokens',
Expand Down Expand Up @@ -72,6 +74,8 @@ const messages = {
cancel: '取消',
settingOpen: '开',
settingClose: '关',
settingAzureDeploymentName: '部署名',
settingAzureEndpoint: 'endpoint',
settingChooseLanguage: '语言',
settingTemperature: 'temperature',
settingMaxTokens: 'max tokens',
Expand Down
58 changes: 48 additions & 10 deletions src/pages/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,27 @@ const replyLanguageList = Object.values(languageMap).map((key) => ({
value: key
}))
const api = ref<'web-api' | 'official'>('web-api')
const api = ref<'web-api' | 'official' | 'azure'>('web-api')
const apiKey = ref('')
const accessToken = ref('')
const azureAPIKey = ref('')
const localLanguage = ref('en')
const replyLanguage = ref('English')
const webModel = ref('default')
const temperature = ref(0.7)
const maxTokens = ref(800)
const model = ref('gpt-3.5-turbo')
const webModel = ref('default')
const replyLanguage = ref('English')
const basePath = ref('')
const proxy = ref<AxiosProxyConfig | false>(false)
const azureAPIEndpoint = ref('')
const azureDeploymentName = ref('')
const azureMaxTokens = ref(800)
const azureTemperature = ref(0.7)
const systemPrompt = ref('')
const systemPromptSelected = ref('')
const systemPromptList = ref<IStringKeyMap[]>([])
Expand Down Expand Up @@ -526,19 +534,25 @@ function handelPromptChange (val: string) {
}
onBeforeMount(async () => {
api.value = localStorage.getItem(localStorageKey.api) as 'web-api' | 'official' ?? 'web-api'
api.value = localStorage.getItem(localStorageKey.api) as 'web-api' | 'official' | 'azure' ?? 'web-api'
replyLanguage.value = localStorage.getItem(localStorageKey.replyLanguage) ?? 'English'
localLanguage.value = localStorage.getItem(localStorageKey.localLanguage) ?? 'en'
apiKey.value = localStorage.getItem(localStorageKey.apiKey) ?? ''
accessToken.value = localStorage.getItem(localStorageKey.accessToken) ?? ''
localLanguage.value = localStorage.getItem(localStorageKey.localLanguage) ?? 'en'
temperature.value = Number(localStorage.getItem(localStorageKey.temperature)) ?? 0.7
maxTokens.value = Number(localStorage.getItem(localStorageKey.maxTokens)) ?? 800
model.value = localStorage.getItem(localStorageKey.model) ?? 'gpt-3.5-turbo'
azureAPIKey.value = localStorage.getItem(localStorageKey.azureAPIKey) ?? ''
webModel.value = localStorage.getItem(localStorageKey.webModel) ?? 'default'
replyLanguage.value = localStorage.getItem(localStorageKey.replyLanguage) ?? 'English'
temperature.value = Number(localStorage.getItem(localStorageKey.temperature)) || 0.7
maxTokens.value = Number(localStorage.getItem(localStorageKey.maxTokens)) || 800
model.value = localStorage.getItem(localStorageKey.model) ?? 'gpt-3.5-turbo'
basePath.value = localStorage.getItem(localStorageKey.basePath) ?? ''
proxy.value = localStorage.getItem(localStorageKey.enableProxy) === 'false'
? false
: JSON.parse(localStorage.getItem(localStorageKey.proxy) || 'false')
azureAPIEndpoint.value = localStorage.getItem(localStorageKey.azureAPIEndpoint) ?? ''
azureDeploymentName.value = localStorage.getItem(localStorageKey.azureDeploymentName) ?? ''
azureMaxTokens.value = Number(localStorage.getItem(localStorageKey.azureMaxTokens)) || 800
console.log(azureMaxTokens.value)
azureTemperature.value = Number(localStorage.getItem(localStorageKey.azureTemperature)) || 0.7
insertType.value = localStorage.getItem(localStorageKey.insertType) ?? 'replace' as 'replace' | 'append' | 'newLine' | 'NoAction'
systemPrompt.value = localStorage.getItem(localStorageKey.defaultSystemPrompt) ?? 'Act like a personal assistant.'
await getSystemPromptList()
Expand Down Expand Up @@ -619,6 +633,29 @@ async function template (taskType: keyof typeof buildInPrompt | 'custom') {
insertType,
loading
)
} else if (api.value === 'azure' && azureAPIKey.value) {
historyDialog.value = [
{
role: 'system',
content: systemMessage
},
{
role: 'user',
content: userMessage
}
]
await API.azure.createChatCompletionStream(
azureAPIKey.value,
azureAPIEndpoint.value,
azureDeploymentName.value,
historyDialog.value,
result,
historyDialog,
errorIssue,
loading,
azureMaxTokens.value,
azureTemperature.value
)
} else {
ElMessage.error('Set API Key or Access Token first')
return
Expand All @@ -637,7 +674,8 @@ function checkApiKey () {
const auth = {
type: api.value,
accessToken: accessToken.value,
apiKey: apiKey.value
apiKey: apiKey.value,
azureAPIKey: azureAPIKey.value
}
if (!checkAuth(auth)) {
ElMessage.error('Set API Key or Access Token first')
Expand Down
Loading

0 comments on commit 61226d1

Please sign in to comment.