// Creating a combined memory
async function createCombinedMemory() {
const response = await fetch('https://langchain.moodmnky.com/api/memories', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
name: "Advanced Customer Memory",
type: "combined",
config: {
memories: [
{
type: "conversation",
config: {
maxMessages: 5,
returnMessages: true,
inputKey: "input",
outputKey: "output",
memoryKey: "chat_history"
}
},
{
type: "summary",
config: {
llm: {
provider: "openai",
model: "gpt-3.5-turbo",
temperature: 0.5
},
promptTemplate: "Summarize the key points from this conversation, focusing on customer needs and preferences:\n\n{chat_history}\n\nSummary:",
summaryInputKey: "chat_history",
summaryOutputKey: "summary",
memoryKey: "conversation_summary"
}
},
{
type: "entity",
config: {
llm: {
provider: "openai",
model: "gpt-3.5-turbo",
temperature: 0.2
},
entityExtractorPrompt: "Extract entities and their attributes from the text. Focus on products, preferences, and personal details:\n\n{text}\n\nEntities:",
knownEntities: ["customer", "products"],
memoryKey: "entities"
}
}
]
}
})
});
return await response.json();
}
// Using combined memory with a chain
async function useCombinedMemoryWithChain() {
// Create the combined memory
const memory = await createCombinedMemory();
console.log('Created combined memory:', memory.memoryId);
// Create a chain
const chainResponse = await fetch('https://langchain.moodmnky.com/api/chains', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
name: "Customer Support Chain",
type: "llm",
config: {
llm: {
provider: "openai",
model: "gpt-4",
temperature: 0.7
},
prompt: `You are a customer support agent for MOOD MNKY, a premium self-care and fragrance company.
Recent conversation: {chat_history}
Overall conversation summary: {conversation_summary}
What we know about the customer: {entities}
Customer: {input}
Respond in a helpful, friendly way that references their preferences and history when relevant.`,
outputKey: "response"
}
})
});
const chain = await chainResponse.json();
// Connect the memory to the chain
await fetch(`https://langchain.moodmnky.com/api/chains/${chain.chainId}/memories`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
memoryId: memory.memoryId
})
});
// Now use the chain with connected memory
const messages = [
"Hi there, I'm looking for some new candles for my home.",
"I prefer floral scents, especially lavender and jasmine. Nothing too strong though.",
"Do you have any recommendations for candles that would help with relaxation before bed?",
"That sounds perfect. Are they made with natural ingredients? I'm trying to avoid synthetic fragrances."
];
for (const message of messages) {
const response = await fetch(`https://langchain.moodmnky.com/api/chains/${chain.chainId}/run`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
inputs: {
input: message
}
})
});
const result = await response.json();
console.log('Customer:', message);
console.log('Agent:', result.output.response);
console.log('---');
}
// Check memory contents after conversation
const contents = await getMemoryContents(memory.memoryId);
console.log('Final memory state:');
console.log('Chat history:', contents.chat_history);
console.log('Conversation summary:', contents.conversation_summary);
console.log('Entities:', contents.entities);
}