// Creating an agent chain with tools
async function createAgentChain() {
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: 'E-commerce Agent',
type: 'agent',
config: {
llm: {
provider: 'openai',
model: 'gpt-4',
temperature: 0.2
},
tools: [
{
name: 'search_products',
description: 'Search for products in the catalog',
parameters: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query for products'
},
category: {
type: 'string',
description: 'Optional category to filter by'
}
},
required: ['query']
}
},
{
name: 'get_product_details',
description: 'Get detailed information about a specific product',
parameters: {
type: 'object',
properties: {
productId: {
type: 'string',
description: 'The ID of the product to get details for'
}
},
required: ['productId']
}
},
{
name: 'check_inventory',
description: 'Check if a product is in stock',
parameters: {
type: 'object',
properties: {
productId: {
type: 'string',
description: 'The ID of the product to check inventory for'
}
},
required: ['productId']
}
}
],
agentType: 'react',
maxIterations: 5,
outputKey: 'output'
}
})
});
return await response.json();
}
// Using the agent chain
async function askAgent(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
},
includeAgentSteps: true // Return the agent's thinking and tool usage
})
});
return await response.json();
}
// Example of using the agent
async function agentExample() {
const agent = await createAgentChain();
console.log('Created agent chain:', agent.chainId);
const result = await askAgent(agent.chainId, "I'm looking for a lavender-scented candle that's currently in stock. Can you help me find one?");
console.log('Final answer:', result.output.output);
console.log('Agent steps:', result.agentSteps);
}