import { Memory } from "../../src/memory"; import dotenv from "dotenv"; // Load environment variables dotenv.config(); async function testSemanticResearchAgent() { console.log("๐Ÿงช Starting Semantic Research Agent Demo"); if (!process.env.OPENAI_API_KEY) { console.error("Error: OPENAI_API_KEY environment variable is required"); process.exit(1); } const memoryInstance = 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-3.5-turbo", }, }, historyDbPath: ":memory:", }); const userId = "research-demo-user"; // Add a comprehensive set of memories to test semantic research console.log("๐Ÿ“ Adding comprehensive test memories..."); const memories = [ // Project Alpha - Core information "Project Alpha is a fitness tracking mobile application", "The app tracks workouts, nutrition, and sleep patterns", "We're using React Native for cross-platform development", "The backend is built with Node.js and Express", "MongoDB is used for storing user data and workout logs", // Team and roles "Sarah Martinez is our lead UI/UX designer", "Sarah has 5 years of experience in mobile app design", "John Smith is the backend developer working on the API", "Maria Rodriguez handles the database architecture", "Alex Chen is responsible for the machine learning components", // Technical details "We implemented OAuth 2.0 for user authentication", "The app uses WebSocket connections for real-time updates", "Push notifications are handled through Firebase", "We're following microservices architecture patterns", "The machine learning models use TensorFlow for workout recognition", // Business and timeline "The project started in January 2024", "We're targeting a beta release in Q2 2024", "The app will be available on both iOS and Android", "Target market includes fitness enthusiasts aged 18-45", "We have secured initial funding of $500K", // Features and functionality "Users can create custom workout plans", "The app provides nutrition recommendations based on goals", "Sleep tracking uses device sensors and manual input", "Social features allow users to share achievements", "Integration with popular fitness devices like Fitbit and Apple Watch", // Challenges and solutions "Data privacy is a key concern for user health information", "We implemented end-to-end encryption for sensitive data", "Performance optimization was challenging for real-time tracking", "Battery life optimization required careful sensor management", "Cross-platform UI consistency took significant effort", ]; for (const memory of memories) { await memoryInstance.add(memory, { userId }); } console.log(`โœ… Added ${memories.length} test memories`); // Test scenarios for semantic research agent const testCases = [ { query: "What technologies are used in Project Alpha?", description: "Basic technology query - should trigger research for related tech details", expectedResearch: "deeper technical information", }, { query: "Tell me about the team working on this project", description: "Team query - should research roles and responsibilities", expectedResearch: "team structure and individual contributions", }, { query: "What are the main challenges faced by the project?", description: "Problem-focused query - should research solutions and mitigation strategies", expectedResearch: "problem-solution pairs and technical challenges", }, { query: "How does the app handle user privacy?", description: "Security-focused query - should research related security measures", expectedResearch: "privacy and security implementations", }, { query: "What is the business model for Project Alpha?", description: "Business query - should research market and funding information", expectedResearch: "business strategy and market analysis", }, ]; for (let i = 0; i < testCases.length; i++) { const testCase = testCases[i]; console.log(`\n=== Test ${i + 1}: ${testCase.description} ===`); console.log(`๐Ÿ” Query: "${testCase.query}"`); console.log(`๐Ÿ“‹ Expected research: ${testCase.expectedResearch}`); try { const searchResult = await memoryInstance.search(testCase.query, { userId, useSearchAgents: true, enableMultihop: true, maxHops: 2, maxAgentSteps: 3, limit: 5 }); console.log(`๐Ÿ“Š Results: ${searchResult.results.length} total memories found`); // Show top results searchResult.results.slice(0, 3).forEach((result, idx) => { console.log(` ${idx + 1}. ${(result.memory || '[No content]').substring(0, 80)}... (Score: ${result.score?.toFixed(3)})`); }); // Analyze agent activity if (searchResult.agentAnalysis) { console.log(`\n๐Ÿค– Agent Analysis:`); if (searchResult.agentAnalysis.isMultihop && searchResult.agentAnalysis.multihopResults) { console.log(` - Multihop analysis: ${searchResult.agentAnalysis.multihopResults.length} hop(s)`); searchResult.agentAnalysis.multihopResults.forEach((hop, hopIdx) => { console.log(` - Hop ${hop.hopNumber}:`); console.log(` โ€ข Reasoning: ${hop.decision.reasoning}`); console.log(` โ€ข Additional steps needed: ${hop.decision.needsAdditionalSteps}`); console.log(` โ€ข Steps executed: ${hop.stepResults.length}`); // Look for semantic research steps const researchSteps = hop.stepResults.filter(step => step.action === 'semantic_research'); if (researchSteps.length > 0) { console.log(` ๐Ÿ”ฌ Semantic research performed:`); researchSteps.forEach(step => { console.log(` โ€ข Research successful: ${step.success}`); if (step.results && Array.isArray(step.results)) { console.log(` โ€ข Found ${step.results.length} additional memories`); if (step.results.length > 0 && step.results[0].query) { console.log(` โ€ข Research query: "${step.results[0].query}"`); console.log(` โ€ข Research type: ${step.results[0].researchType || 'N/A'}`); } } }); } console.log(` โ€ข Additional results found: ${hop.additionalResults.length}`); }); } else if (searchResult.agentAnalysis.decision) { console.log(` - Single-hop analysis`); console.log(` - Additional steps needed: ${searchResult.agentAnalysis.decision.needsAdditionalSteps}`); console.log(` - Reasoning: ${searchResult.agentAnalysis.decision.reasoning}`); const researchSteps = searchResult.agentAnalysis.stepResults?.filter(step => step.action === 'semantic_research') || []; if (researchSteps.length > 0) { console.log(` ๐Ÿ”ฌ Semantic research performed: ${researchSteps.length} searches`); } } } else { console.log(` - No agent analysis available`); } } catch (error) { console.error(`โŒ Test ${i + 1} failed:`, error); } // Add delay between tests await new Promise(resolve => setTimeout(resolve, 1000)); } // Test chat with semantic research console.log(`\n=== Chat Test: Complex Query with Semantic Research ===`); try { const chatResult = await memoryInstance.chat( "Give me a comprehensive overview of Project Alpha including its technology stack, team, challenges, and business aspects", { userId, enableMultihop: true, maxHops: 3, maxAgentSteps: 4, customInstructions: "Provide a detailed analysis organized into clear sections. Use information from semantic research to give comprehensive coverage.", } ); console.log(`๐Ÿ’ฌ Chat Response (${chatResult.response.length} characters):`); console.log(chatResult.response); console.log(`\n๐Ÿ“Š Chat Metadata:`); console.log(` - Sources used: ${chatResult.metadata.sourcesCount}`); console.log(` - Processing time: ${chatResult.metadata.processingTimeMs}ms`); console.log(` - Multihop enabled: ${chatResult.metadata.multihopEnabled}`); console.log(` - Hops used: ${chatResult.metadata.hopsUsed}`); console.log(` - Custom instructions: ${chatResult.metadata.customInstructions ? 'Applied' : 'None'}`); } catch (error) { console.error("โŒ Chat test failed:", error); } // Clean up await memoryInstance.reset(); console.log("\nโœ… Semantic Research Agent demo completed!"); } testSemanticResearchAgent() .catch(error => { console.error("Fatal error:", error); process.exit(1); });