Skip to content

Commit bd068c9

Browse files
authored
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.
1 parent 7997c31 commit bd068c9

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

api/app/clients/chatgpt-client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require('dotenv').config();
22
const { KeyvFile } = require('keyv-file');
3+
const { genAzureEndpoint } = require('../../utils/genAzureEndpoints');
34

45
const askClient = async ({
56
text,
@@ -43,7 +44,11 @@ const askClient = async ({
4344

4445
if (azure) {
4546
apiKey = process.env.AZURE_OPENAI_API_KEY;
46-
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}`;
47+
clientOptions.reverseProxyUrl = genAzureEndpoint({
48+
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
49+
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
50+
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION
51+
});
4752
}
4853

4954
const client = new ChatGPTClient(apiKey, clientOptions, store);

api/app/titleConvo.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { Configuration, OpenAIApi } = require('openai');
22
const _ = require('lodash');
3+
const { genAzureEndpoint } = require('../utils/genAzureEndpoints');
34

4-
const proxyEnvToAxiosProxy = proxyString => {
5+
const proxyEnvToAxiosProxy = (proxyString) => {
56
if (!proxyString) return null;
67

78
const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/;
@@ -47,9 +48,21 @@ const titleConvo = async ({ endpoint, text, response }) => {
4748
frequency_penalty: 0
4849
};
4950

50-
const titleGenClient = new ChatGPTClient(process.env.OPENAI_KEY, titleGenClientOptions);
51+
const azure = process.env.AZURE_OPENAI_API_KEY ? true : false;
52+
let apiKey = process.env.OPENAI_KEY;
53+
54+
if (azure) {
55+
apiKey = process.env.AZURE_OPENAI_API_KEY;
56+
titleGenClientOptions.reverseProxyUrl = genAzureEndpoint({
57+
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
58+
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
59+
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION
60+
});
61+
}
62+
63+
const titleGenClient = new ChatGPTClient(apiKey, titleGenClientOptions);
5164
const result = await titleGenClient.getCompletion([instructionsPayload], null);
52-
title = result.choices[0].message.content.replace(/\s+/g, ' ').trim();
65+
title = result.choices[0].message.content.replace(/\s+/g, ' ').replaceAll('"', '').trim();
5366
} catch (e) {
5467
console.error(e);
5568
console.log('There was an issue generating title, see error above');

api/utils/genAzureEndpoints.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function genAzureEndpoint({ azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName, azureOpenAIApiVersion }) {
2+
return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}/chat/completions?api-version=${azureOpenAIApiVersion}`;
3+
}
4+
5+
module.exports = { genAzureEndpoint };

0 commit comments

Comments
 (0)