Skip to content

Commit

Permalink
fix: azure chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Tam authored and glorat committed Jul 3, 2023
1 parent abfd6eb commit 38c2e77
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/AudioTranscriber.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<q-btn round flat :icon="recording ? matStop :matMic " :color="recording ? 'primary' : ''" @click="toggleRecording">
</q-btn>
<btn v-if="props.showPlay" @click="playRecording" :disabled="!audioUrl">Play Recording</btn>
<q-btn v-if="props.showPlay" @click="playRecording" :disabled="!audioUrl">Play Recording</q-btn>
</template>

<script setup lang="ts">
Expand Down
9 changes: 7 additions & 2 deletions src/lib/ai/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function applyAiUserSettings(settings:AiUserSettings) {
}
}

export const getOpenAIAPI = (deployment?: string) => {
export function getOpenAIConfig(deployment?: string) {
if (!OpenAIParams.apiKey) throw new Error('OPENAPI_KEY env not setup. Please check .env file or environment variables')
const params = {...OpenAIParams}
if (params.basePath) {
Expand All @@ -58,7 +58,12 @@ export const getOpenAIAPI = (deployment?: string) => {
throw new Error(`Custom endpoint ${params.basePath} needs a deployment name`)
}
}
return new OpenAIApi(new Configuration(params));
return new Configuration(params)
}

export const getOpenAIAPI = (deployment?: string) => {
const config = getOpenAIConfig(deployment)
return new OpenAIApi(config);
}

export function getSettingsFromLocalStorage() {
Expand Down
19 changes: 16 additions & 3 deletions src/lib/ai/openaiWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Function must be idempotent
*/
import {CreateEmbeddingRequest} from 'openai'
import {Config, getOpenAIAPI, OpenAIParams} from 'src/lib/ai/config'
import {Config, getOpenAIAPI, getOpenAIConfig, OpenAIParams} from 'src/lib/ai/config'
import {logger} from 'src/lib/ai/logger'
import {callWithRetry} from 'src/lib/ai/callWithRetry'
import {LRUCache} from 'lru-cache'
Expand Down Expand Up @@ -115,10 +115,23 @@ export async function createTranscriptionDirect(arg: {blob:Blob}) {
return res.data?.text;
}

function getChatGPTClientOptions(clientOptions?:any) {
const cfg = getOpenAIConfig(Config.chatModel)
if (cfg.basePath) {
// Set up azure mode
// https://kevin-test-openai-1.openai.azure.com//openai/deployments/gpt-35-turbo/chat/completions?api-version=2023-03-15-preview
const opts = {azure: true, reverseProxyUrl: `${cfg.basePath}/chat/completions?api-version=2023-03-15-preview`}
return {...(clientOptions??{}), ...opts}
} else {
return clientOptions ?? {}
}

}

export async function sendChatMessageDirect(arg: {message:string, chatOptions?:any, cache: any, clientOptions?:any}) {
const clientOptions = {}
const clientOptions = getChatGPTClientOptions(arg.clientOptions)
// TODO: make this work for Azure too
const client = new ChatGPTClient(OpenAIParams.apiKey, clientOptions ?? {}, arg.cache)
const client = new ChatGPTClient(OpenAIParams.apiKey, clientOptions, arg.cache)
const res = await client.sendMessage(arg.message, arg.chatOptions)
return res
}

0 comments on commit 38c2e77

Please sign in to comment.