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