@agentic-kit/ollama 0.0.2 → 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/esm/index.js +25 -14
- package/index.d.ts +2 -0
- package/index.js +25 -14
- package/package.json +2 -2
package/esm/index.js
CHANGED
|
@@ -12,11 +12,17 @@ export default class OllamaClient {
|
|
|
12
12
|
const data = await response.json();
|
|
13
13
|
return data.tags;
|
|
14
14
|
}
|
|
15
|
-
async generate(input) {
|
|
15
|
+
async generate(input, onChunk) {
|
|
16
|
+
if (input.stream && onChunk) {
|
|
17
|
+
return this.generateStreamingResponse(input.prompt, onChunk);
|
|
18
|
+
}
|
|
16
19
|
const response = await fetch(`${this.baseUrl}/api/generate`, {
|
|
17
20
|
method: 'POST',
|
|
18
21
|
headers: { 'Content-Type': 'application/json' },
|
|
19
|
-
body: JSON.stringify(
|
|
22
|
+
body: JSON.stringify({
|
|
23
|
+
...input,
|
|
24
|
+
stream: input.stream || false
|
|
25
|
+
}),
|
|
20
26
|
});
|
|
21
27
|
if (!response.ok) {
|
|
22
28
|
let errorText;
|
|
@@ -129,21 +135,26 @@ export default class OllamaClient {
|
|
|
129
135
|
throw new Error('Failed to get response reader');
|
|
130
136
|
}
|
|
131
137
|
const decoder = new TextDecoder();
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
let done = false;
|
|
139
|
+
while (!done) {
|
|
140
|
+
const result = await reader.read();
|
|
141
|
+
done = result.done;
|
|
134
142
|
if (done)
|
|
135
143
|
break;
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
144
|
+
const value = result.value;
|
|
145
|
+
if (value) {
|
|
146
|
+
const chunk = decoder.decode(value);
|
|
147
|
+
const lines = chunk.split('\n').filter(Boolean);
|
|
148
|
+
for (const line of lines) {
|
|
149
|
+
try {
|
|
150
|
+
const data = JSON.parse(line);
|
|
151
|
+
if (data.response) {
|
|
152
|
+
onChunk(data.response);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
console.error('Error parsing chunk:', error);
|
|
143
157
|
}
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
console.error('Error parsing chunk:', error);
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export interface GenerateInput {
|
|
2
2
|
model: string;
|
|
3
3
|
prompt: string;
|
|
4
|
+
stream?: boolean;
|
|
4
5
|
}
|
|
5
6
|
export default class OllamaClient {
|
|
6
7
|
private baseUrl;
|
|
7
8
|
constructor(baseUrl?: string);
|
|
8
9
|
listModels(): Promise<string[]>;
|
|
9
10
|
generate(input: GenerateInput): Promise<string>;
|
|
11
|
+
generate(input: GenerateInput, onChunk: (chunk: string) => void): Promise<string | void>;
|
|
10
12
|
pullModel(model: string): Promise<void>;
|
|
11
13
|
deleteModel(model: string): Promise<void>;
|
|
12
14
|
generateEmbedding(text: string): Promise<number[]>;
|
package/index.js
CHANGED
|
@@ -17,11 +17,17 @@ class OllamaClient {
|
|
|
17
17
|
const data = await response.json();
|
|
18
18
|
return data.tags;
|
|
19
19
|
}
|
|
20
|
-
async generate(input) {
|
|
20
|
+
async generate(input, onChunk) {
|
|
21
|
+
if (input.stream && onChunk) {
|
|
22
|
+
return this.generateStreamingResponse(input.prompt, onChunk);
|
|
23
|
+
}
|
|
21
24
|
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
|
|
22
25
|
method: 'POST',
|
|
23
26
|
headers: { 'Content-Type': 'application/json' },
|
|
24
|
-
body: JSON.stringify(
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
...input,
|
|
29
|
+
stream: input.stream || false
|
|
30
|
+
}),
|
|
25
31
|
});
|
|
26
32
|
if (!response.ok) {
|
|
27
33
|
let errorText;
|
|
@@ -134,21 +140,26 @@ class OllamaClient {
|
|
|
134
140
|
throw new Error('Failed to get response reader');
|
|
135
141
|
}
|
|
136
142
|
const decoder = new TextDecoder();
|
|
137
|
-
|
|
138
|
-
|
|
143
|
+
let done = false;
|
|
144
|
+
while (!done) {
|
|
145
|
+
const result = await reader.read();
|
|
146
|
+
done = result.done;
|
|
139
147
|
if (done)
|
|
140
148
|
break;
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
149
|
+
const value = result.value;
|
|
150
|
+
if (value) {
|
|
151
|
+
const chunk = decoder.decode(value);
|
|
152
|
+
const lines = chunk.split('\n').filter(Boolean);
|
|
153
|
+
for (const line of lines) {
|
|
154
|
+
try {
|
|
155
|
+
const data = JSON.parse(line);
|
|
156
|
+
if (data.response) {
|
|
157
|
+
onChunk(data.response);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
console.error('Error parsing chunk:', error);
|
|
148
162
|
}
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
console.error('Error parsing chunk:', error);
|
|
152
163
|
}
|
|
153
164
|
}
|
|
154
165
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentic-kit/ollama",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Ollama",
|
|
6
6
|
"main": "index.js",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"cross-fetch": "^4.1.0"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "429d3cbe616b763f2e1e8b2a35738b3f9a600c57"
|
|
37
37
|
}
|