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