Skip to content

Commit

Permalink
Add support for Azure OpenAI GPT4 (#457)
Browse files Browse the repository at this point in the history
- Azure's GPT4 model response doesn't seem to contain the `delta` field in each `choice`, add null check to ignore the empty content
- Add Azure OpenAI GPT 4 models for building API endpoint
  • Loading branch information
TidesOfMemories committed Nov 11, 2023
1 parent 3e501ea commit 914ab69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ export const getChatCompletion = async (
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const model = config.model === 'gpt-3.5-turbo' ? 'gpt-35-turbo' : config.model === 'gpt-3.5-turbo-16k' ? 'gpt-35-turbo-16k' : config.model;
const modelmapping = {
'gpt-3': 'gpt3',
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt35-turbo-16k',
'gpt-4': 'gpt4-4',
'gpt-4-32k': 'gpt4-4-32k',
}

const model = modelmapping[config.model] || config.model

Check failure on line 29 in src/api/api.ts

View workflow job for this annotation

GitHub Actions / build

Element implicitly has an 'any' type because expression of type 'ModelOptions' can't be used to index type '{ 'gpt-3': string; 'gpt-3.5-turbo': string; 'gpt-3.5-turbo-16k': string; 'gpt-4': string; 'gpt-4-32k': string; }'.

const apiVersion = '2023-03-15-preview';
// set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview
const apiVersion = model === 'gpt4-4' || model === 'gpt4-4-32k' ? '2023-07-01-preview' : '2023-03-15-preview'

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

Expand Down Expand Up @@ -63,10 +72,18 @@ export const getChatCompletionStream = async (
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const model = config.model === 'gpt-3.5-turbo' ? 'gpt-35-turbo' : config.model === 'gpt-3.5-turbo-16k' ? 'gpt-35-turbo-16k' : config.model;
const modelmapping = {
'gpt-3': 'gpt3',
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt35-turbo-16k',
'gpt-4': 'gpt4-4',
'gpt-4-32k': 'gpt4-4-32k',
}

const apiVersion = '2023-03-15-preview';
const model = modelmapping[config.model] || config.model

Check failure on line 83 in src/api/api.ts

View workflow job for this annotation

GitHub Actions / build

Element implicitly has an 'any' type because expression of type 'ModelOptions' can't be used to index type '{ 'gpt-3': string; 'gpt-3.5-turbo': string; 'gpt-3.5-turbo-16k': string; 'gpt-4': string; 'gpt-4-32k': string; }'.

// set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview
const apiVersion = model === 'gpt4-4' || model === 'gpt4-4-32k' ? '2023-07-01-preview' : '2023-03-15-preview'
const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`;

if (!endpoint.endsWith(path)) {
Expand All @@ -89,6 +106,7 @@ export const getChatCompletionStream = async (
});
if (response.status === 404 || response.status === 405) {
const text = await response.text();

if (text.includes('model_not_found')) {
throw new Error(
text +
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const useSubmit = () => {
if (typeof curr === 'string') {
partial += curr;
} else {
const content = curr.choices[0].delta.content;
const content = curr.choices[0]?.delta?.content ?? null;
if (content) output += content;
}
return output;
Expand Down

0 comments on commit 914ab69

Please sign in to comment.