Skip to content

feat(js/plugins/compat-oai): Add explicit model types for dynamic resolution #3120

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

Merged
merged 2 commits into from
Jun 23, 2025
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
7 changes: 2 additions & 5 deletions js/plugins/compat-oai/src/openai/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { z, type ModelReference } from 'genkit';
import { z } from 'genkit';
import { GenerationCommonConfigSchema, modelRef } from 'genkit/model';

export const ChatCompletionConfigSchema = GenerationCommonConfigSchema.extend({
Expand Down Expand Up @@ -291,10 +291,7 @@ export const gpt35Turbo = modelRef({
configSchema: ChatCompletionConfigSchema,
});

export const SUPPORTED_GPT_MODELS: Record<
string,
ModelReference<typeof ChatCompletionConfigSchema>
> = {
export const SUPPORTED_GPT_MODELS = {
'gpt-4.5': gpt45,
'gpt-4o': gpt4o,
'gpt-4o-mini': gpt4oMini,
Expand Down
53 changes: 44 additions & 9 deletions js/plugins/compat-oai/src/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,45 @@ export function openAIPlugin(options?: OpenAIPluginOptions): GenkitPlugin {

export type OpenAIPlugin = {
(params?: OpenAIPluginOptions): GenkitPlugin;
model(
name:
| keyof typeof SUPPORTED_GPT_MODELS
| (`gpt-${string}` & {})
| (`o${number}` & {}),
config?: z.infer<typeof ChatCompletionConfigSchema>
): ModelReference<typeof ChatCompletionConfigSchema>;
model(
name:
| keyof typeof SUPPORTED_IMAGE_MODELS
| (`dall-e${string}` & {})
| (`gpt-image-${string}` & {}),
config?: z.infer<typeof ImageGenerationConfigSchema>
): ModelReference<typeof ImageGenerationConfigSchema>;
model(
name:
| keyof typeof SUPPORTED_TTS_MODELS
| (`tts-${string}` & {})
| (`${string}-tts` & {}),
config?: z.infer<typeof SpeechConfigSchema>
): ModelReference<typeof SpeechConfigSchema>;
model(
name:
| keyof typeof SUPPORTED_STT_MODELS
| (`whisper-${string}` & {})
| (`${string}-transcribe` & {}),
config?: z.infer<typeof TranscriptionConfigSchema>
): ModelReference<typeof TranscriptionConfigSchema>;
model(name: string, config?: any): ModelReference<z.ZodTypeAny>;
embedder(
name:
| keyof typeof SUPPORTED_EMBEDDING_MODELS
| (`${string}-embedding-${string}` & {}),
config?: z.infer<typeof TextEmbeddingConfigSchema>
): EmbedderReference<typeof TextEmbeddingConfigSchema>;
embedder(name: string, config?: any): EmbedderReference<z.ZodTypeAny>;
};

export const openAI = openAIPlugin as OpenAIPlugin;
// provide generic implementation for the model function overloads.
(openAI as any).model = (
name: string,
config?: any
): ModelReference<z.ZodTypeAny> => {
const model = ((name: string, config?: any): ModelReference<z.ZodTypeAny> => {
if (name.includes('gpt-image-1') || name.includes('dall-e')) {
return modelRef({
name: `openai/${name}`,
Expand All @@ -248,8 +277,9 @@ export const openAI = openAIPlugin as OpenAIPlugin;
config,
configSchema: ChatCompletionConfigSchema,
});
};
openAI.embedder = (
}) as OpenAIPlugin['model'];

const embedder = ((
name: string,
config?: any
): EmbedderReference<z.ZodTypeAny> => {
Expand All @@ -258,6 +288,11 @@ openAI.embedder = (
config,
configSchema: TextEmbeddingConfigSchema,
});
};
}) as OpenAIPlugin['embedder'];

export const openAI: OpenAIPlugin = Object.assign(openAIPlugin, {
model,
embedder,
});

export default openAI;
4 changes: 2 additions & 2 deletions js/testapps/compat-oai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { genkit, z } from 'genkit';
dotenv.config();

const ai = genkit({
plugins: [openAI({ apiKey: process.env.OPENAI_API_KEY, name: 'openai' })],
plugins: [openAI()],
});

export const jokeFlow = ai.defineFlow(
Expand Down Expand Up @@ -51,7 +51,7 @@ export const embedFlow = ai.defineFlow(
},
async (text) => {
const embedding = await ai.embed({
embedder: 'openai/text-embedding-ada-002',
embedder: openAI.embedder('text-embedding-ada-002'),
content: text,
});

Expand Down