Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rag optimizations/fixes for context. #3248

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions packages/core/src/ragknowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class RAGKnowledgeManager implements IRAGKnowledgeManager {
}

private readonly defaultRAGMatchThreshold = 0.85;
private readonly defaultRAGMatchCount = 5;
private readonly defaultRAGMatchCount = 8;

/**
* Common English stop words to filter out from query analysis
Expand Down Expand Up @@ -108,7 +108,7 @@ export class RAGKnowledgeManager implements IRAGKnowledgeManager {
return query
.toLowerCase()
.split(" ")
.filter((term) => term.length > 3) // Filter very short words
.filter((term) => term.length > 2) // Filter very short words
.filter((term) => !this.stopWords.has(term)); // Filter stop words
}

Expand Down Expand Up @@ -146,19 +146,34 @@ export class RAGKnowledgeManager implements IRAGKnowledgeManager {
}

private hasProximityMatch(text: string, terms: string[]): boolean {
const words = text.toLowerCase().split(" ");
const positions = terms
.map((term) => words.findIndex((w) => w.includes(term)))
.filter((pos) => pos !== -1);

if (positions.length < 2) return false;

// Check if any matches are within 5 words of each other
for (let i = 0; i < positions.length - 1; i++) {
if (Math.abs(positions[i] - positions[i + 1]) <= 5) {
if (!text || !terms.length) {
return false;
}

const words = text.toLowerCase().split(" ").filter(w => w.length > 0);

// Find all positions for each term (not just first occurrence)
const allPositions = terms.flatMap(term =>
words.reduce((positions, word, idx) => {
if (word.includes(term)) positions.push(idx);
return positions;
}, [] as number[])
).sort((a, b) => a - b);

if (allPositions.length < 2) return false;

// Check proximity
for (let i = 0; i < allPositions.length - 1; i++) {
if (Math.abs(allPositions[i] - allPositions[i + 1]) <= 5) {
elizaLogger.debug("[Proximity Match]", {
terms,
positions: allPositions,
matchFound: `${allPositions[i]} - ${allPositions[i + 1]}`
});
return true;
}
}

return false;
}

Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,14 +1462,16 @@ Text: ${attachment.text}

if (this.character.settings?.ragKnowledge) {
const recentContext = recentMessagesData
.slice(-3) // Last 3 messages
.sort((a, b) => b.createdAt - a.createdAt) // Sort by timestamp descending (newest first)
.slice(0, 3) // Get the 3 most recent messages
.reverse() // Reverse to get chronological order
.map((msg) => msg.content.text)
.join(" ");

knowledgeData = await this.ragKnowledgeManager.getKnowledge({
query: message.content.text,
conversationContext: recentContext,
limit: 5,
limit: 8,
});

formattedKnowledge = formatKnowledge(knowledgeData);
Expand Down Expand Up @@ -1770,7 +1772,16 @@ Text: ${attachment.text}
}

const formatKnowledge = (knowledge: KnowledgeItem[]) => {
return knowledge
.map((knowledge) => `- ${knowledge.content.text}`)
.join("\n");
// Group related content in a more natural way
return knowledge.map(item => {
// Get the main content text
const text = item.content.text;

// Clean up formatting but maintain natural text flow
const cleanedText = text
.trim()
.replace(/\n{3,}/g, '\n\n'); // Replace excessive newlines

return cleanedText;
}).join('\n\n'); // Separate distinct pieces with double newlines
};