UNPKG

ai-sdk-ollama

Version:

Vercel AI SDK Provider for Ollama using official ollama-js library

84 lines 2.92 kB
import { RerankingModelV4, RerankingModelV4CallOptions } from '@ai-sdk/provider'; import { Ollama } from 'ollama'; /** * Configuration for the Ollama embedding-based reranking model */ export interface OllamaEmbeddingRerankingConfig { client: Ollama; provider: string; } /** * Settings for configuring Ollama embedding-based reranking */ export interface OllamaEmbeddingRerankingSettings { /** * Embedding model to use for computing document similarity. * If not specified, uses the modelId passed to the constructor. * Recommended models: 'bge-m3', 'nomic-embed-text', 'mxbai-embed-large' */ embeddingModel?: string; /** * Maximum number of texts to embed per request. Smaller batches reduce * memory/latency spikes for large document sets while still avoiding one * request per document. Defaults to 16. */ maxBatchSize?: number; } /** * Embedding-Based Reranking Model (Workaround) * * Since Ollama doesn't have native reranking support yet (PR #11389 not merged), * this implementation uses embedding similarity as a workaround: * * 1. Embed the query using an embedding model * 2. Embed all documents using the same model * 3. Calculate cosine similarity between query and each document * 4. Sort documents by similarity score (descending) * * This approach works with any Ollama embedding model and provides * reasonable reranking results for most use cases. * * @example * ```ts * import { ollama } from 'ai-sdk-ollama'; * import { rerank } from 'ai'; * * const result = await rerank({ * model: ollama.embeddingReranking('bge-m3'), * query: 'What is machine learning?', * documents: [ * 'Machine learning is a subset of AI...', * 'The weather today is sunny...', * 'Deep learning uses neural networks...', * ], * topN: 2, * }); * * console.log(result.rerankedDocuments); * // Documents sorted by relevance to the query * ``` */ export declare class OllamaEmbeddingRerankingModel implements RerankingModelV4 { readonly specificationVersion: 'v4'; readonly modelId: string; private readonly config; private readonly settings; constructor(modelId: string, settings: OllamaEmbeddingRerankingSettings, config: OllamaEmbeddingRerankingConfig); get provider(): string; /** * Get the effective embedding model to use */ private get embeddingModelId(); /** * Normalized batch size for embedding requests. Ensures we never request * non-positive batch sizes. */ private get embeddingBatchSize(); /** * Embed a batch of texts while keeping their order aligned with the * embeddings that are returned. */ private embedBatch; doRerank({ documents, query, topN, }: RerankingModelV4CallOptions): Promise<Awaited<ReturnType<RerankingModelV4['doRerank']>>>; } //# sourceMappingURL=embedding-reranking-model.d.ts.map