From bd068c9a5ad49e00611ca649ea8ddfae7e62a782 Mon Sep 17 00:00:00 2001 From: Danny Avila <110412045+danny-avila@users.noreply.github.com> Date: Wed, 10 May 2023 23:47:26 -0400 Subject: [PATCH] feat(chatgpt-client.js, titleConvo.js, genAzureEndpoints.js): add support for Azure OpenAI API endpoint generation (#234) This commit adds support for generating Azure OpenAI API endpoints in the `chatgpt-client.js` and `titleConvo.js` files. The `genAzureEndpoint` function in `genAzureEndpoints.js` generates the endpoint URL based on the provided parameters. The `chatgpt-client.js` and `titleConvo.js` files now use this function to generate the endpoint URL when the `AZURE_OPENAI_API_KEY` environment variable is set. --- api/app/clients/chatgpt-client.js | 7 ++++++- api/app/titleConvo.js | 19 ++++++++++++++++--- api/utils/genAzureEndpoints.js | 5 +++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 api/utils/genAzureEndpoints.js diff --git a/api/app/clients/chatgpt-client.js b/api/app/clients/chatgpt-client.js index ced6f4e562..bc18d1e67c 100644 --- a/api/app/clients/chatgpt-client.js +++ b/api/app/clients/chatgpt-client.js @@ -1,5 +1,6 @@ require('dotenv').config(); const { KeyvFile } = require('keyv-file'); +const { genAzureEndpoint } = require('../../utils/genAzureEndpoints'); const askClient = async ({ text, @@ -43,7 +44,11 @@ const askClient = async ({ if (azure) { apiKey = process.env.AZURE_OPENAI_API_KEY; - clientOptions.reverseProxyUrl = `https://${process.env.AZURE_OPENAI_API_INSTANCE_NAME}.openai.azure.com/openai/deployments/${process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME}/chat/completions?api-version=${process.env.AZURE_OPENAI_API_VERSION}`; + clientOptions.reverseProxyUrl = genAzureEndpoint({ + azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, + azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, + azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION + }); } const client = new ChatGPTClient(apiKey, clientOptions, store); diff --git a/api/app/titleConvo.js b/api/app/titleConvo.js index 280c66cbcf..7d624664b3 100644 --- a/api/app/titleConvo.js +++ b/api/app/titleConvo.js @@ -1,7 +1,8 @@ const { Configuration, OpenAIApi } = require('openai'); const _ = require('lodash'); +const { genAzureEndpoint } = require('../utils/genAzureEndpoints'); -const proxyEnvToAxiosProxy = proxyString => { +const proxyEnvToAxiosProxy = (proxyString) => { if (!proxyString) return null; const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/; @@ -47,9 +48,21 @@ const titleConvo = async ({ endpoint, text, response }) => { frequency_penalty: 0 }; - const titleGenClient = new ChatGPTClient(process.env.OPENAI_KEY, titleGenClientOptions); + const azure = process.env.AZURE_OPENAI_API_KEY ? true : false; + let apiKey = process.env.OPENAI_KEY; + + if (azure) { + apiKey = process.env.AZURE_OPENAI_API_KEY; + titleGenClientOptions.reverseProxyUrl = genAzureEndpoint({ + azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, + azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, + azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION + }); + } + + const titleGenClient = new ChatGPTClient(apiKey, titleGenClientOptions); const result = await titleGenClient.getCompletion([instructionsPayload], null); - title = result.choices[0].message.content.replace(/\s+/g, ' ').trim(); + title = result.choices[0].message.content.replace(/\s+/g, ' ').replaceAll('"', '').trim(); } catch (e) { console.error(e); console.log('There was an issue generating title, see error above'); diff --git a/api/utils/genAzureEndpoints.js b/api/utils/genAzureEndpoints.js new file mode 100644 index 0000000000..76b8d10d7e --- /dev/null +++ b/api/utils/genAzureEndpoints.js @@ -0,0 +1,5 @@ +function genAzureEndpoint({ azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName, azureOpenAIApiVersion }) { + return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}/chat/completions?api-version=${azureOpenAIApiVersion}`; +} + +module.exports = { genAzureEndpoint };