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 CHANGED
@@ -92,7 +92,7 @@ console.log('对话结果:', response);
92
92
 
93
93
  - `generate(prompt, configs, callback)`: 文本生成
94
94
  - `chat(prompt, configs, callback)`: 聊天对话
95
- - `responses(prompt, configs, callback)`: 结构化响应
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: 'devstral-small-2',
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huxy-llm-api",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.0",
4
4
  "description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
5
5
  "type": "module",
6
6
  "module": "./src/index.js",
package/src/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import {Ollama as T} from 'ollama';
2
- import E from 'openai';
3
- import {fetch as I, Agent as L} from 'undici';
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, a) => I(t, {...a, dispatcher: new L({headersTimeout: P})}),
6
- x = k;
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 _ = async (t, a, o) => {
25
- if (a) {
26
- let r = '',
27
- e = '';
28
- for await (let n of t) {
29
- let {type: c, delta: i} = n;
30
- (c === 'response.reasoning_text.delta' && (e += i), c === 'response.output_text.delta' && (r += i), o?.({content: r, reasoning: e}, n));
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: r, reasoning: e};
33
+ return {content: o, reasoning: n};
33
34
  }
34
- let [s = {}] = t.output ?? [];
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
- R = async (t, a, o) => {
38
- if (a) {
39
- let n = '',
40
- c = '';
41
- for await (let i of t) {
42
- let {delta: p} = i.choices?.[0] ?? {},
43
- {reasoning: u, content: f} = p ?? {};
44
- (u && (c += u), f && (n += f), o?.({content: n, reasoning: c}, i));
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: n, reasoning: c};
46
+ return {content: e, reasoning: i};
47
47
  }
48
- let {message: s} = t.choices?.[0] ?? {},
49
- {content: r, reasoning: e} = s;
50
- return (o?.(t), {content: r, reasoning: e});
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 d = async (t, a, o) => {
53
- if (a) {
54
- let r = '',
55
- e = '';
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 c of t) {
70
- let i = c.reasoning ?? c.thinking,
71
- p = c.content ?? c.response;
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: e, reasoning: n};
62
+ return {content: o, reasoning: n};
75
63
  }
76
- let s = t.reasoning ?? t.thinking,
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
- w = async (t, a, o) => {
81
- if (a) {
66
+ A = async (t, r, s) => {
67
+ if (r) {
82
68
  let n = '',
83
- c = '';
69
+ e = '';
84
70
  for await (let i of t) {
85
- let {message: p} = i,
86
- u = p.reasoning ?? p.thinking,
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
- (u && (c += u), f && (n += f), o?.({content: n, reasoning: c}, i));
89
+ (l && (i += l), f && (e += f), s?.({content: e, reasoning: i}, c));
89
90
  }
90
- return {content: n, reasoning: c};
91
+ return {content: e, reasoning: i};
91
92
  }
92
- let {message: s} = t,
93
- r = s.reasoning ?? s.thinking,
94
- e = s.content ?? s.response;
95
- return (o?.(t), {content: e, reasoning: r});
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 m = (t, a = {}, o = 'chat') => {
98
+ var u = (t, r = {}, s = 'chat') => {
98
99
  if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
99
- if (o === 'chat') {
100
- let n = Array.isArray(t) ? t : [{role: 'user', content: t}],
101
- {system: c, ...i} = a;
102
- return (c && (n = [{role: 'system', content: c}, ...n]), {messages: n, ...i});
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
- let s = Array.isArray(t) ? t.slice(-1)[0]?.content : t,
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
- l = ({options: t, extra_body: a, ...o}, s, r) => {
109
- let e = {...s.params, ...o},
110
- n = {...s.options, ...t};
111
- return (r === 'openai' ? (e.extra_body = {...n, ...a}) : (e.options = n), e);
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 y = {
116
+ var x = {
114
117
  openai: {
115
- API: t => new E({...t, fetch: x}),
118
+ API: t => new q({...t, fetch: y}),
116
119
  config: h,
117
120
  llm: t => ({
118
- chat: async (a, o = {}, s) => {
119
- let r = R,
120
- e = m(a, o, 'chat'),
121
- n = await t.chat.completions.create(l(e, h, 'openai'));
122
- return r(n, e.stream, s);
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 (a, o = {}, s) => {
125
- let r = _,
126
- e = m(a, o, 'responses'),
127
- n = await t.responses.create(l(e, h, 'openai'));
128
- return r(n, e.stream, s);
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: x}),
136
+ API: t => new T({...t, fetch: y}),
134
137
  config: g,
135
138
  llm: t => ({
136
- chat: async (a, o = {}, s) => {
137
- let r = w,
138
- e = m(a, o, 'chat'),
139
- n = await t.chat(l(e, g, 'ollama'));
140
- return r(n, e.stream, s);
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 (a, o = {}, s) => {
143
- let r = A,
144
- e = m(a, o, 'generate'),
145
- n = await t.generate(l(e, g, 'ollama'));
146
- return r(n, e.stream, s);
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 (a, o = {}, s) => {
149
- let r = d,
150
- e = m(a, o, 'responses'),
151
- n = await t.responses(l(e, g, 'ollama'));
152
- return r(n, e.stream, s);
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 K = (t = 'ollama', a) => {
158
- let o = y[t] ?? y.ollama,
159
- {API: s, config: r, llm: e} = o,
160
- n = s({...r.config, ...a});
161
- return e(n);
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
- Q = K;
164
- export {Q as default, K as startApi};
166
+ W = U;
167
+ export {W as default, U as startApi};