Azure OpenAI API Fix (#297)

* Azure OpenAI API Fix

* improve code

* improve code

---------

Co-authored-by: Jing Hua <tohjinghua123@gmail.com>
This commit is contained in:
Othman El houfi 2023-05-17 18:04:06 +02:00 committed by GitHub
parent 82f8761397
commit 52fa56c1da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,24 @@ export const getChatCompletion = async (
...customHeaders, ...customHeaders,
}; };
if (apiKey) headers.Authorization = `Bearer ${apiKey}`; 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, { const response = await fetch(endpoint, {
method: 'POST', method: 'POST',
@ -43,7 +60,24 @@ export const getChatCompletionStream = async (
...customHeaders, ...customHeaders,
}; };
if (apiKey) headers.Authorization = `Bearer ${apiKey}`; 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, { const response = await fetch(endpoint, {
method: 'POST', method: 'POST',