huxy-llm-api 1.0.5 → 1.0.6

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