@devilsdev/rag-core 3.4.0 → 3.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/README.md +1 -1
- package/dist/all.d.ts +58 -0
- package/dist/index.cjs +275 -15
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/types/adapters/mastra.d.ts +6 -2
- package/dist/types/agentic/agentic-pipeline.d.ts +1 -1
- package/dist/types/agentic/iterative-retriever.d.ts +1 -1
- package/dist/types/agentic/query-planner.d.ts +1 -1
- package/dist/types/agentic/self-critique.d.ts +1 -1
- package/dist/types/ai/federation/federated-learning-coordinator.d.ts +1 -1
- package/dist/types/ai/training/model-training-orchestrator.d.ts +1 -1
- package/dist/types/chunking/chunking-engine.d.ts +1 -1
- package/dist/types/citation/citation-tracker.d.ts +1 -1
- package/dist/types/connectors/anthropic-connector.d.ts +35 -1
- package/dist/types/connectors/base-connector.d.ts +1 -1
- package/dist/types/connectors/connector-utils.d.ts +20 -0
- package/dist/types/cost/cost-calculator.d.ts +1 -1
- package/dist/types/cost/cost-tracker.d.ts +1 -1
- package/dist/types/cost/token-budget.d.ts +1 -1
- package/dist/types/dashboard/dashboard-generator.d.ts +1 -1
- package/dist/types/dashboard/metrics-aggregator.d.ts +1 -1
- package/dist/types/debug/execution-tracer.d.ts +1 -1
- package/dist/types/debug/trace-inspector.d.ts +1 -1
- package/dist/types/dev/dev-server.d.ts +1 -1
- package/dist/types/dev/hot-reload.d.ts +1 -1
- package/dist/types/ecosystem/plugin-analytics-dashboard.d.ts +1 -1
- package/dist/types/ecosystem/plugin-certification.d.ts +1 -1
- package/dist/types/ecosystem/plugin-hub.d.ts +1 -1
- package/dist/types/enterprise/audit-logging.d.ts +1 -1
- package/dist/types/enterprise/data-governance.d.ts +1 -1
- package/dist/types/enterprise/multi-tenancy.d.ts +1 -1
- package/dist/types/enterprise/sso-integration.d.ts +1 -1
- package/dist/types/evaluate/pipeline-evaluator.d.ts +1 -1
- package/dist/types/graph/entity-extractor.d.ts +1 -1
- package/dist/types/graph/graph-retriever.d.ts +1 -1
- package/dist/types/graph/knowledge-graph.d.ts +1 -1
- package/dist/types/grounding/llm-judge.d.ts +10 -3
- package/dist/types/grounding/provider-citations.d.ts +244 -0
- package/dist/types/guardrails/detectors.d.ts +88 -0
- package/dist/types/guardrails/evaluate-detectors.d.ts +61 -0
- package/dist/types/guardrails/guardrails-pipeline.d.ts +2 -9
- package/dist/types/guardrails/post-generation-guard.d.ts +33 -3
- package/dist/types/guardrails/pre-retrieval-guard.d.ts +29 -1
- package/dist/types/guardrails/retrieval-guard.d.ts +30 -1
- package/dist/types/mcp/mcp-server.d.ts +1 -1
- package/dist/types/observability/otel.d.ts +9 -3
- package/dist/types/observability/scores.d.ts +57 -0
- package/dist/types/reranker/cascade-reranker.d.ts +1 -1
- package/dist/types/reranker/embedding-reranker.d.ts +1 -1
- package/dist/types/reranker/scoring-reranker.d.ts +1 -1
- package/dist/types/retrieval/bm25-search.d.ts +1 -1
- package/dist/types/retrieval/hybrid-retriever.d.ts +1 -1
- package/dist/types/security/jwt-validator.d.ts +1 -1
- package/dist/types/security/plugin-sandbox.d.ts +1 -1
- package/dist/types/streaming/sse-adapter.d.ts +1 -1
- package/dist/types/streaming/stream-router.d.ts +1 -1
- package/dist/types/streaming/streaming-embedder.d.ts +1 -1
- package/dist/types/streaming/websocket-adapter.d.ts +1 -1
- package/dist/types/templates/scaffold.d.ts +1 -1
- package/dist/types/testing/contract-tests.d.ts +8 -4
- package/dist/types/utils/batch-processor.d.ts +1 -1
- package/dist/types/utils/connection-pool.d.ts +1 -1
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @extends EventEmitter
|
|
6
6
|
*/
|
|
7
|
-
export class SelfCritiqueChecker extends EventEmitter
|
|
7
|
+
export class SelfCritiqueChecker extends EventEmitter {
|
|
8
8
|
/**
|
|
9
9
|
* @param {object} [options] - Override default configuration
|
|
10
10
|
* @param {number} [options.approvalThreshold=0.5] - Minimum groundedness score to approve an answer
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @extends EventEmitter
|
|
6
6
|
*/
|
|
7
|
-
export class CitationTracker extends EventEmitter
|
|
7
|
+
export class CitationTracker extends EventEmitter {
|
|
8
8
|
/**
|
|
9
9
|
* @param {object} [options] - Override default configuration
|
|
10
10
|
* @param {number} [options.similarityThreshold=0.5] - Minimum similarity for a citation match
|
|
@@ -66,11 +66,37 @@ export class AnthropicConnector extends BaseConnector {
|
|
|
66
66
|
* @param {Array<Object|string>} [context=[]] - Retrieved documents for RAG
|
|
67
67
|
* @param {Object} [options={}] - Override model, temperature, maxTokens per-call
|
|
68
68
|
* @param {AbortSignal} [options.signal] - Cancels the request
|
|
69
|
-
* @returns {Promise<string>} The
|
|
69
|
+
* @returns {Promise<string>} The answer: every text block of the reply,
|
|
70
|
+
* joined. Rejects (code MODEL_REFUSED or NO_TEXT) when the reply has no
|
|
71
|
+
* text, such as a refusal or a reply that ran out of maxTokens while
|
|
72
|
+
* thinking.
|
|
70
73
|
*/
|
|
71
74
|
generate(query: string, context?: Array<any | string>, options?: {
|
|
72
75
|
signal?: AbortSignal;
|
|
73
76
|
}): Promise<string>;
|
|
77
|
+
/**
|
|
78
|
+
* Like generate(), with the reply's token usage. The LLM judge uses it so
|
|
79
|
+
* `check.judge.usage` reports what the judge cost.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} query
|
|
82
|
+
* @param {Array<Object|string>} [context=[]]
|
|
83
|
+
* @param {Object} [options={}] - As for generate()
|
|
84
|
+
* @returns {Promise<{text: string, usage: {inputTokens: number, outputTokens: number, totalTokens: number}}>}
|
|
85
|
+
*/
|
|
86
|
+
generateWithUsage(query: string, context?: Array<any | string>, options?: any): Promise<{
|
|
87
|
+
text: string;
|
|
88
|
+
usage: {
|
|
89
|
+
inputTokens: number;
|
|
90
|
+
outputTokens: number;
|
|
91
|
+
totalTokens: number;
|
|
92
|
+
};
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* POST a Messages request with retries; a model that rejects
|
|
96
|
+
* `temperature` gets the request again without it.
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
private _post;
|
|
74
100
|
/**
|
|
75
101
|
* Stream a response from Claude token-by-token via SSE.
|
|
76
102
|
*
|
|
@@ -108,6 +134,14 @@ export namespace DEFAULT_CONFIG {
|
|
|
108
134
|
export let retryDelay: number;
|
|
109
135
|
export const apiKey: string;
|
|
110
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* The answer text of a Messages API response: every `text` block, joined.
|
|
139
|
+
* Models that think reply with `thinking` blocks before the text, so the
|
|
140
|
+
* first block is not always the answer.
|
|
141
|
+
* @param {Object} data - The response body
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
export function responseText(data: any): string;
|
|
111
145
|
import { BaseConnector } from "./base-connector";
|
|
112
146
|
/**
|
|
113
147
|
* Default configuration for the Anthropic connector.
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Provides connection lifecycle, health checking, and retry logic.
|
|
4
4
|
* @extends EventEmitter
|
|
5
5
|
*/
|
|
6
|
-
export class BaseConnector extends EventEmitter
|
|
6
|
+
export class BaseConnector extends EventEmitter {
|
|
7
7
|
/**
|
|
8
8
|
* @param {Object} options
|
|
9
9
|
* @param {string} [options.name] - Connector display name
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An axios error without what it carries from the request. axios errors
|
|
3
|
+
* keep the request config, headers included, so logging or serializing one
|
|
4
|
+
* prints the API key. This copies what callers and the retry logic use
|
|
5
|
+
* (the response status, headers and body, and the network error code)
|
|
6
|
+
* into a plain Error, and puts the API's own explanation in the message:
|
|
7
|
+
* "Anthropic API error 401: authentication_error: invalid x-api-key".
|
|
8
|
+
*
|
|
9
|
+
* @param {*} err
|
|
10
|
+
* @param {string} provider - Name for the message, e.g. "Anthropic"
|
|
11
|
+
* @returns {*} A clean Error for an axios error; anything else unchanged
|
|
12
|
+
*/
|
|
13
|
+
export function toConnectorError(err: any, provider: string): any;
|
|
14
|
+
/**
|
|
15
|
+
* Make every error from an axios client a toConnectorError() one.
|
|
16
|
+
* @param {Object} client - From axios.create()
|
|
17
|
+
* @param {string} provider
|
|
18
|
+
* @returns {Object} The client
|
|
19
|
+
*/
|
|
20
|
+
export function cleanAxiosErrors(client: any, provider: string): any;
|
|
1
21
|
/**
|
|
2
22
|
* The text of a streamed response body, chunk by chunk. Handles what axios
|
|
3
23
|
* returns for `responseType: 'stream'` on each runtime: a Node stream of
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* const cost = calc.estimate('gpt-4o', 1500, 500);
|
|
7
7
|
* console.log(cost.totalCost); // 0.015
|
|
8
8
|
*/
|
|
9
|
-
export class CostCalculator extends EventEmitter
|
|
9
|
+
export class CostCalculator extends EventEmitter {
|
|
10
10
|
/**
|
|
11
11
|
* Provenance of the built-in pricing table.
|
|
12
12
|
* @returns {typeof PRICING_METADATA}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* console.log(result.cost); // { inputTokens, outputTokens, estimatedCost, model }
|
|
9
9
|
* console.log(tracker.getSummary());
|
|
10
10
|
*/
|
|
11
|
-
export class CostTracker extends EventEmitter
|
|
11
|
+
export class CostTracker extends EventEmitter {
|
|
12
12
|
/**
|
|
13
13
|
* @param {object} [options]
|
|
14
14
|
* @param {string} [options.model='gpt-3.5-turbo'] - Default LLM model for cost estimation.
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* const status = budget.check(2000, 0.10);
|
|
13
13
|
* if (status.allowed) budget.record(2000, 0.10);
|
|
14
14
|
*/
|
|
15
|
-
export class TokenBudget extends EventEmitter
|
|
15
|
+
export class TokenBudget extends EventEmitter {
|
|
16
16
|
/**
|
|
17
17
|
* @param {object} [options]
|
|
18
18
|
* @param {number} [options.maxTokens=Infinity] - Hard token limit.
|
|
@@ -16,7 +16,7 @@ export type DashboardGeneratorConfig = {
|
|
|
16
16
|
* Generates standalone HTML dashboards from pipeline metrics snapshots.
|
|
17
17
|
* @extends EventEmitter
|
|
18
18
|
*/
|
|
19
|
-
export class DashboardGenerator extends EventEmitter
|
|
19
|
+
export class DashboardGenerator extends EventEmitter {
|
|
20
20
|
/**
|
|
21
21
|
* @param {Partial<DashboardGeneratorConfig>} options
|
|
22
22
|
*/
|
|
@@ -71,7 +71,7 @@ export type ErrorData = {
|
|
|
71
71
|
* Collects and aggregates pipeline metrics into dashboard-ready snapshots.
|
|
72
72
|
* @extends EventEmitter
|
|
73
73
|
*/
|
|
74
|
-
export class MetricsAggregator extends EventEmitter
|
|
74
|
+
export class MetricsAggregator extends EventEmitter {
|
|
75
75
|
/**
|
|
76
76
|
* @param {Partial<MetricsAggregatorConfig>} options
|
|
77
77
|
*/
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Execution tracer that wraps pipelines to capture run traces
|
|
3
3
|
* @extends EventEmitter
|
|
4
4
|
*/
|
|
5
|
-
export class ExecutionTracer extends EventEmitter
|
|
5
|
+
export class ExecutionTracer extends EventEmitter {
|
|
6
6
|
/**
|
|
7
7
|
* Create a new ExecutionTracer
|
|
8
8
|
* @param {object} [options] - Tracer configuration
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Trace inspector that analyzes execution traces produced by ExecutionTracer
|
|
3
3
|
* @extends EventEmitter
|
|
4
4
|
*/
|
|
5
|
-
export class TraceInspector extends EventEmitter
|
|
5
|
+
export class TraceInspector extends EventEmitter {
|
|
6
6
|
/**
|
|
7
7
|
* Create a new TraceInspector
|
|
8
8
|
* @param {object} [options] - Inspector configuration
|
|
@@ -11,7 +11,7 @@ export function toIgnoreMatcher(patterns: Array<string | RegExp | Function>): (f
|
|
|
11
11
|
*
|
|
12
12
|
* Manages file watching and hot reloading of plugins and configuration.
|
|
13
13
|
*/
|
|
14
|
-
export class HotReloadManager extends EventEmitter
|
|
14
|
+
export class HotReloadManager extends EventEmitter {
|
|
15
15
|
constructor(options?: {});
|
|
16
16
|
options: {
|
|
17
17
|
enabled: boolean;
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* evaluator.on('evaluated', (result) => console.log(result.scores));
|
|
10
10
|
* const result = evaluator.evaluate({ query: 'What is RAG?', answer: '...', results: [...] });
|
|
11
11
|
*/
|
|
12
|
-
export class PipelineEvaluator extends EventEmitter
|
|
12
|
+
export class PipelineEvaluator extends EventEmitter {
|
|
13
13
|
/**
|
|
14
14
|
* Create a new PipelineEvaluator.
|
|
15
15
|
* @param {object} [options={}] - Configuration options
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Uses configurable regex patterns to identify named entities,
|
|
5
5
|
* dates, measurements, URLs, emails, and technical terms.
|
|
6
6
|
*/
|
|
7
|
-
export class EntityExtractor extends EventEmitter
|
|
7
|
+
export class EntityExtractor extends EventEmitter {
|
|
8
8
|
/**
|
|
9
9
|
* @param {Object} [options={}] - Extractor options
|
|
10
10
|
* @param {Object<string, RegExp>} [options.patterns] - Custom patterns to merge with defaults
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Combines entity-based graph traversal with token-overlap text scoring
|
|
5
5
|
* to rank and retrieve relevant documents from a corpus.
|
|
6
6
|
*/
|
|
7
|
-
export class GraphRetriever extends EventEmitter
|
|
7
|
+
export class GraphRetriever extends EventEmitter {
|
|
8
8
|
/**
|
|
9
9
|
* @param {Object} [options={}] - Retriever options
|
|
10
10
|
* @param {number} [options.traversalDepth=2] - Graph traversal depth
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Stores typed entities and directed relationships between them,
|
|
5
5
|
* supporting traversal, pattern-based querying, and adjacency lookups.
|
|
6
6
|
*/
|
|
7
|
-
export class KnowledgeGraph extends EventEmitter
|
|
7
|
+
export class KnowledgeGraph extends EventEmitter {
|
|
8
8
|
/**
|
|
9
9
|
* @param {Object} [options={}] - Graph configuration options
|
|
10
10
|
*/
|
|
@@ -41,15 +41,18 @@ export function parseJudgeVerdicts(raw: any, claimCount: number): {
|
|
|
41
41
|
*
|
|
42
42
|
* @param {Array<{text: string, classification: string}>} sentences - From
|
|
43
43
|
* CitationTracker's hallucination report
|
|
44
|
-
* @param {Array<{content: string}>} docs
|
|
44
|
+
* @param {Array<{id?: string, content: string}>} docs
|
|
45
|
+
* @param {Array<string[]>} [allowed] - Per sentence, the ids of the only
|
|
46
|
+
* docs that may support it (verifyCitations: the passages it cites)
|
|
45
47
|
* @returns {string[]}
|
|
46
48
|
*/
|
|
47
49
|
export function lexicalReasons(sentences: Array<{
|
|
48
50
|
text: string;
|
|
49
51
|
classification: string;
|
|
50
52
|
}>, docs: Array<{
|
|
53
|
+
id?: string;
|
|
51
54
|
content: string;
|
|
52
|
-
}>): string[];
|
|
55
|
+
}>, allowed?: Array<string[]>): string[];
|
|
53
56
|
/**
|
|
54
57
|
* Run the judge on the sentences that failed for lack of overlap and
|
|
55
58
|
* combine its verdicts with the lexical result.
|
|
@@ -63,9 +66,12 @@ export function lexicalReasons(sentences: Array<{
|
|
|
63
66
|
* @param {{generate: Function}} params.judge
|
|
64
67
|
* @param {Array<{text: string, classification: string, bestSource: Object|null}>} params.sentences
|
|
65
68
|
* @param {Array<{id: string, content: string}>} params.docs
|
|
69
|
+
* @param {Array<string[]>} [params.allowed] - Per sentence, the ids of the
|
|
70
|
+
* only docs that may support it; a verdict naming another doc is kept
|
|
71
|
+
* unsupported. Default: any doc.
|
|
66
72
|
* @returns {Promise<{verdicts: Array<Object>, calls: number, usage?: Object, error?: string}>}
|
|
67
73
|
*/
|
|
68
|
-
export function runJudge({ judge, sentences, docs }: {
|
|
74
|
+
export function runJudge({ judge, sentences, docs, allowed }: {
|
|
69
75
|
judge: {
|
|
70
76
|
generate: Function;
|
|
71
77
|
};
|
|
@@ -78,6 +84,7 @@ export function runJudge({ judge, sentences, docs }: {
|
|
|
78
84
|
id: string;
|
|
79
85
|
content: string;
|
|
80
86
|
}>;
|
|
87
|
+
allowed?: Array<string[]>;
|
|
81
88
|
}): Promise<{
|
|
82
89
|
verdicts: Array<any>;
|
|
83
90
|
calls: number;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One citation of a claim, normalised.
|
|
3
|
+
*/
|
|
4
|
+
export type ProviderCitation = {
|
|
5
|
+
/**
|
|
6
|
+
* - Your document id, the document title or index, a search result's source, a URL or an OpenAI file id
|
|
7
|
+
*/
|
|
8
|
+
sourceId: string;
|
|
9
|
+
title: string | null;
|
|
10
|
+
/**
|
|
11
|
+
* - Where the passage is; `type` is the provider's citation type
|
|
12
|
+
*/
|
|
13
|
+
location: any;
|
|
14
|
+
/**
|
|
15
|
+
* - The cited text; empty when the response doesn't carry it
|
|
16
|
+
*/
|
|
17
|
+
passages: string[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A claim of the answer and the citations attached to it.
|
|
21
|
+
*/
|
|
22
|
+
export type CitedClaim = {
|
|
23
|
+
text: string;
|
|
24
|
+
/**
|
|
25
|
+
* - Offset in `answer`
|
|
26
|
+
*/
|
|
27
|
+
start: number;
|
|
28
|
+
end: number;
|
|
29
|
+
citations: ProviderCitation[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* What fromAnthropicCitations() and fromOpenAIFileCitations() return.
|
|
33
|
+
*/
|
|
34
|
+
export type ProviderCitations = {
|
|
35
|
+
provider: "anthropic" | "openai";
|
|
36
|
+
/**
|
|
37
|
+
* - The answer text
|
|
38
|
+
*/
|
|
39
|
+
answer: string;
|
|
40
|
+
claims: CitedClaim[];
|
|
41
|
+
/**
|
|
42
|
+
* - Citations of other kinds, by type
|
|
43
|
+
*/
|
|
44
|
+
skipped: {
|
|
45
|
+
[x: string]: number;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* - OpenAI: whether the response had file search results
|
|
49
|
+
*/
|
|
50
|
+
results?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* - Claude: the message's stop_reason ("refusal" when Claude declined)
|
|
53
|
+
*/
|
|
54
|
+
stopReason?: string | null;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* A claim's verdict from verifyCitations().
|
|
58
|
+
*/
|
|
59
|
+
export type VerifiedClaim = {
|
|
60
|
+
text: string;
|
|
61
|
+
start: number;
|
|
62
|
+
end: number;
|
|
63
|
+
verdict: "supported" | "unsupported" | "unverifiable";
|
|
64
|
+
lexicalReason: "supported" | "low_overlap" | "number_mismatch" | "polarity_mismatch" | "unverifiable";
|
|
65
|
+
/**
|
|
66
|
+
* - Why, in words
|
|
67
|
+
*/
|
|
68
|
+
reason: string;
|
|
69
|
+
decidedBy: "lexical" | "judge";
|
|
70
|
+
/**
|
|
71
|
+
* - Support of the claim by all its cited passages together, 0..1
|
|
72
|
+
*/
|
|
73
|
+
score: number | null;
|
|
74
|
+
/**
|
|
75
|
+
* - Each citation's own support; null when it had no cited text
|
|
76
|
+
*/
|
|
77
|
+
citations: Array<{
|
|
78
|
+
sourceId: string;
|
|
79
|
+
title: string | null;
|
|
80
|
+
location: any;
|
|
81
|
+
score: number | null;
|
|
82
|
+
supports: boolean | null;
|
|
83
|
+
}>;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* What verifyCitations() resolves to.
|
|
87
|
+
*/
|
|
88
|
+
export type CitationVerification = {
|
|
89
|
+
/**
|
|
90
|
+
* - Not a refusal, at least one claim, every claim supported and every citation checkable; uncited sentences don't count against it (see `uncited`)
|
|
91
|
+
*/
|
|
92
|
+
verified: boolean;
|
|
93
|
+
provider: "anthropic" | "openai";
|
|
94
|
+
claims: VerifiedClaim[];
|
|
95
|
+
/**
|
|
96
|
+
* - Sentences with content that no citation covers
|
|
97
|
+
*/
|
|
98
|
+
uncited: Array<{
|
|
99
|
+
text: string;
|
|
100
|
+
start: number;
|
|
101
|
+
end: number;
|
|
102
|
+
}>;
|
|
103
|
+
summary: {
|
|
104
|
+
claims: number;
|
|
105
|
+
supported: number;
|
|
106
|
+
unsupported: number;
|
|
107
|
+
unverifiable: number;
|
|
108
|
+
citations: number;
|
|
109
|
+
unverifiableCitations: number;
|
|
110
|
+
uncitedSentences: number;
|
|
111
|
+
};
|
|
112
|
+
skipped: {
|
|
113
|
+
[x: string]: number;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* - Claude declined to answer (stop_reason "refusal"); never verified
|
|
117
|
+
*/
|
|
118
|
+
refused: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* - Only with the `judge` option
|
|
121
|
+
*/
|
|
122
|
+
judge?: {
|
|
123
|
+
calls: number;
|
|
124
|
+
usage?: any;
|
|
125
|
+
error?: string;
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Normalise the citations of a Claude Messages API response.
|
|
130
|
+
*
|
|
131
|
+
* Each text block that carries citations is a claim. Pass a stream's
|
|
132
|
+
* final message (`stream.finalMessage()`).
|
|
133
|
+
*
|
|
134
|
+
* @param {object} message - The Message, as the SDK returns it or as JSON
|
|
135
|
+
* @param {object} [options]
|
|
136
|
+
* @param {Array<object>} [options.documents] - The request's document blocks
|
|
137
|
+
* in order (`document_index` counts them), as your own `{ id }` records,
|
|
138
|
+
* so citations name your ids; otherwise a citation is named by the
|
|
139
|
+
* document's title, or its index
|
|
140
|
+
* @returns {ProviderCitations}
|
|
141
|
+
*/
|
|
142
|
+
export function fromAnthropicCitations(message: object, { documents }?: {
|
|
143
|
+
documents?: Array<object>;
|
|
144
|
+
}): ProviderCitations;
|
|
145
|
+
/**
|
|
146
|
+
* Normalise the file citations of an OpenAI Responses API response.
|
|
147
|
+
*
|
|
148
|
+
* A `file_citation` carries only `file_id`, `filename` and an `index` into
|
|
149
|
+
* the text, not the passage: the passages come from the file search
|
|
150
|
+
* results, which the request must ask for with
|
|
151
|
+
* `include: ["file_search_call.results"]`. Without them every citation is
|
|
152
|
+
* unverifiable. OpenAI gives a position, not a span, and puts it after the
|
|
153
|
+
* text it cites, a sentence or a paragraph (several citations can share
|
|
154
|
+
* it): each sentence from the previous citation's position up to this one
|
|
155
|
+
* is a claim carrying its citations. The boundary is inferred.
|
|
156
|
+
* Web and container citations are counted in `skipped`. Pass a stream's
|
|
157
|
+
* completed response.
|
|
158
|
+
*
|
|
159
|
+
* @param {object} response - The Response, as the SDK returns it or as JSON
|
|
160
|
+
* @returns {ProviderCitations}
|
|
161
|
+
*/
|
|
162
|
+
export function fromOpenAIFileCitations(response: object): ProviderCitations;
|
|
163
|
+
/**
|
|
164
|
+
* Check every claim against the passages it cites: together, so a claim
|
|
165
|
+
* that takes one fact from each of two passages is supported. Each
|
|
166
|
+
* citation's own score is reported too.
|
|
167
|
+
*
|
|
168
|
+
* @param {ProviderCitations} input - From fromAnthropicCitations() or fromOpenAIFileCitations()
|
|
169
|
+
* @param {object} [options]
|
|
170
|
+
* @param {{generate: Function}} [options.judge] - Opt-in LLM judge for claims
|
|
171
|
+
* that failed for lack of word overlap (paraphrases), as in checkAnswer();
|
|
172
|
+
* it may only name a passage the claim cites, and never overturns a
|
|
173
|
+
* number or negation mismatch. One call at most. Errors from
|
|
174
|
+
* `judge.generate` are not caught.
|
|
175
|
+
* @returns {Promise<CitationVerification>}
|
|
176
|
+
*/
|
|
177
|
+
export function verifyCitations(input: ProviderCitations, { judge }?: {
|
|
178
|
+
judge?: {
|
|
179
|
+
generate: Function;
|
|
180
|
+
};
|
|
181
|
+
}): Promise<CitationVerification>;
|
|
182
|
+
/**
|
|
183
|
+
* One citation of a claim, normalised.
|
|
184
|
+
*
|
|
185
|
+
* @typedef {Object} ProviderCitation
|
|
186
|
+
* @property {string} sourceId - Your document id, the document title or index, a search result's source, a URL or an OpenAI file id
|
|
187
|
+
* @property {string|null} title
|
|
188
|
+
* @property {Object} location - Where the passage is; `type` is the provider's citation type
|
|
189
|
+
* @property {string[]} passages - The cited text; empty when the response doesn't carry it
|
|
190
|
+
*/
|
|
191
|
+
/**
|
|
192
|
+
* A claim of the answer and the citations attached to it.
|
|
193
|
+
*
|
|
194
|
+
* @typedef {Object} CitedClaim
|
|
195
|
+
* @property {string} text
|
|
196
|
+
* @property {number} start - Offset in `answer`
|
|
197
|
+
* @property {number} end
|
|
198
|
+
* @property {ProviderCitation[]} citations
|
|
199
|
+
*/
|
|
200
|
+
/**
|
|
201
|
+
* What fromAnthropicCitations() and fromOpenAIFileCitations() return.
|
|
202
|
+
*
|
|
203
|
+
* @typedef {Object} ProviderCitations
|
|
204
|
+
* @property {'anthropic'|'openai'} provider
|
|
205
|
+
* @property {string} answer - The answer text
|
|
206
|
+
* @property {CitedClaim[]} claims
|
|
207
|
+
* @property {Object<string, number>} skipped - Citations of other kinds, by type
|
|
208
|
+
* @property {boolean} [results] - OpenAI: whether the response had file search results
|
|
209
|
+
* @property {string|null} [stopReason] - Claude: the message's stop_reason ("refusal" when Claude declined)
|
|
210
|
+
*/
|
|
211
|
+
/**
|
|
212
|
+
* A claim's verdict from verifyCitations().
|
|
213
|
+
*
|
|
214
|
+
* @typedef {Object} VerifiedClaim
|
|
215
|
+
* @property {string} text
|
|
216
|
+
* @property {number} start
|
|
217
|
+
* @property {number} end
|
|
218
|
+
* @property {'supported'|'unsupported'|'unverifiable'} verdict
|
|
219
|
+
* @property {'supported'|'low_overlap'|'number_mismatch'|'polarity_mismatch'|'unverifiable'} lexicalReason
|
|
220
|
+
* @property {string} reason - Why, in words
|
|
221
|
+
* @property {'lexical'|'judge'} decidedBy
|
|
222
|
+
* @property {number|null} score - Support of the claim by all its cited passages together, 0..1
|
|
223
|
+
* @property {Array<{sourceId: string, title: string|null, location: Object, score: number|null, supports: boolean|null}>} citations
|
|
224
|
+
* - Each citation's own support; null when it had no cited text
|
|
225
|
+
*/
|
|
226
|
+
/**
|
|
227
|
+
* What verifyCitations() resolves to.
|
|
228
|
+
*
|
|
229
|
+
* @typedef {Object} CitationVerification
|
|
230
|
+
* @property {boolean} verified - Not a refusal, at least one claim, every claim supported and every citation checkable; uncited sentences don't count against it (see `uncited`)
|
|
231
|
+
* @property {'anthropic'|'openai'} provider
|
|
232
|
+
* @property {VerifiedClaim[]} claims
|
|
233
|
+
* @property {Array<{text: string, start: number, end: number}>} uncited - Sentences with content that no citation covers
|
|
234
|
+
* @property {{claims: number, supported: number, unsupported: number, unverifiable: number, citations: number, unverifiableCitations: number, uncitedSentences: number}} summary
|
|
235
|
+
* @property {Object<string, number>} skipped
|
|
236
|
+
* @property {boolean} refused - Claude declined to answer (stop_reason "refusal"); never verified
|
|
237
|
+
* @property {{calls: number, usage?: Object, error?: string}} [judge] - Only with the `judge` option
|
|
238
|
+
*/
|
|
239
|
+
/** Sentences of `text` with their offsets (the splitSentences rule). */
|
|
240
|
+
export function sentencesWithOffsets(text: any): {
|
|
241
|
+
text: string;
|
|
242
|
+
start: number;
|
|
243
|
+
end: number;
|
|
244
|
+
}[];
|