async function askAgentWithKnowledgeBase(agentId, knowledgeBaseId, question) {
try {
// First, search the knowledge base for relevant information
const kbResponse = await axios.post(
'https://app.dumplingai.com/api/v1/knowledge-bases/query',
{
knowledgeBaseId: knowledgeBaseId,
query: question,
resultCount: 3
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
}
);
const context = kbResponse.data.results.map(result => result.content).join('\n\n');
// Then, ask the agent a question with the knowledge base context
const agentResponse = await axios.post(
'https://app.dumplingai.com/api/v1/agents/generate-completion',
{
messages: [
{
role: 'system',
content: `You are a knowledgeable assistant with expertise in the company's products. Use the following information to answer the user's question. If you don't know, say so.\n\nContext from knowledge base:\n${context}`
},
{
role: 'user',
content: question
}
],
agentId: agentId,
parseJson: false
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
}
);
console.log('Answer with Knowledge Base:', agentResponse.data.text);
return agentResponse.data.text;
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
}
// Ask a question using the agent and knowledge base
const knowledgeBaseId = 'your_kb_id'; // Replace with your knowledge base ID
const answer = await askAgentWithKnowledgeBase(
agentId,
knowledgeBaseId,
'How do I reset my password?'
);