huxy-llm-api 1.1.2 → 1.1.3
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/package.json +3 -3
- package/src/index.js +46 -43
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "huxy-llm-api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.js",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"ollama": "^0.6.3",
|
|
38
|
-
"openai": "^6.
|
|
39
|
-
"undici": "^
|
|
38
|
+
"openai": "^6.42.0",
|
|
39
|
+
"undici": "^8.4.1"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=20.0.0"
|
package/src/index.js
CHANGED
|
@@ -5,7 +5,7 @@ var U = (t, o) => {
|
|
|
5
5
|
import {Ollama as rt} from 'ollama';
|
|
6
6
|
import at from 'openai';
|
|
7
7
|
import {fetch as F, Agent as M} from 'undici';
|
|
8
|
-
var T =
|
|
8
|
+
var T = 3600 * 1e3,
|
|
9
9
|
z = t => (o, e) => F(o, {...e, dispatcher: new M({headersTimeout: T, ...t})}),
|
|
10
10
|
y = z;
|
|
11
11
|
var N = {config: {apiKey: process.env.OLLM_API_KEY || 'ollm_key', host: process.env.OLLM_API_HOST}, params: {}, options: {}},
|
|
@@ -41,68 +41,71 @@ var Y = ['temperature', 'seed', 'stop', 'top_p'],
|
|
|
41
41
|
var R =
|
|
42
42
|
({params: t, options: o} = {}, e) =>
|
|
43
43
|
(s, a = {}, n) => {
|
|
44
|
-
let {options: r, extra_body: i, ...
|
|
45
|
-
|
|
46
|
-
return ((
|
|
44
|
+
let {options: r, extra_body: i, ...p} = a,
|
|
45
|
+
c = V(s, {...t, ...p}, n);
|
|
46
|
+
return ((c.options = {...o, ...i, ...r}), e === 'openai' ? H(c) : c);
|
|
47
47
|
};
|
|
48
48
|
var W = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
49
49
|
A = async (t, o, e) => {
|
|
50
50
|
if (o) {
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
for await (let
|
|
54
|
-
let {type:
|
|
55
|
-
(W.includes(
|
|
51
|
+
let r = '',
|
|
52
|
+
i = '';
|
|
53
|
+
for await (let p of t) {
|
|
54
|
+
let {type: c, delta: m} = p;
|
|
55
|
+
(W.includes(c) && (i += m), c === 'response.output_text.delta' && (r += m), e?.({content: r, reasoning: i}, p));
|
|
56
56
|
}
|
|
57
|
-
return {content:
|
|
57
|
+
return {content: r, reasoning: i};
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
let a = (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text,
|
|
60
|
+
n = t.output_text;
|
|
61
|
+
return (e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
60
62
|
},
|
|
61
63
|
b = async (t, o, e) => {
|
|
62
64
|
if (o) {
|
|
63
65
|
let r = '',
|
|
64
66
|
i = '';
|
|
65
|
-
for await (let
|
|
66
|
-
let {delta:
|
|
67
|
-
{reasoning: m, content: f} =
|
|
68
|
-
(m && (i += m), f && (r += f), e?.({content: r, reasoning: i},
|
|
67
|
+
for await (let p of t) {
|
|
68
|
+
let {delta: c} = p.choices?.[0] ?? {},
|
|
69
|
+
{reasoning: m, content: f} = c ?? {};
|
|
70
|
+
(m && (i += m), f && (r += f), e?.({content: r, reasoning: i}, p));
|
|
69
71
|
}
|
|
70
72
|
return {content: r, reasoning: i};
|
|
71
73
|
}
|
|
72
|
-
e?.(t);
|
|
73
74
|
let {message: s} = t.choices?.[0] ?? {},
|
|
74
75
|
{content: a, reasoning: n} = s;
|
|
75
|
-
return {content: a, reasoning: n};
|
|
76
|
+
return (e?.({content: a, reasoning: n}, t), {content: a, reasoning: n});
|
|
76
77
|
};
|
|
77
78
|
var d = {};
|
|
78
79
|
U(d, {chat: () => u, default: () => Q, generate: () => G, image: () => J, responses: () => x});
|
|
79
80
|
var D = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta'],
|
|
80
81
|
x = async (t, o, e) => {
|
|
81
82
|
if (o) {
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
for await (let
|
|
85
|
-
let {type:
|
|
86
|
-
(D.includes(
|
|
83
|
+
let r = '',
|
|
84
|
+
i = '';
|
|
85
|
+
for await (let p of t) {
|
|
86
|
+
let {type: c, delta: m} = p;
|
|
87
|
+
(D.includes(c) && (i += m), c === 'response.output_text.delta' && (r += m), e?.({content: r, reasoning: i}, p));
|
|
87
88
|
}
|
|
88
|
-
return {content:
|
|
89
|
+
return {content: r, reasoning: i};
|
|
89
90
|
}
|
|
90
|
-
|
|
91
|
+
let a = (t.output?.[0]?.content ?? t.output?.[0]?.summary)?.[0]?.text,
|
|
92
|
+
n = t.output_text;
|
|
93
|
+
return (e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
91
94
|
},
|
|
92
95
|
G = async (t, o, e) => {
|
|
93
96
|
if (o) {
|
|
94
97
|
let n = '',
|
|
95
98
|
r = '';
|
|
96
99
|
for await (let i of t) {
|
|
97
|
-
let
|
|
98
|
-
|
|
99
|
-
(
|
|
100
|
+
let p = i.reasoning ?? i.thinking,
|
|
101
|
+
c = i.content ?? i.response;
|
|
102
|
+
(p && (r += p), c && (n += c), e?.({content: n, reasoning: r}, i));
|
|
100
103
|
}
|
|
101
104
|
return {content: n, reasoning: r};
|
|
102
105
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return {content:
|
|
106
|
+
let s = t.reasoning ?? t.thinking,
|
|
107
|
+
a = t.content ?? t.response;
|
|
108
|
+
return (e?.({content: a, reasoning: s}, t), {content: a, reasoning: s});
|
|
106
109
|
},
|
|
107
110
|
J = async (t, o, e) => {
|
|
108
111
|
for await (let s of t) e?.(s);
|
|
@@ -111,18 +114,18 @@ var D = ['response.reasoning_text.delta', 'response.reasoning_summary_text.delta
|
|
|
111
114
|
if (o) {
|
|
112
115
|
let r = '',
|
|
113
116
|
i = '';
|
|
114
|
-
for await (let
|
|
115
|
-
let {message:
|
|
116
|
-
m =
|
|
117
|
-
f =
|
|
118
|
-
(m && (i += m), f && (r += f), e?.({content: r, reasoning: i},
|
|
117
|
+
for await (let p of t) {
|
|
118
|
+
let {message: c} = p,
|
|
119
|
+
m = c.reasoning ?? c.thinking,
|
|
120
|
+
f = c.content ?? c.response;
|
|
121
|
+
(m && (i += m), f && (r += f), e?.({content: r, reasoning: i}, p));
|
|
119
122
|
}
|
|
120
123
|
return {content: r, reasoning: i};
|
|
121
124
|
}
|
|
122
|
-
let {message: s} = t
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return {content:
|
|
125
|
+
let {message: s} = t,
|
|
126
|
+
a = s.reasoning ?? s.thinking,
|
|
127
|
+
n = s.content ?? s.response;
|
|
128
|
+
return (e?.({content: n, reasoning: a}, t), {content: n, reasoning: a});
|
|
126
129
|
},
|
|
127
130
|
Q = u;
|
|
128
131
|
import C from 'node:fs/promises';
|
|
@@ -205,12 +208,12 @@ var it = {
|
|
|
205
208
|
ct = (t = 'ollama', o = {}, e = {}) => {
|
|
206
209
|
t = ['ollama', 'openai'].includes(t) ? t : 'ollama';
|
|
207
210
|
let {hostKey: s, envConfig: a, API: n} = it[t],
|
|
208
|
-
{config: r, params: i, options:
|
|
209
|
-
{baseURL:
|
|
210
|
-
if (((l[s] = m ||
|
|
211
|
+
{config: r, params: i, options: p} = a,
|
|
212
|
+
{baseURL: c, host: m, dispatcher: f, ...l} = {...r, ...o};
|
|
213
|
+
if (((l[s] = m || c), !l[s])) throw Error('\u8BF7\u914D\u7F6E\u5927\u6A21\u578B API \u5730\u5740 host/baseURL !');
|
|
211
214
|
let P = n({fetch: y(f), ...l}),
|
|
212
215
|
{options: v, extra_body: K, ...$} = e,
|
|
213
|
-
j = {params: {...i, ...$}, options: {...
|
|
216
|
+
j = {params: {...i, ...$}, options: {...p, ...K, ...v}},
|
|
214
217
|
k = R(j, t);
|
|
215
218
|
return E[t](P, k);
|
|
216
219
|
},
|