kimiflare 0.88.1 → 0.88.2
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/index.js +86 -40
- package/dist/index.js.map +1 -1
- package/dist/sdk/index.js +86 -40
- package/dist/sdk/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3418,15 +3418,87 @@ async function fetchWithRetry(url, init, retries = 3) {
|
|
|
3418
3418
|
}
|
|
3419
3419
|
throw lastError ?? new Error("embeddings request failed after retries");
|
|
3420
3420
|
}
|
|
3421
|
+
function parseOpenAiEmbeddingResponse(json2) {
|
|
3422
|
+
if (!json2 || typeof json2 !== "object") {
|
|
3423
|
+
throw new Error("embeddings response was not an object");
|
|
3424
|
+
}
|
|
3425
|
+
const data = json2.data;
|
|
3426
|
+
if (!Array.isArray(data)) {
|
|
3427
|
+
throw new Error("embeddings response contained no data array");
|
|
3428
|
+
}
|
|
3429
|
+
const indexed = [];
|
|
3430
|
+
for (const item of data) {
|
|
3431
|
+
if (!item || typeof item !== "object") continue;
|
|
3432
|
+
const embedding = item.embedding;
|
|
3433
|
+
const idx = item.index;
|
|
3434
|
+
if (!Array.isArray(embedding)) continue;
|
|
3435
|
+
indexed.push({
|
|
3436
|
+
index: typeof idx === "number" ? idx : indexed.length,
|
|
3437
|
+
vector: new Float32Array(embedding)
|
|
3438
|
+
});
|
|
3439
|
+
}
|
|
3440
|
+
if (indexed.length === 0) {
|
|
3441
|
+
throw new Error("embeddings response contained no vectors");
|
|
3442
|
+
}
|
|
3443
|
+
indexed.sort((a, b) => a.index - b.index);
|
|
3444
|
+
return indexed.map((item) => {
|
|
3445
|
+
if (item.vector.length === 0) {
|
|
3446
|
+
throw new Error("embeddings response contained empty vector");
|
|
3447
|
+
}
|
|
3448
|
+
return item.vector;
|
|
3449
|
+
});
|
|
3450
|
+
}
|
|
3451
|
+
function parseWorkersAiEmbeddingResponse(json2) {
|
|
3452
|
+
let vectors = [];
|
|
3453
|
+
if (json2 && typeof json2 === "object") {
|
|
3454
|
+
const result = json2.result;
|
|
3455
|
+
if (result && typeof result === "object") {
|
|
3456
|
+
const data = result.data;
|
|
3457
|
+
if (Array.isArray(data)) {
|
|
3458
|
+
if (Array.isArray(data[0])) {
|
|
3459
|
+
vectors = data;
|
|
3460
|
+
} else {
|
|
3461
|
+
const shape = result.shape;
|
|
3462
|
+
if (shape && shape.length === 2) {
|
|
3463
|
+
const dim = shape[1];
|
|
3464
|
+
const flat = data;
|
|
3465
|
+
vectors = [];
|
|
3466
|
+
for (let i = 0; i < flat.length; i += dim) {
|
|
3467
|
+
vectors.push(flat.slice(i, i + dim));
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
}
|
|
3471
|
+
}
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3474
|
+
if (vectors.length === 0) {
|
|
3475
|
+
throw new Error("embeddings response contained no vectors");
|
|
3476
|
+
}
|
|
3477
|
+
return vectors.map((vec) => {
|
|
3478
|
+
const arr = new Float32Array(vec);
|
|
3479
|
+
if (arr.length === 0) {
|
|
3480
|
+
throw new Error("embeddings response contained empty vector");
|
|
3481
|
+
}
|
|
3482
|
+
return arr;
|
|
3483
|
+
});
|
|
3484
|
+
}
|
|
3421
3485
|
async function fetchEmbeddings(opts2) {
|
|
3422
3486
|
const model = opts2.model ?? DEFAULT_MODEL2;
|
|
3487
|
+
const texts = opts2.texts.map(truncateForEmbedding);
|
|
3488
|
+
if (texts.length === 0) {
|
|
3489
|
+
return [];
|
|
3490
|
+
}
|
|
3423
3491
|
let url;
|
|
3424
3492
|
const headers = {
|
|
3425
3493
|
"Content-Type": "application/json",
|
|
3426
3494
|
"User-Agent": getUserAgent()
|
|
3427
3495
|
};
|
|
3496
|
+
let body;
|
|
3497
|
+
let parseResponse;
|
|
3428
3498
|
if (opts2.gateway) {
|
|
3429
|
-
url = `https://gateway.ai.cloudflare.com/v1/${
|
|
3499
|
+
url = `https://gateway.ai.cloudflare.com/v1/${encodeURIComponent(
|
|
3500
|
+
opts2.accountId
|
|
3501
|
+
)}/${encodeURIComponent(opts2.gateway.id)}/compat/embeddings`;
|
|
3430
3502
|
headers.Authorization = `Bearer ${opts2.apiToken}`;
|
|
3431
3503
|
const merged = {
|
|
3432
3504
|
...opts2.gateway.metadata ?? {},
|
|
@@ -3440,48 +3512,22 @@ async function fetchEmbeddings(opts2) {
|
|
|
3440
3512
|
if (opts2.gateway.skipCache !== void 0) {
|
|
3441
3513
|
headers["cf-aig-skip-cache"] = String(opts2.gateway.skipCache);
|
|
3442
3514
|
}
|
|
3515
|
+
body = JSON.stringify({
|
|
3516
|
+
model: `workers-ai/${model}`,
|
|
3517
|
+
input: texts
|
|
3518
|
+
});
|
|
3519
|
+
parseResponse = parseOpenAiEmbeddingResponse;
|
|
3443
3520
|
} else {
|
|
3444
|
-
url = `https://api.cloudflare.com/client/v4/accounts/${
|
|
3521
|
+
url = `https://api.cloudflare.com/client/v4/accounts/${encodeURIComponent(
|
|
3522
|
+
opts2.accountId
|
|
3523
|
+
)}/ai/run/${encodeURIComponent(model)}`;
|
|
3445
3524
|
headers.Authorization = `Bearer ${opts2.apiToken}`;
|
|
3525
|
+
body = JSON.stringify({ text: texts });
|
|
3526
|
+
parseResponse = parseWorkersAiEmbeddingResponse;
|
|
3446
3527
|
}
|
|
3447
|
-
const
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
const body = JSON.stringify({ text: [truncated] });
|
|
3451
|
-
const res = await fetchWithRetry(url, { method: "POST", headers, body });
|
|
3452
|
-
const json2 = await res.json();
|
|
3453
|
-
let vectors = [];
|
|
3454
|
-
if (json2 && typeof json2 === "object") {
|
|
3455
|
-
const result = json2.result;
|
|
3456
|
-
if (result && typeof result === "object") {
|
|
3457
|
-
const data = result.data;
|
|
3458
|
-
if (Array.isArray(data)) {
|
|
3459
|
-
if (Array.isArray(data[0])) {
|
|
3460
|
-
vectors = data;
|
|
3461
|
-
} else {
|
|
3462
|
-
const shape = result.shape;
|
|
3463
|
-
if (shape && shape.length === 2) {
|
|
3464
|
-
const dim = shape[1];
|
|
3465
|
-
const flat = data;
|
|
3466
|
-
vectors = [];
|
|
3467
|
-
for (let i = 0; i < flat.length; i += dim) {
|
|
3468
|
-
vectors.push(flat.slice(i, i + dim));
|
|
3469
|
-
}
|
|
3470
|
-
}
|
|
3471
|
-
}
|
|
3472
|
-
}
|
|
3473
|
-
}
|
|
3474
|
-
}
|
|
3475
|
-
if (vectors.length === 0) {
|
|
3476
|
-
throw new Error("embeddings response contained no vectors");
|
|
3477
|
-
}
|
|
3478
|
-
const vec = new Float32Array(vectors[0]);
|
|
3479
|
-
if (vec.length === 0) {
|
|
3480
|
-
throw new Error("embeddings response contained empty vector");
|
|
3481
|
-
}
|
|
3482
|
-
results.push(vec);
|
|
3483
|
-
}
|
|
3484
|
-
return results;
|
|
3528
|
+
const res = await fetchWithRetry(url, { method: "POST", headers, body });
|
|
3529
|
+
const json2 = await res.json();
|
|
3530
|
+
return parseResponse(json2);
|
|
3485
3531
|
}
|
|
3486
3532
|
function cosineSimilarity(a, b) {
|
|
3487
3533
|
if (a.length !== b.length) {
|