Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support azure openai batching #931

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/providers/azure-openai/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ const AzureOpenAIAPIConfig: ProviderAPIConfig = {
}
}

const segments = gatewayRequestURL?.split('/');
const id = segments?.at(-1) ?? '';
const path = gatewayRequestURL.split('/v1')?.[1];

switch (mappedFn) {
case 'complete': {
Expand Down Expand Up @@ -105,15 +104,23 @@ const AzureOpenAIAPIConfig: ProviderAPIConfig = {
return `/realtime?api-version=${apiVersion}&deployment=${providerOptions.deploymentId}`;
}
case 'uploadFile':
return `/files?api-version=${apiVersion}`;
return `${path}?api-version=${apiVersion}`;
case 'retrieveFile':
return `/files/${id}?api-version=${apiVersion}`;
return `${path}?api-version=${apiVersion}`;
case 'listFiles':
return `/files?api-version=${apiVersion}`;
return `${path}?api-version=${apiVersion}`;
case 'deleteFile':
return `/files/${id}?api-version=${apiVersion}`;
return `${path}?api-version=${apiVersion}`;
case 'retrieveFileContent':
return `/files/${id}/content?api-version=${apiVersion}`;
return `${path}?api-version=${apiVersion}`;
case 'createBatch':
return `${path}?api-version=${apiVersion}`;
case 'retrieveBatch':
return `${path}?api-version=${apiVersion}`;
case 'cancelBatch':
return `${path}/cancel?api-version=${apiVersion}`;
case 'listBatches':
return `${path}?api-version=${apiVersion}`;
default:
return '';
}
Expand Down
21 changes: 21 additions & 0 deletions src/providers/azure-openai/createBatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProviderConfig } from '../types';

export const AzureOpenAICreateBatchConfig: ProviderConfig = {
input_file_id: {
param: 'input_file_id',
required: true,
},
endpoint: {
param: 'endpoint',
required: true,
},
completion_window: {
param: 'completion_window',
default: '24h',
required: true,
},
metadata: {
param: 'metadata',
required: false,
},
};
84 changes: 84 additions & 0 deletions src/providers/azure-openai/getBatchOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Context } from 'hono';
import AzureOpenAIAPIConfig from './api';
import { Options } from '../../types/requestBody';
import { RetrieveBatchResponse } from '../types';

// Return a ReadableStream containing batches output data
export const AzureOpenAIGetBatchOutputRequestHandler = async ({
c,
providerOptions,
requestURL,
}: {
c: Context;
providerOptions: Options;
requestURL: string;
}) => {
// get batch details which has ouptut file id
// get file content as ReadableStream
// return file content
const baseUrl = AzureOpenAIAPIConfig.getBaseURL({
providerOptions,
fn: 'retrieveBatch',
c,
});
const retrieveBatchRequestURL = requestURL.replace('/output', '');
const retrieveBatchURL =
baseUrl +
AzureOpenAIAPIConfig.getEndpoint({
providerOptions,
fn: 'retrieveBatch',
gatewayRequestURL: retrieveBatchRequestURL,
c,
gatewayRequestBodyJSON: {},
gatewayRequestBody: {},
});
const retrieveBatchesHeaders = await AzureOpenAIAPIConfig.headers({
c,
providerOptions,
fn: 'retrieveBatch',
transformedRequestBody: {},
transformedRequestUrl: retrieveBatchURL,
gatewayRequestBody: {},
});
const retrieveBatchesResponse = await fetch(retrieveBatchURL, {
method: 'GET',
headers: retrieveBatchesHeaders,
});

const batchDetails: RetrieveBatchResponse =
await retrieveBatchesResponse.json();

const outputFileId = batchDetails.output_file_id;
if (!outputFileId) {
const errors = batchDetails.errors;
if (errors) {
return new Response(JSON.stringify(errors), {
status: 200,
});
}
}
const retrieveFileContentRequestURL = `https://api.portkey.ai/v1/files/${outputFileId}/content`; // construct the entire url instead of the path of sanity sake
const retrieveFileContentURL =
baseUrl +
AzureOpenAIAPIConfig.getEndpoint({
providerOptions,
fn: 'retrieveFileContent',
gatewayRequestURL: retrieveFileContentRequestURL,
c,
gatewayRequestBodyJSON: {},
gatewayRequestBody: {},
});
const retrieveFileContentHeaders = await AzureOpenAIAPIConfig.headers({
c,
providerOptions,
fn: 'retrieveFileContent',
transformedRequestBody: {},
transformedRequestUrl: retrieveFileContentURL,
gatewayRequestBody: {},
});
const response = fetch(retrieveFileContentURL, {
method: 'GET',
headers: retrieveFileContentHeaders,
});
return response;
};
10 changes: 10 additions & 0 deletions src/providers/azure-openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { AzureOpenAICreateTranscriptionResponseTransform } from './createTranscr
import { AzureOpenAICreateTranslationResponseTransform } from './createTranslation';
import { AzureOpenAIResponseTransform } from './utils';
import { AzureOpenAIRequestTransform } from './uploadFile';
import { AzureOpenAICreateBatchConfig } from './createBatch';
import { AzureOpenAIGetBatchOutputRequestHandler } from './getBatchOutput';

const AzureOpenAIConfig: ProviderConfigs = {
complete: AzureOpenAICompleteConfig,
Expand All @@ -34,7 +36,11 @@ const AzureOpenAIConfig: ProviderConfigs = {
createSpeech: AzureOpenAICreateSpeechConfig,
createTranscription: {},
createTranslation: {},
createBatch: AzureOpenAICreateBatchConfig,
realtime: {},
requestHandlers: {
getBatchOutput: AzureOpenAIGetBatchOutputRequestHandler,
},
responseTransforms: {
complete: AzureOpenAICompleteResponseTransform,
chatComplete: AzureOpenAIChatCompleteResponseTransform,
Expand All @@ -49,6 +55,10 @@ const AzureOpenAIConfig: ProviderConfigs = {
retrieveFile: AzureOpenAIResponseTransform,
deleteFile: AzureOpenAIResponseTransform,
retrieveFileContent: AzureOpenAIResponseTransform,
createBatch: AzureOpenAIResponseTransform,
retrieveBatch: AzureOpenAIResponseTransform,
cancelBatch: AzureOpenAIResponseTransform,
listBatches: AzureOpenAIResponseTransform,
},
requestTransforms: {
uploadFile: AzureOpenAIRequestTransform,
Expand Down