huxy-llm-api 1.0.8 → 1.0.9

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.
package/README.md CHANGED
@@ -32,6 +32,9 @@ import startApi from 'huxy-llm-api';
32
32
  const ollamaApi = startApi('ollama', {
33
33
  apiKey: 'your-api-key',
34
34
  host: 'http://localhost:11434',
35
+ dispatcher: {
36
+ headersTimeout: 10 * 60 * 1000,
37
+ },
35
38
  }, {
36
39
  model: 'qwen3-vl:latest',
37
40
  options: {
@@ -89,7 +92,8 @@ console.log('对话结果:', response);
89
92
  **参数:**
90
93
 
91
94
  - `apiType`: `'ollama'` 或 `'openai'` - 指定要使用的 API 类型
92
- - `userConfig`: 对象 - 自定义配置,会覆盖默认配置
95
+ - `userConfig`: 对象 - 自定义 API 接口配置,如 apiKey、baseURL、fetch 等
96
+ - `userOption`: 对象 - 通用模型参数配置
93
97
 
94
98
  **返回:** API 客户端实例,包含以下方法:
95
99
 
@@ -126,8 +130,8 @@ console.log('对话结果:', response);
126
130
 
127
131
  ```javascript
128
132
  {
129
- apiKey: process.env.OLLM_API_KEY || 'huxy@zys',
130
- host: process.env.OLLM_API_HOST || 'http://192.168.0.111:11434',
133
+ apiKey: process.env.OLLM_API_KEY || '1234',
134
+ host: process.env.OLLM_API_HOST || 'http://localhost:11434',
131
135
  params: {
132
136
  model: 'qwen3-vl:latest',
133
137
  keep_alive: -1,
@@ -146,8 +150,8 @@ console.log('对话结果:', response);
146
150
 
147
151
  ```javascript
148
152
  {
149
- apiKey: process.env.LLM_API_KEY || 'huxy@zys',
150
- baseURL: process.env.LLM_API_BASEURL || 'http://192.168.0.111:11434/v1',
153
+ apiKey: process.env.LLM_API_KEY || '1234',
154
+ baseURL: process.env.LLM_API_BASEURL || 'http://localhost:11434/v1',
151
155
  timeout: 3 * 60 * 60 * 1000,
152
156
  maxRetries: 3,
153
157
  params: {
package/example.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import startApi from './index.js';
2
2
 
3
3
  const ollamaApi = startApi('ollama', {
4
- apiKey: '123',
4
+ apiKey: '1234',
5
5
  host: 'http://192.168.0.111:11434',
6
6
  // headers
7
7
  // fetch
8
8
  });
9
9
 
10
10
  const openaiApi = startApi('openai', {
11
- apiKey: '123',
11
+ apiKey: '1234',
12
12
  baseURL: 'http://192.168.0.111:11434/v1',
13
13
  // headers
14
14
  // fetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huxy-llm-api",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
5
5
  "type": "module",
6
6
  "module": "./src/index.js",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "ollama": "^0.6.3",
38
- "openai": "^6.15.0",
38
+ "openai": "^6.16.0",
39
39
  "undici": "^7.18.2"
40
40
  },
41
41
  "engines": {
package/src/index.js CHANGED
@@ -1,168 +1,168 @@
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: {}},
1
+ import {Ollama as z} from 'ollama';
2
+ import D from 'openai';
3
+ import {fetch as v, Agent as b} from 'undici';
4
+ var E = 300 * 60 * 1e3,
5
+ k = t => (c, o) => v(c, {...o, dispatcher: new b({headersTimeout: E, ...t})}),
8
6
  l = k;
9
- var U = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
7
+ var U = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
10
8
  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;
9
+ var M = {config: {apiKey: process.env.LLM_API_KEY || 'llm_key', baseURL: process.env.LLM_API_BASEURL}, params: {}, options: {}},
10
+ x = M;
11
+ var F = ['temperature', 'seed', 'stop', 'top_p'],
12
+ S = t => {
13
+ let {max_tokens: c, options: o = {}, ...r} = t,
14
+ {num_ctx: s, ...n} = o;
15
15
  return (
16
- (r.max_tokens = c ?? o),
16
+ (r.max_tokens = c ?? s),
17
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]));
18
+ F.includes(e) ? (r[e] = n[e]) : (r.extra_body || (r.extra_body = {}), (r.extra_body[e] = n[e]));
19
19
  }),
20
20
  r
21
21
  );
22
22
  },
23
- S = (t, c = {}, s = 'chat') => {
23
+ Y = (t, c = {}, o = 'chat') => {
24
24
  if (!t) throw Error('\u8BF7\u4F20\u5165\u4F60\u7684 prompt !');
25
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}],
26
+ if (o === 'chat') {
27
+ let s = Array.isArray(t) ? t : [{role: 'user', content: t}],
28
28
  {system: n, ...e} = c;
29
- return (n && (o = [{role: 'system', content: n}, ...o]), {messages: o, ...e});
29
+ return (n && (s = [{role: 'system', content: n}, ...s]), {messages: s, ...e});
30
30
  }
31
- if (s === 'responses') {
32
- let {instructions: o, system: n, ...e} = c;
33
- return (o || (e.instructions = n), {input: t, ...e});
31
+ if (o === 'responses') {
32
+ let {instructions: s, system: n, ...e} = c;
33
+ return (s || (e.instructions = n), {input: t, ...e});
34
34
  }
35
35
  return {prompt: Array.isArray(t) ? t.slice(-1)[0]?.content : t, ...c};
36
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);
37
+ var h =
38
+ ({params: t, options: c} = {}, o) =>
39
+ (r, s = {}, n) => {
40
+ let {options: e, extra_body: a, ...i} = s,
41
+ p = Y(r, {...t, ...i}, n);
42
+ return ((p.options = {...c, ...a, ...e}), o === 'openai' ? S(p) : p);
43
43
  };
44
- var Y = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
45
- h = async (t, c, s) => {
44
+ var j = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
45
+ y = async (t, c, o) => {
46
46
  if (c) {
47
- let o = '',
47
+ let s = '',
48
48
  n = '';
49
49
  for await (let e of t) {
50
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));
51
+ (j.includes(a) && (n += i), a === 'response.output_text.delta' && (s += i), o?.({content: s, reasoning: n}, e));
52
52
  }
53
- return {content: o, reasoning: n};
53
+ return {content: s, reasoning: n};
54
54
  }
55
- return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
55
+ return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
56
56
  },
57
- y = async (t, c, s) => {
57
+ d = async (t, c, o) => {
58
58
  if (c) {
59
59
  let e = '',
60
60
  a = '';
61
61
  for await (let i of t) {
62
62
  let {delta: p} = i.choices?.[0] ?? {},
63
- {reasoning: f, content: u} = p ?? {};
64
- (f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
63
+ {reasoning: u, content: f} = p ?? {};
64
+ (u && (a += u), f && (e += f), o?.({content: e, reasoning: a}, i));
65
65
  }
66
66
  return {content: e, reasoning: a};
67
67
  }
68
- s?.(t);
68
+ o?.(t);
69
69
  let {message: r} = t.choices?.[0] ?? {},
70
- {content: o, reasoning: n} = r;
71
- return {content: o, reasoning: n};
70
+ {content: s, reasoning: n} = r;
71
+ return {content: s, reasoning: n};
72
72
  };
73
- var B = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
74
- d = async (t, c, s) => {
73
+ var H = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
74
+ _ = async (t, c, o) => {
75
75
  if (c) {
76
- let o = '',
76
+ let s = '',
77
77
  n = '';
78
78
  for await (let e of t) {
79
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));
80
+ (H.includes(a) && (n += i), a === 'response.output_text.delta' && (s += i), o?.({content: s, reasoning: n}, e));
81
81
  }
82
- return {content: o, reasoning: n};
82
+ return {content: s, reasoning: n};
83
83
  }
84
- return (s?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
84
+ return (o?.(t), {reasoning: (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text, content: t.output_text});
85
85
  },
86
- _ = async (t, c, s) => {
86
+ R = async (t, c, o) => {
87
87
  if (c) {
88
88
  let n = '',
89
89
  e = '';
90
90
  for await (let a of t) {
91
91
  let i = a.reasoning ?? a.thinking,
92
92
  p = a.content ?? a.response;
93
- (i && (e += i), p && (n += p), s?.({content: n, reasoning: e}, a));
93
+ (i && (e += i), p && (n += p), o?.({content: n, reasoning: e}, a));
94
94
  }
95
95
  return {content: n, reasoning: e};
96
96
  }
97
- s?.(t);
97
+ o?.(t);
98
98
  let r = t.reasoning ?? t.thinking;
99
99
  return {content: t.content ?? t.response, reasoning: r};
100
100
  },
101
- R = async (t, c, s) => {
101
+ A = async (t, c, o) => {
102
102
  if (c) {
103
103
  let e = '',
104
104
  a = '';
105
105
  for await (let i of t) {
106
106
  let {message: p} = i,
107
- f = p.reasoning ?? p.thinking,
108
- u = p.content ?? p.response;
109
- (f && (a += f), u && (e += u), s?.({content: e, reasoning: a}, i));
107
+ u = p.reasoning ?? p.thinking,
108
+ f = p.content ?? p.response;
109
+ (u && (a += u), f && (e += f), o?.({content: e, reasoning: a}, i));
110
110
  }
111
111
  return {content: e, reasoning: a};
112
112
  }
113
113
  let {message: r} = t;
114
- s?.(t);
115
- let o = r.reasoning ?? r.thinking;
116
- return {content: r.content ?? r.response, reasoning: o};
114
+ o?.(t);
115
+ let s = r.reasoning ?? r.thinking;
116
+ return {content: r.content ?? r.response, reasoning: s};
117
117
  };
118
- var A = {
118
+ var w = {
119
119
  openai: (t, c) => ({
120
- chat: async (s, r = {}, o) => {
121
- let n = c(s, r, 'chat'),
122
- e = y,
120
+ chat: async (o, r = {}, s) => {
121
+ let n = c(o, r, 'chat'),
122
+ e = d,
123
123
  a = await t.chat.completions.create(n);
124
- return e(a, n.stream, o);
124
+ return e(a, n.stream, s);
125
125
  },
126
- responses: async (s, r = {}, o) => {
127
- let n = c(s, r, 'responses'),
128
- e = h,
126
+ responses: async (o, r = {}, s) => {
127
+ let n = c(o, r, 'responses'),
128
+ e = y,
129
129
  a = await t.responses.create(n);
130
- return e(a, n.stream, o);
130
+ return e(a, n.stream, s);
131
131
  },
132
132
  }),
133
133
  ollama: (t, c) => ({
134
- chat: async (s, r = {}, o) => {
135
- let n = c(s, r, 'chat'),
136
- e = R,
134
+ chat: async (o, r = {}, s) => {
135
+ let n = c(o, r, 'chat'),
136
+ e = A,
137
137
  a = await t.chat(n);
138
- return e(a, n.stream, o);
138
+ return e(a, n.stream, s);
139
139
  },
140
- generate: async (s, r = {}, o) => {
141
- let n = c(s, r, 'generate'),
142
- e = _,
140
+ generate: async (o, r = {}, s) => {
141
+ let n = c(o, r, 'generate'),
142
+ e = R,
143
143
  a = await t.generate(n);
144
- return e(a, n.stream, o);
144
+ return e(a, n.stream, s);
145
145
  },
146
- responses: async (s, r = {}, o) => {
147
- let n = c(s, r, 'responses'),
148
- e = d,
146
+ responses: async (o, r = {}, s) => {
147
+ let n = c(o, r, 'responses'),
148
+ e = _,
149
149
  a = await t.responses(n);
150
- return e(a, n.stream, o);
150
+ return e(a, n.stream, s);
151
151
  },
152
152
  }),
153
153
  };
154
- var D = {ollama: {hostKey: 'host', envConfig: l, API: q}, openai: {hostKey: 'baseURL', envConfig: g, API: z}},
155
- G = (t = 'ollama', c = {}, s = {}) => {
154
+ var G = {ollama: {hostKey: 'host', envConfig: g, API: z}, openai: {hostKey: 'baseURL', envConfig: x, API: D}},
155
+ J = (t = 'ollama', c = {}, o = {}) => {
156
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);
157
+ let {hostKey: r, envConfig: s, API: n} = G[t],
158
+ {config: e, params: a, options: i} = s,
159
+ {baseURL: p, host: u, dispatcher: f, ...m} = {...e, ...c};
160
+ if (((m[r] = u || p), !m[r])) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
161
+ let I = new n({fetch: l(f), ...m}),
162
+ {options: P, extra_body: C, ...L} = o,
163
+ O = {params: {...a, ...L}, options: {...i, ...C, ...P}},
164
+ K = h(O, t);
165
+ return w[t](I, K);
166
166
  },
167
- st = G;
168
- export {st as default, G as startApi};
167
+ rt = J;
168
+ export {rt as default, J as startApi};