huxy-llm-api 1.0.7 → 1.0.8
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 +1 -1
- package/src/index.js +123 -130
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,175 +1,168 @@
|
|
|
1
1
|
import {Ollama as q} from 'ollama';
|
|
2
2
|
import z from 'openai';
|
|
3
|
-
import {fetch as
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
import {fetch as K, Agent as v} from 'undici';
|
|
4
|
+
var b = 300 * 60 * 1e3,
|
|
5
|
+
E = (t, c) => K(t, {...c, dispatcher: new v({headersTimeout: b})}),
|
|
6
|
+
m = E;
|
|
7
|
+
var k = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
|
|
8
|
+
l = k;
|
|
9
|
+
var U = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
|
|
10
|
+
g = U;
|
|
11
|
+
var M = ['temperature', 'seed', 'stop', 'top_p'],
|
|
12
|
+
F = t => {
|
|
13
|
+
let {max_tokens: c, options: s = {}, ...r} = t,
|
|
14
|
+
{num_ctx: o, ...n} = s;
|
|
15
|
+
return (
|
|
16
|
+
(r.max_tokens = c ?? o),
|
|
17
|
+
Object.keys(n).map(e => {
|
|
18
|
+
M.includes(e) ? (r[e] = n[e]) : (r.extra_body || (r.extra_body = {}), (r.extra_body[e] = n[e]));
|
|
19
|
+
}),
|
|
20
|
+
r
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
S = (t, c = {}, s = 'chat') => {
|
|
24
|
+
if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
|
|
25
|
+
if (!c.model) throw Error('\u8BF7\u914D\u7F6E\u8981\u4F7F\u7528\u7684\u5927\u6A21\u578B model !');
|
|
26
|
+
if (s === 'chat') {
|
|
27
|
+
let o = Array.isArray(t) ? t : [{role: 'user', content: t}],
|
|
28
|
+
{system: n, ...e} = c;
|
|
29
|
+
return (n && (o = [{role: 'system', content: n}, ...o]), {messages: o, ...e});
|
|
30
|
+
}
|
|
31
|
+
if (s === 'responses') {
|
|
32
|
+
let {instructions: o, system: n, ...e} = c;
|
|
33
|
+
return (o || (e.instructions = n), {input: t, ...e});
|
|
34
|
+
}
|
|
35
|
+
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...c};
|
|
36
|
+
};
|
|
37
|
+
var x =
|
|
38
|
+
({params: t, options: c} = {}, s) =>
|
|
39
|
+
(r, o = {}, n) => {
|
|
40
|
+
let {options: e, extra_body: a, ...i} = o,
|
|
41
|
+
p = S(r, {...t, ...i}, n);
|
|
42
|
+
return ((p.options = {...c, ...a, ...e}), s === 'openai' ? F(p) : p);
|
|
43
|
+
};
|
|
44
|
+
var Y = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
45
|
+
h = async (t, c, s) => {
|
|
46
|
+
if (c) {
|
|
14
47
|
let o = '',
|
|
15
48
|
n = '';
|
|
16
49
|
for await (let e of t) {
|
|
17
|
-
let {type:
|
|
18
|
-
(
|
|
50
|
+
let {type: a, delta: i} = e;
|
|
51
|
+
(Y.includes(a) && (n += i), a === 'response.output_text.delta' && (o += i), s?.({content: o, reasoning: n}, e));
|
|
19
52
|
}
|
|
20
53
|
return {content: o, reasoning: n};
|
|
21
54
|
}
|
|
22
55
|
return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
23
56
|
},
|
|
24
|
-
|
|
25
|
-
if (
|
|
57
|
+
y = async (t, c, s) => {
|
|
58
|
+
if (c) {
|
|
26
59
|
let e = '',
|
|
27
|
-
|
|
60
|
+
a = '';
|
|
28
61
|
for await (let i of t) {
|
|
29
62
|
let {delta: p} = i.choices?.[0] ?? {},
|
|
30
63
|
{reasoning: f, content: u} = p ?? {};
|
|
31
|
-
(f && (
|
|
64
|
+
(f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
|
|
32
65
|
}
|
|
33
|
-
return {content: e, reasoning:
|
|
66
|
+
return {content: e, reasoning: a};
|
|
34
67
|
}
|
|
35
68
|
s?.(t);
|
|
36
|
-
let {message:
|
|
37
|
-
{content: o, reasoning: n} =
|
|
69
|
+
let {message: r} = t.choices?.[0] ?? {},
|
|
70
|
+
{content: o, reasoning: n} = r;
|
|
38
71
|
return {content: o, reasoning: n};
|
|
39
72
|
};
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
if (
|
|
73
|
+
var B = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
74
|
+
d = async (t, c, s) => {
|
|
75
|
+
if (c) {
|
|
43
76
|
let o = '',
|
|
44
77
|
n = '';
|
|
45
78
|
for await (let e of t) {
|
|
46
|
-
let {type:
|
|
47
|
-
(
|
|
79
|
+
let {type: a, delta: i} = e;
|
|
80
|
+
(B.includes(a) && (n += i), a === 'response.output_text.delta' && (o += i), s?.({content: o, reasoning: n}, e));
|
|
48
81
|
}
|
|
49
82
|
return {content: o, reasoning: n};
|
|
50
83
|
}
|
|
51
84
|
return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
52
85
|
},
|
|
53
|
-
|
|
54
|
-
if (
|
|
86
|
+
_ = async (t, c, s) => {
|
|
87
|
+
if (c) {
|
|
55
88
|
let n = '',
|
|
56
89
|
e = '';
|
|
57
|
-
for await (let
|
|
58
|
-
let i =
|
|
59
|
-
p =
|
|
60
|
-
(i && (e += i), p && (n += p), s?.({content: n, reasoning: e},
|
|
90
|
+
for await (let a of t) {
|
|
91
|
+
let i = a.reasoning ?? a.thinking,
|
|
92
|
+
p = a.content ?? a.response;
|
|
93
|
+
(i && (e += i), p && (n += p), s?.({content: n, reasoning: e}, a));
|
|
61
94
|
}
|
|
62
95
|
return {content: n, reasoning: e};
|
|
63
96
|
}
|
|
64
97
|
s?.(t);
|
|
65
|
-
let
|
|
66
|
-
return {content: t.content ?? t.response, reasoning:
|
|
98
|
+
let r = t.reasoning ?? t.thinking;
|
|
99
|
+
return {content: t.content ?? t.response, reasoning: r};
|
|
67
100
|
},
|
|
68
|
-
|
|
69
|
-
if (
|
|
101
|
+
R = async (t, c, s) => {
|
|
102
|
+
if (c) {
|
|
70
103
|
let e = '',
|
|
71
|
-
|
|
104
|
+
a = '';
|
|
72
105
|
for await (let i of t) {
|
|
73
106
|
let {message: p} = i,
|
|
74
107
|
f = p.reasoning ?? p.thinking,
|
|
75
108
|
u = p.content ?? p.response;
|
|
76
|
-
(f && (
|
|
109
|
+
(f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
|
|
77
110
|
}
|
|
78
|
-
return {content: e, reasoning:
|
|
111
|
+
return {content: e, reasoning: a};
|
|
79
112
|
}
|
|
80
|
-
let {message:
|
|
113
|
+
let {message: r} = t;
|
|
81
114
|
s?.(t);
|
|
82
|
-
let o =
|
|
83
|
-
return {content:
|
|
84
|
-
};
|
|
85
|
-
var j = ['temperature', 'seed', 'stop', 'top_p'],
|
|
86
|
-
B = t => {
|
|
87
|
-
let {max_tokens: r, options: s = {}, ...a} = t,
|
|
88
|
-
{num_ctx: o, ...n} = s;
|
|
89
|
-
return (
|
|
90
|
-
(a.max_tokens = r ?? o),
|
|
91
|
-
Object.keys(n).map(e => {
|
|
92
|
-
j.includes(e) ? (a[e] = n[e]) : (a.extra_body || (a.extra_body = {}), (a.extra_body[e] = n[e]));
|
|
93
|
-
}),
|
|
94
|
-
a
|
|
95
|
-
);
|
|
96
|
-
},
|
|
97
|
-
H = (t, r = {}, s = 'chat') => {
|
|
98
|
-
if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
|
|
99
|
-
if (!r.model) throw Error('\u8BF7\u914D\u7F6E\u8981\u4F7F\u7528\u7684\u5927\u6A21\u578B model !');
|
|
100
|
-
if (s === 'chat') {
|
|
101
|
-
let o = Array.isArray(t) ? t : [{role: 'user', content: t}],
|
|
102
|
-
{system: n, ...e} = r;
|
|
103
|
-
return (n && (o = [{role: 'system', content: n}, ...o]), {messages: o, ...e});
|
|
104
|
-
}
|
|
105
|
-
if (s === 'responses') {
|
|
106
|
-
let {instructions: o, system: n, ...e} = r;
|
|
107
|
-
return (o || (e.instructions = n), {input: t, ...e});
|
|
108
|
-
}
|
|
109
|
-
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...r};
|
|
110
|
-
};
|
|
111
|
-
var R =
|
|
112
|
-
({params: t, options: r} = {}, s) =>
|
|
113
|
-
(a, o = {}, n) => {
|
|
114
|
-
let {options: e, extra_body: c, ...i} = o,
|
|
115
|
-
p = H(a, {...t, ...i}, n);
|
|
116
|
-
return ((p.options = {...r, ...c, ...e}), s === 'openai' ? B(p) : p);
|
|
115
|
+
let o = r.reasoning ?? r.thinking;
|
|
116
|
+
return {content: r.content ?? r.response, reasoning: o};
|
|
117
117
|
};
|
|
118
|
-
var
|
|
119
|
-
openai: (t
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
},
|
|
154
|
-
generate: async (u, h = {}, g) => {
|
|
155
|
-
let m = f(u, h, 'generate'),
|
|
156
|
-
x = O,
|
|
157
|
-
l = await n.generate(m);
|
|
158
|
-
return x(l, m.stream, g);
|
|
159
|
-
},
|
|
160
|
-
responses: async (u, h = {}, g) => {
|
|
161
|
-
let m = f(u, h, 'responses'),
|
|
162
|
-
x = I,
|
|
163
|
-
l = await n.responses(m);
|
|
164
|
-
return x(l, m.stream, g);
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
},
|
|
118
|
+
var A = {
|
|
119
|
+
openai: (t, c) => ({
|
|
120
|
+
chat: async (s, r = {}, o) => {
|
|
121
|
+
let n = c(s, r, 'chat'),
|
|
122
|
+
e = y,
|
|
123
|
+
a = await t.chat.completions.create(n);
|
|
124
|
+
return e(a, n.stream, o);
|
|
125
|
+
},
|
|
126
|
+
responses: async (s, r = {}, o) => {
|
|
127
|
+
let n = c(s, r, 'responses'),
|
|
128
|
+
e = h,
|
|
129
|
+
a = await t.responses.create(n);
|
|
130
|
+
return e(a, n.stream, o);
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
ollama: (t, c) => ({
|
|
134
|
+
chat: async (s, r = {}, o) => {
|
|
135
|
+
let n = c(s, r, 'chat'),
|
|
136
|
+
e = R,
|
|
137
|
+
a = await t.chat(n);
|
|
138
|
+
return e(a, n.stream, o);
|
|
139
|
+
},
|
|
140
|
+
generate: async (s, r = {}, o) => {
|
|
141
|
+
let n = c(s, r, 'generate'),
|
|
142
|
+
e = _,
|
|
143
|
+
a = await t.generate(n);
|
|
144
|
+
return e(a, n.stream, o);
|
|
145
|
+
},
|
|
146
|
+
responses: async (s, r = {}, o) => {
|
|
147
|
+
let n = c(s, r, 'responses'),
|
|
148
|
+
e = d,
|
|
149
|
+
a = await t.responses(n);
|
|
150
|
+
return e(a, n.stream, o);
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
168
153
|
};
|
|
169
|
-
var D =
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
var D = {ollama: {hostKey: 'host', envConfig: l, API: q}, openai: {hostKey: 'baseURL', envConfig: g, API: z}},
|
|
155
|
+
G = (t = 'ollama', c = {}, s = {}) => {
|
|
156
|
+
t = ['ollama', 'openai'].includes(t) ? t : 'ollama';
|
|
157
|
+
let {hostKey: r, envConfig: o, API: n} = D[t],
|
|
158
|
+
{config: e, params: a, options: i} = o,
|
|
159
|
+
{baseURL: p, host: f, ...u} = {...e, ...c};
|
|
160
|
+
if (((u[r] = f || p), !u[r])) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
|
|
161
|
+
let w = new n({fetch: m, ...u}),
|
|
162
|
+
{options: I, extra_body: P, ...C} = s,
|
|
163
|
+
L = {params: {...a, ...C}, options: {...i, ...P, ...I}},
|
|
164
|
+
O = x(L, t);
|
|
165
|
+
return A[t](w, O);
|
|
173
166
|
},
|
|
174
|
-
st =
|
|
175
|
-
export {st as default,
|
|
167
|
+
st = G;
|
|
168
|
+
export {st as default, G as startApi};
|