@agentic-kit/ollama 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Interweb, Inc. <developers@hyperweb.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Ollama
2
+
3
+ ## Credits
4
+
5
+ 🛠 Built by Hyperweb (formerly Cosmology) — if you like our tools, please checkout and contribute to [our github ⚛️](https://github.com/hyperweb-io)
6
+
7
+
8
+ ## Disclaimer
9
+
10
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
11
+
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.
package/esm/index.js ADDED
@@ -0,0 +1,151 @@
1
+ import fetch from 'cross-fetch';
2
+ export default class OllamaClient {
3
+ baseUrl;
4
+ constructor(baseUrl = 'http://localhost:11434') {
5
+ this.baseUrl = baseUrl;
6
+ }
7
+ async listModels() {
8
+ const response = await fetch(`${this.baseUrl}/api/tags`);
9
+ if (!response.ok) {
10
+ throw new Error(`Failed to list models: ${response.status} ${response.statusText}`);
11
+ }
12
+ const data = await response.json();
13
+ return data.tags;
14
+ }
15
+ async generate(input) {
16
+ const response = await fetch(`${this.baseUrl}/api/generate`, {
17
+ method: 'POST',
18
+ headers: { 'Content-Type': 'application/json' },
19
+ body: JSON.stringify(input),
20
+ });
21
+ if (!response.ok) {
22
+ let errorText;
23
+ try {
24
+ const errorData = await response.json();
25
+ errorText = errorData.message ?? JSON.stringify(errorData);
26
+ }
27
+ catch {
28
+ errorText = await response.text();
29
+ }
30
+ throw new Error(`Generate request failed: ${response.status} ${response.statusText} - ${errorText}`);
31
+ }
32
+ const data = await response.json();
33
+ return data.text;
34
+ }
35
+ async pullModel(model) {
36
+ const response = await fetch(`${this.baseUrl}/api/pull`, {
37
+ method: 'POST',
38
+ headers: { 'Content-Type': 'application/json' },
39
+ body: JSON.stringify({ name: model }),
40
+ });
41
+ if (!response.ok) {
42
+ let errorText;
43
+ try {
44
+ const errorData = await response.json();
45
+ errorText = errorData.message ?? JSON.stringify(errorData);
46
+ }
47
+ catch {
48
+ errorText = await response.text();
49
+ }
50
+ throw new Error(`Pull model failed: ${response.status} ${response.statusText} - ${errorText}`);
51
+ }
52
+ }
53
+ async deleteModel(model) {
54
+ const response = await fetch(`${this.baseUrl}/api/delete`, {
55
+ method: 'DELETE',
56
+ headers: { 'Content-Type': 'application/json' },
57
+ body: JSON.stringify({ name: model }),
58
+ });
59
+ if (!response.ok) {
60
+ let errorText;
61
+ try {
62
+ const errorData = await response.json();
63
+ errorText = errorData.message ?? JSON.stringify(errorData);
64
+ }
65
+ catch {
66
+ errorText = await response.text();
67
+ }
68
+ throw new Error(`Delete model failed: ${response.status} ${response.statusText} - ${errorText}`);
69
+ }
70
+ }
71
+ async generateEmbedding(text) {
72
+ const response = await fetch(`${this.baseUrl}/api/embeddings`, {
73
+ method: 'POST',
74
+ headers: {
75
+ 'Content-Type': 'application/json',
76
+ },
77
+ body: JSON.stringify({
78
+ model: 'mistral',
79
+ prompt: text,
80
+ }),
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error(`Failed to generate embedding: ${response.statusText}`);
84
+ }
85
+ const data = await response.json();
86
+ return data.embedding;
87
+ }
88
+ async generateResponse(prompt, context) {
89
+ const fullPrompt = context
90
+ ? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
91
+ : prompt;
92
+ const response = await fetch(`${this.baseUrl}/api/generate`, {
93
+ method: 'POST',
94
+ headers: {
95
+ 'Content-Type': 'application/json',
96
+ },
97
+ body: JSON.stringify({
98
+ model: 'mistral',
99
+ prompt: fullPrompt,
100
+ stream: false,
101
+ }),
102
+ });
103
+ if (!response.ok) {
104
+ throw new Error(`Failed to generate response: ${response.statusText}`);
105
+ }
106
+ const data = await response.json();
107
+ return data.response;
108
+ }
109
+ async generateStreamingResponse(prompt, onChunk, context) {
110
+ const fullPrompt = context
111
+ ? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
112
+ : prompt;
113
+ const response = await fetch(`${this.baseUrl}/api/generate`, {
114
+ method: 'POST',
115
+ headers: {
116
+ 'Content-Type': 'application/json',
117
+ },
118
+ body: JSON.stringify({
119
+ model: 'mistral',
120
+ prompt: fullPrompt,
121
+ stream: true,
122
+ }),
123
+ });
124
+ if (!response.ok) {
125
+ throw new Error(`Failed to generate streaming response: ${response.statusText}`);
126
+ }
127
+ const reader = response.body?.getReader();
128
+ if (!reader) {
129
+ throw new Error('Failed to get response reader');
130
+ }
131
+ const decoder = new TextDecoder();
132
+ while (true) {
133
+ const { done, value } = await reader.read();
134
+ if (done)
135
+ 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);
143
+ }
144
+ }
145
+ catch (error) {
146
+ console.error('Error parsing chunk:', error);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
package/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export interface GenerateInput {
2
+ model: string;
3
+ prompt: string;
4
+ }
5
+ export default class OllamaClient {
6
+ private baseUrl;
7
+ constructor(baseUrl?: string);
8
+ listModels(): Promise<string[]>;
9
+ generate(input: GenerateInput): Promise<string>;
10
+ pullModel(model: string): Promise<void>;
11
+ deleteModel(model: string): Promise<void>;
12
+ generateEmbedding(text: string): Promise<number[]>;
13
+ generateResponse(prompt: string, context?: string): Promise<string>;
14
+ generateStreamingResponse(prompt: string, onChunk: (chunk: string) => void, context?: string): Promise<void>;
15
+ }
package/index.js ADDED
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
7
+ class OllamaClient {
8
+ baseUrl;
9
+ constructor(baseUrl = 'http://localhost:11434') {
10
+ this.baseUrl = baseUrl;
11
+ }
12
+ async listModels() {
13
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/tags`);
14
+ if (!response.ok) {
15
+ throw new Error(`Failed to list models: ${response.status} ${response.statusText}`);
16
+ }
17
+ const data = await response.json();
18
+ return data.tags;
19
+ }
20
+ async generate(input) {
21
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
22
+ method: 'POST',
23
+ headers: { 'Content-Type': 'application/json' },
24
+ body: JSON.stringify(input),
25
+ });
26
+ if (!response.ok) {
27
+ let errorText;
28
+ try {
29
+ const errorData = await response.json();
30
+ errorText = errorData.message ?? JSON.stringify(errorData);
31
+ }
32
+ catch {
33
+ errorText = await response.text();
34
+ }
35
+ throw new Error(`Generate request failed: ${response.status} ${response.statusText} - ${errorText}`);
36
+ }
37
+ const data = await response.json();
38
+ return data.text;
39
+ }
40
+ async pullModel(model) {
41
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/pull`, {
42
+ method: 'POST',
43
+ headers: { 'Content-Type': 'application/json' },
44
+ body: JSON.stringify({ name: model }),
45
+ });
46
+ if (!response.ok) {
47
+ let errorText;
48
+ try {
49
+ const errorData = await response.json();
50
+ errorText = errorData.message ?? JSON.stringify(errorData);
51
+ }
52
+ catch {
53
+ errorText = await response.text();
54
+ }
55
+ throw new Error(`Pull model failed: ${response.status} ${response.statusText} - ${errorText}`);
56
+ }
57
+ }
58
+ async deleteModel(model) {
59
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/delete`, {
60
+ method: 'DELETE',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify({ name: model }),
63
+ });
64
+ if (!response.ok) {
65
+ let errorText;
66
+ try {
67
+ const errorData = await response.json();
68
+ errorText = errorData.message ?? JSON.stringify(errorData);
69
+ }
70
+ catch {
71
+ errorText = await response.text();
72
+ }
73
+ throw new Error(`Delete model failed: ${response.status} ${response.statusText} - ${errorText}`);
74
+ }
75
+ }
76
+ async generateEmbedding(text) {
77
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/embeddings`, {
78
+ method: 'POST',
79
+ headers: {
80
+ 'Content-Type': 'application/json',
81
+ },
82
+ body: JSON.stringify({
83
+ model: 'mistral',
84
+ prompt: text,
85
+ }),
86
+ });
87
+ if (!response.ok) {
88
+ throw new Error(`Failed to generate embedding: ${response.statusText}`);
89
+ }
90
+ const data = await response.json();
91
+ return data.embedding;
92
+ }
93
+ async generateResponse(prompt, context) {
94
+ const fullPrompt = context
95
+ ? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
96
+ : prompt;
97
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
98
+ method: 'POST',
99
+ headers: {
100
+ 'Content-Type': 'application/json',
101
+ },
102
+ body: JSON.stringify({
103
+ model: 'mistral',
104
+ prompt: fullPrompt,
105
+ stream: false,
106
+ }),
107
+ });
108
+ if (!response.ok) {
109
+ throw new Error(`Failed to generate response: ${response.statusText}`);
110
+ }
111
+ const data = await response.json();
112
+ return data.response;
113
+ }
114
+ async generateStreamingResponse(prompt, onChunk, context) {
115
+ const fullPrompt = context
116
+ ? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
117
+ : prompt;
118
+ const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
119
+ method: 'POST',
120
+ headers: {
121
+ 'Content-Type': 'application/json',
122
+ },
123
+ body: JSON.stringify({
124
+ model: 'mistral',
125
+ prompt: fullPrompt,
126
+ stream: true,
127
+ }),
128
+ });
129
+ if (!response.ok) {
130
+ throw new Error(`Failed to generate streaming response: ${response.statusText}`);
131
+ }
132
+ const reader = response.body?.getReader();
133
+ if (!reader) {
134
+ throw new Error('Failed to get response reader');
135
+ }
136
+ const decoder = new TextDecoder();
137
+ while (true) {
138
+ const { done, value } = await reader.read();
139
+ if (done)
140
+ 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);
148
+ }
149
+ }
150
+ catch (error) {
151
+ console.error('Error parsing chunk:', error);
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ exports.default = OllamaClient;
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@agentic-kit/ollama",
3
+ "version": "0.0.2",
4
+ "author": "Dan Lynch <pyramation@gmail.com>",
5
+ "description": "Ollama",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/hyperweb-io/agentic-kit",
10
+ "license": "SEE LICENSE IN LICENSE",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/hyperweb-io/agentic-kit"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/hyperweb-io/agentic-kit/issues"
21
+ },
22
+ "scripts": {
23
+ "copy": "copyfiles -f ../../LICENSE README.md package.json dist",
24
+ "clean": "rimraf dist/**",
25
+ "prepare": "npm run build",
26
+ "build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
27
+ "build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
28
+ "lint": "eslint . --fix",
29
+ "test": "jest",
30
+ "test:watch": "jest --watch"
31
+ },
32
+ "keywords": [],
33
+ "dependencies": {
34
+ "cross-fetch": "^4.1.0"
35
+ },
36
+ "gitHead": "93ed791457a8bc671058efd517f7978e41563b8a"
37
+ }