@agentic-kit/ollama 1.2.1 → 2.0.1
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 +10 -108
- package/index.d.ts +7 -151
- package/index.js +25 -123
- package/package.json +5 -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
|
@@ -1,78 +1,5 @@
|
|
|
1
|
+
import { calculateUsageCost, clone, createAssistantMessage, DefaultAssistantMessageEventStream, } from '@agentic-kit/protocol';
|
|
1
2
|
const fetch = globalThis.fetch.bind(globalThis);
|
|
2
|
-
class EventStream {
|
|
3
|
-
isTerminal;
|
|
4
|
-
extractResult;
|
|
5
|
-
queue = [];
|
|
6
|
-
waiting = [];
|
|
7
|
-
done = false;
|
|
8
|
-
finalResultPromise;
|
|
9
|
-
resolveFinalResult;
|
|
10
|
-
constructor(isTerminal, extractResult) {
|
|
11
|
-
this.isTerminal = isTerminal;
|
|
12
|
-
this.extractResult = extractResult;
|
|
13
|
-
this.finalResultPromise = new Promise((resolve) => {
|
|
14
|
-
this.resolveFinalResult = resolve;
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
push(event) {
|
|
18
|
-
if (this.done) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
if (this.isTerminal(event)) {
|
|
22
|
-
this.done = true;
|
|
23
|
-
this.resolveFinalResult(this.extractResult(event));
|
|
24
|
-
}
|
|
25
|
-
const waiter = this.waiting.shift();
|
|
26
|
-
if (waiter) {
|
|
27
|
-
waiter({ value: event, done: false });
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
this.queue.push(event);
|
|
31
|
-
}
|
|
32
|
-
end(result) {
|
|
33
|
-
this.done = true;
|
|
34
|
-
if (result !== undefined) {
|
|
35
|
-
this.resolveFinalResult(result);
|
|
36
|
-
}
|
|
37
|
-
while (this.waiting.length > 0) {
|
|
38
|
-
this.waiting.shift()({ value: undefined, done: true });
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
async *[Symbol.asyncIterator]() {
|
|
42
|
-
while (true) {
|
|
43
|
-
if (this.queue.length > 0) {
|
|
44
|
-
yield this.queue.shift();
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
if (this.done) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const next = await new Promise((resolve) => {
|
|
51
|
-
this.waiting.push(resolve);
|
|
52
|
-
});
|
|
53
|
-
if (next.done) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
yield next.value;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
result() {
|
|
60
|
-
return this.finalResultPromise;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
class DefaultAssistantMessageEventStream extends EventStream {
|
|
64
|
-
constructor() {
|
|
65
|
-
super((event) => event.type === 'done' || event.type === 'error', (event) => {
|
|
66
|
-
if (event.type === 'done') {
|
|
67
|
-
return event.message;
|
|
68
|
-
}
|
|
69
|
-
if (event.type === 'error') {
|
|
70
|
-
return event.error;
|
|
71
|
-
}
|
|
72
|
-
throw new Error('Unexpected terminal event');
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
3
|
export const OLLAMA_MODELS = [];
|
|
77
4
|
export class OllamaClient {
|
|
78
5
|
baseUrl;
|
|
@@ -118,16 +45,19 @@ export class OllamaClient {
|
|
|
118
45
|
}
|
|
119
46
|
}
|
|
120
47
|
async generateEmbedding(text, model = 'nomic-embed-text') {
|
|
121
|
-
const response = await fetch(`${this.baseUrl}/api/
|
|
48
|
+
const response = await fetch(`${this.baseUrl}/api/embed`, {
|
|
122
49
|
method: 'POST',
|
|
123
50
|
headers: { 'Content-Type': 'application/json' },
|
|
124
|
-
body: JSON.stringify({ model,
|
|
51
|
+
body: JSON.stringify({ model, input: text }),
|
|
125
52
|
});
|
|
126
53
|
if (!response.ok) {
|
|
127
54
|
throw new Error(`generateEmbedding failed: ${response.status} ${response.statusText}`);
|
|
128
55
|
}
|
|
129
56
|
const payload = (await response.json());
|
|
130
|
-
return
|
|
57
|
+
return {
|
|
58
|
+
embedding: payload.embeddings[0],
|
|
59
|
+
promptTokens: payload.prompt_eval_count ?? 0,
|
|
60
|
+
};
|
|
131
61
|
}
|
|
132
62
|
async generate(input, onChunk) {
|
|
133
63
|
const context = legacyInputToContext(input);
|
|
@@ -189,6 +119,9 @@ export class OllamaAdapter {
|
|
|
189
119
|
async listModels() {
|
|
190
120
|
return this.client.listModels();
|
|
191
121
|
}
|
|
122
|
+
async embed(text, model = 'nomic-embed-text') {
|
|
123
|
+
return this.client.generateEmbedding(text, model);
|
|
124
|
+
}
|
|
192
125
|
stream(model, context, options) {
|
|
193
126
|
const stream = new DefaultAssistantMessageEventStream();
|
|
194
127
|
const output = createAssistantMessage(model);
|
|
@@ -399,34 +332,6 @@ function toOllamaMessages(context) {
|
|
|
399
332
|
}
|
|
400
333
|
return messages;
|
|
401
334
|
}
|
|
402
|
-
function createAssistantMessage(model) {
|
|
403
|
-
return {
|
|
404
|
-
role: 'assistant',
|
|
405
|
-
api: model.api,
|
|
406
|
-
provider: model.provider,
|
|
407
|
-
model: model.id,
|
|
408
|
-
content: [],
|
|
409
|
-
usage: {
|
|
410
|
-
input: 0,
|
|
411
|
-
output: 0,
|
|
412
|
-
reasoning: 0,
|
|
413
|
-
cacheRead: 0,
|
|
414
|
-
cacheWrite: 0,
|
|
415
|
-
totalTokens: 0,
|
|
416
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
417
|
-
},
|
|
418
|
-
stopReason: 'stop',
|
|
419
|
-
timestamp: Date.now(),
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
function calculateUsageCost(model, usage) {
|
|
423
|
-
usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
|
|
424
|
-
usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
|
|
425
|
-
usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
|
|
426
|
-
usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
|
|
427
|
-
usage.cost.total =
|
|
428
|
-
usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
429
|
-
}
|
|
430
335
|
function legacyInputToContext(input) {
|
|
431
336
|
const messages = input.messages
|
|
432
337
|
? input.messages
|
|
@@ -461,9 +366,6 @@ function legacyInputToContext(input) {
|
|
|
461
366
|
messages,
|
|
462
367
|
};
|
|
463
368
|
}
|
|
464
|
-
function clone(value) {
|
|
465
|
-
return JSON.parse(JSON.stringify(value));
|
|
466
|
-
}
|
|
467
369
|
async function* iterateResponseBody(body) {
|
|
468
370
|
if ('getReader' in body && typeof body.getReader === 'function') {
|
|
469
371
|
const reader = body.getReader();
|
package/index.d.ts
CHANGED
|
@@ -1,153 +1,4 @@
|
|
|
1
|
-
type
|
|
2
|
-
type JsonValue = JsonPrimitive | JsonObject | JsonValue[];
|
|
3
|
-
interface JsonObject {
|
|
4
|
-
[key: string]: JsonValue | undefined;
|
|
5
|
-
}
|
|
6
|
-
interface ModelDescriptor {
|
|
7
|
-
id: string;
|
|
8
|
-
name: string;
|
|
9
|
-
api: string;
|
|
10
|
-
provider: string;
|
|
11
|
-
baseUrl: string;
|
|
12
|
-
input: Array<'text' | 'image'>;
|
|
13
|
-
reasoning: boolean;
|
|
14
|
-
tools?: boolean;
|
|
15
|
-
contextWindow?: number;
|
|
16
|
-
maxOutputTokens?: number;
|
|
17
|
-
cost?: {
|
|
18
|
-
input?: number;
|
|
19
|
-
output?: number;
|
|
20
|
-
cacheRead?: number;
|
|
21
|
-
cacheWrite?: number;
|
|
22
|
-
};
|
|
23
|
-
headers?: Record<string, string>;
|
|
24
|
-
}
|
|
25
|
-
interface TextContent {
|
|
26
|
-
type: 'text';
|
|
27
|
-
text: string;
|
|
28
|
-
}
|
|
29
|
-
interface ImageContent {
|
|
30
|
-
type: 'image';
|
|
31
|
-
data: string;
|
|
32
|
-
mimeType: string;
|
|
33
|
-
}
|
|
34
|
-
interface ThinkingContent {
|
|
35
|
-
type: 'thinking';
|
|
36
|
-
thinking: string;
|
|
37
|
-
}
|
|
38
|
-
interface ToolCallContent {
|
|
39
|
-
type: 'toolCall';
|
|
40
|
-
id: string;
|
|
41
|
-
name: string;
|
|
42
|
-
arguments: Record<string, JsonValue | undefined>;
|
|
43
|
-
}
|
|
44
|
-
interface Usage {
|
|
45
|
-
input: number;
|
|
46
|
-
output: number;
|
|
47
|
-
reasoning: number;
|
|
48
|
-
cacheRead: number;
|
|
49
|
-
cacheWrite: number;
|
|
50
|
-
totalTokens: number;
|
|
51
|
-
cost: {
|
|
52
|
-
input: number;
|
|
53
|
-
output: number;
|
|
54
|
-
cacheRead: number;
|
|
55
|
-
cacheWrite: number;
|
|
56
|
-
total: number;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
type Message = {
|
|
60
|
-
role: 'user';
|
|
61
|
-
content: string | Array<TextContent | ImageContent>;
|
|
62
|
-
timestamp: number;
|
|
63
|
-
} | {
|
|
64
|
-
role: 'assistant';
|
|
65
|
-
content: Array<TextContent | ThinkingContent | ToolCallContent>;
|
|
66
|
-
api: string;
|
|
67
|
-
provider: string;
|
|
68
|
-
model: string;
|
|
69
|
-
usage: Usage;
|
|
70
|
-
stopReason: 'stop' | 'length' | 'toolUse' | 'error' | 'aborted';
|
|
71
|
-
errorMessage?: string;
|
|
72
|
-
timestamp: number;
|
|
73
|
-
} | {
|
|
74
|
-
role: 'toolResult';
|
|
75
|
-
toolCallId: string;
|
|
76
|
-
toolName: string;
|
|
77
|
-
content: Array<TextContent | ImageContent>;
|
|
78
|
-
isError: boolean;
|
|
79
|
-
details?: unknown;
|
|
80
|
-
timestamp: number;
|
|
81
|
-
};
|
|
82
|
-
interface Context {
|
|
83
|
-
systemPrompt?: string;
|
|
84
|
-
messages: Message[];
|
|
85
|
-
}
|
|
86
|
-
interface StreamOptions {
|
|
87
|
-
maxTokens?: number;
|
|
88
|
-
signal?: AbortSignal;
|
|
89
|
-
temperature?: number;
|
|
90
|
-
}
|
|
91
|
-
type AssistantMessage = Extract<Message, {
|
|
92
|
-
role: 'assistant';
|
|
93
|
-
}>;
|
|
94
|
-
type AssistantMessageEvent = {
|
|
95
|
-
type: 'start';
|
|
96
|
-
partial: AssistantMessage;
|
|
97
|
-
} | {
|
|
98
|
-
type: 'text_start';
|
|
99
|
-
contentIndex: number;
|
|
100
|
-
partial: AssistantMessage;
|
|
101
|
-
} | {
|
|
102
|
-
type: 'text_delta';
|
|
103
|
-
contentIndex: number;
|
|
104
|
-
delta: string;
|
|
105
|
-
partial: AssistantMessage;
|
|
106
|
-
} | {
|
|
107
|
-
type: 'text_end';
|
|
108
|
-
contentIndex: number;
|
|
109
|
-
content: string;
|
|
110
|
-
partial: AssistantMessage;
|
|
111
|
-
} | {
|
|
112
|
-
type: 'thinking_start';
|
|
113
|
-
contentIndex: number;
|
|
114
|
-
partial: AssistantMessage;
|
|
115
|
-
} | {
|
|
116
|
-
type: 'thinking_delta';
|
|
117
|
-
contentIndex: number;
|
|
118
|
-
delta: string;
|
|
119
|
-
partial: AssistantMessage;
|
|
120
|
-
} | {
|
|
121
|
-
type: 'thinking_end';
|
|
122
|
-
contentIndex: number;
|
|
123
|
-
content: string;
|
|
124
|
-
partial: AssistantMessage;
|
|
125
|
-
} | {
|
|
126
|
-
type: 'toolcall_start';
|
|
127
|
-
contentIndex: number;
|
|
128
|
-
partial: AssistantMessage;
|
|
129
|
-
} | {
|
|
130
|
-
type: 'toolcall_delta';
|
|
131
|
-
contentIndex: number;
|
|
132
|
-
delta: string;
|
|
133
|
-
partial: AssistantMessage;
|
|
134
|
-
} | {
|
|
135
|
-
type: 'toolcall_end';
|
|
136
|
-
contentIndex: number;
|
|
137
|
-
toolCall: ToolCallContent;
|
|
138
|
-
partial: AssistantMessage;
|
|
139
|
-
} | {
|
|
140
|
-
type: 'done';
|
|
141
|
-
reason: 'stop' | 'length' | 'toolUse';
|
|
142
|
-
message: AssistantMessage;
|
|
143
|
-
} | {
|
|
144
|
-
type: 'error';
|
|
145
|
-
reason: 'error' | 'aborted';
|
|
146
|
-
error: AssistantMessage;
|
|
147
|
-
};
|
|
148
|
-
interface AssistantMessageEventStream extends AsyncIterable<AssistantMessageEvent> {
|
|
149
|
-
result(): Promise<AssistantMessage>;
|
|
150
|
-
}
|
|
1
|
+
import { type AssistantMessageEventStream, type Context, type ModelDescriptor, type StreamOptions } from '@agentic-kit/protocol';
|
|
151
2
|
export interface ChatMessage {
|
|
152
3
|
role: 'system' | 'user' | 'assistant';
|
|
153
4
|
content: string;
|
|
@@ -161,6 +12,10 @@ export interface GenerateInput {
|
|
|
161
12
|
temperature?: number;
|
|
162
13
|
maxTokens?: number;
|
|
163
14
|
}
|
|
15
|
+
export interface EmbeddingResult {
|
|
16
|
+
embedding: number[];
|
|
17
|
+
promptTokens: number;
|
|
18
|
+
}
|
|
164
19
|
export declare const OLLAMA_MODELS: ModelDescriptor[];
|
|
165
20
|
export declare class OllamaClient {
|
|
166
21
|
private readonly baseUrl;
|
|
@@ -171,7 +26,7 @@ export declare class OllamaClient {
|
|
|
171
26
|
} | null>;
|
|
172
27
|
pullModel(model: string): Promise<void>;
|
|
173
28
|
deleteModel(model: string): Promise<void>;
|
|
174
|
-
generateEmbedding(text: string, model?: string): Promise<
|
|
29
|
+
generateEmbedding(text: string, model?: string): Promise<EmbeddingResult>;
|
|
175
30
|
generate(input: GenerateInput): Promise<string>;
|
|
176
31
|
generate(input: GenerateInput, onChunk: (chunk: string) => void): Promise<void>;
|
|
177
32
|
}
|
|
@@ -184,6 +39,7 @@ export declare class OllamaAdapter {
|
|
|
184
39
|
constructor(baseUrl?: string);
|
|
185
40
|
createModel(modelId: string, overrides?: Partial<ModelDescriptor>): ModelDescriptor;
|
|
186
41
|
listModels(): Promise<Array<ModelDescriptor | string>>;
|
|
42
|
+
embed(text: string, model?: string): Promise<EmbeddingResult>;
|
|
187
43
|
stream(model: ModelDescriptor, context: Context, options?: StreamOptions): AssistantMessageEventStream;
|
|
188
44
|
}
|
|
189
45
|
export default OllamaClient;
|
package/index.js
CHANGED
|
@@ -1,81 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OllamaAdapter = exports.OllamaClient = exports.OLLAMA_MODELS = void 0;
|
|
4
|
+
const protocol_1 = require("@agentic-kit/protocol");
|
|
4
5
|
const fetch = globalThis.fetch.bind(globalThis);
|
|
5
|
-
class EventStream {
|
|
6
|
-
isTerminal;
|
|
7
|
-
extractResult;
|
|
8
|
-
queue = [];
|
|
9
|
-
waiting = [];
|
|
10
|
-
done = false;
|
|
11
|
-
finalResultPromise;
|
|
12
|
-
resolveFinalResult;
|
|
13
|
-
constructor(isTerminal, extractResult) {
|
|
14
|
-
this.isTerminal = isTerminal;
|
|
15
|
-
this.extractResult = extractResult;
|
|
16
|
-
this.finalResultPromise = new Promise((resolve) => {
|
|
17
|
-
this.resolveFinalResult = resolve;
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
push(event) {
|
|
21
|
-
if (this.done) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
if (this.isTerminal(event)) {
|
|
25
|
-
this.done = true;
|
|
26
|
-
this.resolveFinalResult(this.extractResult(event));
|
|
27
|
-
}
|
|
28
|
-
const waiter = this.waiting.shift();
|
|
29
|
-
if (waiter) {
|
|
30
|
-
waiter({ value: event, done: false });
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
this.queue.push(event);
|
|
34
|
-
}
|
|
35
|
-
end(result) {
|
|
36
|
-
this.done = true;
|
|
37
|
-
if (result !== undefined) {
|
|
38
|
-
this.resolveFinalResult(result);
|
|
39
|
-
}
|
|
40
|
-
while (this.waiting.length > 0) {
|
|
41
|
-
this.waiting.shift()({ value: undefined, done: true });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async *[Symbol.asyncIterator]() {
|
|
45
|
-
while (true) {
|
|
46
|
-
if (this.queue.length > 0) {
|
|
47
|
-
yield this.queue.shift();
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
if (this.done) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
const next = await new Promise((resolve) => {
|
|
54
|
-
this.waiting.push(resolve);
|
|
55
|
-
});
|
|
56
|
-
if (next.done) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
yield next.value;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
result() {
|
|
63
|
-
return this.finalResultPromise;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
class DefaultAssistantMessageEventStream extends EventStream {
|
|
67
|
-
constructor() {
|
|
68
|
-
super((event) => event.type === 'done' || event.type === 'error', (event) => {
|
|
69
|
-
if (event.type === 'done') {
|
|
70
|
-
return event.message;
|
|
71
|
-
}
|
|
72
|
-
if (event.type === 'error') {
|
|
73
|
-
return event.error;
|
|
74
|
-
}
|
|
75
|
-
throw new Error('Unexpected terminal event');
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
6
|
exports.OLLAMA_MODELS = [];
|
|
80
7
|
class OllamaClient {
|
|
81
8
|
baseUrl;
|
|
@@ -121,16 +48,19 @@ class OllamaClient {
|
|
|
121
48
|
}
|
|
122
49
|
}
|
|
123
50
|
async generateEmbedding(text, model = 'nomic-embed-text') {
|
|
124
|
-
const response = await fetch(`${this.baseUrl}/api/
|
|
51
|
+
const response = await fetch(`${this.baseUrl}/api/embed`, {
|
|
125
52
|
method: 'POST',
|
|
126
53
|
headers: { 'Content-Type': 'application/json' },
|
|
127
|
-
body: JSON.stringify({ model,
|
|
54
|
+
body: JSON.stringify({ model, input: text }),
|
|
128
55
|
});
|
|
129
56
|
if (!response.ok) {
|
|
130
57
|
throw new Error(`generateEmbedding failed: ${response.status} ${response.statusText}`);
|
|
131
58
|
}
|
|
132
59
|
const payload = (await response.json());
|
|
133
|
-
return
|
|
60
|
+
return {
|
|
61
|
+
embedding: payload.embeddings[0],
|
|
62
|
+
promptTokens: payload.prompt_eval_count ?? 0,
|
|
63
|
+
};
|
|
134
64
|
}
|
|
135
65
|
async generate(input, onChunk) {
|
|
136
66
|
const context = legacyInputToContext(input);
|
|
@@ -193,9 +123,12 @@ class OllamaAdapter {
|
|
|
193
123
|
async listModels() {
|
|
194
124
|
return this.client.listModels();
|
|
195
125
|
}
|
|
126
|
+
async embed(text, model = 'nomic-embed-text') {
|
|
127
|
+
return this.client.generateEmbedding(text, model);
|
|
128
|
+
}
|
|
196
129
|
stream(model, context, options) {
|
|
197
|
-
const stream = new DefaultAssistantMessageEventStream();
|
|
198
|
-
const output = createAssistantMessage(model);
|
|
130
|
+
const stream = new protocol_1.DefaultAssistantMessageEventStream();
|
|
131
|
+
const output = (0, protocol_1.createAssistantMessage)(model);
|
|
199
132
|
void (async () => {
|
|
200
133
|
const body = {
|
|
201
134
|
model: model.id,
|
|
@@ -219,7 +152,7 @@ class OllamaAdapter {
|
|
|
219
152
|
const text = await response.text().catch(() => '');
|
|
220
153
|
throw new Error(`Ollama error ${response.status}: ${text}`);
|
|
221
154
|
}
|
|
222
|
-
stream.push({ type: 'start', partial: clone(output) });
|
|
155
|
+
stream.push({ type: 'start', partial: (0, protocol_1.clone)(output) });
|
|
223
156
|
if (!response.body) {
|
|
224
157
|
throw new Error('No response body');
|
|
225
158
|
}
|
|
@@ -236,7 +169,7 @@ class OllamaAdapter {
|
|
|
236
169
|
stream.push({
|
|
237
170
|
type: 'thinking_start',
|
|
238
171
|
contentIndex: thinkingIndex,
|
|
239
|
-
partial: clone(output),
|
|
172
|
+
partial: (0, protocol_1.clone)(output),
|
|
240
173
|
});
|
|
241
174
|
}
|
|
242
175
|
const block = output.content[thinkingIndex];
|
|
@@ -245,14 +178,14 @@ class OllamaAdapter {
|
|
|
245
178
|
type: 'thinking_delta',
|
|
246
179
|
contentIndex: thinkingIndex,
|
|
247
180
|
delta: thinking,
|
|
248
|
-
partial: clone(output),
|
|
181
|
+
partial: (0, protocol_1.clone)(output),
|
|
249
182
|
});
|
|
250
183
|
}
|
|
251
184
|
const text = payload.message?.content ?? payload.response ?? '';
|
|
252
185
|
if (text) {
|
|
253
186
|
if (textIndex === undefined) {
|
|
254
187
|
textIndex = output.content.push({ type: 'text', text: '' }) - 1;
|
|
255
|
-
stream.push({ type: 'text_start', contentIndex: textIndex, partial: clone(output) });
|
|
188
|
+
stream.push({ type: 'text_start', contentIndex: textIndex, partial: (0, protocol_1.clone)(output) });
|
|
256
189
|
}
|
|
257
190
|
const block = output.content[textIndex];
|
|
258
191
|
block.text += text;
|
|
@@ -260,7 +193,7 @@ class OllamaAdapter {
|
|
|
260
193
|
type: 'text_delta',
|
|
261
194
|
contentIndex: textIndex,
|
|
262
195
|
delta: text,
|
|
263
|
-
partial: clone(output),
|
|
196
|
+
partial: (0, protocol_1.clone)(output),
|
|
264
197
|
});
|
|
265
198
|
}
|
|
266
199
|
if (!payload.done) {
|
|
@@ -269,14 +202,14 @@ class OllamaAdapter {
|
|
|
269
202
|
output.usage.input = payload.prompt_eval_count ?? output.usage.input;
|
|
270
203
|
output.usage.output = payload.eval_count ?? output.usage.output;
|
|
271
204
|
output.usage.totalTokens = output.usage.input + output.usage.output;
|
|
272
|
-
calculateUsageCost(model, output.usage);
|
|
205
|
+
(0, protocol_1.calculateUsageCost)(model, output.usage);
|
|
273
206
|
output.stopReason = payload.done_reason === 'length' ? 'length' : 'stop';
|
|
274
207
|
if (thinkingIndex !== undefined) {
|
|
275
208
|
stream.push({
|
|
276
209
|
type: 'thinking_end',
|
|
277
210
|
contentIndex: thinkingIndex,
|
|
278
211
|
content: output.content[thinkingIndex].thinking,
|
|
279
|
-
partial: clone(output),
|
|
212
|
+
partial: (0, protocol_1.clone)(output),
|
|
280
213
|
});
|
|
281
214
|
}
|
|
282
215
|
if (textIndex !== undefined) {
|
|
@@ -284,13 +217,13 @@ class OllamaAdapter {
|
|
|
284
217
|
type: 'text_end',
|
|
285
218
|
contentIndex: textIndex,
|
|
286
219
|
content: output.content[textIndex].text,
|
|
287
|
-
partial: clone(output),
|
|
220
|
+
partial: (0, protocol_1.clone)(output),
|
|
288
221
|
});
|
|
289
222
|
}
|
|
290
223
|
stream.push({
|
|
291
224
|
type: 'done',
|
|
292
225
|
reason: output.stopReason === 'length' ? 'length' : 'stop',
|
|
293
|
-
message: clone(output),
|
|
226
|
+
message: (0, protocol_1.clone)(output),
|
|
294
227
|
});
|
|
295
228
|
stream.end(output);
|
|
296
229
|
finished = true;
|
|
@@ -323,7 +256,7 @@ class OllamaAdapter {
|
|
|
323
256
|
type: 'thinking_end',
|
|
324
257
|
contentIndex: thinkingIndex,
|
|
325
258
|
content: output.content[thinkingIndex].thinking,
|
|
326
|
-
partial: clone(output),
|
|
259
|
+
partial: (0, protocol_1.clone)(output),
|
|
327
260
|
});
|
|
328
261
|
}
|
|
329
262
|
if (textIndex !== undefined) {
|
|
@@ -331,13 +264,13 @@ class OllamaAdapter {
|
|
|
331
264
|
type: 'text_end',
|
|
332
265
|
contentIndex: textIndex,
|
|
333
266
|
content: output.content[textIndex].text,
|
|
334
|
-
partial: clone(output),
|
|
267
|
+
partial: (0, protocol_1.clone)(output),
|
|
335
268
|
});
|
|
336
269
|
}
|
|
337
270
|
stream.push({
|
|
338
271
|
type: 'done',
|
|
339
272
|
reason: output.stopReason === 'length' ? 'length' : 'stop',
|
|
340
|
-
message: clone(output),
|
|
273
|
+
message: (0, protocol_1.clone)(output),
|
|
341
274
|
});
|
|
342
275
|
stream.end(output);
|
|
343
276
|
}
|
|
@@ -347,7 +280,7 @@ class OllamaAdapter {
|
|
|
347
280
|
stream.push({
|
|
348
281
|
type: 'error',
|
|
349
282
|
reason: output.stopReason === 'aborted' ? 'aborted' : 'error',
|
|
350
|
-
error: clone(output),
|
|
283
|
+
error: (0, protocol_1.clone)(output),
|
|
351
284
|
});
|
|
352
285
|
stream.end(output);
|
|
353
286
|
}
|
|
@@ -404,34 +337,6 @@ function toOllamaMessages(context) {
|
|
|
404
337
|
}
|
|
405
338
|
return messages;
|
|
406
339
|
}
|
|
407
|
-
function createAssistantMessage(model) {
|
|
408
|
-
return {
|
|
409
|
-
role: 'assistant',
|
|
410
|
-
api: model.api,
|
|
411
|
-
provider: model.provider,
|
|
412
|
-
model: model.id,
|
|
413
|
-
content: [],
|
|
414
|
-
usage: {
|
|
415
|
-
input: 0,
|
|
416
|
-
output: 0,
|
|
417
|
-
reasoning: 0,
|
|
418
|
-
cacheRead: 0,
|
|
419
|
-
cacheWrite: 0,
|
|
420
|
-
totalTokens: 0,
|
|
421
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
422
|
-
},
|
|
423
|
-
stopReason: 'stop',
|
|
424
|
-
timestamp: Date.now(),
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
function calculateUsageCost(model, usage) {
|
|
428
|
-
usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
|
|
429
|
-
usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
|
|
430
|
-
usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
|
|
431
|
-
usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
|
|
432
|
-
usage.cost.total =
|
|
433
|
-
usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
434
|
-
}
|
|
435
340
|
function legacyInputToContext(input) {
|
|
436
341
|
const messages = input.messages
|
|
437
342
|
? input.messages
|
|
@@ -466,9 +371,6 @@ function legacyInputToContext(input) {
|
|
|
466
371
|
messages,
|
|
467
372
|
};
|
|
468
373
|
}
|
|
469
|
-
function clone(value) {
|
|
470
|
-
return JSON.parse(JSON.stringify(value));
|
|
471
|
-
}
|
|
472
374
|
async function* iterateResponseBody(body) {
|
|
473
375
|
if ('getReader' in body && typeof body.getReader === 'function') {
|
|
474
376
|
const reader = body.getReader();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentic-kit/ollama",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Ollama",
|
|
6
6
|
"main": "index.js",
|
|
@@ -39,6 +39,9 @@
|
|
|
39
39
|
"test:live:extended": "node ./scripts/run-live-tests.js extended",
|
|
40
40
|
"test:watch": "jest --watch"
|
|
41
41
|
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@agentic-kit/protocol": "1.1.0"
|
|
44
|
+
},
|
|
42
45
|
"keywords": [],
|
|
43
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "fbf34d1146e8543664d904f75e5b3440f2b1d2a4"
|
|
44
47
|
}
|