feat: make FactExtractor extracts multiple facts per input
This commit is contained in:
@@ -2,11 +2,15 @@ import { IdentityDBError } from '../core/errors';
|
||||
import { normalizeTopicName } from '../core/utils';
|
||||
import type { FactExtractor, ExtractedFact } from './types';
|
||||
|
||||
export async function extractFact(
|
||||
export async function extractFacts(
|
||||
input: string,
|
||||
extractor: FactExtractor,
|
||||
): Promise<ExtractedFact> {
|
||||
): Promise<ExtractedFact[]> {
|
||||
const extracted = await extractor.extract(input);
|
||||
return extracted.map((fact) => validateAndNormalizeFact(input, fact));
|
||||
}
|
||||
|
||||
function validateAndNormalizeFact(input: string, extracted: ExtractedFact): ExtractedFact {
|
||||
const statement = extracted.statement?.trim() || input.trim();
|
||||
|
||||
if (statement.length === 0) {
|
||||
@@ -31,12 +35,12 @@ export async function extractFact(
|
||||
throw new IdentityDBError('Extractor returned no usable topics.');
|
||||
}
|
||||
|
||||
return {
|
||||
statement,
|
||||
summary: extracted.summary ?? null,
|
||||
source: extracted.source ?? null,
|
||||
confidence: extracted.confidence ?? null,
|
||||
metadata: extracted.metadata ?? null,
|
||||
topics: Array.from(dedupedTopics.values()),
|
||||
};
|
||||
return {
|
||||
statement,
|
||||
summary: extracted.summary ?? null,
|
||||
source: extracted.source ?? null,
|
||||
confidence: extracted.confidence ?? null,
|
||||
metadata: extracted.metadata ?? null,
|
||||
topics: Array.from(dedupedTopics.values()),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user