agentfootprint
Version:
The explainable agent framework — backtrack a wrong answer to the exact context that caused it (evidence, not guesses). Built on footprintjs.
65 lines (64 loc) • 2.29 kB
TypeScript
/**
* llmFactExtractor — LLM-backed fact extractor.
*
* Uses an LLMProvider (typically a cheap model like Claude Haiku or
* GPT-4o-mini) to pull stable, timeless claims out of a conversation
* turn. Complements `patternFactExtractor` — regex catches the obvious
* self-disclosures, the LLM catches the open-ended ones.
*
* The extractor asks the LLM for a JSON response in this shape:
*
* ```json
* {
* "facts": [
* {
* "key": "user.name",
* "value": "Alice",
* "confidence": 0.95,
* "category": "identity",
* "refs": ["msg-1-0"]
* }
* ]
* }
* ```
*
* The extractor parses, clamps confidence via `asConfidence()`, dedups
* by `key` (last occurrence wins, matching `patternFactExtractor`),
* and returns the facts. Malformed responses fall back to `[]` — a bad
* extraction should not break the agent turn.
*
* Usage:
* ```ts
* import { anthropic } from 'agentfootprint';
* import { llmFactExtractor, factPipeline, InMemoryStore } from 'agentfootprint/memory';
*
* const pipeline = factPipeline({
* store: new InMemoryStore(),
* extractor: llmFactExtractor({ provider: yourLLMProvider }),
* });
* ```
*/
import type { LLMProvider } from '../../provider-contract/types.js';
import type { FactExtractor } from './extractor.js';
export interface LLMFactExtractorConfig {
/** The provider used for extraction. Typically a cheap/fast model. */
readonly provider: LLMProvider;
/**
* Override the system prompt. Defaults to a one-paragraph instruction
* that elicits the JSON shape described in the module docs.
*/
readonly systemPrompt?: string;
/**
* Dev-mode logger invoked when the LLM response fails to parse.
* Defaults to `console.warn` — production consumers can route the
* signal to their telemetry pipeline.
*/
readonly onParseError?: (error: unknown, rawContent: string) => void;
/**
* Include up to this many existing facts in the user prompt so the
* model can update / refine rather than duplicate. Set to `0` to
* skip — cheaper but loses update awareness. Default `16`.
*/
readonly includeExistingLimit?: number;
}
export declare function llmFactExtractor(config: LLMFactExtractorConfig): FactExtractor;