Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
update relationship weights
perf3ct733fdcf8ba6e
diff --git a/src/services/llm/embeddings/base_embeddings.ts b/src/services/llm/embeddings/base_embeddings.tsindex be2b1a2a9f..29e52d1ce3 100644--- a/src/services/llm/embeddings/base_embeddings.ts+++ b/src/services/llm/embeddings/base_embeddings.ts@@ -29,11 +29,68 @@ export abstract class BaseEmbeddingProvider implements EmbeddingProvider {* Generates a rich text representation of a note's context for embedding*/protected generateNoteContextText(context: NoteEmbeddingContext): string {+ // Build a relationship-focused summary first+ const relationshipSummary = [];++ // Summarize the note's place in the hierarchy+ if (context.parentTitles.length > 0) {+ relationshipSummary.push(`This note is a child of: ${context.parentTitles.map(t => this.cleanText(t)).join(', ')}.`);+ }++ if (context.childTitles.length > 0) {+ relationshipSummary.push(`This note has children: ${context.childTitles.map(t => this.cleanText(t)).join(', ')}.`);+ }++ // Emphasize relationships with other notes+ if (context.relatedNotes && context.relatedNotes.length > 0) {+ // Group by relation type for better understanding+ const relationsByType: Record<string, string[]> = {};+ for (const rel of context.relatedNotes) {+ if (!relationsByType[rel.relationName]) {+ relationsByType[rel.relationName] = [];+ }+ relationsByType[rel.relationName].push(this.cleanText(rel.targetTitle));+ }++ for (const [relType, targets] of Object.entries(relationsByType)) {+ relationshipSummary.push(`This note has ${relType} relationship with: ${targets.join(', ')}.`);+ }+ }++ // Emphasize backlinks for bidirectional relationships+ if (context.backlinks && context.backlinks.length > 0) {+ // Group by relation type+ const backlinksByType: Record<string, string[]> = {};+ for (const link of context.backlinks) {+ if (!backlinksByType[link.relationName]) {+ backlinksByType[link.relationName] = [];+ }+ backlinksByType[link.relationName].push(this.cleanText(link.sourceTitle));+ }++ for (const [relType, sources] of Object.entries(backlinksByType)) {+ relationshipSummary.push(`This note is ${relType} of: ${sources.join(', ')}.`);+ }+ }++ // Emphasize templates/inheritance+ if (context.templateTitles && context.templateTitles.length > 0) {+ relationshipSummary.push(`This note inherits from: ${context.templateTitles.map(t => this.cleanText(t)).join(', ')}.`);+ }+// Start with core note informationlet result =`Title: ${this.cleanText(context.title)}\n` +`Type: ${context.type}\n` +- `MIME: ${context.mime}\n` ++ `MIME: ${context.mime}\n`;++ // Add the relationship summary at the beginning for emphasis+ if (relationshipSummary.length > 0) {+ result += `Relationships: ${relationshipSummary.join(' ')}\n`;+ }++ // Continue with dates+ result +=`Created: ${context.dateCreated}\n` +`Modified: ${context.dateModified}\n`;@@ -55,22 +112,21 @@ export abstract class BaseEmbeddingProvider implements EmbeddingProvider {result += labelTexts.join('; ') + '\n';}- // Add parents concisely+ // Parents, children, templates, relations, and backlinks are now handled in the relationship summary+ // But we'll include them again in a more structured format for organization+if (context.parentTitles.length > 0) {result += `Parents: ${context.parentTitles.map(t => this.cleanText(t)).join('; ')}\n`;}- // Add children conciselyif (context.childTitles.length > 0) {result += `Children: ${context.childTitles.map(t => this.cleanText(t)).join('; ')}\n`;}- // Add template/inheritance relationships conciselyif (context.templateTitles && context.templateTitles.length > 0) {result += `Templates: ${context.templateTitles.map(t => this.cleanText(t)).join('; ')}\n`;}- // Add related notes conciselyif (context.relatedNotes && context.relatedNotes.length > 0) {result += 'Related: ';const relatedTexts = context.relatedNotes.map(rel =>@@ -79,7 +135,6 @@ export abstract class BaseEmbeddingProvider implements EmbeddingProvider {result += relatedTexts.join('; ') + '\n';}- // Add backlinks conciselyif (context.backlinks && context.backlinks.length > 0) {result += 'Referenced By: ';const backlinkTexts = context.backlinks.map(link =>diff --git a/src/services/llm/embeddings/vector_store.ts b/src/services/llm/embeddings/vector_store.tsindex 3c767bb830..b3c2b2f6ee 100644--- a/src/services/llm/embeddings/vector_store.ts+++ b/src/services/llm/embeddings/vector_store.ts@@ -148,7 +148,7 @@ export async function findSimilarNotes(providerId: string,modelId: string,limit = 10,- threshold = 0.7+ threshold = 0.65 // Slightly lowered from 0.7 to account for relationship focus): Promise<{noteId: string, similarity: number}[]> {// Get all embeddings for the given provider and modelconst rows = await sql.getRows(`@@ -172,7 +172,7 @@ export async function findSimilarNotes(};});- // Filter by threshold and sort by similarity (descending)+ // Filter by threshold and sort by similarity (highest first)return similarities.filter(item => item.similarity >= threshold).sort((a, b) => b.similarity - a.similarity)