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