huxy-llm-api 1.0.7 → 1.0.9
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 +9 -5
- package/example.js +2 -2
- package/package.json +2 -2
- package/src/index.js +138 -145
package/README.md
CHANGED
|
@@ -32,6 +32,9 @@ import startApi from 'huxy-llm-api';
|
|
|
32
32
|
const ollamaApi = startApi('ollama', {
|
|
33
33
|
apiKey: 'your-api-key',
|
|
34
34
|
host: 'http://localhost:11434',
|
|
35
|
+
dispatcher: {
|
|
36
|
+
headersTimeout: 10 * 60 * 1000,
|
|
37
|
+
},
|
|
35
38
|
}, {
|
|
36
39
|
model: 'qwen3-vl:latest',
|
|
37
40
|
options: {
|
|
@@ -89,7 +92,8 @@ console.log('对话结果:', response);
|
|
|
89
92
|
**参数:**
|
|
90
93
|
|
|
91
94
|
- `apiType`: `'ollama'` 或 `'openai'` - 指定要使用的 API 类型
|
|
92
|
-
- `userConfig`: 对象 -
|
|
95
|
+
- `userConfig`: 对象 - 自定义 API 接口配置,如 apiKey、baseURL、fetch 等
|
|
96
|
+
- `userOption`: 对象 - 通用模型参数配置
|
|
93
97
|
|
|
94
98
|
**返回:** API 客户端实例,包含以下方法:
|
|
95
99
|
|
|
@@ -126,8 +130,8 @@ console.log('对话结果:', response);
|
|
|
126
130
|
|
|
127
131
|
```javascript
|
|
128
132
|
{
|
|
129
|
-
apiKey: process.env.OLLM_API_KEY || '
|
|
130
|
-
host: process.env.OLLM_API_HOST || 'http://
|
|
133
|
+
apiKey: process.env.OLLM_API_KEY || '1234',
|
|
134
|
+
host: process.env.OLLM_API_HOST || 'http://localhost:11434',
|
|
131
135
|
params: {
|
|
132
136
|
model: 'qwen3-vl:latest',
|
|
133
137
|
keep_alive: -1,
|
|
@@ -146,8 +150,8 @@ console.log('对话结果:', response);
|
|
|
146
150
|
|
|
147
151
|
```javascript
|
|
148
152
|
{
|
|
149
|
-
apiKey: process.env.LLM_API_KEY || '
|
|
150
|
-
baseURL: process.env.LLM_API_BASEURL || 'http://
|
|
153
|
+
apiKey: process.env.LLM_API_KEY || '1234',
|
|
154
|
+
baseURL: process.env.LLM_API_BASEURL || 'http://localhost:11434/v1',
|
|
151
155
|
timeout: 3 * 60 * 60 * 1000,
|
|
152
156
|
maxRetries: 3,
|
|
153
157
|
params: {
|
package/example.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import startApi from './index.js';
|
|
2
2
|
|
|
3
3
|
const ollamaApi = startApi('ollama', {
|
|
4
|
-
apiKey: '
|
|
4
|
+
apiKey: '1234',
|
|
5
5
|
host: 'http://192.168.0.111:11434',
|
|
6
6
|
// headers
|
|
7
7
|
// fetch
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
const openaiApi = startApi('openai', {
|
|
11
|
-
apiKey: '
|
|
11
|
+
apiKey: '1234',
|
|
12
12
|
baseURL: 'http://192.168.0.111:11434/v1',
|
|
13
13
|
// headers
|
|
14
14
|
// fetch
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "huxy-llm-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"ollama": "^0.6.3",
|
|
38
|
-
"openai": "^6.
|
|
38
|
+
"openai": "^6.16.0",
|
|
39
39
|
"undici": "^7.18.2"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
package/src/index.js
CHANGED
|
@@ -1,175 +1,168 @@
|
|
|
1
|
-
import {Ollama as
|
|
2
|
-
import
|
|
3
|
-
import {fetch as
|
|
4
|
-
var
|
|
5
|
-
k = (
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
var M = {config: {apiKey: process.env.
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import {Ollama as z} from 'ollama';
|
|
2
|
+
import D from 'openai';
|
|
3
|
+
import {fetch as v, Agent as b} from 'undici';
|
|
4
|
+
var E = 300 * 60 * 1e3,
|
|
5
|
+
k = t => (c, o) => v(c, {...o, dispatcher: new b({headersTimeout: E, ...t})}),
|
|
6
|
+
l = k;
|
|
7
|
+
var U = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
|
|
8
|
+
g = U;
|
|
9
|
+
var M = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
|
|
10
|
+
x = M;
|
|
11
|
+
var F = ['temperature', 'seed', 'stop', 'top_p'],
|
|
12
|
+
S = t => {
|
|
13
|
+
let {max_tokens: c, options: o = {}, ...r} = t,
|
|
14
|
+
{num_ctx: s, ...n} = o;
|
|
15
|
+
return (
|
|
16
|
+
(r.max_tokens = c ?? s),
|
|
17
|
+
Object.keys(n).map(e => {
|
|
18
|
+
F.includes(e) ? (r[e] = n[e]) : (r.extra_body || (r.extra_body = {}), (r.extra_body[e] = n[e]));
|
|
19
|
+
}),
|
|
20
|
+
r
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
Y = (t, c = {}, o = '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 (o === 'chat') {
|
|
27
|
+
let s = Array.isArray(t) ? t : [{role: 'user', content: t}],
|
|
28
|
+
{system: n, ...e} = c;
|
|
29
|
+
return (n && (s = [{role: 'system', content: n}, ...s]), {messages: s, ...e});
|
|
30
|
+
}
|
|
31
|
+
if (o === 'responses') {
|
|
32
|
+
let {instructions: s, system: n, ...e} = c;
|
|
33
|
+
return (s || (e.instructions = n), {input: t, ...e});
|
|
34
|
+
}
|
|
35
|
+
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...c};
|
|
36
|
+
};
|
|
37
|
+
var h =
|
|
38
|
+
({params: t, options: c} = {}, o) =>
|
|
39
|
+
(r, s = {}, n) => {
|
|
40
|
+
let {options: e, extra_body: a, ...i} = s,
|
|
41
|
+
p = Y(r, {...t, ...i}, n);
|
|
42
|
+
return ((p.options = {...c, ...a, ...e}), o === 'openai' ? S(p) : p);
|
|
43
|
+
};
|
|
44
|
+
var j = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
45
|
+
y = async (t, c, o) => {
|
|
46
|
+
if (c) {
|
|
47
|
+
let s = '',
|
|
15
48
|
n = '';
|
|
16
49
|
for await (let e of t) {
|
|
17
|
-
let {type:
|
|
18
|
-
(
|
|
50
|
+
let {type: a, delta: i} = e;
|
|
51
|
+
(j.includes(a) && (n += i), a === 'response.output_text.delta' && (s += i), o?.({content: s, reasoning: n}, e));
|
|
19
52
|
}
|
|
20
|
-
return {content:
|
|
53
|
+
return {content: s, reasoning: n};
|
|
21
54
|
}
|
|
22
|
-
return (
|
|
55
|
+
return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
23
56
|
},
|
|
24
|
-
|
|
25
|
-
if (
|
|
57
|
+
d = async (t, c, o) => {
|
|
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
|
-
{reasoning:
|
|
31
|
-
(
|
|
63
|
+
{reasoning: u, content: f} = p ?? {};
|
|
64
|
+
(u && (a += u), f && (e += f), o?.({content: e, reasoning: a}, i));
|
|
32
65
|
}
|
|
33
|
-
return {content: e, reasoning:
|
|
66
|
+
return {content: e, reasoning: a};
|
|
34
67
|
}
|
|
35
|
-
|
|
36
|
-
let {message:
|
|
37
|
-
{content:
|
|
38
|
-
return {content:
|
|
68
|
+
o?.(t);
|
|
69
|
+
let {message: r} = t.choices?.[0] ?? {},
|
|
70
|
+
{content: s, reasoning: n} = r;
|
|
71
|
+
return {content: s, reasoning: n};
|
|
39
72
|
};
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
let
|
|
73
|
+
var H = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
74
|
+
_ = async (t, c, o) => {
|
|
75
|
+
if (c) {
|
|
76
|
+
let s = '',
|
|
44
77
|
n = '';
|
|
45
78
|
for await (let e of t) {
|
|
46
|
-
let {type:
|
|
47
|
-
(
|
|
79
|
+
let {type: a, delta: i} = e;
|
|
80
|
+
(H.includes(a) && (n += i), a === 'response.output_text.delta' && (s += i), o?.({content: s, reasoning: n}, e));
|
|
48
81
|
}
|
|
49
|
-
return {content:
|
|
82
|
+
return {content: s, reasoning: n};
|
|
50
83
|
}
|
|
51
|
-
return (
|
|
84
|
+
return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
52
85
|
},
|
|
53
|
-
|
|
54
|
-
if (
|
|
86
|
+
R = async (t, c, o) => {
|
|
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),
|
|
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), o?.({content: n, reasoning: e}, a));
|
|
61
94
|
}
|
|
62
95
|
return {content: n, reasoning: e};
|
|
63
96
|
}
|
|
64
|
-
|
|
65
|
-
let
|
|
66
|
-
return {content: t.content ?? t.response, reasoning:
|
|
97
|
+
o?.(t);
|
|
98
|
+
let r = t.reasoning ?? t.thinking;
|
|
99
|
+
return {content: t.content ?? t.response, reasoning: r};
|
|
67
100
|
},
|
|
68
|
-
|
|
69
|
-
if (
|
|
101
|
+
A = async (t, c, o) => {
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
(
|
|
107
|
+
u = p.reasoning ?? p.thinking,
|
|
108
|
+
f = p.content ?? p.response;
|
|
109
|
+
(u && (a += u), f && (e += f), o?.({content: e, reasoning: a}, i));
|
|
77
110
|
}
|
|
78
|
-
return {content: e, reasoning:
|
|
79
|
-
}
|
|
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});
|
|
111
|
+
return {content: e, reasoning: a};
|
|
108
112
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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);
|
|
113
|
+
let {message: r} = t;
|
|
114
|
+
o?.(t);
|
|
115
|
+
let s = r.reasoning ?? r.thinking;
|
|
116
|
+
return {content: r.content ?? r.response, reasoning: s};
|
|
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 w = {
|
|
119
|
+
openai: (t, c) => ({
|
|
120
|
+
chat: async (o, r = {}, s) => {
|
|
121
|
+
let n = c(o, r, 'chat'),
|
|
122
|
+
e = d,
|
|
123
|
+
a = await t.chat.completions.create(n);
|
|
124
|
+
return e(a, n.stream, s);
|
|
125
|
+
},
|
|
126
|
+
responses: async (o, r = {}, s) => {
|
|
127
|
+
let n = c(o, r, 'responses'),
|
|
128
|
+
e = y,
|
|
129
|
+
a = await t.responses.create(n);
|
|
130
|
+
return e(a, n.stream, s);
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
ollama: (t, c) => ({
|
|
134
|
+
chat: async (o, r = {}, s) => {
|
|
135
|
+
let n = c(o, r, 'chat'),
|
|
136
|
+
e = A,
|
|
137
|
+
a = await t.chat(n);
|
|
138
|
+
return e(a, n.stream, s);
|
|
139
|
+
},
|
|
140
|
+
generate: async (o, r = {}, s) => {
|
|
141
|
+
let n = c(o, r, 'generate'),
|
|
142
|
+
e = R,
|
|
143
|
+
a = await t.generate(n);
|
|
144
|
+
return e(a, n.stream, s);
|
|
145
|
+
},
|
|
146
|
+
responses: async (o, r = {}, s) => {
|
|
147
|
+
let n = c(o, r, 'responses'),
|
|
148
|
+
e = _,
|
|
149
|
+
a = await t.responses(n);
|
|
150
|
+
return e(a, n.stream, s);
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
168
153
|
};
|
|
169
|
-
var
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
var G = {ollama: {hostKey: 'host', envConfig: g, API: z}, openai: {hostKey: 'baseURL', envConfig: x, API: D}},
|
|
155
|
+
J = (t = 'ollama', c = {}, o = {}) => {
|
|
156
|
+
t = ['ollama', 'openai'].includes(t) ? t : 'ollama';
|
|
157
|
+
let {hostKey: r, envConfig: s, API: n} = G[t],
|
|
158
|
+
{config: e, params: a, options: i} = s,
|
|
159
|
+
{baseURL: p, host: u, dispatcher: f, ...m} = {...e, ...c};
|
|
160
|
+
if (((m[r] = u || p), !m[r])) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
|
|
161
|
+
let I = new n({fetch: l(f), ...m}),
|
|
162
|
+
{options: P, extra_body: C, ...L} = o,
|
|
163
|
+
O = {params: {...a, ...L}, options: {...i, ...C, ...P}},
|
|
164
|
+
K = h(O, t);
|
|
165
|
+
return w[t](I, K);
|
|
173
166
|
},
|
|
174
|
-
|
|
175
|
-
export {
|
|
167
|
+
rt = J;
|
|
168
|
+
export {rt as default, J as startApi};
|