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