@agentic-kit/ollama 1.2.0 → 2.0.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/README.md +9 -4
- package/esm/index.js +20 -3
- package/index.d.ts +13 -1
- package/index.js +20 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -50,9 +50,10 @@ await client.generate(
|
|
|
50
50
|
// Pull a model to local cache
|
|
51
51
|
await client.pullModel('mistral');
|
|
52
52
|
|
|
53
|
-
// Generate embeddings
|
|
54
|
-
const
|
|
55
|
-
console.log('Embedding vector length:', embedding.length);
|
|
53
|
+
// Generate embeddings (with token count from /api/embed)
|
|
54
|
+
const result = await client.generateEmbedding('Compute embeddings');
|
|
55
|
+
console.log('Embedding vector length:', result.embedding.length);
|
|
56
|
+
console.log('Prompt tokens:', result.promptTokens);
|
|
56
57
|
|
|
57
58
|
// Delete a pulled model when done
|
|
58
59
|
await client.deleteModel('mistral');
|
|
@@ -64,7 +65,7 @@ await client.deleteModel('mistral');
|
|
|
64
65
|
- `.listModels(): Promise<string[]>`
|
|
65
66
|
- `.showModel(model: string): Promise<{ capabilities?: string[] } | null>`
|
|
66
67
|
- `.generate(input: GenerateInput, onChunk?: (chunk: string) => void): Promise<string | void>`
|
|
67
|
-
- `.generateEmbedding(text: string, model?: string): Promise<number[]
|
|
68
|
+
- `.generateEmbedding(text: string, model?: string): Promise<EmbeddingResult>` — returns `{ embedding: number[], promptTokens: number }`, defaults to `nomic-embed-text`
|
|
68
69
|
- `.pullModel(model: string): Promise<void>`
|
|
69
70
|
- `.deleteModel(model: string): Promise<void>`
|
|
70
71
|
|
|
@@ -75,6 +76,10 @@ import { OllamaAdapter } from '@agentic-kit/ollama';
|
|
|
75
76
|
|
|
76
77
|
const provider = new OllamaAdapter('http://localhost:11434');
|
|
77
78
|
const model = provider.createModel('llama3');
|
|
79
|
+
|
|
80
|
+
// Embeddings with real token counts
|
|
81
|
+
const result = await provider.embed('Compute embeddings', 'nomic-embed-text');
|
|
82
|
+
console.log(result.embedding.length, result.promptTokens);
|
|
78
83
|
```
|
|
79
84
|
|
|
80
85
|
## Local Live Tests
|
package/esm/index.js
CHANGED
|
@@ -118,16 +118,19 @@ export class OllamaClient {
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
async generateEmbedding(text, model = 'nomic-embed-text') {
|
|
121
|
-
const response = await fetch(`${this.baseUrl}/api/
|
|
121
|
+
const response = await fetch(`${this.baseUrl}/api/embed`, {
|
|
122
122
|
method: 'POST',
|
|
123
123
|
headers: { 'Content-Type': 'application/json' },
|
|
124
|
-
body: JSON.stringify({ model,
|
|
124
|
+
body: JSON.stringify({ model, input: text }),
|
|
125
125
|
});
|
|
126
126
|
if (!response.ok) {
|
|
127
127
|
throw new Error(`generateEmbedding failed: ${response.status} ${response.statusText}`);
|
|
128
128
|
}
|
|
129
129
|
const payload = (await response.json());
|
|
130
|
-
return
|
|
130
|
+
return {
|
|
131
|
+
embedding: payload.embeddings[0],
|
|
132
|
+
promptTokens: payload.prompt_eval_count ?? 0,
|
|
133
|
+
};
|
|
131
134
|
}
|
|
132
135
|
async generate(input, onChunk) {
|
|
133
136
|
const context = legacyInputToContext(input);
|
|
@@ -189,6 +192,9 @@ export class OllamaAdapter {
|
|
|
189
192
|
async listModels() {
|
|
190
193
|
return this.client.listModels();
|
|
191
194
|
}
|
|
195
|
+
async embed(text, model = 'nomic-embed-text') {
|
|
196
|
+
return this.client.generateEmbedding(text, model);
|
|
197
|
+
}
|
|
192
198
|
stream(model, context, options) {
|
|
193
199
|
const stream = new DefaultAssistantMessageEventStream();
|
|
194
200
|
const output = createAssistantMessage(model);
|
|
@@ -265,6 +271,7 @@ export class OllamaAdapter {
|
|
|
265
271
|
output.usage.input = payload.prompt_eval_count ?? output.usage.input;
|
|
266
272
|
output.usage.output = payload.eval_count ?? output.usage.output;
|
|
267
273
|
output.usage.totalTokens = output.usage.input + output.usage.output;
|
|
274
|
+
calculateUsageCost(model, output.usage);
|
|
268
275
|
output.stopReason = payload.done_reason === 'length' ? 'length' : 'stop';
|
|
269
276
|
if (thinkingIndex !== undefined) {
|
|
270
277
|
stream.push({
|
|
@@ -408,6 +415,7 @@ function createAssistantMessage(model) {
|
|
|
408
415
|
usage: {
|
|
409
416
|
input: 0,
|
|
410
417
|
output: 0,
|
|
418
|
+
reasoning: 0,
|
|
411
419
|
cacheRead: 0,
|
|
412
420
|
cacheWrite: 0,
|
|
413
421
|
totalTokens: 0,
|
|
@@ -417,6 +425,14 @@ function createAssistantMessage(model) {
|
|
|
417
425
|
timestamp: Date.now(),
|
|
418
426
|
};
|
|
419
427
|
}
|
|
428
|
+
function calculateUsageCost(model, usage) {
|
|
429
|
+
usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
|
|
430
|
+
usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
|
|
431
|
+
usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
|
|
432
|
+
usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
|
|
433
|
+
usage.cost.total =
|
|
434
|
+
usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
435
|
+
}
|
|
420
436
|
function legacyInputToContext(input) {
|
|
421
437
|
const messages = input.messages
|
|
422
438
|
? input.messages
|
|
@@ -431,6 +447,7 @@ function legacyInputToContext(input) {
|
|
|
431
447
|
usage: {
|
|
432
448
|
input: 0,
|
|
433
449
|
output: 0,
|
|
450
|
+
reasoning: 0,
|
|
434
451
|
cacheRead: 0,
|
|
435
452
|
cacheWrite: 0,
|
|
436
453
|
totalTokens: 0,
|
package/index.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ interface ModelDescriptor {
|
|
|
14
14
|
tools?: boolean;
|
|
15
15
|
contextWindow?: number;
|
|
16
16
|
maxOutputTokens?: number;
|
|
17
|
+
cost?: {
|
|
18
|
+
input?: number;
|
|
19
|
+
output?: number;
|
|
20
|
+
cacheRead?: number;
|
|
21
|
+
cacheWrite?: number;
|
|
22
|
+
};
|
|
17
23
|
headers?: Record<string, string>;
|
|
18
24
|
}
|
|
19
25
|
interface TextContent {
|
|
@@ -38,6 +44,7 @@ interface ToolCallContent {
|
|
|
38
44
|
interface Usage {
|
|
39
45
|
input: number;
|
|
40
46
|
output: number;
|
|
47
|
+
reasoning: number;
|
|
41
48
|
cacheRead: number;
|
|
42
49
|
cacheWrite: number;
|
|
43
50
|
totalTokens: number;
|
|
@@ -154,6 +161,10 @@ export interface GenerateInput {
|
|
|
154
161
|
temperature?: number;
|
|
155
162
|
maxTokens?: number;
|
|
156
163
|
}
|
|
164
|
+
export interface EmbeddingResult {
|
|
165
|
+
embedding: number[];
|
|
166
|
+
promptTokens: number;
|
|
167
|
+
}
|
|
157
168
|
export declare const OLLAMA_MODELS: ModelDescriptor[];
|
|
158
169
|
export declare class OllamaClient {
|
|
159
170
|
private readonly baseUrl;
|
|
@@ -164,7 +175,7 @@ export declare class OllamaClient {
|
|
|
164
175
|
} | null>;
|
|
165
176
|
pullModel(model: string): Promise<void>;
|
|
166
177
|
deleteModel(model: string): Promise<void>;
|
|
167
|
-
generateEmbedding(text: string, model?: string): Promise<
|
|
178
|
+
generateEmbedding(text: string, model?: string): Promise<EmbeddingResult>;
|
|
168
179
|
generate(input: GenerateInput): Promise<string>;
|
|
169
180
|
generate(input: GenerateInput, onChunk: (chunk: string) => void): Promise<void>;
|
|
170
181
|
}
|
|
@@ -177,6 +188,7 @@ export declare class OllamaAdapter {
|
|
|
177
188
|
constructor(baseUrl?: string);
|
|
178
189
|
createModel(modelId: string, overrides?: Partial<ModelDescriptor>): ModelDescriptor;
|
|
179
190
|
listModels(): Promise<Array<ModelDescriptor | string>>;
|
|
191
|
+
embed(text: string, model?: string): Promise<EmbeddingResult>;
|
|
180
192
|
stream(model: ModelDescriptor, context: Context, options?: StreamOptions): AssistantMessageEventStream;
|
|
181
193
|
}
|
|
182
194
|
export default OllamaClient;
|
package/index.js
CHANGED
|
@@ -121,16 +121,19 @@ class OllamaClient {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
async generateEmbedding(text, model = 'nomic-embed-text') {
|
|
124
|
-
const response = await fetch(`${this.baseUrl}/api/
|
|
124
|
+
const response = await fetch(`${this.baseUrl}/api/embed`, {
|
|
125
125
|
method: 'POST',
|
|
126
126
|
headers: { 'Content-Type': 'application/json' },
|
|
127
|
-
body: JSON.stringify({ model,
|
|
127
|
+
body: JSON.stringify({ model, input: text }),
|
|
128
128
|
});
|
|
129
129
|
if (!response.ok) {
|
|
130
130
|
throw new Error(`generateEmbedding failed: ${response.status} ${response.statusText}`);
|
|
131
131
|
}
|
|
132
132
|
const payload = (await response.json());
|
|
133
|
-
return
|
|
133
|
+
return {
|
|
134
|
+
embedding: payload.embeddings[0],
|
|
135
|
+
promptTokens: payload.prompt_eval_count ?? 0,
|
|
136
|
+
};
|
|
134
137
|
}
|
|
135
138
|
async generate(input, onChunk) {
|
|
136
139
|
const context = legacyInputToContext(input);
|
|
@@ -193,6 +196,9 @@ class OllamaAdapter {
|
|
|
193
196
|
async listModels() {
|
|
194
197
|
return this.client.listModels();
|
|
195
198
|
}
|
|
199
|
+
async embed(text, model = 'nomic-embed-text') {
|
|
200
|
+
return this.client.generateEmbedding(text, model);
|
|
201
|
+
}
|
|
196
202
|
stream(model, context, options) {
|
|
197
203
|
const stream = new DefaultAssistantMessageEventStream();
|
|
198
204
|
const output = createAssistantMessage(model);
|
|
@@ -269,6 +275,7 @@ class OllamaAdapter {
|
|
|
269
275
|
output.usage.input = payload.prompt_eval_count ?? output.usage.input;
|
|
270
276
|
output.usage.output = payload.eval_count ?? output.usage.output;
|
|
271
277
|
output.usage.totalTokens = output.usage.input + output.usage.output;
|
|
278
|
+
calculateUsageCost(model, output.usage);
|
|
272
279
|
output.stopReason = payload.done_reason === 'length' ? 'length' : 'stop';
|
|
273
280
|
if (thinkingIndex !== undefined) {
|
|
274
281
|
stream.push({
|
|
@@ -413,6 +420,7 @@ function createAssistantMessage(model) {
|
|
|
413
420
|
usage: {
|
|
414
421
|
input: 0,
|
|
415
422
|
output: 0,
|
|
423
|
+
reasoning: 0,
|
|
416
424
|
cacheRead: 0,
|
|
417
425
|
cacheWrite: 0,
|
|
418
426
|
totalTokens: 0,
|
|
@@ -422,6 +430,14 @@ function createAssistantMessage(model) {
|
|
|
422
430
|
timestamp: Date.now(),
|
|
423
431
|
};
|
|
424
432
|
}
|
|
433
|
+
function calculateUsageCost(model, usage) {
|
|
434
|
+
usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
|
|
435
|
+
usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
|
|
436
|
+
usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
|
|
437
|
+
usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
|
|
438
|
+
usage.cost.total =
|
|
439
|
+
usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
440
|
+
}
|
|
425
441
|
function legacyInputToContext(input) {
|
|
426
442
|
const messages = input.messages
|
|
427
443
|
? input.messages
|
|
@@ -436,6 +452,7 @@ function legacyInputToContext(input) {
|
|
|
436
452
|
usage: {
|
|
437
453
|
input: 0,
|
|
438
454
|
output: 0,
|
|
455
|
+
reasoning: 0,
|
|
439
456
|
cacheRead: 0,
|
|
440
457
|
cacheWrite: 0,
|
|
441
458
|
totalTokens: 0,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentic-kit/ollama",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Ollama",
|
|
6
6
|
"main": "index.js",
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"test:watch": "jest --watch"
|
|
41
41
|
},
|
|
42
42
|
"keywords": [],
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "c2b13e65b26f14c9983e9b7cc4eafbe4919b13d4"
|
|
44
44
|
}
|