ai-sdk-ollama 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,73 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2025-08-06
9
+
10
+ ### Added
11
+
12
+ - ๐ŸŽ‰ Initial release of AI SDK Ollama Provider
13
+ - โœ… Full support for Vercel AI SDK v5 (`LanguageModelV2` and `EmbeddingModelV2`)
14
+ - ๐Ÿค– **Chat Language Model** with streaming support
15
+ - Text generation with **dual parameter support** (AI SDK standard + native Ollama)
16
+ - **Cross provider compatibility** via AI SDK parameters (temperature, maxOutputTokens, etc.)
17
+ - **Advanced Ollama features** via native options (mirostat, num_ctx, etc.)
18
+ - **Parameter precedence system** - Ollama options override AI SDK parameters
19
+ - Structured output support (JSON objects)
20
+ - Tool calling capabilities
21
+ - Image input support (with compatible models)
22
+ - ๐Ÿ” **Embedding Model** for text embeddings
23
+ - Batch embedding support (up to 2048 texts)
24
+ - Support for all Ollama embedding models
25
+ - ๐Ÿง  **Model Intelligence System**
26
+ - Comprehensive model capability database
27
+ - Smart model suggestions based on requirements
28
+ - Automatic feature detection and validation
29
+ - Helpful error messages with actionable recommendations
30
+ - ๐Ÿ› ๏ธ **Provider Features**
31
+ - Official `ollama` package integration with **direct option pass-through**
32
+ - **Future proof**: All current and future Ollama parameters supported automatically
33
+ - Custom base URL configuration
34
+ - Custom headers support
35
+ - Custom fetch implementation
36
+ - Comprehensive error handling with custom OllamaError class
37
+ - ๐Ÿ“ฆ **Modern Package**
38
+ - TypeScript with full type safety
39
+ - ES modules with CommonJS compatibility
40
+ - Node.js 22+ support
41
+ - Clean, organized codebase structure
42
+ - ๐Ÿงช **Quality Assurance**
43
+ - Tests (unit + integration)
44
+ - Full TypeScript coverage
45
+ - Linting with ESLint + Prettier
46
+ - Automated testing with Vitest
47
+ - ๐Ÿ“š **Examples & Documentation**
48
+ - 8 comprehensive examples covering all features
49
+ - Basic chat, streaming, tool calling, embeddings
50
+ - Dual parameter demonstrations
51
+ - Model capabilities and validation examples
52
+ - Comprehensive README with AI SDK v5+ compatibility
53
+ - ๐Ÿ–ผ๏ธ **Image Processing Support**: Complete implementation of AI SDK v5 image handling with Ollama
54
+ - Support for image URLs, base64 encoded images, and multiple images
55
+ - Full integration with vision models like LLaVA and bakllava
56
+ - Streaming support with images
57
+ - Mixed content types (text + image + text)
58
+
59
+ ### Technical Details
60
+
61
+ - Built with AI SDK v5 (`@ai-sdk/provider: ^2.0.0`)
62
+ - Uses official Ollama client (`ollama: ^0.5.16`)
63
+ - Requires Node.js >=22
64
+ - Fully typed with TypeScript 5.9+
65
+ - ES module first with CJS fallback
66
+
67
+ ### Supported Models
68
+
69
+ - **Chat Models**: llama3.2, llama3.1, mistral, phi4-mini, qwen2.5, codellama, and all Ollama chat models
70
+ - **Vision Models**: llava, bakllava, llama3.2-vision, minicpm-v
71
+ - **Embedding Models**: nomic-embed-text, all-minilm, mxbai-embed-large, and all Ollama embedding models
72
+
73
+ [0.1.0]: https://github.com/jagreehal/ai-sdk-ollama/releases/tag/v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Jag Reehal 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,277 @@
1
+ # AI SDK Ollama Provider
2
+
3
+ [![npm version](https://badge.fury.io/js/ai-sdk-ollama.svg)](https://badge.fury.io/js/ai-sdk-ollama)
4
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9+-blue.svg)](https://www.typescriptlang.org/)
5
+ [![Node.js](https://img.shields.io/badge/Node.js-22+-green.svg)](https://nodejs.org/)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ A Vercel AI SDK v5+ provider for Ollama built on the official `ollama` package. Type safe, future proof, with cross provider compatibility and native Ollama features.
9
+
10
+ ## Contents
11
+
12
+ - [AI SDK Ollama Provider](#ai-sdk-ollama-provider)
13
+ - [Contents](#contents)
14
+ - [Prerequisites](#prerequisites)
15
+ - [Quick Start](#quick-start)
16
+ - [Installation](#installation)
17
+ - [Basic Usage](#basic-usage)
18
+ - [Key Features](#key-features)
19
+ - [Cross Provider Compatibility](#cross-provider-compatibility)
20
+ - [Native Ollama Power](#native-ollama-power)
21
+ - [Tool Calling Support](#tool-calling-support)
22
+ - [Smart Model Intelligence](#smart-model-intelligence)
23
+ - [Advanced Features](#advanced-features)
24
+ - [Custom Ollama Instance](#custom-ollama-instance)
25
+ - [Structured Output](#structured-output)
26
+ - [Common Issues](#common-issues)
27
+ - [Supported Models](#supported-models)
28
+ - [Learn More](#learn-more)
29
+ - [License](#license)
30
+
31
+ ```typescript
32
+ import { ollama } from 'ai-sdk-ollama';
33
+ import { generateText } from 'ai';
34
+
35
+ // Standard AI SDK parameters work everywhere
36
+ const { text } = await generateText({
37
+ model: ollama('llama3.2'),
38
+ prompt: 'Write a haiku about coding',
39
+ temperature: 0.8,
40
+ maxOutputTokens: 100,
41
+ });
42
+
43
+ // Plus access to Ollama's advanced features
44
+ const { text: advancedText } = await generateText({
45
+ model: ollama('llama3.2', {
46
+ options: {
47
+ mirostat: 2, // Advanced sampling algorithm
48
+ repeat_penalty: 1.1, // Fine-tune repetition
49
+ num_ctx: 8192, // Larger context window
50
+ },
51
+ }),
52
+ prompt: 'Write a haiku about coding',
53
+ temperature: 0.8, // Standard parameters still work
54
+ });
55
+ ```
56
+
57
+ ## Prerequisites
58
+
59
+ - Node.js 22+
60
+ - [Ollama](https://ollama.com) installed locally or running on a remote server
61
+ - AI SDK v5+ (`ai` package)
62
+ - TypeScript 5.9+ (for TypeScript users)
63
+
64
+ ## Quick Start
65
+
66
+ ### Installation
67
+
68
+ ```bash
69
+ npm install ai-sdk-ollama ai@^5.0.0
70
+ ```
71
+
72
+ Ensure you have Ollama running locally:
73
+
74
+ ```bash
75
+ # Install Ollama from ollama.com
76
+ ollama serve
77
+
78
+ # Pull a model
79
+ ollama pull llama3.2
80
+ ```
81
+
82
+ ### Basic Usage
83
+
84
+ ```typescript
85
+ import { ollama } from 'ai-sdk-ollama';
86
+ import { generateText, streamText, embed } from 'ai';
87
+
88
+ // Simple text generation
89
+ const { text } = await generateText({
90
+ model: ollama('llama3.2'),
91
+ prompt: 'What is the capital of France?',
92
+ });
93
+
94
+ console.log(text); // "The capital of France is Paris."
95
+
96
+ // Streaming responses
97
+ const { textStream } = await streamText({
98
+ model: ollama('llama3.2'),
99
+ prompt: 'Tell me a story about a robot',
100
+ });
101
+
102
+ for await (const chunk of textStream) {
103
+ process.stdout.write(chunk);
104
+ }
105
+
106
+ // Embeddings
107
+ const { embedding } = await embed({
108
+ model: ollama.embedding('nomic-embed-text'),
109
+ value: 'Hello world',
110
+ });
111
+
112
+ console.log(embedding.length); // 768 dimensions
113
+ ```
114
+
115
+ ## Key Features
116
+
117
+ ### Cross Provider Compatibility
118
+
119
+ Write code that works with any AI SDK provider:
120
+
121
+ ```typescript
122
+ // This exact code works with OpenAI, Anthropic, or Ollama
123
+ const { text } = await generateText({
124
+ model: ollama('llama3.2'), // or openai('gpt-4') or anthropic('claude-3')
125
+ prompt: 'Write a haiku',
126
+ temperature: 0.8,
127
+ maxOutputTokens: 100,
128
+ topP: 0.9,
129
+ });
130
+ ```
131
+
132
+ ### Native Ollama Power
133
+
134
+ Access Ollama's advanced features without losing portability:
135
+
136
+ ```typescript
137
+ const { text } = await generateText({
138
+ model: ollama('llama3.2', {
139
+ options: {
140
+ mirostat: 2, // Advanced sampling algorithm
141
+ repeat_penalty: 1.1, // Repetition control
142
+ num_ctx: 8192, // Context window size
143
+ },
144
+ }),
145
+ prompt: 'Write a haiku',
146
+ temperature: 0.8, // Standard parameters still work
147
+ });
148
+ ```
149
+
150
+ > **Parameter Precedence**: When both AI SDK parameters and Ollama options are specified, **Ollama options take precedence**. For example, if you set `temperature: 0.5` in Ollama options and `temperature: 0.8` in the `generateText` call, the final value will be `0.5`. This allows you to use standard AI SDK parameters for portability while having fine-grained control with Ollama-specific options when needed.
151
+
152
+ ### Tool Calling Support
153
+
154
+ Ollama supports tool calling with compatible models:
155
+
156
+ ```typescript
157
+ import { z } from 'zod';
158
+
159
+ const { text, toolCalls } = await generateText({
160
+ model: ollama('llama3.2'),
161
+ prompt: 'What is the weather in San Francisco?',
162
+ tools: {
163
+ getWeather: {
164
+ description: 'Get current weather for a location',
165
+ parameters: z.object({
166
+ location: z.string().describe('City name'),
167
+ unit: z.enum(['celsius', 'fahrenheit']).optional(),
168
+ }),
169
+ execute: async ({ location, unit = 'celsius' }) => {
170
+ // Your actual weather API call here
171
+ return { temp: 18, unit, condition: 'sunny' };
172
+ },
173
+ },
174
+ },
175
+ });
176
+ ```
177
+
178
+ ### Smart Model Intelligence
179
+
180
+ The provider automatically detects model capabilities and provides helpful suggestions:
181
+
182
+ ```typescript
183
+ // The provider will automatically warn you if a model doesn't support tools
184
+ const { text } = await generateText({
185
+ model: ollama('llama3'), // Older model without tool support
186
+ prompt: 'What is the weather?',
187
+ tools: {
188
+ /* ... */
189
+ }, // This will throw a helpful error with suggestions
190
+ });
191
+
192
+ // Get model recommendations for specific features
193
+ import { suggestModelsForFeatures } from 'ai-sdk-ollama';
194
+
195
+ const suggestions = suggestModelsForFeatures({
196
+ toolCalling: true,
197
+ performance: 'fast',
198
+ });
199
+ // Returns: [{ modelId: 'llama3.2', reason: 'Excellent tool calling...' }]
200
+ ```
201
+
202
+ ## Advanced Features
203
+
204
+ ### Custom Ollama Instance
205
+
206
+ ```typescript
207
+ import { createOllama } from 'ai-sdk-ollama';
208
+
209
+ const ollama = createOllama({
210
+ baseURL: 'http://my-ollama-server:11434',
211
+ headers: {
212
+ 'Custom-Header': 'value',
213
+ },
214
+ });
215
+
216
+ const { text } = await generateText({
217
+ model: ollama('llama3.2'),
218
+ prompt: 'Hello!',
219
+ });
220
+ ```
221
+
222
+ ### Structured Output
223
+
224
+ ```typescript
225
+ import { generateObject } from 'ai';
226
+ import { z } from 'zod';
227
+
228
+ const { object } = await generateObject({
229
+ model: ollama('llama3.2', { structuredOutputs: true }),
230
+ schema: z.object({
231
+ name: z.string(),
232
+ age: z.number(),
233
+ interests: z.array(z.string()),
234
+ }),
235
+ prompt: 'Generate a random person profile',
236
+ });
237
+
238
+ console.log(object);
239
+ // { name: "Alice", age: 28, interests: ["reading", "hiking"] }
240
+ ```
241
+
242
+ ## Common Issues
243
+
244
+ - **Make sure Ollama is running** - Run `ollama serve` before using the provider
245
+ - **Pull models first** - Use `ollama pull model-name` before generating text
246
+ - **Model compatibility errors** - The provider will throw errors if you try to use unsupported features (e.g., tools with non-compatible models)
247
+ - **Network issues** - Verify Ollama is accessible at the configured URL
248
+ - **TypeScript support** - Full type safety with TypeScript 5.9+
249
+ - **AI SDK v5+ compatibility** - Built for the latest AI SDK specification
250
+
251
+ ## Supported Models
252
+
253
+ Works with any model in your Ollama installation:
254
+
255
+ - **Chat**: `llama3.2`, `llama3.1`, `mistral`, `phi4-mini`, `qwen2.5`, `codellama`
256
+ - **Vision**: `llava`, `bakllava`, `llama3.2-vision`, `minicpm-v`
257
+ - **Embeddings**: `nomic-embed-text`, `all-minilm`, `mxbai-embed-large`
258
+
259
+ ## Learn More
260
+
261
+ ๐Ÿ“š **[Examples Directory](./examples/)** - Comprehensive usage patterns with real working code
262
+
263
+ ๐Ÿš€ **[Quick Start Guide](./examples/basic-chat.ts)** - Get running in 2 minutes
264
+
265
+ โš™๏ธ **[Dual Parameters Demo](./examples/dual-parameter-example.ts)** - See the key feature in action
266
+
267
+ ๐Ÿ”ง **[Tool Calling Guide](./examples/tool-calling-example.ts)** - Function calling with Ollama
268
+
269
+ ๐Ÿ–ผ๏ธ **[Image Processing Guide](./examples/image-handling-example.ts)** - Vision models with LLaVA
270
+
271
+ ๐Ÿ“ก **[Streaming Examples](./examples/streaming-simple-test.ts)** - Real-time responses
272
+
273
+ ## License
274
+
275
+ MIT ยฉ [Jag Reehal](https://jagreehal.com)
276
+
277
+ See [LICENSE](./LICENSE) for details.