huxy-llm-api 1.0.0-beta.0 → 1.0.1
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 +7 -21
- package/example.js +15 -1
- package/package.json +1 -1
- package/src/index.js +130 -132
package/README.md
CHANGED
|
@@ -32,6 +32,11 @@ 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
|
+
}, {
|
|
36
|
+
model: 'qwen3-vl:latest',
|
|
37
|
+
options: {
|
|
38
|
+
num_ctx: 4096,
|
|
39
|
+
},
|
|
35
40
|
});
|
|
36
41
|
|
|
37
42
|
// 初始化 OpenAI API
|
|
@@ -92,7 +97,7 @@ console.log('对话结果:', response);
|
|
|
92
97
|
|
|
93
98
|
- `generate(prompt, configs, callback)`: 文本生成
|
|
94
99
|
- `chat(prompt, configs, callback)`: 聊天对话
|
|
95
|
-
-
|
|
100
|
+
- <del>`responses(prompt, configs, callback)`: 结构化响应</del>
|
|
96
101
|
|
|
97
102
|
### OpenAI 方法
|
|
98
103
|
|
|
@@ -106,7 +111,7 @@ console.log('对话结果:', response);
|
|
|
106
111
|
- `model`: 模型名称
|
|
107
112
|
- `stream`: 是否流式响应(默认: false)
|
|
108
113
|
- `system`: 系统提示(聊天模式)
|
|
109
|
-
- `options`:
|
|
114
|
+
- `options`: 其他模型参数(OpenAI 可使用 `extra_body`)
|
|
110
115
|
- `temperature`: 生成温度(0-1)
|
|
111
116
|
- `top_p`: 核采样概率
|
|
112
117
|
- `callback`: 函数 - 流式响应回调
|
|
@@ -161,25 +166,6 @@ console.log('对话结果:', response);
|
|
|
161
166
|
}
|
|
162
167
|
```
|
|
163
168
|
|
|
164
|
-
## 高级用法
|
|
165
|
-
|
|
166
|
-
### 自定义 Fetch
|
|
167
|
-
|
|
168
|
-
项目使用 Undici 实现高性能 HTTP 请求,并支持自定义超时:
|
|
169
|
-
|
|
170
|
-
```javascript
|
|
171
|
-
import customFetch from 'huxy-llm-api/customFetch';
|
|
172
|
-
|
|
173
|
-
// 自定义请求
|
|
174
|
-
const response = await customFetch('https://api.example.com', {
|
|
175
|
-
method: 'POST',
|
|
176
|
-
headers: {
|
|
177
|
-
'Content-Type': 'application/json',
|
|
178
|
-
},
|
|
179
|
-
body: JSON.stringify({prompt: 'Hello'}),
|
|
180
|
-
});
|
|
181
|
-
```
|
|
182
|
-
|
|
183
169
|
### 环境变量
|
|
184
170
|
|
|
185
171
|
支持通过环境变量配置 API 密钥和地址:
|
package/example.js
CHANGED
|
@@ -12,11 +12,13 @@ const openaiApi = startApi('openai', {
|
|
|
12
12
|
baseURL: 'http://192.168.0.111:11434/v1',
|
|
13
13
|
// headers
|
|
14
14
|
// fetch
|
|
15
|
+
}, {
|
|
16
|
+
model: 'qwen3-vl',
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
const demo = async () => {
|
|
18
20
|
const ollamaResult = await ollamaApi.generate('你好', {
|
|
19
|
-
model: '
|
|
21
|
+
model: 'ministral3:14b-reasoning',
|
|
20
22
|
stream: false,
|
|
21
23
|
options: {
|
|
22
24
|
temperature: 0.15,
|
|
@@ -38,6 +40,18 @@ const demo = async () => {
|
|
|
38
40
|
console.log(mesg, resp);
|
|
39
41
|
});
|
|
40
42
|
console.log(openaiResult);
|
|
43
|
+
|
|
44
|
+
const responsesResult = await openaiApi.responses('你是谁', {
|
|
45
|
+
model: 'qwen3-vl',
|
|
46
|
+
temperature: 0.15,
|
|
47
|
+
stream: true,
|
|
48
|
+
options: {
|
|
49
|
+
top_k: 20,
|
|
50
|
+
},
|
|
51
|
+
}, (mesg, resp) => {
|
|
52
|
+
console.log(mesg, resp);
|
|
53
|
+
});
|
|
54
|
+
console.log(responsesResult);
|
|
41
55
|
};
|
|
42
56
|
|
|
43
57
|
demo();
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,164 +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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
I = q;
|
|
19
|
+
var F = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
20
|
+
C = async (t, r, o) => {
|
|
21
|
+
if (r) {
|
|
22
|
+
let s = '',
|
|
23
|
+
n = '';
|
|
24
|
+
for await (let e of t) {
|
|
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));
|
|
31
27
|
}
|
|
32
|
-
return {content:
|
|
28
|
+
return {content: s, reasoning: n};
|
|
33
29
|
}
|
|
34
|
-
|
|
35
|
-
return (o?.(t), {reasoning: s.content?.[0]?.text, content: t.output_text});
|
|
30
|
+
return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
36
31
|
},
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
for await (let
|
|
42
|
-
let {delta: p} =
|
|
43
|
-
{reasoning: u, content:
|
|
44
|
-
(u && (
|
|
32
|
+
P = async (t, r, o) => {
|
|
33
|
+
if (r) {
|
|
34
|
+
let e = '',
|
|
35
|
+
a = '';
|
|
36
|
+
for await (let c of t) {
|
|
37
|
+
let {delta: p} = c.choices?.[0] ?? {},
|
|
38
|
+
{reasoning: u, content: m} = p ?? {};
|
|
39
|
+
(u && (a += u), m && (e += m), o?.({content: e, reasoning: a}, c));
|
|
45
40
|
}
|
|
46
|
-
return {content:
|
|
41
|
+
return {content: e, reasoning: a};
|
|
47
42
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
o?.(t);
|
|
44
|
+
let {message: i} = t.choices?.[0] ?? {},
|
|
45
|
+
{content: s, reasoning: n} = i;
|
|
46
|
+
return {content: s, reasoning: n};
|
|
51
47
|
};
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
var Y = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
49
|
+
k = async (t, r, o) => {
|
|
50
|
+
if (r) {
|
|
51
|
+
let s = '',
|
|
52
|
+
n = '';
|
|
53
|
+
for await (let e of t) {
|
|
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));
|
|
59
56
|
}
|
|
60
|
-
return {content:
|
|
57
|
+
return {content: s, reasoning: n};
|
|
61
58
|
}
|
|
62
|
-
|
|
63
|
-
return (o?.(t), {reasoning: s.content?.[0]?.text, content: t.output_text});
|
|
59
|
+
return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
64
60
|
},
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
let
|
|
68
|
-
|
|
69
|
-
for await (let
|
|
70
|
-
let
|
|
71
|
-
p =
|
|
72
|
-
(
|
|
61
|
+
v = async (t, r, o) => {
|
|
62
|
+
if (r) {
|
|
63
|
+
let n = '',
|
|
64
|
+
e = '';
|
|
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));
|
|
73
69
|
}
|
|
74
|
-
return {content:
|
|
70
|
+
return {content: n, reasoning: e};
|
|
75
71
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return
|
|
72
|
+
o?.(t);
|
|
73
|
+
let i = t.reasoning ?? t.thinking;
|
|
74
|
+
return {content: t.content ?? t.response, reasoning: i};
|
|
79
75
|
},
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
for await (let
|
|
85
|
-
let {message: p} =
|
|
76
|
+
O = async (t, r, o) => {
|
|
77
|
+
if (r) {
|
|
78
|
+
let e = '',
|
|
79
|
+
a = '';
|
|
80
|
+
for await (let c of t) {
|
|
81
|
+
let {message: p} = c,
|
|
86
82
|
u = p.reasoning ?? p.thinking,
|
|
87
|
-
|
|
88
|
-
(u && (
|
|
83
|
+
m = p.content ?? p.response;
|
|
84
|
+
(u && (a += u), m && (e += m), o?.({content: e, reasoning: a}, c));
|
|
89
85
|
}
|
|
90
|
-
return {content:
|
|
86
|
+
return {content: e, reasoning: a};
|
|
91
87
|
}
|
|
92
|
-
let {message:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return
|
|
88
|
+
let {message: i} = t;
|
|
89
|
+
o?.(t);
|
|
90
|
+
let s = i.reasoning ?? i.thinking;
|
|
91
|
+
return {content: i.content ?? i.response, reasoning: s};
|
|
96
92
|
};
|
|
97
|
-
var
|
|
93
|
+
var x = (t, r = {}, o = 'chat') => {
|
|
98
94
|
if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
|
|
99
95
|
if (o === 'chat') {
|
|
100
|
-
let
|
|
101
|
-
{system:
|
|
102
|
-
return (
|
|
96
|
+
let s = Array.isArray(t) ? t : [{role: 'user', content: t}],
|
|
97
|
+
{system: n, ...e} = r;
|
|
98
|
+
return (n && (s = [{role: 'system', content: n}, ...s]), {messages: s, ...e});
|
|
103
99
|
}
|
|
104
|
-
|
|
105
|
-
{instructions:
|
|
106
|
-
|
|
100
|
+
if (o === 'responses') {
|
|
101
|
+
let {instructions: s, system: n, ...e} = r;
|
|
102
|
+
return (s || (e.instructions = n), {input: t, ...e});
|
|
103
|
+
}
|
|
104
|
+
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...r};
|
|
107
105
|
},
|
|
108
|
-
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
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);
|
|
112
110
|
};
|
|
113
|
-
var
|
|
114
|
-
openai: {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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);
|
|
123
124
|
},
|
|
124
|
-
responses: async (
|
|
125
|
-
let
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
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);
|
|
129
130
|
},
|
|
130
|
-
}
|
|
131
|
+
};
|
|
131
132
|
},
|
|
132
|
-
ollama: {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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);
|
|
141
144
|
},
|
|
142
|
-
generate: async (
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
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);
|
|
147
150
|
},
|
|
148
|
-
responses: async (
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
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);
|
|
153
156
|
},
|
|
154
|
-
}
|
|
157
|
+
};
|
|
155
158
|
},
|
|
156
159
|
};
|
|
157
|
-
var
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
n = s({...r.config, ...a});
|
|
161
|
-
return e(n);
|
|
162
|
-
},
|
|
163
|
-
Q = K;
|
|
164
|
-
export {Q as default, K 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};
|