utilitas 2000.3.48 → 2000.3.49
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/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/manifest.mjs +1 -1
- package/lib/rag.mjs +46 -7
- package/package.json +1 -1
package/lib/manifest.mjs
CHANGED
package/lib/rag.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { BASE64, convert } from './storage.mjs';
|
|
1
2
|
import { countTokens, trimText } from './alan.mjs';
|
|
2
|
-
import { convert } from './storage.mjs';
|
|
3
3
|
import { ensureArray, ensureString, need } from './utilitas.mjs';
|
|
4
4
|
|
|
5
5
|
const _NEED = ['openai', '@google-cloud/discoveryengine'];
|
|
@@ -14,6 +14,7 @@ const [
|
|
|
14
14
|
GOOGLE_MODEL_GEMINI_EMBED,
|
|
15
15
|
JINA_MODEL_V_4,
|
|
16
16
|
GOOGLE_MODEL_SEMANTIC_RANKER,
|
|
17
|
+
JINA_MODEL_RERANKER_M0,
|
|
17
18
|
] = [
|
|
18
19
|
'OPENAI', 'GOOGLE', 'OPENROUTER', 'JINA',
|
|
19
20
|
'global', 'default_ranking_config',
|
|
@@ -22,6 +23,7 @@ const [
|
|
|
22
23
|
'gemini-embedding-001', // dim: 768(default), 1536, or 3072(google default)
|
|
23
24
|
'jina-embeddings-v4', // dim: 256‑2048
|
|
24
25
|
'semantic-ranker-default@latest',
|
|
26
|
+
'jina-reranker-m0',
|
|
25
27
|
];
|
|
26
28
|
|
|
27
29
|
const PROVIDER_BASE_URL = {
|
|
@@ -37,6 +39,7 @@ const DEFAULT_EMBEDDING_MODELS = {
|
|
|
37
39
|
|
|
38
40
|
const DEFAULT_RERANKER_MODELS = {
|
|
39
41
|
[GOOGLE]: GOOGLE_MODEL_SEMANTIC_RANKER,
|
|
42
|
+
[JINA]: JINA_MODEL_RERANKER_M0,
|
|
40
43
|
};
|
|
41
44
|
|
|
42
45
|
const MODEL_CONFIG = {
|
|
@@ -64,6 +67,11 @@ const MODEL_CONFIG = {
|
|
|
64
67
|
},
|
|
65
68
|
[GOOGLE_MODEL_SEMANTIC_RANKER]: {
|
|
66
69
|
source: 'google', image: false, maxTokens: 1024, recordsLimit: 200,
|
|
70
|
+
options: { ignoreRecordDetailsInResponse: true },
|
|
71
|
+
},
|
|
72
|
+
[JINA_MODEL_RERANKER_M0]: {
|
|
73
|
+
source: 'jina', image: true, maxTokens: 1024, recordsLimit: 200,
|
|
74
|
+
options: { return_documents: false },
|
|
67
75
|
},
|
|
68
76
|
};
|
|
69
77
|
|
|
@@ -147,7 +155,7 @@ const embed = async (input, options = {}) => {
|
|
|
147
155
|
);
|
|
148
156
|
if (options?.input) {
|
|
149
157
|
x.image = await convert(
|
|
150
|
-
x.image, { ...options, expected:
|
|
158
|
+
x.image, { ...options, expected: BASE64 }
|
|
151
159
|
);
|
|
152
160
|
}
|
|
153
161
|
}
|
|
@@ -181,6 +189,7 @@ const embed = async (input, options = {}) => {
|
|
|
181
189
|
|
|
182
190
|
const initReranker = async (options = {}) => {
|
|
183
191
|
const provider = ensureRerankerProvider(options);
|
|
192
|
+
const model = options?.model || DEFAULT_RERANKER_MODELS[provider];
|
|
184
193
|
switch (provider) {
|
|
185
194
|
case GOOGLE:
|
|
186
195
|
ensureGoogleCredentials(options);
|
|
@@ -195,13 +204,20 @@ const initReranker = async (options = {}) => {
|
|
|
195
204
|
};
|
|
196
205
|
const client = new RankServiceClient(clientOptions);
|
|
197
206
|
rerankerClients[provider] = {
|
|
198
|
-
|
|
199
|
-
client, rankingConfigPath: client.rankingConfigPath(
|
|
207
|
+
client, model, rankingConfigPath: client.rankingConfigPath(
|
|
200
208
|
options.projectId, location,
|
|
201
209
|
options?.rerankerConfigId || GOOGLE_RERANK_CONFIG_ID
|
|
202
210
|
),
|
|
203
211
|
};
|
|
204
212
|
break;
|
|
213
|
+
case JINA:
|
|
214
|
+
const OpenAI = await need('openai');
|
|
215
|
+
const baseURL = options?.baseURL || PROVIDER_BASE_URL[provider];
|
|
216
|
+
rerankerClients[provider] = {
|
|
217
|
+
client: new OpenAI({ ...options, baseURL }),
|
|
218
|
+
model, source: MODEL_CONFIG[model]?.source,
|
|
219
|
+
};
|
|
220
|
+
break;
|
|
205
221
|
default:
|
|
206
222
|
throw new Error(`Unsupported reranker provider: ${provider}`);
|
|
207
223
|
}
|
|
@@ -226,20 +242,43 @@ const rerank = async (query, records, options = {}) => {
|
|
|
226
242
|
records[i].content = availableTokens > 0 ? await trimText(
|
|
227
243
|
records[i].content, availableTokens
|
|
228
244
|
) : '';
|
|
245
|
+
records[i].image = records[i].image ? await convert(records[i].image, {
|
|
246
|
+
...options, expected: BASE64,
|
|
247
|
+
}) : undefined;
|
|
229
248
|
}
|
|
230
249
|
switch (provider) {
|
|
231
250
|
case GOOGLE:
|
|
232
|
-
|
|
251
|
+
var body = {
|
|
233
252
|
model, query, rankingConfig: rankingConfigPath,
|
|
234
253
|
records, topN: ~~options?.topN || records.length,
|
|
254
|
+
...MODEL_CONFIG[model]?.options || {},
|
|
255
|
+
...options?.requestOptions || {},
|
|
256
|
+
};
|
|
257
|
+
result = (await client.rank(body))?.[0]?.records;
|
|
258
|
+
options?.raw || (result = result.map(x => ({
|
|
259
|
+
index: ~~x.id, score: x.score,
|
|
260
|
+
})));
|
|
261
|
+
break;
|
|
262
|
+
case JINA:
|
|
263
|
+
records = records.map(x =>
|
|
264
|
+
((x.title || x.content) ? { text: [x.title, x.content].filter(x => x).join('\n') } : null)
|
|
265
|
+
|| (x.image ? { image: x.image } : null)
|
|
266
|
+
).filter(x => x);
|
|
267
|
+
assert(records.length, 'No valid records found.', 400);
|
|
268
|
+
var body = {
|
|
269
|
+
model, query, documents: records,
|
|
270
|
+
...MODEL_CONFIG[model]?.options || {},
|
|
235
271
|
...options?.requestOptions || {},
|
|
236
272
|
};
|
|
237
|
-
result = (await client.
|
|
273
|
+
result = (await client.post('/rerank', { body }))?.results;
|
|
274
|
+
options?.raw || (result = result.map(x => ({
|
|
275
|
+
index: ~~(x.index), score: x.relevance_score,
|
|
276
|
+
})));
|
|
238
277
|
break;
|
|
239
278
|
default:
|
|
240
279
|
throw new Error(`Unsupported reranker provider: ${provider}`);
|
|
241
280
|
}
|
|
242
|
-
|
|
281
|
+
result.sort((a, b) => b.score - a.score);
|
|
243
282
|
return result || [];
|
|
244
283
|
};
|
|
245
284
|
|