27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
import type {
|
|
ExtractedFact,
|
|
FactExtractor,
|
|
LlmFactExtractorOptions,
|
|
} from "./types";
|
|
|
|
const DEFAULT_INSTRUCTIONS = [
|
|
"Extract structured facts from the user input.",
|
|
"Return a JSON array of fact objects. Do not include markdown, explanations, or prose outside the JSON array.",
|
|
'Each fact object must have a "statement", "summary", "source", "confidence", and "topics" array.',
|
|
'Each topic in "topics" must have a "name", and may include "category", "granularity", and "role".',
|
|
"Only include topics that are explicitly in the input.",
|
|
"If the input contains multiple distinct facts, return them as separate objects in the array.",
|
|
].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,
|
|
});
|
|
}
|
|
}
|