@agentic-kit/ollama 0.1.1 → 1.0.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/CHANGELOG.md +28 -0
- package/README.md +14 -8
- package/__tests__/first.test.ts +188 -0
- package/dist/README.md +123 -0
- package/dist/esm/index.js +139 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +145 -0
- package/dist/package.json +34 -0
- package/jest.config.js +19 -0
- package/jest.setup.js +4 -0
- package/package.json +8 -11
- package/src/index.ts +224 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +9 -0
- package/LICENSE +0 -21
- package/esm/index.js +0 -162
- package/index.d.ts +0 -17
- package/index.js +0 -168
package/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export interface GenerateInput {
|
|
2
|
-
model: string;
|
|
3
|
-
prompt: string;
|
|
4
|
-
stream?: boolean;
|
|
5
|
-
}
|
|
6
|
-
export default class OllamaClient {
|
|
7
|
-
private baseUrl;
|
|
8
|
-
constructor(baseUrl?: string);
|
|
9
|
-
listModels(): Promise<string[]>;
|
|
10
|
-
generate(input: GenerateInput): Promise<string>;
|
|
11
|
-
generate(input: GenerateInput, onChunk: (chunk: string) => void): Promise<string | void>;
|
|
12
|
-
pullModel(model: string): Promise<void>;
|
|
13
|
-
deleteModel(model: string): Promise<void>;
|
|
14
|
-
generateEmbedding(text: string): Promise<number[]>;
|
|
15
|
-
generateResponse(prompt: string, context?: string): Promise<string>;
|
|
16
|
-
generateStreamingResponse(prompt: string, onChunk: (chunk: string) => void, context?: string): Promise<void>;
|
|
17
|
-
}
|
package/index.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
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, onChunk) {
|
|
21
|
-
if (input.stream && onChunk) {
|
|
22
|
-
return this.generateStreamingResponse(input.prompt, onChunk);
|
|
23
|
-
}
|
|
24
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
|
|
25
|
-
method: 'POST',
|
|
26
|
-
headers: { 'Content-Type': 'application/json' },
|
|
27
|
-
body: JSON.stringify({
|
|
28
|
-
...input,
|
|
29
|
-
stream: input.stream || false
|
|
30
|
-
}),
|
|
31
|
-
});
|
|
32
|
-
if (!response.ok) {
|
|
33
|
-
let errorText;
|
|
34
|
-
try {
|
|
35
|
-
const errorData = await response.json();
|
|
36
|
-
errorText = errorData.message ?? JSON.stringify(errorData);
|
|
37
|
-
}
|
|
38
|
-
catch {
|
|
39
|
-
errorText = await response.text();
|
|
40
|
-
}
|
|
41
|
-
throw new Error(`Generate request failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
42
|
-
}
|
|
43
|
-
const data = await response.json();
|
|
44
|
-
return data.text;
|
|
45
|
-
}
|
|
46
|
-
async pullModel(model) {
|
|
47
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/pull`, {
|
|
48
|
-
method: 'POST',
|
|
49
|
-
headers: { 'Content-Type': 'application/json' },
|
|
50
|
-
body: JSON.stringify({ name: model }),
|
|
51
|
-
});
|
|
52
|
-
if (!response.ok) {
|
|
53
|
-
let errorText;
|
|
54
|
-
try {
|
|
55
|
-
const errorData = await response.json();
|
|
56
|
-
errorText = errorData.message ?? JSON.stringify(errorData);
|
|
57
|
-
}
|
|
58
|
-
catch {
|
|
59
|
-
errorText = await response.text();
|
|
60
|
-
}
|
|
61
|
-
throw new Error(`Pull model failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async deleteModel(model) {
|
|
65
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/delete`, {
|
|
66
|
-
method: 'DELETE',
|
|
67
|
-
headers: { 'Content-Type': 'application/json' },
|
|
68
|
-
body: JSON.stringify({ name: model }),
|
|
69
|
-
});
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
let errorText;
|
|
72
|
-
try {
|
|
73
|
-
const errorData = await response.json();
|
|
74
|
-
errorText = errorData.message ?? JSON.stringify(errorData);
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
errorText = await response.text();
|
|
78
|
-
}
|
|
79
|
-
throw new Error(`Delete model failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
async generateEmbedding(text) {
|
|
83
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/embeddings`, {
|
|
84
|
-
method: 'POST',
|
|
85
|
-
headers: {
|
|
86
|
-
'Content-Type': 'application/json',
|
|
87
|
-
},
|
|
88
|
-
body: JSON.stringify({
|
|
89
|
-
model: 'mistral',
|
|
90
|
-
prompt: text,
|
|
91
|
-
}),
|
|
92
|
-
});
|
|
93
|
-
if (!response.ok) {
|
|
94
|
-
throw new Error(`Failed to generate embedding: ${response.statusText}`);
|
|
95
|
-
}
|
|
96
|
-
const data = await response.json();
|
|
97
|
-
return data.embedding;
|
|
98
|
-
}
|
|
99
|
-
async generateResponse(prompt, context) {
|
|
100
|
-
const fullPrompt = context
|
|
101
|
-
? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
|
|
102
|
-
: prompt;
|
|
103
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
|
|
104
|
-
method: 'POST',
|
|
105
|
-
headers: {
|
|
106
|
-
'Content-Type': 'application/json',
|
|
107
|
-
},
|
|
108
|
-
body: JSON.stringify({
|
|
109
|
-
model: 'mistral',
|
|
110
|
-
prompt: fullPrompt,
|
|
111
|
-
stream: false,
|
|
112
|
-
}),
|
|
113
|
-
});
|
|
114
|
-
if (!response.ok) {
|
|
115
|
-
throw new Error(`Failed to generate response: ${response.statusText}`);
|
|
116
|
-
}
|
|
117
|
-
const data = await response.json();
|
|
118
|
-
return data.response;
|
|
119
|
-
}
|
|
120
|
-
async generateStreamingResponse(prompt, onChunk, context) {
|
|
121
|
-
const fullPrompt = context
|
|
122
|
-
? `Context: ${context}\n\nQuestion: ${prompt}\n\nAnswer:`
|
|
123
|
-
: prompt;
|
|
124
|
-
const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/generate`, {
|
|
125
|
-
method: 'POST',
|
|
126
|
-
headers: {
|
|
127
|
-
'Content-Type': 'application/json',
|
|
128
|
-
},
|
|
129
|
-
body: JSON.stringify({
|
|
130
|
-
model: 'mistral',
|
|
131
|
-
prompt: fullPrompt,
|
|
132
|
-
stream: true,
|
|
133
|
-
}),
|
|
134
|
-
});
|
|
135
|
-
if (!response.ok) {
|
|
136
|
-
throw new Error(`Failed to generate streaming response: ${response.statusText}`);
|
|
137
|
-
}
|
|
138
|
-
const reader = response.body?.getReader();
|
|
139
|
-
if (!reader) {
|
|
140
|
-
throw new Error('Failed to get response reader');
|
|
141
|
-
}
|
|
142
|
-
const decoder = new TextDecoder();
|
|
143
|
-
let done = false;
|
|
144
|
-
while (!done) {
|
|
145
|
-
const result = await reader.read();
|
|
146
|
-
done = result.done;
|
|
147
|
-
if (done)
|
|
148
|
-
break;
|
|
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);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
exports.default = OllamaClient;
|