huxy-llm-api 1.0.2 → 1.0.4

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