@agentic-kit/ollama 0.0.2 → 0.1.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 +76 -6
- package/esm/index.js +25 -14
- package/index.d.ts +2 -0
- package/index.js +25 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,12 +1,82 @@
|
|
|
1
|
-
# Ollama
|
|
1
|
+
# Ollama Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A JavaScript/TypeScript client for the Ollama LLM server, supporting model listing, text generation, streaming responses, embeddings, and model management.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
+
```bash
|
|
8
|
+
npm install @agentic-kit/ollama
|
|
9
|
+
# or
|
|
10
|
+
yarn add @agentic-kit/ollama
|
|
11
|
+
```
|
|
7
12
|
|
|
8
|
-
##
|
|
13
|
+
## Usage
|
|
9
14
|
|
|
10
|
-
|
|
15
|
+
```typescript
|
|
16
|
+
import OllamaClient, { GenerateInput } from '@agentic-kit/ollama';
|
|
11
17
|
|
|
12
|
-
|
|
18
|
+
// Create a client (default port 11434)
|
|
19
|
+
const client = new OllamaClient('http://localhost:11434');
|
|
20
|
+
|
|
21
|
+
// List available models
|
|
22
|
+
const models = await client.listModels();
|
|
23
|
+
console.log('Available models:', models);
|
|
24
|
+
|
|
25
|
+
// Non-streaming text generation
|
|
26
|
+
const output = await client.generate({ model: 'mistral', prompt: 'Hello, Ollama!' });
|
|
27
|
+
console.log(output);
|
|
28
|
+
|
|
29
|
+
// Streaming generation
|
|
30
|
+
await client.generate(
|
|
31
|
+
{ model: 'mistral', prompt: 'Hello, streaming!', stream: true },
|
|
32
|
+
(chunk) => {
|
|
33
|
+
console.log('Received chunk:', chunk);
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Pull a model to local cache
|
|
38
|
+
await client.pullModel('mistral');
|
|
39
|
+
|
|
40
|
+
// Generate embeddings
|
|
41
|
+
const embedding = await client.generateEmbedding('Compute embeddings');
|
|
42
|
+
console.log('Embedding vector length:', embedding.length);
|
|
43
|
+
|
|
44
|
+
// Generate a conversational response with context
|
|
45
|
+
const response = await client.generateResponse(
|
|
46
|
+
'What is the capital of France?',
|
|
47
|
+
'Geography trivia'
|
|
48
|
+
);
|
|
49
|
+
console.log(response);
|
|
50
|
+
|
|
51
|
+
// Delete a pulled model when done
|
|
52
|
+
await client.deleteModel('mistral');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API Reference
|
|
56
|
+
|
|
57
|
+
- `new OllamaClient(baseUrl?: string)` – defaults to `http://localhost:11434`
|
|
58
|
+
- `.listModels(): Promise<string[]>`
|
|
59
|
+
- `.generate(input: GenerateInput, onChunk?: (chunk: string) => void): Promise<string | void>`
|
|
60
|
+
- `.generateStreamingResponse(prompt: string, onChunk: (chunk: string) => void, context?: string): Promise<void>`
|
|
61
|
+
- `.generateEmbedding(text: string): Promise<number[]>`
|
|
62
|
+
- `.generateResponse(prompt: string, context?: string): Promise<string>`
|
|
63
|
+
- `.pullModel(model: string): Promise<void>`
|
|
64
|
+
- `.deleteModel(model: string): Promise<void>`
|
|
65
|
+
|
|
66
|
+
## GenerateInput type
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
interface GenerateInput {
|
|
70
|
+
model: string;
|
|
71
|
+
prompt: string;
|
|
72
|
+
stream?: boolean;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Contributing
|
|
77
|
+
|
|
78
|
+
Please open issues or pull requests on [GitHub](https://github.com/hyperweb-io/agentic-kit).
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
© Hyperweb (formerly Cosmology). See LICENSE for full licensing and disclaimer.
|
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.
|
|
3
|
+
"version": "0.1.1",
|
|
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": "0204aa87ae43833af29965684146ff22ebf77564"
|
|
37
37
|
}
|