feat: make extract input structured
This commit is contained in:
@@ -15,25 +15,10 @@ export class LlmFactExtractor implements FactExtractor {
|
||||
constructor(private readonly options: LlmFactExtractorOptions) {}
|
||||
|
||||
async extract(input: string): Promise<ExtractedFact> {
|
||||
const prompt = this.buildPrompt(input);
|
||||
return this.options.model.generateText(prompt);
|
||||
}
|
||||
|
||||
private buildPrompt(input: string): string {
|
||||
if (this.options.promptBuilder) {
|
||||
return this.options.promptBuilder(input, this.options.instructions);
|
||||
}
|
||||
|
||||
const instructions = this.options.instructions?.trim();
|
||||
|
||||
return [
|
||||
DEFAULT_INSTRUCTIONS,
|
||||
instructions && instructions.length > 0
|
||||
? `Additional instructions:\n${instructions}`
|
||||
: null,
|
||||
`Input:\n${input.trim()}`,
|
||||
]
|
||||
.filter((value): value is string => value !== null)
|
||||
.join("\n\n");
|
||||
return this.options.model.generateText({
|
||||
instruction: DEFAULT_INSTRUCTIONS,
|
||||
input,
|
||||
additionalInstruction: this.options.additionalInstructions,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ import type {
|
||||
AddFactInput,
|
||||
EmbeddingProvider,
|
||||
TopicLinkInput,
|
||||
} from '../types/api';
|
||||
} from "../types/api";
|
||||
|
||||
export interface ExtractedFact {
|
||||
statement?: string;
|
||||
summary?: string | null;
|
||||
source?: string | null;
|
||||
confidence?: number | null;
|
||||
metadata?: AddFactInput['metadata'];
|
||||
metadata?: AddFactInput["metadata"];
|
||||
topics: TopicLinkInput[];
|
||||
}
|
||||
|
||||
@@ -17,14 +17,19 @@ export interface FactExtractor {
|
||||
extract(input: string): Promise<ExtractedFact>;
|
||||
}
|
||||
|
||||
export interface LlmTextGenerationModelInput {
|
||||
instruction: string;
|
||||
input: string;
|
||||
additionalInstruction?: string | undefined;
|
||||
}
|
||||
|
||||
export interface LlmTextGenerationModel {
|
||||
generateText(prompt: string): Promise<ExtractedFact>;
|
||||
generateText(prompt: LlmTextGenerationModelInput): Promise<ExtractedFact>;
|
||||
}
|
||||
|
||||
export interface LlmFactExtractorOptions {
|
||||
model: LlmTextGenerationModel;
|
||||
instructions?: string;
|
||||
promptBuilder?: (input: string, instructions?: string) => string;
|
||||
additionalInstructions?: string | undefined;
|
||||
}
|
||||
|
||||
export interface IngestStatementOptions {
|
||||
|
||||
Reference in New Issue
Block a user