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 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
- - `responses(prompt, configs, callback)`: 结构化响应
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: 'devstral-small-2',
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huxy-llm-api",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.1",
4
4
  "description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
5
5
  "type": "module",
6
6
  "module": "./src/index.js",
package/src/index.js CHANGED
@@ -1,164 +1,162 @@
1
- import {Ollama as T} from 'ollama';
2
- import E from 'openai';
3
- import {fetch as I, Agent as L} from 'undici';
4
- var P = 300 * 60 * 1e3,
5
- k = (t, a) => I(t, {...a, dispatcher: new L({headersTimeout: P})}),
6
- x = k;
7
- var v = {
8
- config: {
9
- apiKey: process.env.LLM_API_KEY || 'ah.yiru@gmail.com',
10
- baseURL: process.env.LLM_API_BASEURL || 'http://192.168.0.111:11434/v1',
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
- h = v;
18
- var C = {
19
- config: {apiKey: process.env.OLLM_API_KEY || 'ah.yiru@gmail.com', host: process.env.OLLM_API_HOST || 'http://192.168.0.111:11434'},
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: 0.6, num_ctx: 4096, top_k: 20, top_p: 0.9, repeat_penalty: 1.15},
16
+ options: {temperature: 1, num_ctx: 4096, top_k: 20, top_p: 0.95, repeat_penalty: 1.05},
22
17
  },
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));
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: r, reasoning: e};
28
+ return {content: s, reasoning: n};
33
29
  }
34
- let [s = {}] = t.output ?? [];
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
- 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));
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: n, reasoning: c};
41
+ return {content: e, reasoning: a};
47
42
  }
48
- let {message: s} = t.choices?.[0] ?? {},
49
- {content: r, reasoning: e} = s;
50
- return (o?.(t), {content: r, reasoning: e});
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 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));
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: r, reasoning: e};
57
+ return {content: s, reasoning: n};
61
58
  }
62
- let [s = {}] = t.output ?? [];
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
- A = async (t, a, o) => {
66
- if (a) {
67
- let e = '',
68
- 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));
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: e, reasoning: n};
70
+ return {content: n, reasoning: e};
75
71
  }
76
- let s = t.reasoning ?? t.thinking,
77
- r = t.content ?? t.response;
78
- return (o?.(t), {content: r, reasoning: s});
72
+ o?.(t);
73
+ let i = t.reasoning ?? t.thinking;
74
+ return {content: t.content ?? t.response, reasoning: i};
79
75
  },
80
- w = async (t, a, o) => {
81
- if (a) {
82
- let n = '',
83
- c = '';
84
- for await (let i of t) {
85
- let {message: p} = i,
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
- f = p.content ?? p.response;
88
- (u && (c += u), f && (n += f), o?.({content: n, reasoning: c}, i));
83
+ m = p.content ?? p.response;
84
+ (u && (a += u), m && (e += m), o?.({content: e, reasoning: a}, c));
89
85
  }
90
- return {content: n, reasoning: c};
86
+ return {content: e, reasoning: a};
91
87
  }
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});
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 m = (t, a = {}, o = 'chat') => {
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 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});
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
- 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});
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
- 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);
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 y = {
114
- openai: {
115
- API: t => new E({...t, fetch: x}),
116
- config: h,
117
- 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);
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 (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);
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
- API: t => new T({...t, fetch: x}),
134
- config: g,
135
- 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);
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 (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 (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 (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 (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 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);
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};