@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
1 lines • 14.4 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 Genkit,\n modelActionMetadata,\n ModelReference,\n z,\n} from 'genkit';\nimport { GenkitPlugin } from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport OpenAI from 'openai';\nimport {\n defineCompatOpenAISpeechModel,\n defineCompatOpenAITranscriptionModel,\n SpeechConfigSchema,\n TranscriptionConfigSchema,\n} from '../audio.js';\nimport { defineCompatOpenAIEmbedder } from '../embedder.js';\nimport {\n defineCompatOpenAIImageModel,\n ImageGenerationCommonConfigSchema,\n} from '../image.js';\nimport { openAICompatible, PluginOptions } from '../index.js';\nimport { defineCompatOpenAIModel } from '../model.js';\nimport {\n gptImage1RequestBuilder,\n openAIImageModelRef,\n SUPPORTED_IMAGE_MODELS,\n} from './dalle.js';\nimport {\n SUPPORTED_EMBEDDING_MODELS,\n TextEmbeddingConfigSchema,\n} from './embedder.js';\nimport {\n OpenAIChatCompletionConfigSchema,\n openAIModelRef,\n SUPPORTED_GPT_MODELS,\n} from './gpt.js';\nimport { openAISpeechModelRef, SUPPORTED_TTS_MODELS } from './tts.js';\nimport {\n openAITranscriptionModelRef,\n SUPPORTED_STT_MODELS,\n} from './whisper.js';\n\nexport type OpenAIPluginOptions = Omit<PluginOptions, 'name' | 'baseURL'>;\n\nconst UNSUPPORTED_MODEL_MATCHERS = ['babbage', 'davinci', 'codex'];\n\nconst resolver = async (\n ai: Genkit,\n client: OpenAI,\n actionType: ActionType,\n actionName: string\n) => {\n if (actionType === 'embedder') {\n defineCompatOpenAIEmbedder({ ai, name: `openai/${actionName}`, client });\n } else if (\n actionName.includes('gpt-image-1') ||\n actionName.includes('dall-e')\n ) {\n const modelRef = openAIImageModelRef({ name: `openai/${actionName}` });\n defineCompatOpenAIImageModel({ ai, name: modelRef.name, client, modelRef });\n } else if (actionName.includes('tts')) {\n const modelRef = openAISpeechModelRef({ name: `openai/${actionName}` });\n defineCompatOpenAISpeechModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n });\n } else if (\n actionName.includes('whisper') ||\n actionName.includes('transcribe')\n ) {\n const modelRef = openAITranscriptionModelRef({\n name: `openai/${actionName}`,\n });\n defineCompatOpenAITranscriptionModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n });\n } else {\n const modelRef = openAIModelRef({ name: `openai/${actionName}` });\n defineCompatOpenAIModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\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: `openai/${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: `openai/${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: `openai/${model.id}` });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else if (\n model.id.includes('whisper') ||\n model.id.includes('transcribe')\n ) {\n const modelRef =\n SUPPORTED_STT_MODELS[model.id] ??\n openAITranscriptionModelRef({ name: `openai/${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] ??\n openAIModelRef({ name: `openai/${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): GenkitPlugin {\n return openAICompatible({\n name: 'openai',\n ...options,\n initializer: async (ai, client) => {\n Object.values(SUPPORTED_GPT_MODELS).forEach((modelRef) =>\n defineCompatOpenAIModel({ ai, name: modelRef.name, client, modelRef })\n );\n Object.values(SUPPORTED_EMBEDDING_MODELS).forEach((embedderRef) =>\n defineCompatOpenAIEmbedder({\n ai,\n name: embedderRef.name,\n client,\n embedderRef,\n })\n );\n Object.values(SUPPORTED_TTS_MODELS).forEach((modelRef) =>\n defineCompatOpenAISpeechModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n })\n );\n Object.values(SUPPORTED_STT_MODELS).forEach((modelRef) =>\n defineCompatOpenAITranscriptionModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n })\n );\n Object.values(SUPPORTED_IMAGE_MODELS).forEach((modelRef) =>\n defineCompatOpenAIImageModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n requestBuilder: modelRef.name.includes('gpt-image-1')\n ? gptImage1RequestBuilder\n : undefined,\n })\n );\n },\n resolver,\n listActions,\n });\n}\n\nexport type OpenAIPlugin = {\n (params?: OpenAIPluginOptions): GenkitPlugin;\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(\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:\n | keyof typeof SUPPORTED_STT_MODELS\n | (`whisper-${string}` & {})\n | (`${string}-transcribe` & {}),\n config?: z.infer<typeof TranscriptionConfigSchema>\n ): ModelReference<typeof TranscriptionConfigSchema>;\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: `openai/${name}`,\n config,\n });\n }\n if (name.includes('tts')) {\n return openAISpeechModelRef({\n name: `openai/${name}`,\n config,\n });\n }\n if (name.includes('whisper') || name.includes('transcribe')) {\n return openAITranscriptionModelRef({\n name: `openai/${name}`,\n config,\n });\n }\n return openAIModelRef({\n name: `openai/${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: `openai/${name}`,\n config,\n configSchema: TextEmbeddingConfigSchema,\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,EAEE;AAAA,EACA;AAAA,EAGA;AAAA,OAGK;AAIP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,kCAAkC;AAC3C;AAAA,EACE;AAAA,OAEK;AACP,SAAS,wBAAuC;AAChD,SAAS,+BAA+B;AACxC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB,4BAA4B;AAC3D;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAIP,MAAM,6BAA6B,CAAC,WAAW,WAAW,OAAO;AAEjE,MAAM,WAAW,OACf,IACA,QACA,YACA,eACG;AACH,MAAI,eAAe,YAAY;AAC7B,+BAA2B,EAAE,IAAI,MAAM,UAAU,UAAU,IAAI,OAAO,CAAC;AAAA,EACzE,WACE,WAAW,SAAS,aAAa,KACjC,WAAW,SAAS,QAAQ,GAC5B;AACA,UAAM,WAAW,oBAAoB,EAAE,MAAM,UAAU,UAAU,GAAG,CAAC;AACrE,iCAA6B,EAA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