Skip to content

Commit 290d255

Browse files
authored
📦 NEW: Add memory agents routing example
1 parent 49a65a1 commit 290d255

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import dotenv from "dotenv";
2+
import { Langbase } from 'langbase';
3+
4+
dotenv.config();
5+
6+
// Initialize Langbase with your API key
7+
const langbase = new Langbase({
8+
apiKey: process.env.LANGBASE_API_KEY!
9+
});
10+
11+
// Router agent checks whether or not to use memory agent.
12+
async function runRouterAgent(query) {
13+
const response = await langbase.pipes.run({
14+
stream: false,
15+
name: 'router-agent',
16+
model: 'openai:gpt-4o-mini', // Ensure this model supports JSON mode
17+
messages: [
18+
{
19+
role: 'system', // Update the content with your memory description
20+
content: `You are an expert query analyzer. Given a query, analyze whether it needs to use the memory agent or not.
21+
The memory agent contains a knowledge base that provides context-aware responses about AI, machine learning,
22+
and related topics. If the query is related to these topics, indicate that the memory agent should be used.
23+
Otherwise, indicate that it should not be used.
24+
Always respond in JSON format with the following structure: {"useMemory": true/false}.`
25+
},
26+
{ role: 'user', content: query }
27+
]
28+
});
29+
30+
// Parse the response to determine if we should use the memory agent
31+
const parsedResponse = JSON.parse(response.completion);
32+
return parsedResponse.useMemory;
33+
}
34+
35+
// Example usage
36+
async function main() {
37+
const query = 'What is AI?';
38+
const useMemory = await runRouterAgent(query);
39+
console.log('Use Memory:', useMemory);
40+
41+
if (useMemory) {
42+
// Run the memory agent
43+
const response = await langbase.pipes.run({
44+
stream: false,
45+
name: 'memory-agent', // Name of your memory agent
46+
messages: [
47+
{ role: 'user', content: query }
48+
]
49+
});
50+
console.log('Response from memory agent:', response);
51+
} else {
52+
// Run the non-memory agent
53+
const response = await langbase.pipes.run({
54+
stream: false,
55+
name: 'non-memory-agent', // Name of your non-memory agent
56+
messages: [
57+
{ role: 'user', content: query }
58+
]
59+
});
60+
console.log('Response from non-memory agent:', response);
61+
}
62+
}
63+
64+
main();

0 commit comments

Comments
 (0)