import { Memory } from "../../src/memory"; import dotenv from "dotenv"; // Load environment variables dotenv.config(); async function enhancedSpecificityDemo() { console.log("šŸŽÆ Enhanced Specificity & Robust Filtering Demo"); console.log("=" .repeat(60)); if (!process.env.OPENAI_API_KEY) { console.error("Error: OPENAI_API_KEY environment variable is required"); process.exit(1); } const memory = new Memory({ embedder: { provider: "openai", config: { apiKey: process.env.OPENAI_API_KEY, model: "text-embedding-3-small", }, }, vectorStore: { provider: "memory", config: { dimension: 1536, }, }, llm: { provider: "openai_structured", config: { apiKey: process.env.OPENAI_API_KEY, model: "gpt-4o-mini", }, }, historyDbPath: ":memory:", }); const userId = "specificity-demo-user"; console.log("\nšŸ“ Setting up test scenario with vague references..."); // Add memories with vague references that can be clarified through context const vaguenessTestMemories = [ // Home country example { role: "user", content: "I'm originally from India, specifically from Mumbai" }, { role: "user", content: "My home country has been going through changes lately" }, // Temporal vagueness { role: "user", content: "Last year I graduated from Stanford with CS degree" }, { role: "user", content: "In 2023, I was job hunting after studies" }, // Activity vagueness { role: "user", content: "I love playing chess, especially online tournaments" }, { role: "user", content: "I spend weekends doing that, it's my main hobby" }, // Company vagueness { role: "user", content: "I work at Google as a software engineer" }, { role: "user", content: "The company has great benefits and culture" }, // Time vagueness { role: "user", content: "On November 15th, 2024, I got promoted to Senior Engineer" }, { role: "user", content: "Recently I've been taking on leadership responsibilities" }, ]; for (const memoryItem of vaguenessTestMemories) { await memory.add([memoryItem], { userId }); await new Promise(resolve => setTimeout(resolve, 50)); } console.log(`āœ… Added ${vaguenessTestMemories.length} memories with vague references`); // Test queries for specificity const testCases = [ { query: "What is my home country?", expected: "Should specify 'India' instead of 'your home country'" }, { query: "When did I graduate?", expected: "Should convert 'last year' to '2023'" }, { query: "What is my main hobby?", expected: "Should clarify to 'chess' instead of 'that activity'" }, { query: "Where do I work?", expected: "Should specify 'Google' instead of 'the company'" }, { query: "When did I get promoted?", expected: "Should specify 'November 15th, 2024' instead of 'recently'" } ]; for (let i = 0; i < testCases.length; i++) { const testCase = testCases[i]; console.log(`\n=== Test ${i + 1}: ${testCase.query} ===`); console.log(`Expected: ${testCase.expected}`); try { const result = await memory.chat(testCase.query, { userId, enableMultihop: true, maxHops: 2, maxAgentSteps: 5, responseCustomInstructions: `Provide the most SPECIFIC answer possible. Use exact names, dates, and details.`, reasoningCustomInstructions: `Focus on SPECIFICITY: Convert vague terms to concrete details using context.` }); console.log(`Answer: "${result.response}"`); console.log(`Sources: ${result.metadata.sourcesCount}, Time: ${result.metadata.processingTimeMs}ms`); } catch (error) { console.error(`āŒ Test ${i + 1} failed:`, error); } await new Promise(resolve => setTimeout(resolve, 1000)); } await memory.reset(); console.log("\nāœ… Demo completed!"); } // Run the demo if (require.main === module) { enhancedSpecificityDemo().catch(console.error); } export { enhancedSpecificityDemo };