import { Memory } from "../../src/memory"; async function customInstructionsDemo() { console.log("šŸŽÆ Custom Instructions Demo"); console.log("=" .repeat(50)); // Initialize memory with OpenAI const memory = new Memory({ llm: { provider: "openai", config: { model: "gpt-4o", apiKey: process.env.OPENAI_API_KEY, }, }, embedder: { provider: "openai", config: { model: "text-embedding-3-small", apiKey: process.env.OPENAI_API_KEY, }, }, vectorStore: { provider: "qdrant", config: { collectionName: "custom_instructions_demo", }, }, }); const userId = "demo-user"; const runId = "custom-instructions-run"; // Add some sample conversations console.log("\nšŸ“ Adding sample conversations..."); await memory.add([ { role: "user", content: "I love playing basketball on weekends" }, { role: "assistant", content: "That's great! Basketball is an excellent way to stay active." }, { role: "user", content: "Yes, I play with my friends at the local court" }, ], { userId, runId }); await memory.add([ { role: "user", content: "I'm thinking about learning to cook Italian food" }, { role: "assistant", content: "Italian cuisine is wonderful! What dishes interest you most?" }, { role: "user", content: "I'd love to learn how to make authentic pasta and pizza" }, ], { userId, runId }); await memory.add([ { role: "user", content: "I work as a software engineer at a tech startup" }, { role: "assistant", content: "That sounds exciting! What kind of projects do you work on?" }, { role: "user", content: "Mainly web applications using React and Node.js" }, ], { userId, runId }); console.log("āœ… Sample conversations added"); // Demo 1: Basic chat without custom instructions console.log("\n" + "=".repeat(50)); console.log("šŸ” Demo 1: Basic Chat (No Custom Instructions)"); console.log("=".repeat(50)); const basicResult = await memory.chat("What do you know about me?", { userId, runId, useSearchAgents: true, enableMultihop: true, }); console.log("šŸ“‹ Basic Response:"); console.log(basicResult.response); console.log(`\nšŸ“Š Sources found: ${basicResult.sources.length}`); console.log(`šŸ”§ Agent analysis: ${basicResult.agentAnalysis?.isMultihop ? 'Multihop' : 'Single-hop'}`); // Demo 2: Chat with reasoning custom instructions console.log("\n" + "=".repeat(50)); console.log("🧠 Demo 2: Custom Reasoning Instructions"); console.log("=".repeat(50)); const reasoningResult = await memory.chat("What do you know about me?", { userId, runId, useSearchAgents: true, enableMultihop: true, reasoningCustomInstructions: ` When analyzing search completeness, prioritize finding information about: 1. Personal hobbies and interests 2. Professional background and skills 3. Future goals and aspirations If the initial search doesn't cover all three categories, consider additional search steps. Be thorough in gathering comprehensive personal information. `, }); console.log("šŸ“‹ Response with Custom Reasoning:"); console.log(reasoningResult.response); console.log(`\nšŸ“Š Sources found: ${reasoningResult.sources.length}`); console.log(`šŸ”§ Agent analysis: ${reasoningResult.agentAnalysis?.isMultihop ? 'Multihop' : 'Single-hop'}`); if (reasoningResult.agentAnalysis?.multihopResults) { console.log(`šŸ”„ Hops performed: ${reasoningResult.agentAnalysis.multihopResults.length}`); reasoningResult.agentAnalysis.multihopResults.forEach((hop, index) => { console.log(` Hop ${hop.hopNumber}: ${hop.decision.reasoning}`); }); } // Demo 3: Chat with response custom instructions console.log("\n" + "=".repeat(50)); console.log("šŸ’¬ Demo 3: Custom Response Instructions"); console.log("=".repeat(50)); const responseResult = await memory.chat("What do you know about me?", { userId, runId, useSearchAgents: true, enableMultihop: true, responseCustomInstructions: ` Format your response as a professional profile summary with the following structure: ## Personal Profile **Interests & Hobbies:** [List hobbies and personal interests] **Professional Background:** [Describe work and technical skills] **Goals & Aspirations:** [Mention any future plans or learning goals] Use bullet points and keep the tone professional yet friendly. If any category lacks information, mention what additional details would be helpful. `, }); console.log("šŸ“‹ Response with Custom Formatting:"); console.log(responseResult.response); console.log(`\nšŸ“Š Sources found: ${responseResult.sources.length}`); // Demo 4: Chat with both types of custom instructions console.log("\n" + "=".repeat(50)); console.log("šŸŽÆ Demo 4: Both Custom Instructions Combined"); console.log("=".repeat(50)); const combinedResult = await memory.chat("Tell me about my interests and career", { userId, runId, useSearchAgents: true, enableMultihop: true, reasoningCustomInstructions: ` For queries about interests and career, ensure you search for: 1. All recreational activities and hobbies mentioned 2. Complete professional information including specific technologies 3. Any learning goals or future aspirations If the search doesn't find comprehensive information in both areas, perform additional search steps to get a complete picture. `, responseCustomInstructions: ` Create a detailed personal and professional summary with these sections: šŸŽÆ **Personal Interests** - List all hobbies and recreational activities - Include specific details about how/where they engage in these activities šŸ’¼ **Professional Profile** - Job title and company type - Technical skills and technologies used - Specific project types or responsibilities šŸš€ **Future Goals** - Learning objectives - Skill development plans Use emojis and make it engaging while being comprehensive. If information is missing, specifically mention what would complete the profile. `, }); console.log("šŸ“‹ Combined Custom Instructions Response:"); console.log(combinedResult.response); console.log(`\nšŸ“Š Sources found: ${combinedResult.sources.length}`); console.log(`šŸ”§ Agent analysis: ${combinedResult.agentAnalysis?.isMultihop ? 'Multihop' : 'Single-hop'}`); // Show metadata comparison console.log("\n" + "=".repeat(50)); console.log("šŸ“ˆ Metadata Comparison"); console.log("=".repeat(50)); console.log("Basic chat metadata:"); console.log(` - Response instructions: ${basicResult.metadata.responseCustomInstructions || 'None'}`); console.log(` - Reasoning instructions: ${basicResult.metadata.reasoningCustomInstructions || 'None'}`); console.log(` - Legacy field: ${basicResult.metadata.customInstructions || 'None'}`); console.log("\nCombined instructions metadata:"); console.log(` - Response instructions: ${combinedResult.metadata.responseCustomInstructions ? 'Present' : 'None'}`); console.log(` - Reasoning instructions: ${combinedResult.metadata.reasoningCustomInstructions ? 'Present' : 'None'}`); console.log(` - Legacy field: ${combinedResult.metadata.customInstructions || 'None'}`); // Demo 5: Legacy customInstructions (backward compatibility) console.log("\n" + "=".repeat(50)); console.log("šŸ”„ Demo 5: Legacy customInstructions (Backward Compatibility)"); console.log("=".repeat(50)); const legacyResult = await memory.chat("What are my hobbies?", { userId, runId, useSearchAgents: true, customInstructions: ` Respond in a casual, friendly tone as if talking to a close friend. Use informal language and include encouraging comments about their interests. `, }); console.log("šŸ“‹ Legacy Custom Instructions Response:"); console.log(legacyResult.response); console.log(`\nšŸ“Š Legacy field used: ${legacyResult.metadata.customInstructions ? 'Yes' : 'No'}`); console.log(`šŸ“Š Response instructions: ${legacyResult.metadata.responseCustomInstructions ? 'Yes' : 'No'}`); console.log("\nšŸŽ‰ Custom Instructions Demo Complete!"); console.log("\nšŸ’” Key Takeaways:"); console.log(" • reasoningCustomInstructions: Guide the search agent's decision-making"); console.log(" • responseCustomInstructions: Control the final response format and tone"); console.log(" • customInstructions: Legacy field (maps to responseCustomInstructions)"); console.log(" • Both can be used together for maximum control"); console.log(" • Reasoning instructions affect search thoroughness"); console.log(" • Response instructions only affect final output formatting"); } // Run the demo if (require.main === module) { customInstructionsDemo().catch(console.error); } export { customInstructionsDemo };