From b7e43dbac0bde4aa80e0bc81a42c630b5fc152fb Mon Sep 17 00:00:00 2001 From: Raj Sharma Date: Sat, 2 Nov 2024 12:55:18 +0530 Subject: [PATCH] feat: added tool to get clustered list of notes --- tools/notes-vectors.ts | 29 +++++++++++++++++++++++++++++ tools/notes.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tools/notes-vectors.ts b/tools/notes-vectors.ts index ff386eb..8877383 100644 --- a/tools/notes-vectors.ts +++ b/tools/notes-vectors.ts @@ -271,3 +271,32 @@ export async function initVectorStoreSync() { export function semantic_search_notes(query: string, limit: number) { return vectorStore.similaritySearch(query, limit); } + +export async function getClusteredFiles(): Promise> { + const result: Record = {}; + + // Query to get filenames and their respective cluster assignments + const queryResult = await vectorStore.client?.query( + `SELECT ${config.columns.metadataColumnName}->>'filename' AS filename, ${config.columns.clusterColumnName} AS cluster + FROM ${config.tableName}` + ); + + if (!queryResult) { + console.log("No clustered files found in the vector store."); + return result; + } + + // Group filenames by cluster + queryResult.rows.forEach((row) => { + const clusterName = `Cluster ${row.cluster}`; // Format the cluster name + const filename = row.filename; + + if (!result[clusterName]) { + result[clusterName] = []; + } + result[clusterName].push(filename); + }); + + console.log("Clustered files:", result); + return result; +} diff --git a/tools/notes.ts b/tools/notes.ts index 6be7798..86512a6 100644 --- a/tools/notes.ts +++ b/tools/notes.ts @@ -6,7 +6,11 @@ import Fuse from "fuse.js"; import { ask, get_transcription } from "./ask"; import { Message } from "../interfaces/message"; import { memory_manager_guide, memory_manager_init } from "./memory-manager"; -import { semantic_search_notes, syncVectorStore } from "./notes-vectors"; +import { + getClusteredFiles, + semantic_search_notes, + syncVectorStore, +} from "./notes-vectors"; import { readFileSync, writeFileSync } from "fs"; import { join } from "path"; import { tmpdir } from "os"; @@ -104,6 +108,21 @@ async function semanticSearchNotes({ } } +const GetClusteredFileListParams = z.object({}); +type GetClusteredFileListParams = z.infer; + +export async function getClusteredFileList({}: GetClusteredFileListParams): Promise { + try { + const results = await getClusteredFiles(); + return { + success: true, + message: JSON.stringify(results, null, 2), + }; + } catch (error: any) { + return { success: false, message: error.message }; + } +} + // Helper function to normalize paths function normalizePath(path: string): string { if (path.startsWith("/notes/")) return path.substring(7); @@ -615,4 +634,12 @@ You can use this to search by: 4. Tags `, }), + zodFunction({ + function: getClusteredFileList, + name: "getClusteredFileList", + schema: GetClusteredFileListParams, + description: `Get the list of notes files based on 4 cluster (unsupervised) semantic clustering. + You can use this to see how the notes are clustered based on their content. + `, + }), ];