@benchme/site-kit 0.4.2 → 0.5.0
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/html.d.ts +6 -0
- package/dist/html.js +3 -3
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/jsonld.d.ts +91 -0
- package/dist/jsonld.js +98 -0
- package/dist/jsonld.js.map +1 -0
- package/dist/mcp-server.d.ts +4 -2
- package/dist/mcp-server.js +2 -1
- package/dist/mcp-server.js.map +1 -1
- package/dist/nlweb/ask-params.d.ts +25 -0
- package/dist/nlweb/ask-params.js +65 -0
- package/dist/nlweb/ask-params.js.map +1 -0
- package/dist/nlweb/ask-routes.d.ts +11 -0
- package/dist/nlweb/ask-routes.js +56 -0
- package/dist/nlweb/ask-routes.js.map +1 -0
- package/dist/nlweb/ask-service.d.ts +24 -0
- package/dist/nlweb/ask-service.js +57 -0
- package/dist/nlweb/ask-service.js.map +1 -0
- package/dist/nlweb/ask-sse.d.ts +10 -0
- package/dist/nlweb/ask-sse.js +42 -0
- package/dist/nlweb/ask-sse.js.map +1 -0
- package/dist/nlweb/eval-metrics.d.ts +37 -0
- package/dist/nlweb/eval-metrics.js +71 -0
- package/dist/nlweb/eval-metrics.js.map +1 -0
- package/dist/nlweb/index.d.ts +15 -0
- package/dist/nlweb/index.js +16 -0
- package/dist/nlweb/index.js.map +1 -0
- package/dist/nlweb/jev-ranker.d.ts +18 -0
- package/dist/nlweb/jev-ranker.js +54 -0
- package/dist/nlweb/jev-ranker.js.map +1 -0
- package/dist/nlweb/lexical.d.ts +28 -0
- package/dist/nlweb/lexical.js +69 -0
- package/dist/nlweb/lexical.js.map +1 -0
- package/dist/nlweb/llm-ranker.d.ts +37 -0
- package/dist/nlweb/llm-ranker.js +98 -0
- package/dist/nlweb/llm-ranker.js.map +1 -0
- package/dist/nlweb/p-limit.d.ts +8 -0
- package/dist/nlweb/p-limit.js +27 -0
- package/dist/nlweb/p-limit.js.map +1 -0
- package/dist/nlweb/ranker-config.d.ts +62 -0
- package/dist/nlweb/ranker-config.js +71 -0
- package/dist/nlweb/ranker-config.js.map +1 -0
- package/dist/nlweb/remote-ranker.d.ts +72 -0
- package/dist/nlweb/remote-ranker.js +92 -0
- package/dist/nlweb/remote-ranker.js.map +1 -0
- package/dist/nlweb/request-url.d.ts +16 -0
- package/dist/nlweb/request-url.js +25 -0
- package/dist/nlweb/request-url.js.map +1 -0
- package/dist/nlweb/schema-routes.d.ts +11 -0
- package/dist/nlweb/schema-routes.js +32 -0
- package/dist/nlweb/schema-routes.js.map +1 -0
- package/dist/nlweb/types.d.ts +121 -0
- package/dist/nlweb/types.js +28 -0
- package/dist/nlweb/types.js.map +1 -0
- package/dist/nlweb/word-variants.d.ts +10 -0
- package/dist/nlweb/word-variants.js +14 -0
- package/dist/nlweb/word-variants.js.map +1 -0
- package/dist/tool-schema.d.ts +14 -0
- package/dist/tool-schema.js +15 -0
- package/dist/tool-schema.js.map +1 -0
- package/dist/webmcp.d.ts +20 -0
- package/dist/webmcp.js +31 -0
- package/dist/webmcp.js.map +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The NLWeb SSE contract. The two header frames go out BEFORE ranking starts,
|
|
3
|
+
* so a client that waits on a paid ranker still sees the stream open and knows
|
|
4
|
+
* the licence/retention terms it is about to receive data under.
|
|
5
|
+
*/
|
|
6
|
+
const LICENSE = { message_type: "license", content: { license: "benchme synthetic data; no restrictions" } };
|
|
7
|
+
const DATA_RETENTION = { message_type: "data_retention", content: { retention: "none" } };
|
|
8
|
+
/**
|
|
9
|
+
* Takes over the raw socket (Fastify's reply serialisation cannot emit frames
|
|
10
|
+
* incrementally) and always terminates with a `complete` frame — including on
|
|
11
|
+
* failure, so a client never hangs waiting for an end that is not coming.
|
|
12
|
+
*/
|
|
13
|
+
export async function streamAsk(reply, service, workspaceId, params, queryId, prefix, rankerOverride) {
|
|
14
|
+
reply.hijack();
|
|
15
|
+
const raw = reply.raw;
|
|
16
|
+
raw.writeHead(200, {
|
|
17
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
18
|
+
"cache-control": "no-cache",
|
|
19
|
+
connection: "keep-alive",
|
|
20
|
+
});
|
|
21
|
+
const frame = (payload) => {
|
|
22
|
+
raw.write(`data: ${JSON.stringify(payload)}\n\n`);
|
|
23
|
+
};
|
|
24
|
+
frame(LICENSE);
|
|
25
|
+
frame(DATA_RETENTION);
|
|
26
|
+
try {
|
|
27
|
+
const answer = await service.ask(workspaceId, { query: params.query, prev: params.prev, queryId, prefix }, rankerOverride);
|
|
28
|
+
frame({ results: answer.results });
|
|
29
|
+
if (answer.ranker_degraded)
|
|
30
|
+
frame({ message_type: "ranker", content: { ranker: answer.ranker, degraded: true } });
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
// A ranker failure's message carries upstream detail (endpoint, key
|
|
34
|
+
// fragment, provider text). The client gets a stable code it can branch
|
|
35
|
+
// on; the detail goes to the operator's log, where it belongs.
|
|
36
|
+
reply.log.error({ err, queryId }, "ask failed");
|
|
37
|
+
frame({ message_type: "error", content: { error: "ASK_FAILED" } });
|
|
38
|
+
}
|
|
39
|
+
frame({ query_id: queryId, complete: true });
|
|
40
|
+
raw.end();
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=ask-sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ask-sse.js","sourceRoot":"","sources":["../../src/nlweb/ask-sse.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,MAAM,OAAO,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,yCAAyC,EAAE,EAAE,CAAC;AAC7G,MAAM,cAAc,GAAG,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,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AAC,CAAC;QAChD,KAAK,CAAC,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC;IACrE,CAAC;IACD,KAAK,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7C,GAAG,CAAC,GAAG,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ranking-quality primitives for tools/ask-eval.mjs (comparing lexical/llm/jev
|
|
3
|
+
* over seed-derived relevance labels). Pure and dependency-free — no HTTP, no
|
|
4
|
+
* randomness — so they're unit-testable on their own and the CLI can import
|
|
5
|
+
* them from the built dist instead of re-implementing scoring math in a
|
|
6
|
+
* `.mjs` file nothing typechecks.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Binary-relevance nDCG@k: discounted gain (1 per relevant hit, 1/log2(rank+1)
|
|
10
|
+
* discount) normalised by the ideal ordering's gain. A query with no relevant
|
|
11
|
+
* items at all returns 1 (nothing to miss) rather than 0/0 — a ranker cannot
|
|
12
|
+
* be penalised for a ground truth that has no answer.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ndcgAt(rankedIds: readonly string[], relevant: ReadonlySet<string> | readonly string[], k: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Fraction of the top-k results that are relevant. An empty top-k (the ranker
|
|
17
|
+
* returned nothing) is 0, not NaN, so a caller can average across queries
|
|
18
|
+
* without special-casing an empty answer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function precisionAt(rankedIds: readonly string[], relevant: ReadonlySet<string> | readonly string[], k: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Linear-interpolated percentile (0..100) over `values`, sorted on a copy so
|
|
23
|
+
* the caller's array is never mutated. Empty input is 0, not NaN — a ranker
|
|
24
|
+
* that answered zero queries should read as "no data", never poison a
|
|
25
|
+
* downstream average.
|
|
26
|
+
*/
|
|
27
|
+
export declare function percentile(values: readonly number[], p: number): number;
|
|
28
|
+
/**
|
|
29
|
+
* How many results the ranker was CONFIDENT about (score >= threshold) yet
|
|
30
|
+
* were not actually relevant — the failure mode an averaged nDCG/precision
|
|
31
|
+
* can hide entirely: a ranker can score well on average while still handing
|
|
32
|
+
* out a handful of high-confidence wrong answers a user would trust.
|
|
33
|
+
*/
|
|
34
|
+
export declare function highConfidenceMisses(results: readonly {
|
|
35
|
+
id: string;
|
|
36
|
+
score: number;
|
|
37
|
+
}[], relevant: ReadonlySet<string> | readonly string[], threshold?: number): number;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ranking-quality primitives for tools/ask-eval.mjs (comparing lexical/llm/jev
|
|
3
|
+
* over seed-derived relevance labels). Pure and dependency-free — no HTTP, no
|
|
4
|
+
* randomness — so they're unit-testable on their own and the CLI can import
|
|
5
|
+
* them from the built dist instead of re-implementing scoring math in a
|
|
6
|
+
* `.mjs` file nothing typechecks.
|
|
7
|
+
*/
|
|
8
|
+
function toSet(relevant) {
|
|
9
|
+
return relevant instanceof Set ? relevant : new Set(relevant);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Binary-relevance nDCG@k: discounted gain (1 per relevant hit, 1/log2(rank+1)
|
|
13
|
+
* discount) normalised by the ideal ordering's gain. A query with no relevant
|
|
14
|
+
* items at all returns 1 (nothing to miss) rather than 0/0 — a ranker cannot
|
|
15
|
+
* be penalised for a ground truth that has no answer.
|
|
16
|
+
*/
|
|
17
|
+
export function ndcgAt(rankedIds, relevant, k) {
|
|
18
|
+
const rel = toSet(relevant);
|
|
19
|
+
if (rel.size === 0)
|
|
20
|
+
return 1;
|
|
21
|
+
const gain = (rank) => 1 / Math.log2(rank + 2); // rank is 0-based; +2 so rank 0 divides by log2(2)=1
|
|
22
|
+
const dcg = rankedIds.slice(0, k).reduce((sum, id, i) => sum + (rel.has(id) ? gain(i) : 0), 0);
|
|
23
|
+
const idealHits = Math.min(rel.size, k);
|
|
24
|
+
let idcg = 0;
|
|
25
|
+
for (let i = 0; i < idealHits; i++)
|
|
26
|
+
idcg += gain(i);
|
|
27
|
+
return idcg === 0 ? 0 : dcg / idcg;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fraction of the top-k results that are relevant. An empty top-k (the ranker
|
|
31
|
+
* returned nothing) is 0, not NaN, so a caller can average across queries
|
|
32
|
+
* without special-casing an empty answer.
|
|
33
|
+
*/
|
|
34
|
+
export function precisionAt(rankedIds, relevant, k) {
|
|
35
|
+
const rel = toSet(relevant);
|
|
36
|
+
const top = rankedIds.slice(0, k);
|
|
37
|
+
if (top.length === 0)
|
|
38
|
+
return 0;
|
|
39
|
+
return top.filter((id) => rel.has(id)).length / top.length;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Linear-interpolated percentile (0..100) over `values`, sorted on a copy so
|
|
43
|
+
* the caller's array is never mutated. Empty input is 0, not NaN — a ranker
|
|
44
|
+
* that answered zero queries should read as "no data", never poison a
|
|
45
|
+
* downstream average.
|
|
46
|
+
*/
|
|
47
|
+
export function percentile(values, p) {
|
|
48
|
+
if (values.length === 0)
|
|
49
|
+
return 0;
|
|
50
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
51
|
+
if (sorted.length === 1)
|
|
52
|
+
return sorted[0];
|
|
53
|
+
const rank = (p / 100) * (sorted.length - 1);
|
|
54
|
+
const lo = Math.floor(rank);
|
|
55
|
+
const hi = Math.ceil(rank);
|
|
56
|
+
if (lo === hi)
|
|
57
|
+
return sorted[lo];
|
|
58
|
+
const frac = rank - lo;
|
|
59
|
+
return sorted[lo] * (1 - frac) + sorted[hi] * frac;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* How many results the ranker was CONFIDENT about (score >= threshold) yet
|
|
63
|
+
* were not actually relevant — the failure mode an averaged nDCG/precision
|
|
64
|
+
* can hide entirely: a ranker can score well on average while still handing
|
|
65
|
+
* out a handful of high-confidence wrong answers a user would trust.
|
|
66
|
+
*/
|
|
67
|
+
export function highConfidenceMisses(results, relevant, threshold = 0.75) {
|
|
68
|
+
const rel = toSet(relevant);
|
|
69
|
+
return results.filter((r) => r.score >= threshold && !rel.has(r.id)).length;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=eval-metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eval-metrics.js","sourceRoot":"","sources":["../../src/nlweb/eval-metrics.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,SAAS,KAAK,CAAC,QAAiD;IAC9D,OAAO,QAAQ,YAAY,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;AAChE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,SAA4B,EAAE,QAAiD,EAAE,CAAS;IAC/G,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5B,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAC7B,MAAM,IAAI,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,qDAAqD;IACrH,MAAM,GAAG,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/F,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CA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IAAI,CAAC,CAAC;IAC5B,MAAM,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3B,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,MAAM,CAAC,EAAE,CAAW,CAAC;IAC3C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;IACvB,OAAQ,MAAM,CAAC,EAAE,CAAY,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAI,MAAM,CAAC,EAAE,CAAY,GAAG,IAAI,CAAC;AAC7E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAiD,EAAE,QAAiD,EAAE,SAAS,GAAG,IAAI;IACzJ,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5B,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,SAAS,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AAC9E,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { parseAskParams, type AskParamError, type AskParamResult, type AskParams } from "./ask-params.js";
|
|
2
|
+
export { registerNlweb, type NlwebDeps } from "./ask-routes.js";
|
|
3
|
+
export { AskService, type AskQuery } from "./ask-service.js";
|
|
4
|
+
export { streamAsk } from "./ask-sse.js";
|
|
5
|
+
export { highConfidenceMisses, ndcgAt, percentile, precisionAt } from "./eval-metrics.js";
|
|
6
|
+
export { JevRanker } from "./jev-ranker.js";
|
|
7
|
+
export { LexicalRanker, retrieve, tokenize } from "./lexical.js";
|
|
8
|
+
export { LlmRanker, NLWEB_RANKING_PROMPT, type LlmRankerOptions } from "./llm-ranker.js";
|
|
9
|
+
export { pLimit, type Limiter } from "./p-limit.js";
|
|
10
|
+
export { buildRanker, defaultRankerRegistry, rankerEnvSchema, readRankerEnv, type RankerEnv } from "./ranker-config.js";
|
|
11
|
+
export { defaultPublicBaseUrl } from "./request-url.js";
|
|
12
|
+
export { RemoteRanker, type RemoteRankerOptions, type Tokens, type Verdict } from "./remote-ranker.js";
|
|
13
|
+
export { registerSchemaRoutes, type SchemaDeps } from "./schema-routes.js";
|
|
14
|
+
export { wordVariants } from "./word-variants.js";
|
|
15
|
+
export { RankerRegistry, type AskDeps, type AskItem, type AskResponse, type AskResult, type AskUsage, type RankedCandidate, type Ranker, type RankerFactory, type RankerUsage, } from "./types.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { parseAskParams } from "./ask-params.js";
|
|
2
|
+
export { registerNlweb } from "./ask-routes.js";
|
|
3
|
+
export { AskService } from "./ask-service.js";
|
|
4
|
+
export { streamAsk } from "./ask-sse.js";
|
|
5
|
+
export { highConfidenceMisses, ndcgAt, percentile, precisionAt } from "./eval-metrics.js";
|
|
6
|
+
export { JevRanker } from "./jev-ranker.js";
|
|
7
|
+
export { LexicalRanker, retrieve, tokenize } from "./lexical.js";
|
|
8
|
+
export { LlmRanker, NLWEB_RANKING_PROMPT } from "./llm-ranker.js";
|
|
9
|
+
export { pLimit } from "./p-limit.js";
|
|
10
|
+
export { buildRanker, defaultRankerRegistry, rankerEnvSchema, readRankerEnv } from "./ranker-config.js";
|
|
11
|
+
export { defaultPublicBaseUrl } from "./request-url.js";
|
|
12
|
+
export { RemoteRanker } from "./remote-ranker.js";
|
|
13
|
+
export { registerSchemaRoutes } from "./schema-routes.js";
|
|
14
|
+
export { wordVariants } from "./word-variants.js";
|
|
15
|
+
export { RankerRegistry, } from "./types.js";
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nlweb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA2D,MAAM,iBAAiB,CAAC;AAC1G,OAAO,EAAE,aAAa,EAAkB,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAiB,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC1F,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAyB,MAAM,iBAAiB,CAAC;AACzF,OAAO,EAAE,MAAM,EAAgB,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAE,eAAe,EAAE,aAAa,EAAkB,MAAM,oBAAoB,CAAC;AACxH,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAuD,MAAM,oBAAoB,CAAC;AACvG,OAAO,EAAE,oBAAoB,EAAmB,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EACL,cAAc,GAUf,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RemoteRanker, type FetchInit, type RemoteRankerOptions, type Tokens, type Verdict } from "./remote-ranker.js";
|
|
2
|
+
import type { AskItem } from "./types.js";
|
|
3
|
+
/** Priced per run like the llm ranker's: a list-price change must not silently misreport cost. */
|
|
4
|
+
export type JevRankerOptions = RemoteRankerOptions & {
|
|
5
|
+
inputUsdPerMTok?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare class JevRanker extends RemoteRanker {
|
|
8
|
+
readonly kind = "jev";
|
|
9
|
+
private readonly inputUsdPerMTok;
|
|
10
|
+
constructor(o: JevRankerOptions);
|
|
11
|
+
protected request(query: string, item: AskItem): {
|
|
12
|
+
url: string;
|
|
13
|
+
init: FetchInit;
|
|
14
|
+
};
|
|
15
|
+
protected usageOf(body: unknown): Tokens;
|
|
16
|
+
protected verdictOf(body: unknown): Verdict;
|
|
17
|
+
protected costUsd(t: Tokens): number;
|
|
18
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { RemoteRanker } from "./remote-ranker.js";
|
|
3
|
+
/**
|
|
4
|
+
* Ranking as a jev (TypeSafe System One) `score` question. The contrast with
|
|
5
|
+
* the llm ranker is the point of the grid: jev is asked for a POSITION on a
|
|
6
|
+
* labelled scale and answers with a calibrated float plus per-level
|
|
7
|
+
* probabilities, instead of being asked to write a number in prose. It emits
|
|
8
|
+
* no text at all, which is why a jev-ranked result keeps the item's own
|
|
9
|
+
* description rather than a generated one.
|
|
10
|
+
*/
|
|
11
|
+
/** The scale the score is a position on; the last index is "perfect match", so levels-1 normalises it. */
|
|
12
|
+
const RELEVANCE_CRITERIA = ["irrelevant", "tangential", "somewhat relevant", "relevant", "exactly what was asked"];
|
|
13
|
+
const RELEVANCE_INSTRUCTIONS = "How relevant is this item to the user's question?";
|
|
14
|
+
/** System One list price, input only: it writes no tokens, so output is free. A default, not a constant — see `JevRankerOptions.inputUsdPerMTok`. */
|
|
15
|
+
const DEFAULT_INPUT_USD_PER_MTOK = 0.042;
|
|
16
|
+
/** Read separately from the answers, so a reply we cannot score is still billed. */
|
|
17
|
+
const systemOneUsage = z.object({ usage: z.object({ input_tokens: z.number(), output_tokens: z.number() }) });
|
|
18
|
+
const systemOneAnswers = z.object({ answers: z.object({ relevance: z.object({ type: z.string(), score: z.number() }) }) });
|
|
19
|
+
export class JevRanker extends RemoteRanker {
|
|
20
|
+
kind = "jev";
|
|
21
|
+
inputUsdPerMTok;
|
|
22
|
+
constructor(o) {
|
|
23
|
+
super(o);
|
|
24
|
+
this.inputUsdPerMTok = o.inputUsdPerMTok ?? DEFAULT_INPUT_USD_PER_MTOK;
|
|
25
|
+
}
|
|
26
|
+
request(query, item) {
|
|
27
|
+
return {
|
|
28
|
+
url: `${this.baseUrl}/v1/systemone`,
|
|
29
|
+
init: {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { authorization: `Bearer ${this.apiKey}`, "content-type": "application/json" },
|
|
32
|
+
// `state` is the world the question is asked about; the question itself
|
|
33
|
+
// is declarative, so the scale — not a prompt — defines what a 3 means.
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
model: this.model,
|
|
36
|
+
state: { question: query, item: item.schema },
|
|
37
|
+
questions: { relevance: { type: "score", instructions: RELEVANCE_INSTRUCTIONS, criteria: RELEVANCE_CRITERIA } },
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
usageOf(body) {
|
|
43
|
+
const { usage } = systemOneUsage.parse(body);
|
|
44
|
+
return { inputTokens: usage.input_tokens, outputTokens: usage.output_tokens };
|
|
45
|
+
}
|
|
46
|
+
verdictOf(body) {
|
|
47
|
+
// The API returns the weighted position over the levels, in [0, levels-1].
|
|
48
|
+
return { score: systemOneAnswers.parse(body).answers.relevance.score / (RELEVANCE_CRITERIA.length - 1) };
|
|
49
|
+
}
|
|
50
|
+
costUsd(t) {
|
|
51
|
+
return (t.inputTokens * this.inputUsdPerMTok) / 1e6;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=jev-ranker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jev-ranker.js","sourceRoot":"","sources":["../../src/nlweb/jev-ranker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAuE,MAAM,oBAAoB,CAAC;AAGvH;;;;;;;GAOG;AAEH,0GAA0G;AAC1G,MAAM,kBAAkB,GAAG,CAAC,YAAY,EAAE,YAAY,EAAE,mBAAmB,EAAE,UAAU,EAAE,wBAAwB,CAAU,CAAC;AAC5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E;oBAC7C,SAAS,EAAE,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,QAAQ,EAAE,kBAAkB,EAAE,EAAE;iBAChH,CAAC;aACH;SACF,CAAC;IACJ,CAAC;IAEkB,OAAO,CAAC,IAAa;QACtC,MAAM,EAAE,KAAK,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC7C,OAAO,EAAE,WAAW,EAAE,KAAK,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,CAAC,aAAa,EAAE,CAAC;IAChF,CAAC;IAEkB,SAAS,CAAC,IAAa;QACxC,2EAA2E;QAC3E,OAAO,EAAE,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,kBAAkB,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC;IAC3G,CAAC;IAEkB,OAAO,CAAC,CAAS;QAClC,OAAO,CAAC,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,GAAG,CAAC;IACtD,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AskItem, RankedCandidate, Ranker, RankerUsage } from "./types.js";
|
|
2
|
+
/** Lowercase, split on non-alphanumerics, drop stopwords and single characters (noise from units/initials). */
|
|
3
|
+
export declare function tokenize(s: string): string[];
|
|
4
|
+
/**
|
|
5
|
+
* Top-`topK` matching items, best first. Ties break by `id` ascending so the
|
|
6
|
+
* same corpus in any order yields the same answer — a benchmark that grades an
|
|
7
|
+
* agent's answer cannot tolerate result order depending on row order.
|
|
8
|
+
* Items scoring 0 are dropped: "no match" is an empty list, never a bad list.
|
|
9
|
+
*/
|
|
10
|
+
export declare function retrieve(query: string, items: readonly AskItem[], topK: number): {
|
|
11
|
+
item: AskItem;
|
|
12
|
+
score: number;
|
|
13
|
+
}[];
|
|
14
|
+
/** The zero-cost default ranker, and the fallback every paid ranker degrades to. */
|
|
15
|
+
export declare class LexicalRanker implements Ranker {
|
|
16
|
+
readonly kind = "lexical";
|
|
17
|
+
/**
|
|
18
|
+
* Scores against the `query` it is handed — the CURRENT turn — rather than
|
|
19
|
+
* re-emitting `lexicalScore`, which retrieval computed over the conversation
|
|
20
|
+
* (earlier turns widen recall). Re-emitting it would let a previous turn's
|
|
21
|
+
* words decide the order of this turn's answer; re-scoring costs one pass
|
|
22
|
+
* over the already-narrowed candidate set.
|
|
23
|
+
*/
|
|
24
|
+
rank(query: string, candidates: AskItem[]): Promise<{
|
|
25
|
+
ranked: RankedCandidate[];
|
|
26
|
+
usage: RankerUsage;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic, zero-cost retrieval over a site's items. It is both the first
|
|
3
|
+
* stage for every ranker (the candidate set an LLM/jev ranker re-scores) and a
|
|
4
|
+
* complete ranker on its own, so /ask always answers even with no credentials.
|
|
5
|
+
*/
|
|
6
|
+
/** Dropped because they match nearly every item and so carry no signal. */
|
|
7
|
+
const STOP = new Set(["the", "a", "an", "for", "of", "and", "or", "to", "in", "on", "with", "is", "are"]);
|
|
8
|
+
/** Lowercase, split on non-alphanumerics, drop stopwords and single characters (noise from units/initials). */
|
|
9
|
+
export function tokenize(s) {
|
|
10
|
+
return s
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.split(/[^a-z0-9]+/)
|
|
13
|
+
.filter((t) => t.length > 1 && !STOP.has(t));
|
|
14
|
+
}
|
|
15
|
+
function fieldTokens(item) {
|
|
16
|
+
return {
|
|
17
|
+
name: new Set(tokenize(item.name)),
|
|
18
|
+
body: new Set([...tokenize(item.text), ...(item.keywords ?? []).flatMap(tokenize)]),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Fraction of the query matched, with a name hit worth twice a body hit — a
|
|
23
|
+
* product whose TITLE is "steel bolt" beats one that merely mentions steel.
|
|
24
|
+
* Normalised by the best possible score so it stays in [0, 1] across queries.
|
|
25
|
+
*/
|
|
26
|
+
function scoreItem(queryTokens, item) {
|
|
27
|
+
if (queryTokens.length === 0)
|
|
28
|
+
return 0;
|
|
29
|
+
const { name, body } = fieldTokens(item);
|
|
30
|
+
let hits = 0;
|
|
31
|
+
for (const t of queryTokens)
|
|
32
|
+
hits += name.has(t) ? 2 : body.has(t) ? 1 : 0;
|
|
33
|
+
return hits / (2 * queryTokens.length);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Top-`topK` matching items, best first. Ties break by `id` ascending so the
|
|
37
|
+
* same corpus in any order yields the same answer — a benchmark that grades an
|
|
38
|
+
* agent's answer cannot tolerate result order depending on row order.
|
|
39
|
+
* Items scoring 0 are dropped: "no match" is an empty list, never a bad list.
|
|
40
|
+
*/
|
|
41
|
+
export function retrieve(query, items, topK) {
|
|
42
|
+
const q = tokenize(query);
|
|
43
|
+
return items
|
|
44
|
+
.map((item) => {
|
|
45
|
+
const score = scoreItem(q, item);
|
|
46
|
+
return { item: { ...item, lexicalScore: score }, score };
|
|
47
|
+
})
|
|
48
|
+
.filter((c) => c.score > 0)
|
|
49
|
+
.sort((a, b) => b.score - a.score || (a.item.id < b.item.id ? -1 : a.item.id > b.item.id ? 1 : 0))
|
|
50
|
+
.slice(0, topK);
|
|
51
|
+
}
|
|
52
|
+
const FREE = { calls: 0, inputTokens: 0, latencyMs: 0, costUsd: 0, degraded: false };
|
|
53
|
+
/** The zero-cost default ranker, and the fallback every paid ranker degrades to. */
|
|
54
|
+
export class LexicalRanker {
|
|
55
|
+
kind = "lexical";
|
|
56
|
+
/**
|
|
57
|
+
* Scores against the `query` it is handed — the CURRENT turn — rather than
|
|
58
|
+
* re-emitting `lexicalScore`, which retrieval computed over the conversation
|
|
59
|
+
* (earlier turns widen recall). Re-emitting it would let a previous turn's
|
|
60
|
+
* words decide the order of this turn's answer; re-scoring costs one pass
|
|
61
|
+
* over the already-narrowed candidate set.
|
|
62
|
+
*/
|
|
63
|
+
async rank(query, candidates) {
|
|
64
|
+
const q = tokenize(query);
|
|
65
|
+
const ranked = candidates.map((c) => ({ id: c.id, score: scoreItem(q, c) }));
|
|
66
|
+
return { ranked, usage: { ...FREE } };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=lexical.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lexical.js","sourceRoot":"","sources":["../../src/nlweb/lexical.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AAEH,2EAA2E;AAC3E,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;AAE1G,+GAA+G;AAC/G,MAAM,UAAU,QAAQ,CAAC,CAAS;IAChC,OAAO,CAAC;SACL,WAAW,EAAE;SACb,KAAK,CAAC,YAAY,CAAC;SACnB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,WAAW,CAAC,IAAa;IAChC,OAAO;QACL,IAAI,EAAE,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClC,IAAI,EAAE,IAAI,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;KACpF,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,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CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SACjG,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AACpB,CAAC;AAED,MAAM,IAAI,GAAgB,EAAE,KAAK,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAElG,oFAAoF;AACpF,MAAM,OAAO,aAAa;IACf,IAAI,GAAG,SAAS,CAAC;IAE1B;;;;;;OAMG;IACH,KAAK,CAAC,IAAI,CAAC,KAAa,EAAE,UAAqB;QAC7C,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC1B,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC7E,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC;IACxC,CAAC;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { RemoteRanker, type FetchInit, type RemoteRankerOptions, type Tokens, type Verdict } from "./remote-ranker.js";
|
|
2
|
+
import type { AskItem } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* NLWeb's own ranking step: one small model call per candidate, scoring the
|
|
5
|
+
* item's JSON-LD against the question. Per-item (rather than one call ranking
|
|
6
|
+
* the whole list) is what NLWeb does and what makes it parallelisable and
|
|
7
|
+
* degradable — a failed item costs that item's score, not the answer.
|
|
8
|
+
*
|
|
9
|
+
* The endpoint is provider-agnostic: anything speaking the Anthropic Messages
|
|
10
|
+
* format (the vendor API, a gateway, a local mock) is configured by base URL.
|
|
11
|
+
*/
|
|
12
|
+
/** Verbatim from NLWeb's ranking prompt; changing it changes what the benchmark measures. */
|
|
13
|
+
export declare const NLWEB_RANKING_PROMPT = "Assign a score between 0 and 100 to the following item based on how relevant it is to the user's question. If the score is above 50, provide a short description. If the score is below 75, in the description, include the reason why it is still relevant. Answer ONLY with JSON {\"score\": <integer>, \"description\": \"<short>\"}.";
|
|
14
|
+
/**
|
|
15
|
+
* The first balanced `{...}` in the reply. Models prepend "Here is the JSON:"
|
|
16
|
+
* often enough that a whole-string JSON.parse would degrade good answers; a
|
|
17
|
+
* brace scan (string-aware, so a `}` inside a description does not end it) is
|
|
18
|
+
* the cheapest thing that survives that.
|
|
19
|
+
*/
|
|
20
|
+
export declare function firstJsonObject(text: string): unknown;
|
|
21
|
+
export type LlmRankerOptions = RemoteRankerOptions & {
|
|
22
|
+
inputUsdPerMTok?: number;
|
|
23
|
+
outputUsdPerMTok?: number;
|
|
24
|
+
};
|
|
25
|
+
export declare class LlmRanker extends RemoteRanker {
|
|
26
|
+
readonly kind = "llm";
|
|
27
|
+
private readonly inputUsdPerMTok;
|
|
28
|
+
private readonly outputUsdPerMTok;
|
|
29
|
+
constructor(o: LlmRankerOptions);
|
|
30
|
+
protected request(query: string, item: AskItem): {
|
|
31
|
+
url: string;
|
|
32
|
+
init: FetchInit;
|
|
33
|
+
};
|
|
34
|
+
protected usageOf(body: unknown): Tokens;
|
|
35
|
+
protected verdictOf(body: unknown): Verdict;
|
|
36
|
+
protected costUsd(t: Tokens): number;
|
|
37
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { RemoteRanker } from "./remote-ranker.js";
|
|
3
|
+
/**
|
|
4
|
+
* NLWeb's own ranking step: one small model call per candidate, scoring the
|
|
5
|
+
* item's JSON-LD against the question. Per-item (rather than one call ranking
|
|
6
|
+
* the whole list) is what NLWeb does and what makes it parallelisable and
|
|
7
|
+
* degradable — a failed item costs that item's score, not the answer.
|
|
8
|
+
*
|
|
9
|
+
* The endpoint is provider-agnostic: anything speaking the Anthropic Messages
|
|
10
|
+
* format (the vendor API, a gateway, a local mock) is configured by base URL.
|
|
11
|
+
*/
|
|
12
|
+
/** Verbatim from NLWeb's ranking prompt; changing it changes what the benchmark measures. */
|
|
13
|
+
export const NLWEB_RANKING_PROMPT = `Assign a score between 0 and 100 to the following item based on how relevant it is to the user's question. If the score is above 50, provide a short description. If the score is below 75, in the description, include the reason why it is still relevant. Answer ONLY with JSON {"score": <integer>, "description": "<short>"}.`;
|
|
14
|
+
const ANTHROPIC_VERSION = "2023-06-01";
|
|
15
|
+
/** A score plus one sentence; anything longer is the model ignoring the prompt. */
|
|
16
|
+
const MAX_TOKENS = 200;
|
|
17
|
+
const DEFAULT_INPUT_USD_PER_MTOK = 1.0;
|
|
18
|
+
const DEFAULT_OUTPUT_USD_PER_MTOK = 5.0;
|
|
19
|
+
/** Read separately from the content, so a reply we cannot score is still billed. */
|
|
20
|
+
const messagesUsage = z.object({ usage: z.object({ input_tokens: z.number(), output_tokens: z.number() }) });
|
|
21
|
+
const messagesContent = z.object({ content: z.array(z.object({ type: z.string(), text: z.string().optional() })) });
|
|
22
|
+
const scoreJson = z.object({ score: z.number(), description: z.string().optional() });
|
|
23
|
+
/**
|
|
24
|
+
* The first balanced `{...}` in the reply. Models prepend "Here is the JSON:"
|
|
25
|
+
* often enough that a whole-string JSON.parse would degrade good answers; a
|
|
26
|
+
* brace scan (string-aware, so a `}` inside a description does not end it) is
|
|
27
|
+
* the cheapest thing that survives that.
|
|
28
|
+
*/
|
|
29
|
+
export function firstJsonObject(text) {
|
|
30
|
+
const start = text.indexOf("{");
|
|
31
|
+
if (start < 0)
|
|
32
|
+
throw new Error("llm ranker: reply contained no JSON object");
|
|
33
|
+
let depth = 0;
|
|
34
|
+
let inString = false;
|
|
35
|
+
let escaped = false;
|
|
36
|
+
for (let i = start; i < text.length; i++) {
|
|
37
|
+
const ch = text[i];
|
|
38
|
+
if (inString) {
|
|
39
|
+
if (escaped)
|
|
40
|
+
escaped = false;
|
|
41
|
+
else if (ch === "\\")
|
|
42
|
+
escaped = true;
|
|
43
|
+
else if (ch === '"')
|
|
44
|
+
inString = false;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (ch === '"')
|
|
48
|
+
inString = true;
|
|
49
|
+
else if (ch === "{")
|
|
50
|
+
depth++;
|
|
51
|
+
else if (ch === "}" && --depth === 0)
|
|
52
|
+
return JSON.parse(text.slice(start, i + 1));
|
|
53
|
+
}
|
|
54
|
+
throw new Error("llm ranker: reply contained an unterminated JSON object");
|
|
55
|
+
}
|
|
56
|
+
export class LlmRanker extends RemoteRanker {
|
|
57
|
+
kind = "llm";
|
|
58
|
+
inputUsdPerMTok;
|
|
59
|
+
outputUsdPerMTok;
|
|
60
|
+
constructor(o) {
|
|
61
|
+
super(o);
|
|
62
|
+
this.inputUsdPerMTok = o.inputUsdPerMTok ?? DEFAULT_INPUT_USD_PER_MTOK;
|
|
63
|
+
this.outputUsdPerMTok = o.outputUsdPerMTok ?? DEFAULT_OUTPUT_USD_PER_MTOK;
|
|
64
|
+
}
|
|
65
|
+
request(query, item) {
|
|
66
|
+
return {
|
|
67
|
+
url: `${this.baseUrl}/v1/messages`,
|
|
68
|
+
init: {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "x-api-key": this.apiKey, "anthropic-version": ANTHROPIC_VERSION, "content-type": "application/json" },
|
|
71
|
+
// The item's JSON-LD, not its prose: the model scores exactly what an
|
|
72
|
+
// agent would receive back, so the score explains the result it grades.
|
|
73
|
+
body: JSON.stringify({
|
|
74
|
+
model: this.model,
|
|
75
|
+
max_tokens: MAX_TOKENS,
|
|
76
|
+
system: NLWEB_RANKING_PROMPT,
|
|
77
|
+
messages: [{ role: "user", content: `Question: ${query}\n\nItem: ${JSON.stringify(item.schema)}` }],
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
usageOf(body) {
|
|
83
|
+
const { usage } = messagesUsage.parse(body);
|
|
84
|
+
return { inputTokens: usage.input_tokens, outputTokens: usage.output_tokens };
|
|
85
|
+
}
|
|
86
|
+
verdictOf(body) {
|
|
87
|
+
const text = messagesContent.parse(body).content.find((b) => b.type === "text")?.text;
|
|
88
|
+
if (!text)
|
|
89
|
+
throw new Error("llm ranker: reply had no text block");
|
|
90
|
+
const scored = scoreJson.parse(firstJsonObject(text));
|
|
91
|
+
const description = scored.description?.trim();
|
|
92
|
+
return { score: scored.score / 100, ...(description ? { description } : {}) };
|
|
93
|
+
}
|
|
94
|
+
costUsd(t) {
|
|
95
|
+
return (t.inputTokens * this.inputUsdPerMTok + t.outputTokens * this.outputUsdPerMTok) / 1e6;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=llm-ranker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-ranker.js","sourceRoot":"","sources":["../../src/nlweb/llm-ranker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAuE,MAAM,oBAAoB,CAAC;AAGvH;;;;;;;;GAQG;AAEH,6FAA6F;AAC7F,MAAM,CAAC,MAAM,oBAAoB,GAAG,oUAAoU,CAAC;AAEzW,MAAM,iBAAiB,GAAG,YAAY,CAAC;AACvC,mFAAmF;AACnF,MAAM,UAAU,GAAG,GAAG,CAAC;AACvB,MAAM,0BAA0B,GAAG,GAAG,CAAC;AACvC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAExC,oFAAoF;AACpF,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC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AE,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5C,OAAO,EAAE,WAAW,EAAE,KAAK,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,CAAC,aAAa,EAAE,CAAC;IAChF,CAAC;IAEkB,SAAS,CAAC,IAAa;QACxC,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,EAAE,IAAI,CAAC;QACtF,IAAI,CAAC,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAClE,MAAM,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;QACtD,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC;QAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,GAAG,GAAG,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;IAChF,CAAC;IAEkB,OAAO,CAAC,CAAS;QAClC,OAAO,CAAC,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC,YAAY,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,GAAG,CAAC;IAC/F,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A four-line concurrency gate, in-tree rather than a dependency: a ranker
|
|
3
|
+
* fans one request out per candidate, and an unbounded fan-out is how a 25-item
|
|
4
|
+
* /ask turns into a provider rate-limit storm (which then degrades the answer).
|
|
5
|
+
*/
|
|
6
|
+
/** Runs `task` when a slot is free; resolves/rejects with the task's own outcome. */
|
|
7
|
+
export type Limiter = <T>(task: () => Promise<T>) => Promise<T>;
|
|
8
|
+
export declare function pLimit(concurrency: number): Limiter;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A four-line concurrency gate, in-tree rather than a dependency: a ranker
|
|
3
|
+
* fans one request out per candidate, and an unbounded fan-out is how a 25-item
|
|
4
|
+
* /ask turns into a provider rate-limit storm (which then degrades the answer).
|
|
5
|
+
*/
|
|
6
|
+
export function pLimit(concurrency) {
|
|
7
|
+
const max = Math.max(1, Math.floor(concurrency));
|
|
8
|
+
const waiting = [];
|
|
9
|
+
let active = 0;
|
|
10
|
+
// Released in `finally`, so a rejected task frees its slot too — otherwise one
|
|
11
|
+
// upstream failure would wedge every remaining candidate behind it.
|
|
12
|
+
const release = () => {
|
|
13
|
+
active--;
|
|
14
|
+
waiting.shift()?.();
|
|
15
|
+
};
|
|
16
|
+
return (task) => new Promise((resolve, reject) => {
|
|
17
|
+
const start = () => {
|
|
18
|
+
active++;
|
|
19
|
+
void task().then(resolve, reject).finally(release);
|
|
20
|
+
};
|
|
21
|
+
if (active < max)
|
|
22
|
+
start();
|
|
23
|
+
else
|
|
24
|
+
waiting.push(start);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=p-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"p-limit.js","sourceRoot":"","sources":["../../src/nlweb/p-limit.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,MAAM,UAAU,MAAM,CAAC,WAAmB;IACxC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC;IACjD,MAAM,OAAO,GAAmB,EAAE,CAAC;IACnC,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,+EAA+E;IAC/E,oEAAoE;IACpE,MAAM,OAAO,GAAG,GAAS,EAAE;QACzB,MAAM,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,CAAC;IACtB,CAAC,CAAC;IAEF,OAAO,CAAI,IAAsB,EAAc,EAAE,CAC/C,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACjC,MAAM,KAAK,GAAG,GAAS,EAAE;YACvB,MAAM,EAAE,CAAC;YACT,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACrD,CAAC,CAAC;QACF,IAAI,MAAM,GAAG,GAAG;YAAE,KAAK,EAAE,CAAC;;YACrB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { RankerRegistry, type Ranker } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Which ranker a deployment runs, and the credentials it needs. Selection is
|
|
5
|
+
* env-driven so the SAME site image serves a lexical, an llm and a jev arm of
|
|
6
|
+
* the grid — the only difference between the arms is this config.
|
|
7
|
+
*
|
|
8
|
+
* A missing key is a BOOT failure, never a quiet fall back to lexical: an arm
|
|
9
|
+
* that silently answers lexically would be recorded as an llm/jev measurement
|
|
10
|
+
* and poison every comparison made from it.
|
|
11
|
+
*/
|
|
12
|
+
export declare const rankerEnvSchema: z.ZodObject<{
|
|
13
|
+
ASK_RANKER: z.ZodDefault<z.ZodEnum<["lexical", "llm", "jev"]>>;
|
|
14
|
+
/** Any Anthropic-Messages-compatible endpoint: the vendor API, a gateway, a mock. */
|
|
15
|
+
LLM_BASE_URL: z.ZodOptional<z.ZodString>;
|
|
16
|
+
LLM_API_KEY: z.ZodOptional<z.ZodString>;
|
|
17
|
+
LLM_MODEL: z.ZodDefault<z.ZodString>;
|
|
18
|
+
/** Priced per run rather than hardcoded, so a model swap does not silently misreport cost. */
|
|
19
|
+
LLM_INPUT_USD_PER_MTOK: z.ZodDefault<z.ZodNumber>;
|
|
20
|
+
LLM_OUTPUT_USD_PER_MTOK: z.ZodDefault<z.ZodNumber>;
|
|
21
|
+
JEV_BASE_URL: z.ZodDefault<z.ZodString>;
|
|
22
|
+
JEV_API_KEY: z.ZodOptional<z.ZodString>;
|
|
23
|
+
JEV_MODEL: z.ZodDefault<z.ZodString>;
|
|
24
|
+
/** Priced per run like the llm prices above; jev writes no tokens, so there is no output price. */
|
|
25
|
+
JEV_INPUT_USD_PER_MTOK: z.ZodDefault<z.ZodNumber>;
|
|
26
|
+
/** In-flight upstream requests per /ask; one request is sent per candidate. */
|
|
27
|
+
RANKER_CONCURRENCY: z.ZodDefault<z.ZodNumber>;
|
|
28
|
+
}, "strip", z.ZodTypeAny, {
|
|
29
|
+
ASK_RANKER: "lexical" | "jev" | "llm";
|
|
30
|
+
LLM_MODEL: string;
|
|
31
|
+
LLM_INPUT_USD_PER_MTOK: number;
|
|
32
|
+
LLM_OUTPUT_USD_PER_MTOK: number;
|
|
33
|
+
JEV_BASE_URL: string;
|
|
34
|
+
JEV_MODEL: string;
|
|
35
|
+
JEV_INPUT_USD_PER_MTOK: number;
|
|
36
|
+
RANKER_CONCURRENCY: number;
|
|
37
|
+
LLM_BASE_URL?: string | undefined;
|
|
38
|
+
LLM_API_KEY?: string | undefined;
|
|
39
|
+
JEV_API_KEY?: string | undefined;
|
|
40
|
+
}, {
|
|
41
|
+
ASK_RANKER?: "lexical" | "jev" | "llm" | undefined;
|
|
42
|
+
LLM_BASE_URL?: string | undefined;
|
|
43
|
+
LLM_API_KEY?: string | undefined;
|
|
44
|
+
LLM_MODEL?: string | undefined;
|
|
45
|
+
LLM_INPUT_USD_PER_MTOK?: number | undefined;
|
|
46
|
+
LLM_OUTPUT_USD_PER_MTOK?: number | undefined;
|
|
47
|
+
JEV_BASE_URL?: string | undefined;
|
|
48
|
+
JEV_API_KEY?: string | undefined;
|
|
49
|
+
JEV_MODEL?: string | undefined;
|
|
50
|
+
JEV_INPUT_USD_PER_MTOK?: number | undefined;
|
|
51
|
+
RANKER_CONCURRENCY?: number | undefined;
|
|
52
|
+
}>;
|
|
53
|
+
export type RankerEnv = z.infer<typeof rankerEnvSchema>;
|
|
54
|
+
/** Apps merge this into their own config schema, so one `readConfig` validates everything. */
|
|
55
|
+
export declare function readRankerEnv(env: NodeJS.ProcessEnv): RankerEnv;
|
|
56
|
+
/**
|
|
57
|
+
* The three kinds that ship. A fourth ranker is a `register` call here (or on a
|
|
58
|
+
* registry a site owns) — never a branch in /ask or in this function.
|
|
59
|
+
*/
|
|
60
|
+
export declare function defaultRankerRegistry(): RankerRegistry;
|
|
61
|
+
/** The ranker `ASK_RANKER` names; throws (failing boot) if it is unknown or unconfigured. */
|
|
62
|
+
export declare function buildRanker(env?: NodeJS.ProcessEnv, registry?: RankerRegistry): Ranker;
|