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