UNPKG

genkitx-openai

Version:

Firebase Genkit AI framework plugin for OpenAI APIs.

1 lines 5.39 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 The Fire Company\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 */\nimport type { Genkit } from 'genkit';\nimport { genkitPlugin } from 'genkit/plugin';\nimport { OpenAI, ClientOptions } from 'openai';\n\nimport { dallE3, dallE3Model } from './dalle.js';\nimport {\n openaiEmbedder,\n SUPPORTED_EMBEDDING_MODELS,\n textEmbedding3Large,\n textEmbedding3Small,\n textEmbeddingAda002,\n} from './embedder.js';\nimport {\n gpt35Turbo,\n gpt45,\n gpt4,\n gpt4Turbo,\n gpt4Vision,\n gpt4o,\n gpt4oMini,\n gptModel,\n o1,\n o1Preview,\n o1Mini,\n o3Mini,\n SUPPORTED_GPT_MODELS,\n} from './gpt.js';\nimport { SUPPORTED_TTS_MODELS, ttsModel, tts1, tts1Hd } from './tts.js';\nimport { whisper1, whisper1Model } from './whisper.js';\nimport { ModelInfo } from 'genkit/model';\nexport {\n dallE3,\n gpt35Turbo,\n gpt45,\n gpt4,\n gpt4Turbo,\n gpt4Vision,\n gpt4o,\n gpt4oMini,\n o1,\n o1Preview,\n o1Mini,\n o3Mini,\n tts1,\n tts1Hd,\n whisper1,\n textEmbedding3Large,\n textEmbedding3Small,\n textEmbeddingAda002,\n};\n\nexport interface PluginOptions extends Partial<ClientOptions> {\n models?: ModelDefinition[];\n}\n\n// Standard model definition\nexport interface ModelDefinition {\n name: string;\n info: ModelInfo;\n configSchema?: any;\n}\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 * - gpt4o: Reference to the GPT-4o model.\n * - gpt4oMini: Reference to the GPT-4o-mini model.\n * - gpt4Turbo: Reference to the GPT-4 Turbo model.\n * - gpt4Vision: Reference to the GPT-4 Vision model.\n * - gpt4: Reference to the GPT-4 model.\n * - gpt35Turbo: Reference to the GPT-3.5 Turbo model.\n * - dallE3: Reference to the DALL-E 3 model.\n * - tts1: Reference to the Text-to-speech 1 model.\n * - tts1Hd: Reference to the Text-to-speech 1 HD model.\n * - whisper: Reference to the Whisper model.\n * - textEmbedding3Large: Reference to the Text Embedding Large model.\n * - textEmbedding3Small: Reference to the Text Embedding Small model.\n * - textEmbeddingAda002: Reference to the Ada model.\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 'genkitx-openai';\n *\n * export default configureGenkit({\n * plugins: [\n * openai({ apiKey: 'your-api-key' })\n * ... // other plugins\n * ]\n * });\n * ```\n */\nexport const openAI = (options?: PluginOptions) =>\n genkitPlugin('openai', async (ai: Genkit) => {\n const client = new OpenAI(options);\n for (const name of Object.keys(SUPPORTED_GPT_MODELS)) {\n gptModel(ai, name, client);\n }\n // Initialize the models if provided in the options\n options?.models?.map((model) => {\n if (!model.name || !model.info || !model.configSchema) {\n throw new Error(`Model ${model.name} is missing required fields`);\n }\n gptModel(ai, model.name, client, model.info, model.configSchema);\n });\n\n dallE3Model(ai, client);\n whisper1Model(ai, client);\n for (const name of Object.keys(SUPPORTED_TTS_MODELS)) {\n ttsModel(ai, name, client);\n }\n for (const name of Object.keys(SUPPORTED_EMBEDDING_MODELS)) {\n openaiEmbedder(ai, name, options);\n }\n });\n\nexport default openAI;\n"],"mappings":";;;AAgBA,SAAS,oBAAoB;AAC7B,SAAS,cAA6B;AAEtC,SAAS,QAAQ,mBAAmB;AACpC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB,UAAU,MAAM,cAAc;AAC7D,SAAS,UAAU,qBAAqB;AA4EjC,MAAM,SAAS,CAAC,YACrB,aAAa,UAAU,CAAO,OAAe;AAxH/C;AAyHI,QAAM,SAAS,IAAI,OAAO,OAAO;AACjC,aAAW,QAAQ,OAAO,KAAK,oBAAoB,GAAG;AACpD,aAAS,IAAI,MAAM,MAAM;AAAA,EAC3B;AAEA,2CAAS,WAAT,mBAAiB,IAAI,CAAC,UAAU;AAC9B,QAAI,CAAC,MAAM,QAAQ,CAAC,MAAM,QAAQ,CAAC,MAAM,cAAc;AACrD,YAAM,IAAI,MAAM,SAAS,MAAM,IAAI,6BAA6B;AAAA,IAClE;AACA,aAAS,IAAI,MAAM,MAAM,QAAQ,MAAM,MAAM,MAAM,YAAY;AAAA,EACjE;AAEA,cAAY,IAAI,MAAM;AACtB,gBAAc,IAAI,MAAM;AACxB,aAAW,QAAQ,OAAO,KAAK,oBAAoB,GAAG;AACpD,aAAS,IAAI,MAAM,MAAM;AAAA,EAC3B;AACA,aAAW,QAAQ,OAAO,KAAK,0BAA0B,GAAG;AAC1D,mBAAe,IAAI,MAAM,OAAO;AAAA,EAClC;AACF,EAAC;AAEH,IAAO,cAAQ;","names":[]}