// Create an agent that can search document collections
async function createDocumentAgent(collectionIds) {
const response = await fetch('https://langchain.moodmnky.com/api/chains', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
name: "Document Search Agent",
type: "agent",
config: {
llm: {
provider: "openai",
model: "gpt-4",
temperature: 0.2
},
tools: [
{
name: "search_documents",
description: "Search document collections for relevant information",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query"
},
collection_id: {
type: "string",
description: "The ID of the collection to search",
enum: collectionIds
},
top_k: {
type: "integer",
description: "Number of results to return",
default: 3
}
},
required: ["query", "collection_id"]
},
function: async ({ query, collection_id, top_k = 3 }) => {
const results = await fetch(`https://langchain.moodmnky.com/api/collections/${collection_id}/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
query: query,
topK: top_k,
includeMetadata: true
})
}).then(r => r.json());
return {
results: results.results.map(r => ({
content: r.content,
metadata: r.metadata,
relevance: r.score
}))
};
}
}
],
memory: {
type: "conversation",
config: {
maxMessages: 10
}
},
systemPrompt: `You are a helpful assistant for MOOD MNKY, a premium self-care and fragrance company. You can search the company's document collections to find accurate information about products, services, and policies.
When answering questions:
1. Search relevant document collections using the search_documents tool
2. Cite your sources by referencing the documents you found
3. If you can't find information, be honest about it
4. Always maintain a helpful, friendly tone aligned with the MOOD MNKY brand
Available document collections:
${collectionIds.join(', ')}`,
maxIterations: 5
}
})
});
return await response.json();
}
// Using the document agent to answer questions
async function askDocumentAgent(chainId, question) {
const response = await fetch(`https://langchain.moodmnky.com/api/chains/${chainId}/run`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
inputs: {
input: question
}
})
});
return await response.json();
}
// Example usage
async function documentAgentDemo() {
// Available collections
const collectionIds = [
"col_01h9f5zk4r9s7l3z8u2y", // Product Knowledge Base
"col_02h9g6al5s0t8m4a9v3z" // Customer FAQs
];
// Create the agent
const agent = await createDocumentAgent(collectionIds);
console.log('Created document agent:', agent.chainId);
// Ask questions
const questions = [
"What are the ingredients in your candles?",
"How long do your candles typically burn?",
"Do you offer any pet-friendly fragrances?"
];
for (const question of questions) {
console.log(`\nQuestion: ${question}`);
const response = await askDocumentAgent(agent.chainId, question);
console.log(`Answer: ${response.outputs.output}`);
}
}