Skip to content

Commit

Permalink
Azure OpenAI API Fix (ztjhz#297)
Browse files Browse the repository at this point in the history
* Azure OpenAI API Fix

* improve code

* improve code

---------

Co-authored-by: Jing Hua <[email protected]>
  • Loading branch information
othmanelhoufi and ztjhz authored May 17, 2023
1 parent 82f8761 commit 52fa56c
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ export const getChatCompletion = async (
...customHeaders,
};
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
if (isAzureEndpoint(endpoint) && apiKey) headers['api-key'] = apiKey;

if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const gpt3forAzure = 'gpt-35-turbo';
const model =
config.model === 'gpt-3.5-turbo' ? gpt3forAzure : config.model;
const apiVersion = '2023-03-15-preview';

const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`;

if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}

const response = await fetch(endpoint, {
method: 'POST',
Expand Down Expand Up @@ -43,7 +60,24 @@ export const getChatCompletionStream = async (
...customHeaders,
};
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
if (isAzureEndpoint(endpoint) && apiKey) headers['api-key'] = apiKey;

if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const gpt3forAzure = 'gpt-35-turbo';
const model =
config.model === 'gpt-3.5-turbo' ? gpt3forAzure : config.model;
const apiVersion = '2023-03-15-preview';

const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`;

if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}

const response = await fetch(endpoint, {
method: 'POST',
Expand Down

0 comments on commit 52fa56c

Please sign in to comment.