@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
1 lines • 2.79 kB
Source Map (JSON)
{"version":3,"sources":["../src/embedder.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\n// import { defineEmbedder, embedderRef } from '@genkit-ai/ai/embedder';\n\nimport type { EmbedderAction, EmbedderReference, Genkit } from 'genkit';\nimport OpenAI from 'openai';\n\n/**\n * Method to define a new Genkit Embedder that is compatibale with the Open AI\n * Embeddings API. \n *\n * @param params An object containing parameters for defining the OpenAI embedder.\n * @param params.ai The Genkit AI instance.\n * @param params.name The name of the embedder.\n * @param params.client The OpenAI client instance.\n * @param params.embedderRef Optional reference to the embedder's configuration and\n * custom options.\n\n * @returns the created {@link EmbedderAction}\n */\nexport function defineCompatOpenAIEmbedder(params: {\n ai: Genkit;\n name: string;\n client: OpenAI;\n embedderRef?: EmbedderReference;\n}): EmbedderAction {\n const { ai, name, client, embedderRef } = params;\n const modelName = name.substring(name.indexOf('/') + 1);\n\n return ai.defineEmbedder(\n {\n name,\n configSchema: embedderRef?.configSchema,\n ...embedderRef?.info,\n },\n async (input, options) => {\n const { encodingFormat: encoding_format, ...restOfConfig } = options;\n const embeddings = await client.embeddings.create({\n model: modelName!,\n input: input.map((d) => d.text),\n encoding_format,\n ...restOfConfig,\n });\n return {\n embeddings: embeddings.data.map((d) => ({ embedding: d.embedding })),\n };\n }\n );\n}\n"],"mappings":"AAmCO,SAAS,2BAA2B,QAKxB;AACjB,QAAM,EAAE,IAAI,MAAM,QAAQ,YAAY,IAAI;AAC1C,QAAM,YAAY,KAAK,UAAU,KAAK,QAAQ,GAAG,IAAI,CAAC;AAEtD,SAAO,GAAG;AAAA,IACR;AAAA,MACE;AAAA,MACA,cAAc,aAAa;AAAA,MAC3B,GAAG,aAAa;AAAA,IAClB;AAAA,IACA,OAAO,OAAO,YAAY;AACxB,YAAM,EAAE,gBAAgB,iBAAiB,GAAG,aAAa,IAAI;AAC7D,YAAM,aAAa,MAAM,OAAO,WAAW,OAAO;AAAA,QAChD,OAAO;AAAA,QACP,OAAO,MAAM,IAAI,CAAC,MAAM,EAAE,IAAI;AAAA,QAC9B;AAAA,QACA,GAAG;AAAA,MACL,CAAC;AACD,aAAO;AAAA,QACL,YAAY,WAAW,KAAK,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE;AAAA,MACrE;AAAA,IACF;AAAA,EACF;AACF;","names":[]}