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: add support to custom base URL and models #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion plugins/openai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
} from './gpt.js';
import { SUPPORTED_TTS_MODELS, ttsModel, tts1, tts1Hd } from './tts.js';
import { whisper1, whisper1Model } from './whisper.js';
import { ModelAction } from '@genkit-ai/ai/model';
import z from 'zod';
export {
dallE3,
gpt35Turbo,
Expand All @@ -52,8 +54,12 @@ export {
textEmbeddingAda002,
};

export type OpenAICustomModelAction = <CustomOptionsSchema extends z.ZodTypeAny = z.ZodTypeAny>(client: OpenAI) => ModelAction<CustomOptionsSchema>

export interface PluginOptions {
apiKey?: string;
baseURL?: string;
customModels?: Array<OpenAICustomModelAction>;
}

/**
Expand Down Expand Up @@ -106,7 +112,8 @@ export const openAI: Plugin<[PluginOptions] | []> = genkitPlugin(
throw new Error(
'please pass in the API key or set the OPENAI_API_KEY environment variable'
);
const client = new OpenAI({ apiKey });
const baseURL = options?.baseURL || process.env.OPENAI_BASE_URL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should set one by default if both are empty

const client = new OpenAI({ apiKey, baseURL });
return {
models: [
...Object.keys(SUPPORTED_GPT_MODELS).map((name) =>
Expand All @@ -117,6 +124,7 @@ export const openAI: Plugin<[PluginOptions] | []> = genkitPlugin(
),
dallE3Model(client),
whisper1Model(client),
...(options?.customModels?.map(model => model(client)) || []),
],
embedders: Object.keys(SUPPORTED_EMBEDDING_MODELS).map((name) =>
openaiEmbedder(name, options)
Expand Down