huxy-llm-api 1.0.7 → 1.0.8

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