Skip to content

Commit

Permalink
fix: deduplicate graph result
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 committed May 25, 2024
1 parent 517eff7 commit dbdfd8d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/services/llamaindex/chating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {AppChatService, type ChatOptions, type ChatStreamEvent} from '@/core/ser
import {LlamaindexRetrieverWrapper, LlamaindexRetrieveService} from '@/core/services/llamaindex/retrieving';
import type {RetrieveOptions} from "@/core/services/retrieving";
import {type AppChatStreamSource, AppChatStreamState} from '@/lib/ai/AppChatStream';
import { deduplicateItems } from '@/lib/array-filters';
import {DocumentChunk, Entity, KnowledgeGraphClient, Relationship, SearchResult} from "@/lib/knowledge-graph/client";
import {uuidToBin} from '@/lib/kysely';
import {buildEmbedding} from '@/lib/llamaindex/builders/embedding';
Expand Down Expand Up @@ -177,8 +178,8 @@ export class LlamaindexChatService extends AppChatService {
);

// Flatten relationships and entities.
result.relationships = result.document_relationships.map(dr => dr.relationships).flat();
result.entities = result.document_relationships.map(dr => dr.entities).flat();
result.relationships = result.document_relationships.map(dr => dr.relationships).flat().filter(deduplicateItems('id'));
result.entities = result.document_relationships.map(dr => dr.entities).flat().filter(deduplicateItems('id'));
}

kgContext = result;
Expand Down Expand Up @@ -375,6 +376,7 @@ export class LlamaindexChatService extends AppChatService {
output: searchResult,
});
console.log(`[KG-Retrieving] Finish knowledge graph searching, take ${duration} ms.`);
// Fixme: DO NOT MUTATE THE LANGFUSE OUTPUT. Langfuse always delays it's push operation, mutations before pushing will affect langfuse tracking.
return searchResult;
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/array-filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function deduplicateItems<T, K extends keyof T> (property: K) {
const set = new Set<T[K]>();
return (item: T) => {
if (set.has(item[property])) {
return false;
}
set.add(item[property]);
return true;
};
}

0 comments on commit dbdfd8d

Please sign in to comment.