huxy-llm-api 1.0.0-beta.0 → 1.0.0
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 +2 -21
- package/example.js +13 -1
- package/package.json +1 -1
- package/src/index.js +111 -108
package/README.md
CHANGED
|
@@ -92,7 +92,7 @@ console.log('对话结果:', response);
|
|
|
92
92
|
|
|
93
93
|
- `generate(prompt, configs, callback)`: 文本生成
|
|
94
94
|
- `chat(prompt, configs, callback)`: 聊天对话
|
|
95
|
-
-
|
|
95
|
+
- <del>`responses(prompt, configs, callback)`: 结构化响应</del>
|
|
96
96
|
|
|
97
97
|
### OpenAI 方法
|
|
98
98
|
|
|
@@ -106,7 +106,7 @@ console.log('对话结果:', response);
|
|
|
106
106
|
- `model`: 模型名称
|
|
107
107
|
- `stream`: 是否流式响应(默认: false)
|
|
108
108
|
- `system`: 系统提示(聊天模式)
|
|
109
|
-
- `options`:
|
|
109
|
+
- `options`: 其他模型参数(OpenAI 可使用 `extra_body`)
|
|
110
110
|
- `temperature`: 生成温度(0-1)
|
|
111
111
|
- `top_p`: 核采样概率
|
|
112
112
|
- `callback`: 函数 - 流式响应回调
|
|
@@ -161,25 +161,6 @@ console.log('对话结果:', response);
|
|
|
161
161
|
}
|
|
162
162
|
```
|
|
163
163
|
|
|
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
164
|
### 环境变量
|
|
184
165
|
|
|
185
166
|
支持通过环境变量配置 API 密钥和地址:
|
package/example.js
CHANGED
|
@@ -16,7 +16,7 @@ const openaiApi = startApi('openai', {
|
|
|
16
16
|
|
|
17
17
|
const demo = async () => {
|
|
18
18
|
const ollamaResult = await ollamaApi.generate('你好', {
|
|
19
|
-
model: '
|
|
19
|
+
model: 'ministral3:14b-reasoning',
|
|
20
20
|
stream: false,
|
|
21
21
|
options: {
|
|
22
22
|
temperature: 0.15,
|
|
@@ -38,6 +38,18 @@ const demo = async () => {
|
|
|
38
38
|
console.log(mesg, resp);
|
|
39
39
|
});
|
|
40
40
|
console.log(openaiResult);
|
|
41
|
+
|
|
42
|
+
const responsesResult = await openaiApi.responses('你是谁', {
|
|
43
|
+
model: 'qwen3-vl',
|
|
44
|
+
temperature: 0.15,
|
|
45
|
+
stream: true,
|
|
46
|
+
options: {
|
|
47
|
+
top_k: 20,
|
|
48
|
+
},
|
|
49
|
+
}, (mesg, resp) => {
|
|
50
|
+
console.log(mesg, resp);
|
|
51
|
+
});
|
|
52
|
+
console.log(responsesResult);
|
|
41
53
|
};
|
|
42
54
|
|
|
43
55
|
demo();
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {Ollama as T} from 'ollama';
|
|
2
|
-
import
|
|
3
|
-
import {fetch as
|
|
2
|
+
import q from 'openai';
|
|
3
|
+
import {fetch as w, Agent as L} from 'undici';
|
|
4
4
|
var P = 300 * 60 * 1e3,
|
|
5
|
-
k = (t,
|
|
6
|
-
|
|
5
|
+
k = (t, r) => w(t, {...r, dispatcher: new L({headersTimeout: P})}),
|
|
6
|
+
y = k;
|
|
7
7
|
var v = {
|
|
8
8
|
config: {
|
|
9
9
|
apiKey: process.env.LLM_API_KEY || 'ah.yiru@gmail.com',
|
|
@@ -21,144 +21,147 @@ var C = {
|
|
|
21
21
|
options: {temperature: 0.6, num_ctx: 4096, top_k: 20, top_p: 0.9, repeat_penalty: 1.15},
|
|
22
22
|
},
|
|
23
23
|
g = C;
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
var K = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
25
|
+
_ = async (t, r, s) => {
|
|
26
|
+
if (r) {
|
|
27
|
+
let o = '',
|
|
28
|
+
n = '';
|
|
29
|
+
for await (let e of t) {
|
|
30
|
+
let {type: i, delta: c} = e;
|
|
31
|
+
(K.includes(i) && (n += c), i === 'response.output_text.delta' && (o += c), s?.({content: o, reasoning: n}, e));
|
|
31
32
|
}
|
|
32
|
-
return {content:
|
|
33
|
+
return {content: o, reasoning: n};
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
return (o?.(t), {reasoning: s.content?.[0]?.text, content: t.output_text});
|
|
35
|
+
return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
36
36
|
},
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
for await (let
|
|
42
|
-
let {delta: p} =
|
|
43
|
-
{reasoning:
|
|
44
|
-
(
|
|
37
|
+
d = async (t, r, s) => {
|
|
38
|
+
if (r) {
|
|
39
|
+
let e = '',
|
|
40
|
+
i = '';
|
|
41
|
+
for await (let c of t) {
|
|
42
|
+
let {delta: p} = c.choices?.[0] ?? {},
|
|
43
|
+
{reasoning: l, content: f} = p ?? {};
|
|
44
|
+
(l && (i += l), f && (e += f), s?.({content: e, reasoning: i}, c));
|
|
45
45
|
}
|
|
46
|
-
return {content:
|
|
46
|
+
return {content: e, reasoning: i};
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
s?.(t);
|
|
49
|
+
let {message: a} = t.choices?.[0] ?? {},
|
|
50
|
+
{content: o, reasoning: n} = a;
|
|
51
|
+
return {content: o, reasoning: n};
|
|
51
52
|
};
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
for await (let n of t) {
|
|
57
|
-
let {type: c, delta: i} = n;
|
|
58
|
-
(c === 'response.reasoning_text.delta' && (e += i), c === 'response.output_text.delta' && (r += i), o?.({content: r, reasoning: e}, n));
|
|
59
|
-
}
|
|
60
|
-
return {content: r, reasoning: e};
|
|
61
|
-
}
|
|
62
|
-
let [s = {}] = t.output ?? [];
|
|
63
|
-
return (o?.(t), {reasoning: s.content?.[0]?.text, content: t.output_text});
|
|
64
|
-
},
|
|
65
|
-
A = async (t, a, o) => {
|
|
66
|
-
if (a) {
|
|
67
|
-
let e = '',
|
|
53
|
+
var O = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
54
|
+
R = async (t, r, s) => {
|
|
55
|
+
if (r) {
|
|
56
|
+
let o = '',
|
|
68
57
|
n = '';
|
|
69
|
-
for await (let
|
|
70
|
-
let i
|
|
71
|
-
|
|
72
|
-
(i && (n += i), p && (e += p), o?.({content: e, reasoning: n}, c));
|
|
58
|
+
for await (let e of t) {
|
|
59
|
+
let {type: i, delta: c} = e;
|
|
60
|
+
(O.includes(i) && (n += c), i === 'response.output_text.delta' && (o += c), s?.({content: o, reasoning: n}, e));
|
|
73
61
|
}
|
|
74
|
-
return {content:
|
|
62
|
+
return {content: o, reasoning: n};
|
|
75
63
|
}
|
|
76
|
-
|
|
77
|
-
r = t.content ?? t.response;
|
|
78
|
-
return (o?.(t), {content: r, reasoning: s});
|
|
64
|
+
return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
|
|
79
65
|
},
|
|
80
|
-
|
|
81
|
-
if (
|
|
66
|
+
A = async (t, r, s) => {
|
|
67
|
+
if (r) {
|
|
82
68
|
let n = '',
|
|
83
|
-
|
|
69
|
+
e = '';
|
|
84
70
|
for await (let i of t) {
|
|
85
|
-
let
|
|
86
|
-
|
|
71
|
+
let c = i.reasoning ?? i.thinking,
|
|
72
|
+
p = i.content ?? i.response;
|
|
73
|
+
(c && (e += c), p && (n += p), s?.({content: n, reasoning: e}, i));
|
|
74
|
+
}
|
|
75
|
+
return {content: n, reasoning: e};
|
|
76
|
+
}
|
|
77
|
+
s?.(t);
|
|
78
|
+
let a = t.reasoning ?? t.thinking;
|
|
79
|
+
return {content: t.content ?? t.response, reasoning: a};
|
|
80
|
+
},
|
|
81
|
+
I = async (t, r, s) => {
|
|
82
|
+
if (r) {
|
|
83
|
+
let e = '',
|
|
84
|
+
i = '';
|
|
85
|
+
for await (let c of t) {
|
|
86
|
+
let {message: p} = c,
|
|
87
|
+
l = p.reasoning ?? p.thinking,
|
|
87
88
|
f = p.content ?? p.response;
|
|
88
|
-
(
|
|
89
|
+
(l && (i += l), f && (e += f), s?.({content: e, reasoning: i}, c));
|
|
89
90
|
}
|
|
90
|
-
return {content:
|
|
91
|
+
return {content: e, reasoning: i};
|
|
91
92
|
}
|
|
92
|
-
let {message:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return
|
|
93
|
+
let {message: a} = t;
|
|
94
|
+
s?.(t);
|
|
95
|
+
let o = a.reasoning ?? a.thinking;
|
|
96
|
+
return {content: a.content ?? a.response, reasoning: o};
|
|
96
97
|
};
|
|
97
|
-
var
|
|
98
|
+
var u = (t, r = {}, s = 'chat') => {
|
|
98
99
|
if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
|
|
99
|
-
if (
|
|
100
|
-
let
|
|
101
|
-
{system:
|
|
102
|
-
return (
|
|
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});
|
|
103
108
|
}
|
|
104
|
-
|
|
105
|
-
{instructions: r, ...e} = a;
|
|
106
|
-
return (r || (e.instructions = e.system), {prompt: s, ...e});
|
|
109
|
+
return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...r};
|
|
107
110
|
},
|
|
108
|
-
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
return (
|
|
111
|
+
m = ({options: t, extra_body: r, ...s}, a, o) => {
|
|
112
|
+
let n = {...a.params, ...s},
|
|
113
|
+
e = {...a.options, ...t};
|
|
114
|
+
return (o === 'openai' ? (n.extra_body = {...e, ...r}) : (n.options = e), n);
|
|
112
115
|
};
|
|
113
|
-
var
|
|
116
|
+
var x = {
|
|
114
117
|
openai: {
|
|
115
|
-
API: t => new
|
|
118
|
+
API: t => new q({...t, fetch: y}),
|
|
116
119
|
config: h,
|
|
117
120
|
llm: t => ({
|
|
118
|
-
chat: async (
|
|
119
|
-
let
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return
|
|
121
|
+
chat: async (r, s = {}, a) => {
|
|
122
|
+
let o = d,
|
|
123
|
+
n = u(r, s, 'chat'),
|
|
124
|
+
e = await t.chat.completions.create(m(n, h, 'openai'));
|
|
125
|
+
return o(e, n.stream, a);
|
|
123
126
|
},
|
|
124
|
-
responses: async (
|
|
125
|
-
let
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return
|
|
127
|
+
responses: async (r, s = {}, a) => {
|
|
128
|
+
let o = _,
|
|
129
|
+
n = u(r, s, 'responses'),
|
|
130
|
+
e = await t.responses.create(m(n, h, 'openai'));
|
|
131
|
+
return o(e, n.stream, a);
|
|
129
132
|
},
|
|
130
133
|
}),
|
|
131
134
|
},
|
|
132
135
|
ollama: {
|
|
133
|
-
API: t => new T({...t, fetch:
|
|
136
|
+
API: t => new T({...t, fetch: y}),
|
|
134
137
|
config: g,
|
|
135
138
|
llm: t => ({
|
|
136
|
-
chat: async (
|
|
137
|
-
let
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return
|
|
139
|
+
chat: async (r, s = {}, a) => {
|
|
140
|
+
let o = I,
|
|
141
|
+
n = u(r, s, 'chat'),
|
|
142
|
+
e = await t.chat(m(n, g, 'ollama'));
|
|
143
|
+
return o(e, n.stream, a);
|
|
141
144
|
},
|
|
142
|
-
generate: async (
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return
|
|
145
|
+
generate: async (r, s = {}, a) => {
|
|
146
|
+
let o = A,
|
|
147
|
+
n = u(r, s, 'generate'),
|
|
148
|
+
e = await t.generate(m(n, g, 'ollama'));
|
|
149
|
+
return o(e, n.stream, a);
|
|
147
150
|
},
|
|
148
|
-
responses: async (
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return
|
|
151
|
+
responses: async (r, s = {}, a) => {
|
|
152
|
+
let o = R,
|
|
153
|
+
n = u(r, s, 'responses'),
|
|
154
|
+
e = await t.responses(m(n, g, 'ollama'));
|
|
155
|
+
return o(e, n.stream, a);
|
|
153
156
|
},
|
|
154
157
|
}),
|
|
155
158
|
},
|
|
156
159
|
};
|
|
157
|
-
var
|
|
158
|
-
let
|
|
159
|
-
{API:
|
|
160
|
-
|
|
161
|
-
return e
|
|
160
|
+
var U = (t = 'ollama', r) => {
|
|
161
|
+
let s = x[t] ?? x.ollama,
|
|
162
|
+
{API: a, config: o, llm: n} = s,
|
|
163
|
+
e = a({...o.config, ...r});
|
|
164
|
+
return n(e);
|
|
162
165
|
},
|
|
163
|
-
|
|
164
|
-
export {
|
|
166
|
+
W = U;
|
|
167
|
+
export {W as default, U as startApi};
|