huxy-llm-api 1.0.6 → 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 +129 -133
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.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,172 +1,168 @@
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},
1
+ import {Ollama as q} from 'ollama';
2
+ import z from 'openai';
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
+ );
11
22
  },
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) {
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) {
22
47
  let o = '',
23
48
  n = '';
24
49
  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));
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));
27
52
  }
28
53
  return {content: o, reasoning: n};
29
54
  }
30
- return (r?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
55
+ return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
31
56
  },
32
- I = async (t, a, r) => {
33
- if (a) {
57
+ y = async (t, c, s) => {
58
+ if (c) {
34
59
  let e = '',
35
- s = '';
60
+ a = '';
36
61
  for await (let i of t) {
37
62
  let {delta: p} = i.choices?.[0] ?? {},
38
63
  {reasoning: f, content: u} = p ?? {};
39
- (f && (s += f), u && (e += u), r?.({content: e, reasoning: s}, i));
64
+ (f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
40
65
  }
41
- return {content: e, reasoning: s};
66
+ return {content: e, reasoning: a};
42
67
  }
43
- r?.(t);
44
- let {message: c} = t.choices?.[0] ?? {},
45
- {content: o, reasoning: n} = c;
68
+ s?.(t);
69
+ let {message: r} = t.choices?.[0] ?? {},
70
+ {content: o, reasoning: n} = r;
46
71
  return {content: o, reasoning: n};
47
72
  };
48
- var S = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
49
- C = async (t, a, r) => {
50
- if (a) {
73
+ var B = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
74
+ d = async (t, c, s) => {
75
+ if (c) {
51
76
  let o = '',
52
77
  n = '';
53
78
  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));
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));
56
81
  }
57
82
  return {content: o, reasoning: n};
58
83
  }
59
- return (r?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
84
+ return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
60
85
  },
61
- O = async (t, a, r) => {
62
- if (a) {
86
+ _ = async (t, c, s) => {
87
+ if (c) {
63
88
  let n = '',
64
89
  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));
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));
69
94
  }
70
95
  return {content: n, reasoning: e};
71
96
  }
72
- r?.(t);
73
- let c = t.reasoning ?? t.thinking;
74
- return {content: t.content ?? t.response, reasoning: c};
97
+ s?.(t);
98
+ let r = t.reasoning ?? t.thinking;
99
+ return {content: t.content ?? t.response, reasoning: r};
75
100
  },
76
- k = async (t, a, r) => {
77
- if (a) {
101
+ R = async (t, c, s) => {
102
+ if (c) {
78
103
  let e = '',
79
- s = '';
104
+ a = '';
80
105
  for await (let i of t) {
81
106
  let {message: p} = i,
82
107
  f = p.reasoning ?? p.thinking,
83
108
  u = p.content ?? p.response;
84
- (f && (s += f), u && (e += u), r?.({content: e, reasoning: s}, i));
109
+ (f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
85
110
  }
86
- return {content: e, reasoning: s};
111
+ return {content: e, reasoning: a};
87
112
  }
88
- let {message: c} = t;
89
- r?.(t);
90
- let o = c.reasoning ?? c.thinking;
91
- return {content: c.content ?? c.response, reasoning: o};
113
+ let {message: r} = t;
114
+ s?.(t);
115
+ let o = r.reasoning ?? r.thinking;
116
+ return {content: r.content ?? r.response, reasoning: o};
92
117
  };
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
- 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);
114
- };
115
- var w = {
116
- openai: (t = {}, a = {}) => {
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');
123
- 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);
129
- },
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);
135
- },
136
- };
137
- },
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}},
143
- f = R(p, 'ollama');
144
- return {
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);
150
- },
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);
156
- },
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);
162
- },
163
- };
164
- },
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
+ }),
165
153
  };
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);
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);
170
166
  },
171
- et = q;
172
- export {et as default, q as startApi};
167
+ st = G;
168
+ export {st as default, G as startApi};