UNPKG

jina-ai-provider

Version:

Jina AI Provider for running Jina AI models with Vercel AI SDK

260 lines (253 loc) 8.7 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { createJina: () => createJina, jina: () => jina }); module.exports = __toCommonJS(index_exports); // src/jina-provider.ts var import_provider_utils3 = require("@ai-sdk/provider-utils"); // src/jina-embedding-model.ts var import_provider = require("@ai-sdk/provider"); var import_provider_utils2 = require("@ai-sdk/provider-utils"); var import_v42 = require("zod/v4"); // src/jina-embedding-options.ts var import_v4 = require("zod/v4"); var jinaEmbeddingOptions = import_v4.z.object({ /** * The input type for the embeddings. * * Defaults to `retrieval.passage`. * * Used to convey intended downstream application to help the model produce better embeddings. * * Must be one of the following values: * - `retrieval.query`: Specifies the given text is a query in a search or retrieval setting. * - `retrieval.passage`: Specifies the given text is a document in a search or retrieval setting. * - `text-matching`: Specifies the given text is used for Semantic Textual Similarity. * - `classification`: Specifies that the embedding is used for classification. * - `separation`: Specifies that the embedding is used for clustering. */ inputType: import_v4.z.enum([ "text-matching", "retrieval.query", "retrieval.passage", "separation", "classification" ]).optional(), /** * The number of dimensions for the resulting output embeddings. * * - `jina-embeddings-v3`: * - Min Output Dimensions: 32 for better performance * - Max Output Dimensions: 1,024 * * - `jina-clip-v2`: * - Min Output Dimensions: 64 * - Max Output Dimensions: 1,024 * * - `jina-clip-v1`: * - Output Dimensions: 768 * * Please refer to the model documentation for the supported values. * * @see https://jina.ai/api-dashboard/embedding */ outputDimension: import_v4.z.number().optional(), /** * Late chunking * * When enabled, the model will automatically split the input into chunks of 1024 tokens each. * * @see https://jina.ai/news/jina-embeddings-v3-a-frontier-multilingual-embedding-model/#parameter-latechunking * * Defaults to false. * * This is only supported in text embedding models. */ lateChunking: import_v4.z.boolean().optional(), /** * The data type for the resulting output embeddings. * * Defaults to `float`. * * - `float`: 32-bit floating-point numbers * - `binary`: 8-bit binary values * - `ubinary`: 8-bit unsigned binary values * - `base64`: Base64 encoded strings */ embeddingType: import_v4.z.enum(["float", "binary", "ubinary", "base64"]).optional(), /** * Whether to normalize the resulting output embeddings. * Scales the embedding so its Euclidean (L2) norm becomes 1, preserving direction. Useful when downstream involves dot-product, classification, visualization. * Defaults to true. */ normalized: import_v4.z.boolean().optional(), /** * Truncate at Maximum Context Length which is 8k tokens * * When enabled, the model will automatically drop the tail that extends beyond the maximum context length allowed by the model instead of throwing an error. * * Defaults to false. */ truncate: import_v4.z.boolean().optional() }); // src/jina-error.ts var import_provider_utils = require("@ai-sdk/provider-utils"); var import_zod = require("zod"); var voyageErrorDataSchema = import_zod.z.object({ error: import_zod.z.object({ code: import_zod.z.string().nullable(), message: import_zod.z.string(), param: import_zod.z.any().nullable(), type: import_zod.z.string() }) }); var voyageFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({ errorSchema: voyageErrorDataSchema, errorToMessage: (data) => data.error.message }); // src/jina-embedding-model.ts var JinaEmbeddingModel = class { specificationVersion = "v2"; modelId; config; get provider() { return this.config.provider; } get maxEmbeddingsPerCall() { return 2048; } get supportsParallelCalls() { return false; } constructor(modelId, config) { this.modelId = modelId; this.config = config; } async doEmbed({ abortSignal, values, headers, providerOptions }) { const embeddingOptions = await (0, import_provider_utils2.parseProviderOptions)({ provider: "jina", providerOptions, schema: jinaEmbeddingOptions }); if (values.length > this.maxEmbeddingsPerCall) { throw new import_provider.TooManyEmbeddingValuesForCallError({ maxEmbeddingsPerCall: this.maxEmbeddingsPerCall, modelId: this.modelId, provider: this.provider, values }); } const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({ abortSignal, body: { model: this.modelId, input: values, task: embeddingOptions?.inputType, embedding_type: embeddingOptions?.embeddingType, dimensions: embeddingOptions?.outputDimension, normalized: embeddingOptions?.normalized ?? true, late_chunking: embeddingOptions?.lateChunking, truncate: embeddingOptions?.truncate ?? false }, failedResponseHandler: voyageFailedResponseHandler, fetch: this.config.fetch, headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), headers), successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)( jinaEmbeddingResponseSchema ), url: `${this.config.baseURL}/embeddings` }); return { embeddings: response.data.map((item) => item.embedding), usage: response.usage ? { tokens: response.usage.total_tokens } : void 0, response: { headers: responseHeaders } }; } }; var jinaEmbeddingResponseSchema = import_v42.z.object({ data: import_v42.z.array( import_v42.z.object({ object: import_v42.z.literal("embedding"), embedding: import_v42.z.array(import_v42.z.number()), index: import_v42.z.number().optional() }) ), usage: import_v42.z.object({ total_tokens: import_v42.z.number(), prompt_tokens: import_v42.z.number().optional() }).nullish(), model: import_v42.z.string().optional() }); // src/jina-provider.ts function createJina(options = {}) { const baseURL = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL) ?? "https://api.jina.ai/v1"; const getHeaders = () => ({ Authorization: `Bearer ${(0, import_provider_utils3.loadApiKey)({ apiKey: options.apiKey, environmentVariableName: "JINA_API_KEY", description: "Jina" })}`, ...options.headers }); const createTextEmbeddingModel = (modelId) => new JinaEmbeddingModel(modelId, { provider: "jina.text.embedding", baseURL, headers: getHeaders, fetch: options.fetch }); const createMultiModalEmbeddingModel = (modelId) => new JinaEmbeddingModel(modelId, { provider: "jina.multimodal.embedding", baseURL, headers: getHeaders, fetch: options.fetch }); const provider = function(modelId) { if (new.target) { throw new Error( "The Jina model function cannot be called with the new keyword." ); } return createTextEmbeddingModel(modelId); }; provider.textEmbeddingModel = createTextEmbeddingModel; provider.multiModalEmbeddingModel = createMultiModalEmbeddingModel; provider.chat = provider.languageModel = () => { throw new Error("languageModel method is not implemented."); }; provider.imageModel = () => { throw new Error("imageModel method is not implemented."); }; return provider; } var jina = createJina(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createJina, jina }); //# sourceMappingURL=index.cjs.map