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 openai organization and project #365

Merged
merged 4 commits into from
May 15, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RESPONSE_HEADER_KEYS,
RETRY_STATUS_CODES,
GOOGLE_VERTEX_AI,
OPEN_AI,
} from '../globals';
import Providers from '../providers';
import { ProviderAPIConfig, endpointStrings } from '../providers/types';
Expand Down Expand Up @@ -989,6 +990,11 @@ export function constructConfigFromRequestHeaders(
workersAiAccountId: requestHeaders[`x-${POWERED_BY}-workers-ai-account-id`],
};

const openAiConfig = {
openaiOrganization: requestHeaders[`x-${POWERED_BY}-openai-organization`],
openaiProject: requestHeaders[`x-${POWERED_BY}-openai-project`],
};

const vertexConfig = {
vertexProjectId: requestHeaders[`x-${POWERED_BY}-vertex-project-id`],
vertexRegion: requestHeaders[`x-${POWERED_BY}-vertex-region`],
Expand Down Expand Up @@ -1024,6 +1030,13 @@ export function constructConfigFromRequestHeaders(
...workersAiConfig,
};
}

if (parsedConfigJson.provider === OPEN_AI) {
parsedConfigJson = {
...parsedConfigJson,
...openAiConfig,
};
}
}
return convertKeysToCamelCase(parsedConfigJson, [
'override_params',
Expand All @@ -1042,5 +1055,6 @@ export function constructConfigFromRequestHeaders(
workersAiConfig),
...(requestHeaders[`x-${POWERED_BY}-provider`] === GOOGLE_VERTEX_AI &&
vertexConfig),
...(requestHeaders[`x-${POWERED_BY}-provider`] === OPEN_AI && openAiConfig),
};
}
3 changes: 3 additions & 0 deletions src/middlewares/requestValidator/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export const configSchema: any = z
// Google Vertex AI specific
vertex_project_id: z.string().optional(),
vertex_region: z.string().optional(),
// OpenAI specific
openai_project: z.string().optional(),
openai_organization: z.string().optional(),
})
.refine(
(value) => {
Expand Down
13 changes: 12 additions & 1 deletion src/providers/openai/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import { ProviderAPIConfig } from '../types';
const OpenAIAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://api.openai.com/v1',
headers: ({ providerOptions }) => {
return { Authorization: `Bearer ${providerOptions.apiKey}` };
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
if (providerOptions.openaiOrganization) {
headersObj['OpenAI-Organization'] = providerOptions.openaiOrganization;
}

if (providerOptions.openaiProject) {
headersObj['OpenAI-Project'] = providerOptions.openaiProject;
}

return headersObj;
},
getEndpoint: ({ fn }) => {
switch (fn) {
Expand Down
4 changes: 4 additions & 0 deletions src/types/requestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface Options {
/** Google Vertex AI specific */
vertexRegion?: string;
vertexProjectId?: string;

/** OpenAI specific */
openaiProject?: string;
openaiOrganization?: string;
}

/**
Expand Down
Loading