update relationship weights

This commit is contained in:
perf3ct 2025-03-08 23:36:04 +00:00
parent 7e232d17e1
commit 733fdcf8ba
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
2 changed files with 63 additions and 8 deletions

View File

@ -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 information
let 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 concisely
if (context.childTitles.length > 0) {
result += `Children: ${context.childTitles.map(t => this.cleanText(t)).join('; ')}\n`;
}
// Add template/inheritance relationships concisely
if (context.templateTitles && context.templateTitles.length > 0) {
result += `Templates: ${context.templateTitles.map(t => this.cleanText(t)).join('; ')}\n`;
}
// Add related notes concisely
if (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 concisely
if (context.backlinks && context.backlinks.length > 0) {
result += 'Referenced By: ';
const backlinkTexts = context.backlinks.map(link =>

View File

@ -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 model
const 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)