huxy-llm-api 1.0.5 → 1.0.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +140 -132
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huxy-llm-api",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
5
5
  "type": "module",
6
6
  "module": "./src/index.js",
package/src/index.js CHANGED
@@ -1,167 +1,175 @@
1
- import {Ollama as H} from 'ollama';
2
- import j from 'openai';
3
- import {fetch as K, Agent as M} from 'undici';
1
+ import {Ollama as q} from 'ollama';
2
+ import z from 'openai';
3
+ import {fetch as E, Agent as K} from 'undici';
4
4
  var U = 300 * 60 * 1e3,
5
- E = (t, a) => K(t, {...a, dispatcher: new M({headersTimeout: U})}),
6
- R = 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},
11
- },
12
- P = T;
13
- var b = {
14
- config: {apiKey: process.env.OLLM_API_KEY || 'ah.yiru@gmail.com', host: process.env.OLLM_API_HOST || 'http://127.0.0.1:11434'},
15
- params: {model: 'qwen3-vl:latest', keep_alive: -1},
16
- options: {temperature: 1, num_ctx: 4096, top_k: 20, top_p: 0.95, repeat_penalty: 1.05},
17
- },
18
- A = b;
19
- var q = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
20
- I = async (t, a, o) => {
21
- if (a) {
22
- let r = '',
23
- e = '';
24
- for await (let n of t) {
25
- let {type: s, delta: i} = n;
26
- (q.includes(s) && (e += i), s === 'response.output_text.delta' && (r += i), o?.({content: r, reasoning: e}, n));
5
+ k = (t, r) => E(t, {...r, dispatcher: new K({headersTimeout: U})}),
6
+ _ = k;
7
+ var v = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
8
+ w = v;
9
+ var M = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
10
+ A = M;
11
+ var T = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
12
+ C = async (t, r, s) => {
13
+ if (r) {
14
+ let o = '',
15
+ n = '';
16
+ for await (let e of t) {
17
+ let {type: c, delta: i} = e;
18
+ (T.includes(c) && (n += i), c === 'response.output_text.delta' && (o += i), s?.({content: o, reasoning: n}, e));
27
19
  }
28
- return {content: r, reasoning: e};
20
+ return {content: o, reasoning: n};
29
21
  }
30
- return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
22
+ return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
31
23
  },
32
- C = async (t, a, o) => {
33
- if (a) {
34
- let n = '',
35
- s = '';
24
+ L = async (t, r, s) => {
25
+ if (r) {
26
+ let e = '',
27
+ c = '';
36
28
  for await (let i of t) {
37
29
  let {delta: p} = i.choices?.[0] ?? {},
38
- {reasoning: l, content: u} = p ?? {};
39
- (l && (s += l), u && (n += u), o?.({content: n, reasoning: s}, i));
30
+ {reasoning: f, content: u} = p ?? {};
31
+ (f && (c += f), u && (e += u), s?.({content: e, reasoning: c}, i));
40
32
  }
41
- return {content: n, reasoning: s};
33
+ return {content: e, reasoning: c};
42
34
  }
43
- o?.(t);
44
- let {message: c} = t.choices?.[0] ?? {},
45
- {content: r, reasoning: e} = c;
46
- return {content: r, reasoning: e};
35
+ s?.(t);
36
+ let {message: a} = t.choices?.[0] ?? {},
37
+ {content: o, reasoning: n} = a;
38
+ return {content: o, reasoning: n};
47
39
  };
48
40
  var S = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
49
- O = async (t, a, o) => {
50
- if (a) {
51
- let r = '',
52
- e = '';
53
- for await (let n of t) {
54
- let {type: s, delta: i} = n;
55
- (S.includes(s) && (e += i), s === 'response.output_text.delta' && (r += i), o?.({content: r, reasoning: e}, n));
41
+ I = async (t, r, s) => {
42
+ if (r) {
43
+ let o = '',
44
+ n = '';
45
+ for await (let e of t) {
46
+ let {type: c, delta: i} = e;
47
+ (S.includes(c) && (n += i), c === 'response.output_text.delta' && (o += i), s?.({content: o, reasoning: n}, e));
56
48
  }
57
- return {content: r, reasoning: e};
49
+ return {content: o, reasoning: n};
58
50
  }
59
- return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
51
+ return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
60
52
  },
61
- k = async (t, a, o) => {
62
- if (a) {
63
- let e = '',
64
- n = '';
65
- for await (let s of t) {
66
- let i = s.reasoning ?? s.thinking,
67
- p = s.content ?? s.response;
68
- (i && (n += i), p && (e += p), o?.({content: e, reasoning: n}, s));
53
+ O = async (t, r, s) => {
54
+ if (r) {
55
+ let n = '',
56
+ e = '';
57
+ for await (let c of t) {
58
+ let i = c.reasoning ?? c.thinking,
59
+ p = c.content ?? c.response;
60
+ (i && (e += i), p && (n += p), s?.({content: n, reasoning: e}, c));
69
61
  }
70
- return {content: e, reasoning: n};
62
+ return {content: n, reasoning: e};
71
63
  }
72
- o?.(t);
73
- let c = t.reasoning ?? t.thinking;
74
- return {content: t.content ?? t.response, reasoning: c};
64
+ s?.(t);
65
+ let a = t.reasoning ?? t.thinking;
66
+ return {content: t.content ?? t.response, reasoning: a};
75
67
  },
76
- v = async (t, a, o) => {
77
- if (a) {
78
- let n = '',
79
- s = '';
68
+ b = async (t, r, s) => {
69
+ if (r) {
70
+ let e = '',
71
+ c = '';
80
72
  for await (let i of t) {
81
73
  let {message: p} = i,
82
- l = p.reasoning ?? p.thinking,
74
+ f = p.reasoning ?? p.thinking,
83
75
  u = p.content ?? p.response;
84
- (l && (s += l), u && (n += u), o?.({content: n, reasoning: s}, i));
76
+ (f && (c += f), u && (e += u), s?.({content: e, reasoning: c}, i));
85
77
  }
86
- return {content: n, reasoning: s};
78
+ return {content: e, reasoning: c};
87
79
  }
88
- let {message: c} = t;
89
- o?.(t);
90
- let r = c.reasoning ?? c.thinking;
91
- return {content: c.content ?? c.response, reasoning: r};
80
+ let {message: a} = t;
81
+ s?.(t);
82
+ let o = a.reasoning ?? a.thinking;
83
+ return {content: a.content ?? a.response, reasoning: o};
92
84
  };
93
- var B = (t, a = {}, o = 'chat') => {
94
- if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
95
- if (o === 'chat') {
96
- let r = Array.isArray(t) ? t : [{role: 'user', content: t}],
97
- {system: e, ...n} = a;
98
- return (e && (r = [{role: 'system', content: e}, ...r]), {messages: r, ...n});
99
- }
100
- if (o === 'responses') {
101
- let {instructions: r, system: e, ...n} = a;
102
- return (r || (n.instructions = e), {input: t, ...n});
103
- }
104
- return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...a};
105
- };
106
- var w =
107
- ({params: t, options: a} = {}, o) =>
108
- (c, r = {}, e) => {
109
- let {options: n, ...s} = r,
110
- i = B(c, {...t, ...s}, e),
111
- p = {...a, ...n};
112
- return (o === 'openai' ? (i.extra_body = p) : (i.options = p), i);
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});
108
+ }
109
+ return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...r};
113
110
  };
114
- var L = {
115
- openai: (t = {}, a = {}) => {
116
- let {config: o, params: c, options: r} = P,
117
- {host: e, baseURL: n, ...s} = t,
118
- i = new j({fetch: R, ...o, ...s, baseURL: e || n}),
119
- {options: p, extra_body: l, ...u} = a,
120
- h = {params: {...c, ...u}, options: {...r, ...l, ...p}},
121
- x = w(h, 'openai');
111
+ var R =
112
+ ({params: t, options: r} = {}, s) =>
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);
117
+ };
118
+ var P = {
119
+ openai: (t = {}, r = {}) => {
120
+ let {config: s, params: a, options: o} = w,
121
+ {host: n, ...e} = t,
122
+ c = new z({fetch: _, ...s, ...e, baseURL: n}),
123
+ {options: i, extra_body: p, ...f} = r,
124
+ u = {params: {...a, ...f}, options: {...o, ...p, ...i}},
125
+ h = R(u, 'openai');
122
126
  return {
123
- chat: async (m, f = {}, g) => {
124
- let y = x(m, f, 'chat'),
125
- _ = C,
126
- d = await i.chat.completions.create(y);
127
- return _(d, y.stream, g);
127
+ chat: async (g, m = {}, x) => {
128
+ let l = h(g, m, 'chat'),
129
+ y = L,
130
+ d = await c.chat.completions.create(l);
131
+ return y(d, l.stream, x);
128
132
  },
129
- responses: async (m, f = {}, g) => {
130
- let y = x(m, f, 'responses'),
131
- _ = I,
132
- d = await i.responses.create(y);
133
- return _(d, y.stream, g);
133
+ responses: async (g, m = {}, x) => {
134
+ let l = h(g, m, 'responses'),
135
+ y = C,
136
+ d = await c.responses.create(l);
137
+ return y(d, l.stream, x);
134
138
  },
135
139
  };
136
140
  },
137
- ollama: (t = {}, a = {}) => {
138
- let {config: o, params: c, options: r} = A,
139
- e = new H({fetch: R, ...o, ...t}),
140
- {options: n, extra_body: s, ...i} = a,
141
- p = {params: {...c, ...i}, options: {...r, ...s, ...n}},
142
- l = w(p, 'ollama');
141
+ ollama: (t = {}, r = {}) => {
142
+ let {config: s, params: a, options: o} = A,
143
+ n = new q({fetch: _, ...s, ...t}),
144
+ {options: e, extra_body: c, ...i} = r,
145
+ p = {params: {...a, ...i}, options: {...o, ...c, ...e}},
146
+ f = R(p, 'ollama');
143
147
  return {
144
- chat: async (u, h = {}, x) => {
145
- let m = l(u, h, 'chat'),
146
- f = v,
147
- g = await e.chat(m);
148
- return f(g, m.stream, x);
148
+ chat: async (u, h = {}, g) => {
149
+ let m = f(u, h, 'chat'),
150
+ x = b,
151
+ l = await n.chat(m);
152
+ return x(l, m.stream, g);
149
153
  },
150
- generate: async (u, h = {}, x) => {
151
- let m = l(u, h, 'generate'),
152
- f = k,
153
- g = await e.generate(m);
154
- return f(g, m.stream, x);
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);
155
159
  },
156
- responses: async (u, h = {}, x) => {
157
- let m = l(u, h, 'responses'),
158
- f = O,
159
- g = await e.responses(m);
160
- return f(g, m.stream, x);
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);
161
165
  },
162
166
  };
163
167
  },
164
168
  };
165
- var z = (t = 'ollama', a, o) => (L[t] ?? L.ollama)(a, o),
166
- ot = z;
167
- export {ot as default, z as startApi};
169
+ var D = (t = 'ollama', r = {}, s = {}) => {
170
+ let {baseURL: a, host: o, ...n} = r;
171
+ if (((n.host = o || a), !n.host)) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
172
+ return (P[t] ?? P.ollama)(n, s);
173
+ },
174
+ st = D;
175
+ export {st as default, D as startApi};