huxy-llm-api 1.1.2 → 1.1.4
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/package.json +3 -3
- package/src/index.js +163 -149
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "huxy-llm-api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.js",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"ollama": "^0.6.3",
|
|
38
|
-
"openai": "^6.
|
|
39
|
-
"undici": "^
|
|
38
|
+
"openai": "^6.42.0",
|
|
39
|
+
"undici": "^8.4.1"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=20.0.0"
|
package/src/index.js
CHANGED
|
@@ -1,218 +1,232 @@
|
|
|
1
1
|
var B = Object.defineProperty;
|
|
2
|
-
var U = (t,
|
|
3
|
-
for (var e in
|
|
2
|
+
var U = (t, r) => {
|
|
3
|
+
for (var e in r) B(t, e, {get: r[e], enumerable: !0});
|
|
4
4
|
};
|
|
5
|
-
import {Ollama as
|
|
6
|
-
import
|
|
7
|
-
import {fetch as
|
|
8
|
-
var T =
|
|
9
|
-
|
|
10
|
-
y =
|
|
11
|
-
var
|
|
12
|
-
w =
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
let {max_tokens:
|
|
5
|
+
import {Ollama as at} from 'ollama';
|
|
6
|
+
import it from 'openai';
|
|
7
|
+
import {fetch as z, Agent as M} from 'undici';
|
|
8
|
+
var T = 3600 * 1e3,
|
|
9
|
+
N = t => (r, e) => z(r, {...e, dispatcher: new M({headersTimeout: T, ...t})}),
|
|
10
|
+
y = N;
|
|
11
|
+
var S = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
|
|
12
|
+
w = S;
|
|
13
|
+
var Y = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
|
|
14
|
+
v = Y;
|
|
15
|
+
var H = ['temperature', 'seed', 'stop', 'top_p'],
|
|
16
|
+
V = t => {
|
|
17
|
+
let {max_tokens: r, options: e = {}, ...o} = t,
|
|
18
18
|
{num_ctx: a, ...n} = e;
|
|
19
19
|
return (
|
|
20
|
-
(
|
|
21
|
-
Object.keys(n).map(
|
|
22
|
-
|
|
20
|
+
(o.max_tokens = r ?? a),
|
|
21
|
+
Object.keys(n).map(s => {
|
|
22
|
+
H.includes(s) ? (o[s] = n[s]) : (o.extra_body || (o.extra_body = {}), (o.extra_body[s] = n[s]));
|
|
23
23
|
}),
|
|
24
|
-
|
|
24
|
+
o
|
|
25
25
|
);
|
|
26
26
|
},
|
|
27
|
-
|
|
27
|
+
W = (t, r = {}, e = 'chat') => {
|
|
28
28
|
if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
|
|
29
|
-
if (!
|
|
29
|
+
if (!r.model) throw Error('\u8BF7\u914D\u7F6E\u8981\u4F7F\u7528\u7684\u5927\u6A21\u578B model !');
|
|
30
30
|
if (e === 'chat') {
|
|
31
31
|
let a = Array.isArray(t) ? t : [{role: 'user', content: t}],
|
|
32
|
-
{system: n, ...
|
|
33
|
-
return (n && (a = [{role: 'system', content: n}, ...a]), {messages: a, ...
|
|
32
|
+
{system: n, ...s} = r;
|
|
33
|
+
return (n && (a = [{role: 'system', content: n}, ...a]), {messages: a, ...s});
|
|
34
34
|
}
|
|
35
35
|
if (e === 'responses') {
|
|
36
|
-
let {instructions: a, system: n, ...
|
|
37
|
-
return (a || (
|
|
36
|
+
let {instructions: a, system: n, ...s} = r;
|
|
37
|
+
return (a || (s.instructions = n), {input: t, ...s});
|
|
38
38
|
}
|
|
39
|
-
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...
|
|
39
|
+
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...r};
|
|
40
40
|
};
|
|
41
41
|
var R =
|
|
42
|
-
({params: t, options:
|
|
43
|
-
(
|
|
44
|
-
let {options:
|
|
45
|
-
p =
|
|
46
|
-
return ((p.options = {...
|
|
42
|
+
({params: t, options: r} = {}, e) =>
|
|
43
|
+
(o, a = {}, n) => {
|
|
44
|
+
let {options: s, extra_body: i, ...c} = a,
|
|
45
|
+
p = W(o, {...t, ...c}, n);
|
|
46
|
+
return ((p.options = {...r, ...i, ...s}), e === 'openai' ? V(p) : p);
|
|
47
47
|
};
|
|
48
|
-
var
|
|
49
|
-
A = async (t,
|
|
50
|
-
if (
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
for await (let
|
|
54
|
-
let {type:
|
|
55
|
-
(
|
|
48
|
+
var q = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
49
|
+
A = async (t, r, e) => {
|
|
50
|
+
if (r) {
|
|
51
|
+
let s = '',
|
|
52
|
+
i = '';
|
|
53
|
+
for await (let c of t) {
|
|
54
|
+
let {type: p, delta: m} = c;
|
|
55
|
+
(q.includes(p) && (i += m), p === 'response.output_text.delta' && (s += m), e?.({content: s, reasoning: i}, c));
|
|
56
56
|
}
|
|
57
|
-
return {content:
|
|
57
|
+
return {content: s, reasoning: i};
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
let a = (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text,
|
|
60
|
+
n = t.output_text;
|
|
61
|
+
return (e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
60
62
|
},
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
let
|
|
63
|
+
$ = async (t, r, e) => {
|
|
64
|
+
if (r) {
|
|
65
|
+
let s = '',
|
|
64
66
|
i = '';
|
|
65
67
|
for await (let c of t) {
|
|
66
68
|
let {delta: p} = c.choices?.[0] ?? {},
|
|
67
|
-
{reasoning: m, content:
|
|
68
|
-
(m && (i += m),
|
|
69
|
+
{reasoning: m, content: u} = p ?? {};
|
|
70
|
+
(m && (i += m), u && (s += u), e?.({content: s, reasoning: i}, c));
|
|
69
71
|
}
|
|
70
|
-
return {content:
|
|
72
|
+
return {content: s, reasoning: i};
|
|
71
73
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return {content: a, reasoning: n};
|
|
74
|
+
let {message: o} = t.choices?.[0] ?? {},
|
|
75
|
+
{content: a, reasoning: n} = o;
|
|
76
|
+
return (e?.({content: a, reasoning: n}, t), {content: a, reasoning: n});
|
|
76
77
|
};
|
|
77
|
-
var
|
|
78
|
-
U(
|
|
79
|
-
var
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
var _ = {};
|
|
79
|
+
U(_, {chat: () => g, default: () => X, generate: () => J, image: () => Q, responses: () => x});
|
|
80
|
+
var G = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
81
|
+
f = t => {
|
|
82
|
+
let {total_duration: r, load_duration: e, prompt_eval_count: o, prompt_eval_duration: a, eval_count: n, eval_duration: s} = t;
|
|
83
|
+
t.analyze = `total duration: ${(r / 1e9).toFixed(2)} s
|
|
84
|
+
load duration: ${(e / 1e6).toFixed(2)} ms
|
|
85
|
+
prompt eval count: ${o} token(s)
|
|
86
|
+
prompt eval duration: ${(a / 1e6).toFixed(2)} ms
|
|
87
|
+
prompt eval rate: ${(o / (a / 1e9)).toFixed(2)} tokens/s
|
|
88
|
+
eval count: ${n} token(s)
|
|
89
|
+
eval duration: ${(s / 1e9).toFixed(2)} s
|
|
90
|
+
eval rate: ${(n / (s / 1e9)).toFixed(2)} tokens/s`;
|
|
91
|
+
},
|
|
92
|
+
x = async (t, r, e) => {
|
|
93
|
+
if (r) {
|
|
94
|
+
let s = '',
|
|
95
|
+
i = '';
|
|
96
|
+
for await (let c of t) {
|
|
97
|
+
let {type: p, delta: m} = c;
|
|
98
|
+
(G.includes(p) && (i += m), p === 'response.output_text.delta' && (s += m), c.done && f(c), e?.({content: s, reasoning: i}, c));
|
|
87
99
|
}
|
|
88
|
-
return {content:
|
|
100
|
+
return {content: s, reasoning: i};
|
|
89
101
|
}
|
|
90
|
-
|
|
102
|
+
let a = (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text,
|
|
103
|
+
n = t.output_text;
|
|
104
|
+
return (f(t), e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
91
105
|
},
|
|
92
|
-
|
|
93
|
-
if (
|
|
106
|
+
J = async (t, r, e) => {
|
|
107
|
+
if (r) {
|
|
94
108
|
let n = '',
|
|
95
|
-
|
|
109
|
+
s = '';
|
|
96
110
|
for await (let i of t) {
|
|
97
111
|
let c = i.reasoning ?? i.thinking,
|
|
98
112
|
p = i.content ?? i.response;
|
|
99
|
-
(c && (
|
|
113
|
+
(c && (s += c), p && (n += p), i.done && f(i), e?.({content: n, reasoning: s}, i));
|
|
100
114
|
}
|
|
101
|
-
return {content: n, reasoning:
|
|
115
|
+
return {content: n, reasoning: s};
|
|
102
116
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return {content:
|
|
117
|
+
let o = t.reasoning ?? t.thinking,
|
|
118
|
+
a = t.content ?? t.response;
|
|
119
|
+
return (f(t), e?.({content: a, reasoning: o}, t), {content: a, reasoning: o});
|
|
106
120
|
},
|
|
107
|
-
|
|
108
|
-
for await (let
|
|
121
|
+
Q = async (t, r, e) => {
|
|
122
|
+
for await (let o of t) e?.(o);
|
|
109
123
|
},
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
let
|
|
124
|
+
g = async (t, r, e) => {
|
|
125
|
+
if (r) {
|
|
126
|
+
let s = '',
|
|
113
127
|
i = '';
|
|
114
128
|
for await (let c of t) {
|
|
115
129
|
let {message: p} = c,
|
|
116
130
|
m = p.reasoning ?? p.thinking,
|
|
117
|
-
|
|
118
|
-
(m && (i += m),
|
|
131
|
+
u = p.content ?? p.response;
|
|
132
|
+
(m && (i += m), u && (s += u), c.done && f(c), e?.({content: s, reasoning: i}, c));
|
|
119
133
|
}
|
|
120
|
-
return {content:
|
|
134
|
+
return {content: s, reasoning: i};
|
|
121
135
|
}
|
|
122
|
-
let {message:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return {content:
|
|
136
|
+
let {message: o} = t,
|
|
137
|
+
a = o.reasoning ?? o.thinking,
|
|
138
|
+
n = o.content ?? o.response;
|
|
139
|
+
return (f(t), e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
126
140
|
},
|
|
127
|
-
|
|
128
|
-
import
|
|
129
|
-
var
|
|
141
|
+
X = g;
|
|
142
|
+
import I from 'node:fs/promises';
|
|
143
|
+
var l = {'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif', '.webp': 'image/webp', '.bmp': 'image/bmp', '.svg': 'image/svg+xml', '.tiff': 'image/tiff'},
|
|
130
144
|
h = '.png',
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
var
|
|
134
|
-
let
|
|
135
|
-
e = t.match(
|
|
136
|
-
return e ? {ext:
|
|
145
|
+
Z = t => Object.fromEntries(Object.entries(t).map(([r, e]) => [e, r])),
|
|
146
|
+
b = Z(l);
|
|
147
|
+
var tt = t => {
|
|
148
|
+
let r = /^data:(image\/[a-z]+);base64,(.+)$/i,
|
|
149
|
+
e = t.match(r);
|
|
150
|
+
return e ? {ext: b[e[1]] ?? h, data: e[2]} : {ext: h, data: t};
|
|
137
151
|
},
|
|
138
|
-
|
|
152
|
+
et = async (t, r = './images', e) => {
|
|
139
153
|
try {
|
|
140
|
-
await
|
|
141
|
-
let {ext:
|
|
142
|
-
n = `${
|
|
143
|
-
return (await
|
|
144
|
-
} catch (
|
|
145
|
-
throw (console.error('\u4FDD\u5B58\u56FE\u7247\u5931\u8D25:',
|
|
154
|
+
await I.mkdir(r, {recursive: !0});
|
|
155
|
+
let {ext: o, data: a} = tt(t),
|
|
156
|
+
n = `${r}/image_${e || Date.now()}${o}`;
|
|
157
|
+
return (await I.writeFile(n, Buffer.from(a, 'base64')), n);
|
|
158
|
+
} catch (o) {
|
|
159
|
+
throw (console.error('\u4FDD\u5B58\u56FE\u7247\u5931\u8D25:', o.message), o);
|
|
146
160
|
}
|
|
147
161
|
},
|
|
148
|
-
|
|
149
|
-
import {readFile as
|
|
150
|
-
import {extname as
|
|
151
|
-
var
|
|
162
|
+
C = et;
|
|
163
|
+
import {readFile as nt} from 'fs/promises';
|
|
164
|
+
import {extname as ot} from 'path';
|
|
165
|
+
var st = async (t, r = !1) => {
|
|
152
166
|
try {
|
|
153
|
-
let e =
|
|
154
|
-
if (!
|
|
155
|
-
let
|
|
156
|
-
return
|
|
167
|
+
let e = ot(t).toLowerCase();
|
|
168
|
+
if (!l[e]) throw new Error(`\u4E0D\u652F\u6301\u7684\u56FE\u7247\u683C\u5F0F: ${e}`);
|
|
169
|
+
let o = await nt(t, 'base64');
|
|
170
|
+
return r ? `data:${l[e]};base64,${o}` : o;
|
|
157
171
|
} catch (e) {
|
|
158
172
|
throw e.code === 'ENOENT' ? new Error(`\u6587\u4EF6\u4E0D\u5B58\u5728: ${t}`) : e;
|
|
159
173
|
}
|
|
160
174
|
},
|
|
161
|
-
|
|
162
|
-
var
|
|
163
|
-
|
|
164
|
-
openai: (t,
|
|
165
|
-
chat: async (e,
|
|
166
|
-
let n =
|
|
167
|
-
|
|
175
|
+
L = st;
|
|
176
|
+
var rt = t => (t?.startsWith('x/') ? 'image' : 'generate'),
|
|
177
|
+
O = {
|
|
178
|
+
openai: (t, r) => ({
|
|
179
|
+
chat: async (e, o = {}, a) => {
|
|
180
|
+
let n = r(e, o, 'chat'),
|
|
181
|
+
s = $,
|
|
168
182
|
i = await t.chat.completions.create(n);
|
|
169
|
-
return
|
|
183
|
+
return s(i, n.stream, a);
|
|
170
184
|
},
|
|
171
|
-
responses: async (e,
|
|
172
|
-
let n =
|
|
173
|
-
|
|
185
|
+
responses: async (e, o = {}, a) => {
|
|
186
|
+
let n = r(e, o, 'responses'),
|
|
187
|
+
s = A,
|
|
174
188
|
i = await t.responses.create(n);
|
|
175
|
-
return
|
|
189
|
+
return s(i, n.stream, a);
|
|
176
190
|
},
|
|
177
191
|
}),
|
|
178
|
-
ollama: (t,
|
|
179
|
-
chat: async (e,
|
|
180
|
-
let n =
|
|
181
|
-
|
|
192
|
+
ollama: (t, r) => ({
|
|
193
|
+
chat: async (e, o = {}, a) => {
|
|
194
|
+
let n = r(e, o, 'chat'),
|
|
195
|
+
s = g,
|
|
182
196
|
i = await t.chat(n);
|
|
183
|
-
return
|
|
197
|
+
return s(i, n.stream, a);
|
|
184
198
|
},
|
|
185
|
-
generate: async (e,
|
|
186
|
-
let n =
|
|
187
|
-
|
|
199
|
+
generate: async (e, o = {}, a) => {
|
|
200
|
+
let n = r(e, o, 'generate'),
|
|
201
|
+
s = _[rt(n.model)],
|
|
188
202
|
i = await t.generate(n);
|
|
189
|
-
return
|
|
203
|
+
return s(i, n.stream, a);
|
|
190
204
|
},
|
|
191
|
-
responses: async (e,
|
|
192
|
-
let n =
|
|
193
|
-
|
|
205
|
+
responses: async (e, o = {}, a) => {
|
|
206
|
+
let n = r(e, o, 'responses'),
|
|
207
|
+
s = x,
|
|
194
208
|
i = await t.responses(n);
|
|
195
|
-
return
|
|
209
|
+
return s(i, n.stream, a);
|
|
196
210
|
},
|
|
197
|
-
saveImage:
|
|
198
|
-
imageToBase64:
|
|
211
|
+
saveImage: C,
|
|
212
|
+
imageToBase64: L,
|
|
199
213
|
}),
|
|
200
214
|
};
|
|
201
|
-
var
|
|
202
|
-
ollama: {hostKey: 'host', envConfig: w, API: ({apiKey: t, headers:
|
|
203
|
-
openai: {hostKey: 'baseURL', envConfig:
|
|
215
|
+
var ct = {
|
|
216
|
+
ollama: {hostKey: 'host', envConfig: w, API: ({apiKey: t, headers: r, ...e}) => new at({headers: {Authorization: t ? `Bearer ${t}` : void 0, ...r}, ...e})},
|
|
217
|
+
openai: {hostKey: 'baseURL', envConfig: v, API: t => new it(t)},
|
|
204
218
|
},
|
|
205
|
-
|
|
219
|
+
pt = (t = 'ollama', r = {}, e = {}) => {
|
|
206
220
|
t = ['ollama', 'openai'].includes(t) ? t : 'ollama';
|
|
207
|
-
let {hostKey:
|
|
208
|
-
{config:
|
|
209
|
-
{baseURL: p, host: m, dispatcher:
|
|
210
|
-
if (((
|
|
211
|
-
let
|
|
212
|
-
{options:
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return
|
|
221
|
+
let {hostKey: o, envConfig: a, API: n} = ct[t],
|
|
222
|
+
{config: s, params: i, options: c} = a,
|
|
223
|
+
{baseURL: p, host: m, dispatcher: u, ...d} = {...s, ...r};
|
|
224
|
+
if (((d[o] = m || p), !d[o])) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
|
|
225
|
+
let E = n({fetch: y(u), ...d}),
|
|
226
|
+
{options: P, extra_body: k, ...F} = e,
|
|
227
|
+
K = {params: {...i, ...F}, options: {...c, ...k, ...P}},
|
|
228
|
+
j = R(K, t);
|
|
229
|
+
return O[t](E, j);
|
|
216
230
|
},
|
|
217
|
-
|
|
218
|
-
export {
|
|
231
|
+
Kt = pt;
|
|
232
|
+
export {Kt as default, pt as startApi};
|