@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
1 lines • 16.9 kB
Source Map (JSON)
{"version":3,"sources":["../../src/openai/index.ts"],"sourcesContent":["/**\n * Copyright 2024 The Fire Company\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ActionMetadata,\n embedderActionMetadata,\n embedderRef,\n EmbedderReference,\n modelActionMetadata,\n ModelReference,\n z,\n} from 'genkit';\nimport { type GenkitPluginV2, type ResolvableAction } from 'genkit/plugin';\nimport { type ActionType } from 'genkit/registry';\nimport OpenAI from 'openai';\nimport {\n defineCompatOpenAISpeechModel,\n defineCompatOpenAITranscriptionModel,\n SpeechConfigSchema,\n TranscriptionConfigSchema,\n} from '../audio.mjs';\nimport { defineCompatOpenAIEmbedder } from '../embedder.mjs';\nimport {\n defineCompatOpenAIImageModel,\n ImageGenerationCommonConfigSchema,\n} from '../image.mjs';\nimport { openAICompatible, PluginOptions } from '../index.mjs';\nimport { defineCompatOpenAIModel } from '../model.mjs';\nimport {\n gptImage1RequestBuilder,\n openAIImageModelRef,\n SUPPORTED_IMAGE_MODELS,\n} from './dalle.mjs';\nimport {\n SUPPORTED_EMBEDDING_MODELS,\n TextEmbeddingConfigSchema,\n} from './embedder.mjs';\nimport {\n OpenAIChatCompletionConfigSchema,\n openAIModelRef,\n SUPPORTED_GPT_MODELS,\n} from './gpt.mjs';\nimport { openAITranscriptionModelRef, SUPPORTED_STT_MODELS } from './stt.mjs';\nimport { openAISpeechModelRef, SUPPORTED_TTS_MODELS } from './tts.mjs';\nimport {\n defineOpenAIWhisperModel,\n openAIWhisperModelRef,\n SUPPORTED_WHISPER_MODELS,\n WhisperConfigSchema,\n} from './whisper.mjs';\n\nexport type OpenAIPluginOptions = Omit<PluginOptions, 'name' | 'baseURL'>;\n\nconst UNSUPPORTED_MODEL_MATCHERS = ['babbage', 'davinci', 'codex'];\n\nfunction createResolver(pluginOptions: PluginOptions) {\n return async (client: OpenAI, actionType: ActionType, actionName: string) => {\n if (actionType === 'embedder') {\n return defineCompatOpenAIEmbedder({\n name: actionName,\n client,\n pluginOptions,\n });\n } else if (\n actionName.includes('gpt-image-1') ||\n actionName.includes('dall-e')\n ) {\n const modelRef = openAIImageModelRef({ name: actionName });\n return defineCompatOpenAIImageModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n });\n } else if (actionName.includes('tts')) {\n const modelRef = openAISpeechModelRef({ name: actionName });\n return defineCompatOpenAISpeechModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n });\n } else if (actionName.includes('whisper')) {\n const modelRef = openAIWhisperModelRef({ name: actionName });\n return defineOpenAIWhisperModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n });\n } else if (actionName.includes('transcribe')) {\n const modelRef = openAITranscriptionModelRef({\n name: actionName,\n });\n return defineCompatOpenAITranscriptionModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n });\n } else {\n const modelRef = openAIModelRef({ name: actionName });\n return defineCompatOpenAIModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n });\n }\n };\n}\n\nfunction filterOpenAiModels(model: OpenAI.Model): boolean {\n return !UNSUPPORTED_MODEL_MATCHERS.some((m) => model.id.includes(m));\n}\n\nconst listActions = async (client: OpenAI): Promise<ActionMetadata[]> => {\n return await client.models.list().then((response) =>\n response.data.filter(filterOpenAiModels).map((model: OpenAI.Model) => {\n if (model.id.includes('embedding')) {\n return embedderActionMetadata({\n name: model.id,\n configSchema: TextEmbeddingConfigSchema,\n info: SUPPORTED_EMBEDDING_MODELS[model.id]?.info,\n });\n } else if (\n model.id.includes('gpt-image-1') ||\n model.id.includes('dall-e')\n ) {\n const modelRef =\n SUPPORTED_IMAGE_MODELS[model.id] ??\n openAIImageModelRef({ name: model.id });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else if (model.id.includes('tts')) {\n const modelRef =\n SUPPORTED_TTS_MODELS[model.id] ??\n openAISpeechModelRef({ name: model.id });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else if (model.id.includes('whisper')) {\n const modelRef =\n SUPPORTED_WHISPER_MODELS[model.id] ??\n openAIWhisperModelRef({ name: model.id });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else if (model.id.includes('transcribe')) {\n const modelRef =\n SUPPORTED_STT_MODELS[model.id] ??\n openAITranscriptionModelRef({ name: model.id });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else {\n const modelRef =\n SUPPORTED_GPT_MODELS[model.id] ?? openAIModelRef({ name: model.id });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n }\n })\n );\n};\n\nexport function openAIPlugin(options?: OpenAIPluginOptions): GenkitPluginV2 {\n const pluginOptions = { name: 'openai', ...options };\n return openAICompatible({\n name: 'openai',\n ...options,\n initializer: async (client) => {\n const models = [] as ResolvableAction[];\n models.push(\n ...Object.values(SUPPORTED_GPT_MODELS).map((modelRef) =>\n defineCompatOpenAIModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n })\n )\n );\n models.push(\n ...Object.values(SUPPORTED_EMBEDDING_MODELS).map((embedderRef) =>\n defineCompatOpenAIEmbedder({\n name: embedderRef.name,\n client,\n pluginOptions,\n embedderRef,\n })\n )\n );\n models.push(\n ...Object.values(SUPPORTED_TTS_MODELS).map((modelRef) =>\n defineCompatOpenAISpeechModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n })\n )\n );\n models.push(\n ...Object.values(SUPPORTED_WHISPER_MODELS).map((modelRef) =>\n defineOpenAIWhisperModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n })\n )\n );\n models.push(\n ...Object.values(SUPPORTED_STT_MODELS).map((modelRef) =>\n defineCompatOpenAITranscriptionModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n })\n )\n );\n models.push(\n ...Object.values(SUPPORTED_IMAGE_MODELS).map((modelRef) =>\n defineCompatOpenAIImageModel({\n name: modelRef.name,\n client,\n pluginOptions,\n modelRef,\n requestBuilder: modelRef.name.includes('gpt-image-1')\n ? gptImage1RequestBuilder\n : undefined,\n })\n )\n );\n return models;\n },\n resolver: createResolver(pluginOptions),\n listActions,\n });\n}\n\nexport type OpenAIPlugin = {\n (params?: OpenAIPluginOptions): GenkitPluginV2;\n model(\n name:\n | keyof typeof SUPPORTED_IMAGE_MODELS\n | (`dall-e${string}` & {})\n | (`gpt-image-${string}` & {}),\n config?: z.infer<typeof ImageGenerationCommonConfigSchema>\n ): ModelReference<typeof ImageGenerationCommonConfigSchema>;\n model(\n name:\n | keyof typeof SUPPORTED_TTS_MODELS\n | (`tts-${string}` & {})\n | (`${string}-tts` & {}),\n config?: z.infer<typeof SpeechConfigSchema>\n ): ModelReference<typeof SpeechConfigSchema>;\n model(\n name: keyof typeof SUPPORTED_WHISPER_MODELS | (`whisper-${string}` & {}),\n config?: z.infer<typeof WhisperConfigSchema>\n ): ModelReference<typeof WhisperConfigSchema>;\n model(\n name: keyof typeof SUPPORTED_STT_MODELS | (`${string}-transcribe` & {}),\n config?: z.infer<typeof TranscriptionConfigSchema>\n ): ModelReference<typeof TranscriptionConfigSchema>;\n model(\n name:\n | keyof typeof SUPPORTED_GPT_MODELS\n | (`gpt-${string}` & {})\n | (`o${number}` & {}),\n config?: z.infer<typeof OpenAIChatCompletionConfigSchema>\n ): ModelReference<typeof OpenAIChatCompletionConfigSchema>;\n model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n embedder(\n name:\n | keyof typeof SUPPORTED_EMBEDDING_MODELS\n | (`${string}-embedding-${string}` & {}),\n config?: z.infer<typeof TextEmbeddingConfigSchema>\n ): EmbedderReference<typeof TextEmbeddingConfigSchema>;\n embedder(name: string, config?: any): EmbedderReference<z.ZodTypeAny>;\n};\n\nconst model = ((name: string, config?: any): ModelReference<z.ZodTypeAny> => {\n if (name.includes('gpt-image-1') || name.includes('dall-e')) {\n return openAIImageModelRef({\n name,\n config,\n });\n }\n if (name.includes('tts')) {\n return openAISpeechModelRef({\n name,\n config,\n });\n }\n if (name.includes('whisper')) {\n return openAIWhisperModelRef({\n name,\n config,\n });\n }\n if (name.includes('transcribe')) {\n return openAITranscriptionModelRef({\n name,\n config,\n });\n }\n return openAIModelRef({\n name,\n config,\n });\n}) as OpenAIPlugin['model'];\n\nconst embedder = ((\n name: string,\n config?: any\n): EmbedderReference<z.ZodTypeAny> => {\n return embedderRef({\n name,\n config,\n configSchema: TextEmbeddingConfigSchema,\n namespace: 'openai',\n });\n}) as OpenAIPlugin['embedder'];\n\n/**\n * This module provides an interface to the OpenAI models through the Genkit\n * plugin system. It allows users to interact with various models by providing\n * an API key and optional configuration.\n *\n * The main export is the `openai` plugin, which can be configured with an API\n * key either directly or through environment variables. It initializes the\n * OpenAI client and makes available the models for use.\n *\n * Exports:\n * - openai: The main plugin function to interact with OpenAI.\n *\n * Usage:\n * To use the models, initialize the openai plugin inside `configureGenkit` and\n * pass the configuration options. If no API key is provided in the options, the\n * environment variable `OPENAI_API_KEY` must be set.\n *\n * Example:\n * ```\n * import { openAI } from '@genkit-ai/compat-oai/openai';\n *\n * export default configureGenkit({\n * plugins: [\n * openai()\n * ... // other plugins\n * ]\n * });\n * ```\n */\nexport const openAI: OpenAIPlugin = Object.assign(openAIPlugin, {\n model,\n embedder,\n});\n\nexport default openAI;\n"],"mappings":"AAiBA;AAAA,E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