25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
import type {
|
|
ExtractedFact,
|
|
FactExtractor,
|
|
LlmFactExtractorOptions,
|
|
} from "./types";
|
|
|
|
const DEFAULT_INSTRUCTIONS = [
|
|
"Extract one structured fact from the user input.",
|
|
"Return JSON only. Do not include markdown, explanations, or prose outside the JSON object.",
|
|
'Use this shape: {"statement": string?, "summary": string|null, "source": string|null, "confidence": number|null, "metadata": object|null, "topics": Array<{"name": string, "category": "entity"|"concept"|"temporal"|"custom"?, "granularity": "abstract"|"concrete"|"mixed"?, "role": string|null, "description": string|null, "metadata": object|null}>}.',
|
|
'Only include topics that are explicitly in the input as-is. For example, topic "I started TypeScript since 2015" can be "I", "TypeScript", "2015".',
|
|
].join("\n");
|
|
|
|
export class LlmFactExtractor implements FactExtractor {
|
|
constructor(private readonly options: LlmFactExtractorOptions) {}
|
|
|
|
async extract(input: string): Promise<ExtractedFact> {
|
|
return this.options.model.generateText({
|
|
instruction: DEFAULT_INSTRUCTIONS,
|
|
input,
|
|
additionalInstruction: this.options.additionalInstructions,
|
|
});
|
|
}
|
|
}
|