UNPKG

ai-sdk-ollama

Version:

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

212 lines 8.35 kB
import { LanguageModelV4, EmbeddingModelV4, RerankingModelV4, ProviderV4 } from '@ai-sdk/provider'; import { Ollama, type Options as OllamaOptions, type ChatRequest, type EmbedRequest, type Config } from 'ollama'; import { z } from 'zod'; import { OllamaRerankingSettings } from './models/reranking-model.js'; import { OllamaEmbeddingRerankingSettings } from './models/embedding-reranking-model.js'; import { ollamaTools } from './ollama-tools.js'; import type { WebSearchToolOptions } from './tool/web-search.js'; import type { WebFetchToolOptions } from './tool/web-fetch.js'; import type { ObjectGenerationOptions } from './utils/object-generation-reliability.js'; export interface Options extends OllamaOptions { /** * Minimum probability threshold for token selection * This parameter is supported by Ollama API but missing from ollama-js TypeScript definitions */ min_p?: number; } export type { Ollama, ChatRequest, EmbedRequest, Config, ToolCall, Tool, Message, ChatResponse, EmbedResponse, } from 'ollama'; /** * Settings for configuring the Ollama provider. * Extends from Ollama's Config type for consistency with the underlying client. */ export interface OllamaProviderSettings extends Pick<Config, 'headers' | 'fetch'> { /** * Base URL for the Ollama API (defaults to http://127.0.0.1:11434) * Maps to Config.host in the Ollama client */ baseURL?: string; /** * Ollama API key for authentication with cloud services. * The API key will be set as Authorization: Bearer {apiKey} header. */ apiKey?: string; /** * Existing Ollama client instance to use instead of creating a new one. * When provided, baseURL, headers, and fetch are ignored. */ client?: Ollama; } export interface OllamaProvider extends ProviderV4 { /** * Create a language model instance */ (modelId: string, settings?: OllamaChatSettings): LanguageModelV4; /** * Create a language model instance with the `chat` method */ chat(modelId: string, settings?: OllamaChatSettings): LanguageModelV4; /** * Create a language model instance with the `languageModel` method */ languageModel(modelId: string, settings?: OllamaChatSettings): LanguageModelV4; /** * Create an embedding model instance */ embedding(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV4; /** * Create an embedding model instance with the `textEmbedding` method */ textEmbedding(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV4; /** * Create an embedding model instance with the `textEmbeddingModel` method */ textEmbeddingModel(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV4; /** * Create a reranking model instance */ reranking(modelId: string, settings?: OllamaRerankingSettings): RerankingModelV4; /** * Create a reranking model instance with the `rerankingModel` method * * NOTE: This uses Ollama's native /api/rerank endpoint which is NOT YET AVAILABLE. * Use `embeddingReranking()` for a working solution. * @see https://github.com/ollama/ollama/pull/11389 */ rerankingModel(modelId: string, settings?: OllamaRerankingSettings): RerankingModelV4; /** * Create an embedding-based reranking model (RECOMMENDED - working now) * * This is a workaround that uses embedding similarity for reranking * since Ollama doesn't have native reranking support yet. * * @param modelId - The embedding model to use (e.g., 'bge-m3', 'nomic-embed-text') * @param settings - Optional settings for the reranking model * * @example * ```ts * const result = await rerank({ * model: ollama.embeddingReranking('bge-m3'), * query: 'What is machine learning?', * documents: [...], * topN: 3, * }); * ``` */ embeddingReranking(modelId: string, settings?: OllamaEmbeddingRerankingSettings): RerankingModelV4; /** * Ollama-specific tools that leverage web search capabilities */ tools: { webSearch: (options?: WebSearchToolOptions) => ReturnType<typeof ollamaTools.webSearch>; webFetch: (options?: WebFetchToolOptions) => ReturnType<typeof ollamaTools.webFetch>; }; } export interface OllamaChatSettings extends Pick<ChatRequest, 'keep_alive' | 'format' | 'tools' | 'think'> { /** * Additional model parameters - uses extended Options type that includes min_p * This automatically includes ALL Ollama parameters including new ones like 'dimensions' */ options?: Partial<Options>; /** * Enable structured output mode */ structuredOutputs?: boolean; /** * Enable reliable tool calling with retry and completion mechanisms. * Defaults to true whenever function tools are provided; set to false to opt out. */ reliableToolCalling?: boolean; /** * Tool calling reliability options. These override the sensible defaults used by the * built-in reliability layer (maxRetries=2, forceCompletion=true, * normalizeParameters=true, validateResults=true). */ toolCallingOptions?: { /** * Maximum number of retry attempts for tool calls */ maxRetries?: number; /** * Whether to force completion when tool calls succeed but no final text is generated */ forceCompletion?: boolean; /** * Whether to normalize parameter names to handle inconsistencies */ normalizeParameters?: boolean; /** * Whether to validate tool results and attempt recovery */ validateResults?: boolean; /** * Custom parameter normalization mappings */ parameterMappings?: Record<string, string[]>; /** * Timeout for tool execution in milliseconds */ toolTimeout?: number; }; /** * Enable reliable object generation with retry and repair mechanisms. * Defaults to true whenever JSON schemas are used; set to false to opt out. */ reliableObjectGeneration?: boolean; /** * Object generation reliability options. These override the sensible defaults used by the * built-in reliability layer (maxRetries=3, attemptRecovery=true, useFallbacks=true, * fixTypeMismatches=true, enableTextRepair=true). */ objectGenerationOptions?: ObjectGenerationOptions; } /** * Settings for configuring Ollama embedding models. * Uses Pick from EmbedRequest for type consistency with the Ollama API. */ export interface OllamaEmbeddingSettings extends Pick<EmbedRequest, 'dimensions'> { /** * Additional embedding parameters (temperature, num_ctx, etc.) */ options?: Partial<Options>; } /** * Schema for validating Ollama provider options */ export declare const ollamaProviderOptionsSchema: z.ZodObject<{ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>; }, z.core.$strip>; /** * Options for configuring Ollama provider calls */ export type OllamaProviderOptions = z.infer<typeof ollamaProviderOptionsSchema>; /** * Schema for validating Ollama chat provider options */ export declare const ollamaChatProviderOptionsSchema: z.ZodObject<{ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>; structuredOutputs: z.ZodOptional<z.ZodBoolean>; }, z.core.$strip>; /** * Options for configuring Ollama chat model calls */ export type OllamaChatProviderOptions = z.infer<typeof ollamaChatProviderOptionsSchema>; /** * Schema for validating Ollama embedding provider options */ export declare const ollamaEmbeddingProviderOptionsSchema: z.ZodObject<{ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>; maxEmbeddingsPerCall: z.ZodOptional<z.ZodNumber>; }, z.core.$strip>; /** * Options for configuring Ollama embedding model calls */ export type OllamaEmbeddingProviderOptions = z.infer<typeof ollamaEmbeddingProviderOptionsSchema>; /** * Create an Ollama provider instance */ export declare function createOllama(options?: OllamaProviderSettings): OllamaProvider; /** * Default Ollama provider instance */ export declare const ollama: OllamaProvider; //# sourceMappingURL=provider.d.ts.map