UNPKG

genkitx-mistral

Version:

Firebase Genkit AI framework plugin for Mistral AI APIs.

1 lines 3.13 kB
{"version":3,"sources":["../src/embedders.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 */\n\nimport { Mistral } from '@mistralai/mistralai';\nimport { Genkit } from 'genkit';\nimport { embedderRef, z } from 'genkit';\n\nexport const TextEmbeddingConfigSchema = z.object({\n embeddingTypes: z.literal('float').optional(),\n encodingFormat: z.union([z.literal('float'), z.literal('base64')]).optional(),\n});\n\nexport type TextEmbeddingConfig = z.infer<typeof TextEmbeddingConfigSchema>;\n\nexport function mistralEmbedder(ai: Genkit, name: string, client: Mistral) {\n const model = SUPPORTED_EMBEDDING_MODELS[name];\n if (!model) throw new Error(`Unsupported model: ${name}`);\n\n ai.defineEmbedder(\n {\n info: model.info!,\n configSchema: TextEmbeddingConfigSchema,\n name: model.name,\n },\n async (input, _) => {\n const embeddings = await client.embeddings.create({\n model: name,\n inputs: input.map((d) => d.text),\n });\n return {\n embeddings: embeddings.data.map((d) => {\n if (!d.embedding) {\n throw new Error('Embedding is undefined');\n }\n return { embedding: d.embedding };\n }),\n };\n }\n );\n}\n\nexport const mistralembed = embedderRef({\n name: 'mistral/mistral-embed',\n configSchema: TextEmbeddingConfigSchema,\n info: {\n dimensions: 1024,\n label: 'Mistral - Mistral Embed',\n supports: {\n input: ['text'],\n },\n },\n});\nexport const SUPPORTED_EMBEDDING_MODELS = {\n 'mistral-embed': mistralembed,\n};\n"],"mappings":";;;AAkBA,SAAS,aAAa,SAAS;AAExB,MAAM,4BAA4B,EAAE,OAAO;AAAA,EAChD,gBAAgB,EAAE,QAAQ,OAAO,EAAE,SAAS;AAAA,EAC5C,gBAAgB,EAAE,MAAM,CAAC,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,QAAQ,CAAC,CAAC,EAAE,SAAS;AAC9E,CAAC;AAIM,SAAS,gBAAgB,IAAY,MAAc,QAAiB;AACzE,QAAM,QAAQ,2BAA2B,IAAI;AAC7C,MAAI,CAAC,MAAO,OAAM,IAAI,MAAM,sBAAsB,IAAI,EAAE;AAExD,KAAG;AAAA,IACD;AAAA,MACE,MAAM,MAAM;AAAA,MACZ,cAAc;AAAA,MACd,MAAM,MAAM;AAAA,IACd;AAAA,IACA,CAAO,OAAO,MAAM;AAClB,YAAM,aAAa,MAAM,OAAO,WAAW,OAAO;AAAA,QAChD,OAAO;AAAA,QACP,QAAQ,MAAM,IAAI,CAAC,MAAM,EAAE,IAAI;AAAA,MACjC,CAAC;AACD,aAAO;AAAA,QACL,YAAY,WAAW,KAAK,IAAI,CAAC,MAAM;AACrC,cAAI,CAAC,EAAE,WAAW;AAChB,kBAAM,IAAI,MAAM,wBAAwB;AAAA,UAC1C;AACA,iBAAO,EAAE,WAAW,EAAE,UAAU;AAAA,QAClC,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;AAEO,MAAM,eAAe,YAAY;AAAA,EACtC,MAAM;AAAA,EACN,cAAc;AAAA,EACd,MAAM;AAAA,IACJ,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,UAAU;AAAA,MACR,OAAO,CAAC,MAAM;AAAA,IAChB;AAAA,EACF;AACF,CAAC;AACM,MAAM,6BAA6B;AAAA,EACxC,iBAAiB;AACnB;","names":[]}