UNPKG

ai-sdk-ollama

Version:

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

220 lines (213 loc) 7.07 kB
import { ProviderV2, LanguageModelV2, EmbeddingModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, JSONValue, LanguageModelV2CallWarning, LanguageModelV2StreamPart, EmbeddingModelV2Embedding } from '@ai-sdk/provider'; import { Ollama } from 'ollama'; interface OllamaProviderSettings { /** * Base URL for the Ollama API (defaults to http://127.0.0.1:11434) */ baseURL?: string; /** * Custom headers for API requests */ headers?: Record<string, string>; /** * Custom fetch implementation */ fetch?: typeof fetch; } interface OllamaProvider extends ProviderV2 { /** * Create a language model instance */ (modelId: string, settings?: OllamaChatSettings): LanguageModelV2; /** * Create a language model instance with the `chat` method */ chat(modelId: string, settings?: OllamaChatSettings): LanguageModelV2; /** * Create a language model instance with the `languageModel` method */ languageModel(modelId: string, settings?: OllamaChatSettings): LanguageModelV2; /** * Create an embedding model instance */ embedding(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV2<string>; /** * Create an embedding model instance with the `textEmbedding` method */ textEmbedding(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV2<string>; /** * Create an embedding model instance with the `textEmbeddingModel` method */ textEmbeddingModel(modelId: string, settings?: OllamaEmbeddingSettings): EmbeddingModelV2<string>; } interface OllamaChatSettings { /** * Enable structured output mode */ structuredOutputs?: boolean; /** * Enable reasoning support for models that support it */ reasoning?: boolean; /** * Additional model parameters */ options?: { num_ctx?: number; num_predict?: number; temperature?: number; top_k?: number; top_p?: number; min_p?: number; seed?: number; stop?: string[]; num_keep?: number; typical_p?: number; repeat_last_n?: number; repeat_penalty?: number; presence_penalty?: number; frequency_penalty?: number; mirostat?: number; mirostat_tau?: number; mirostat_eta?: number; penalize_newline?: boolean; numa?: boolean; num_thread?: number; num_gpu?: number; main_gpu?: number; low_vram?: boolean; f16_kv?: boolean; vocab_only?: boolean; use_mmap?: boolean; use_mlock?: boolean; }; } interface OllamaEmbeddingSettings { /** * Additional embedding parameters */ options?: { num_thread?: number; }; } /** * Options for configuring Ollama provider calls */ interface OllamaProviderOptions { /** * Additional headers to include in requests */ headers?: Record<string, string>; } /** * Options for configuring Ollama chat model calls */ interface OllamaChatProviderOptions extends OllamaProviderOptions { /** * Enable structured output mode for object generation */ structuredOutputs?: boolean; } /** * Options for configuring Ollama embedding model calls */ interface OllamaEmbeddingProviderOptions extends OllamaProviderOptions { /** * Maximum number of embeddings to process in a single call */ maxEmbeddingsPerCall?: number; } /** * Create an Ollama provider instance for browser environments */ declare function createOllama(options?: OllamaProviderSettings): OllamaProvider; /** * Default Ollama provider instance for browser environments */ declare const ollama: OllamaProvider; interface OllamaChatConfig { client: Ollama; provider: string; } declare class OllamaChatLanguageModel implements LanguageModelV2 { readonly modelId: string; readonly settings: OllamaChatSettings; private readonly config; readonly specificationVersion: "v2"; readonly defaultObjectGenerationMode = "json"; readonly supportsImages = true; readonly supportsVideoURLs = false; readonly supportsAudioURLs = false; readonly supportsVideoFile = false; readonly supportsAudioFile = false; readonly supportsImageFile = true; readonly supportedUrls: Record<string, RegExp[]>; constructor(modelId: string, settings: OllamaChatSettings, config: OllamaChatConfig); get provider(): string; get supportsStructuredOutputs(): boolean; /** * Check if structured outputs should be enabled based on the call options * This is used internally to auto-detect when structured outputs are needed */ private shouldEnableStructuredOutputs; private getCallOptions; doGenerate(options: LanguageModelV2CallOptions): Promise<{ content: LanguageModelV2Content[]; finishReason: LanguageModelV2FinishReason; usage: LanguageModelV2Usage; providerMetadata?: Record<string, Record<string, JSONValue>>; request?: { body: string; }; response?: { id?: string; timestamp?: Date; modelId?: string; }; warnings: LanguageModelV2CallWarning[]; }>; doStream(options: LanguageModelV2CallOptions): Promise<{ stream: ReadableStream<LanguageModelV2StreamPart>; rawCall: { rawPrompt: unknown; rawSettings: Record<string, unknown>; }; warnings?: LanguageModelV2CallWarning[]; }>; } interface OllamaEmbeddingConfig { client: Ollama; provider: string; } declare class OllamaEmbeddingModel implements EmbeddingModelV2<string> { private readonly settings; private readonly config; readonly specificationVersion: "v2"; readonly modelId: string; readonly maxEmbeddingsPerCall = 2048; readonly supportsParallelCalls = true; constructor(modelId: string, settings: OllamaEmbeddingSettings, config: OllamaEmbeddingConfig); get provider(): string; doEmbed(params: { values: string[]; abortSignal?: AbortSignal; }): Promise<{ embeddings: EmbeddingModelV2Embedding[]; }>; } interface OllamaErrorData { message: string; code?: string; details?: unknown; } declare class OllamaError extends Error { readonly cause?: unknown; readonly data?: OllamaErrorData; constructor({ message, cause, data, }: { message: string; cause?: unknown; data?: OllamaErrorData; }); static isOllamaError(error: unknown): error is OllamaError; } export { type OllamaChatConfig, OllamaChatLanguageModel, type OllamaChatProviderOptions, type OllamaChatSettings, type OllamaEmbeddingConfig, OllamaEmbeddingModel, type OllamaEmbeddingProviderOptions, type OllamaEmbeddingSettings, OllamaError, type OllamaErrorData, type OllamaProvider, type OllamaProviderOptions, type OllamaProviderSettings, createOllama, ollama };