@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 CHANGED
@@ -1,12 +1,82 @@
1
- # Ollama
1
+ # Ollama Client
2
2
 
3
- ## Credits
3
+ A JavaScript/TypeScript client for the Ollama LLM server, supporting model listing, text generation, streaming responses, embeddings, and model management.
4
4
 
5
- 🛠 Built by Hyperweb (formerly Cosmology) — if you like our tools, please checkout and contribute to [our github ⚛️](https://github.com/hyperweb-io)
5
+ ## Installation
6
6
 
7
+ ```bash
8
+ npm install @agentic-kit/ollama
9
+ # or
10
+ yarn add @agentic-kit/ollama
11
+ ```
7
12
 
8
- ## Disclaimer
13
+ ## Usage
9
14
 
10
- AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
15
+ ```typescript
16
+ import OllamaClient, { GenerateInput } from '@agentic-kit/ollama';
11
17
 
12
- No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
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(input),
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
- while (true) {
133
- const { done, value } = await reader.read();
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 chunk = decoder.decode(value);
137
- const lines = chunk.split('\n').filter(Boolean);
138
- for (const line of lines) {
139
- try {
140
- const data = JSON.parse(line);
141
- if (data.response) {
142
- onChunk(data.response);
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(input),
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
- while (true) {
138
- const { done, value } = await reader.read();
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 chunk = decoder.decode(value);
142
- const lines = chunk.split('\n').filter(Boolean);
143
- for (const line of lines) {
144
- try {
145
- const data = JSON.parse(line);
146
- if (data.response) {
147
- onChunk(data.response);
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.2",
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": "93ed791457a8bc671058efd517f7978e41563b8a"
36
+ "gitHead": "0204aa87ae43833af29965684146ff22ebf77564"
37
37
  }