diff --git a/src/brainyData.ts b/src/brainyData.ts index 623716c6..46a16df2 100644 --- a/src/brainyData.ts +++ b/src/brainyData.ts @@ -755,6 +755,44 @@ export class BrainyData implements BrainyDataInterface { return searchResults } + /** + * Find entities similar to a given entity ID + * @param id ID of the entity to find similar entities for + * @param options Additional options + * @returns Array of search results with similarity scores + */ + public async findSimilar( + id: string, + options: { + limit?: number // Number of results to return + nounTypes?: string[] // Optional array of noun types to search within + includeVerbs?: boolean // Whether to include associated GraphVerbs in the results + searchMode?: 'local' | 'remote' | 'combined' // Where to search: local, remote, or both + } = {} + ): Promise[]> { + await this.ensureInitialized() + + // Get the entity by ID + const entity = await this.get(id) + if (!entity) { + throw new Error(`Entity with ID ${id} not found`) + } + + // Use the entity's vector to search for similar entities + const k = (options.limit || 10) + 1 // Add 1 to account for the original entity + const searchResults = await this.search(entity.vector, k, { + forceEmbed: false, + nounTypes: options.nounTypes, + includeVerbs: options.includeVerbs, + searchMode: options.searchMode + }) + + // Filter out the original entity and limit to the requested number + return searchResults + .filter(result => result.id !== id) + .slice(0, options.limit || 10) + } + /** * Get a vector by ID */ diff --git a/src/types/brainyDataInterface.ts b/src/types/brainyDataInterface.ts index ef35d5df..414932d5 100644 --- a/src/types/brainyDataInterface.ts +++ b/src/types/brainyDataInterface.ts @@ -44,4 +44,12 @@ export interface BrainyDataInterface { * @returns The ID of the created relationship */ relate(sourceId: string, targetId: string, relationType: string, metadata?: unknown): Promise + + /** + * Find entities similar to a given entity ID + * @param id ID of the entity to find similar entities for + * @param options Additional options + * @returns Array of search results with similarity scores + */ + findSimilar(id: string, options?: { limit?: number }): Promise }