huxy-llm-api 1.0.6 → 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 +118 -115
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huxy-llm-api",
3
- "version": "1.0.6",
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,172 +1,175 @@
1
- import {Ollama as H} from 'ollama';
2
- import j from 'openai';
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
- 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: {temperature: 1, max_tokens: 4096, top_p: 0.95},
10
- options: {top_k: 20, repeat_penalty: 1.05},
11
- },
12
- L = M;
13
- var T = {
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: {},
16
- options: {temperature: 1, num_ctx: 4096, top_k: 20, top_p: 0.95, repeat_penalty: 1.05},
17
- },
18
- P = T;
19
- var b = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
20
- A = async (t, a, r) => {
21
- if (a) {
1
+ import {Ollama as q} from 'ollama';
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) {
22
14
  let o = '',
23
15
  n = '';
24
16
  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));
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
20
  return {content: o, reasoning: n};
29
21
  }
30
- return (r?.(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
- I = async (t, a, r) => {
33
- if (a) {
24
+ L = async (t, r, s) => {
25
+ if (r) {
34
26
  let e = '',
35
- s = '';
27
+ c = '';
36
28
  for await (let i of t) {
37
29
  let {delta: p} = i.choices?.[0] ?? {},
38
30
  {reasoning: f, content: u} = p ?? {};
39
- (f && (s += f), u && (e += u), r?.({content: e, reasoning: s}, i));
31
+ (f && (c += f), u && (e += u), s?.({content: e, reasoning: c}, i));
40
32
  }
41
- return {content: e, reasoning: s};
33
+ return {content: e, reasoning: c};
42
34
  }
43
- r?.(t);
44
- let {message: c} = t.choices?.[0] ?? {},
45
- {content: o, reasoning: n} = c;
35
+ s?.(t);
36
+ let {message: a} = t.choices?.[0] ?? {},
37
+ {content: o, reasoning: n} = a;
46
38
  return {content: o, reasoning: n};
47
39
  };
48
40
  var S = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
49
- C = async (t, a, r) => {
50
- if (a) {
41
+ I = async (t, r, s) => {
42
+ if (r) {
51
43
  let o = '',
52
44
  n = '';
53
45
  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));
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
49
  return {content: o, reasoning: n};
58
50
  }
59
- return (r?.(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
- O = async (t, a, r) => {
62
- if (a) {
53
+ O = async (t, r, s) => {
54
+ if (r) {
63
55
  let n = '',
64
56
  e = '';
65
- for await (let s of t) {
66
- let i = s.reasoning ?? s.thinking,
67
- p = s.content ?? s.response;
68
- (i && (e += i), p && (n += p), r?.({content: n, reasoning: e}, s));
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
62
  return {content: n, reasoning: e};
71
63
  }
72
- r?.(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
- k = async (t, a, r) => {
77
- if (a) {
68
+ b = async (t, r, s) => {
69
+ if (r) {
78
70
  let e = '',
79
- s = '';
71
+ c = '';
80
72
  for await (let i of t) {
81
73
  let {message: p} = i,
82
74
  f = p.reasoning ?? p.thinking,
83
75
  u = p.content ?? p.response;
84
- (f && (s += f), u && (e += u), r?.({content: e, reasoning: s}, i));
76
+ (f && (c += f), u && (e += u), s?.({content: e, reasoning: c}, i));
85
77
  }
86
- return {content: e, reasoning: s};
78
+ return {content: e, reasoning: c};
87
79
  }
88
- let {message: c} = t;
89
- r?.(t);
90
- let o = c.reasoning ?? c.thinking;
91
- return {content: c.content ?? c.response, reasoning: o};
80
+ let {message: a} = t;
81
+ 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};
92
110
  };
93
- var B = (t, a = {}, r = 'chat') => {
94
- if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
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});
100
- }
101
- if (r === 'responses') {
102
- let {instructions: o, system: n, ...e} = a;
103
- return (o || (e.instructions = n), {input: t, ...e});
104
- }
105
- return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...a};
106
- };
107
111
  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);
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);
114
117
  };
115
- var w = {
116
- openai: (t = {}, a = {}) => {
117
- let {config: r, params: c, options: o} = L,
118
+ var P = {
119
+ openai: (t = {}, r = {}) => {
120
+ let {config: s, params: a, options: o} = w,
118
121
  {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
+ 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');
123
126
  return {
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);
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);
129
132
  },
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);
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);
135
138
  },
136
139
  };
137
140
  },
138
- ollama: (t = {}, a = {}) => {
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}},
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}},
143
146
  f = R(p, 'ollama');
144
147
  return {
145
- chat: async (u, x = {}, g) => {
146
- let m = f(u, x, 'chat'),
147
- h = k,
148
+ chat: async (u, h = {}, g) => {
149
+ let m = f(u, h, 'chat'),
150
+ x = b,
148
151
  l = await n.chat(m);
149
- return h(l, m.stream, g);
152
+ return x(l, m.stream, g);
150
153
  },
151
- generate: async (u, x = {}, g) => {
152
- let m = f(u, x, 'generate'),
153
- h = O,
154
+ generate: async (u, h = {}, g) => {
155
+ let m = f(u, h, 'generate'),
156
+ x = O,
154
157
  l = await n.generate(m);
155
- return h(l, m.stream, g);
158
+ return x(l, m.stream, g);
156
159
  },
157
- responses: async (u, x = {}, g) => {
158
- let m = f(u, x, 'responses'),
159
- h = C,
160
+ responses: async (u, h = {}, g) => {
161
+ let m = f(u, h, 'responses'),
162
+ x = I,
160
163
  l = await n.responses(m);
161
- return h(l, m.stream, g);
164
+ return x(l, m.stream, g);
162
165
  },
163
166
  };
164
167
  },
165
168
  };
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);
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);
170
173
  },
171
- et = q;
172
- export {et as default, q as startApi};
174
+ st = D;
175
+ export {st as default, D as startApi};