RAG Tool Agent

This project involves setting up and running a system of agents to add content to a RAG (Retrieval-Augmented Generation) tool, perform queries, and return relevant information. We use Composio to setup this Local Tool and OpenAI GPT-4o to power the agents. Follow this guide to set up and run the project.

1

Imports and Environment Setup

Import the necessary libraries and set up your environment variables.

Import libraries
1import dotenv from 'dotenv';
2import { ExecEnv, LangchainToolSet } from 'composio-core';
3import { ChatOpenAI } from '@langchain/openai';
4import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
5import { pull } from 'langchain/hub';
6
7// Load environment variables
8dotenv.config();
2

Initialize Language Model and Define Tools

Initialize the language model with OpenAI API key and model name, and set up the necessary tools for the agents.

LLM and Tools
1// Initialize the LLM with the OpenAI GPT-4-turbo model
2const llm = new ChatOpenAI({ model: "gpt-4-turbo" });
3
4// Initialize the Composio ToolSet
5const composioToolset = new LangchainToolSet({
6 apiKey: process.env.COMPOSIO_API_KEY,
7 workspaceEnv: ExecEnv.DOCKER
8});
9
10// Get the RAG tool actions from the Composio ToolSet
11const tools = await composioToolset.getTools({
12 actions: ["ragtool_add_content", "ragtool_query"]
13});
3

Define the RAG Agent

Define the RAG agent with its llm, prompt and tools.

Creating Agents
1const prompt = await pull("hwchase17/openai-functions-agent");
2
3const agent = await createOpenAIToolsAgent({
4 llm,
5 tools,
6 prompt,
7});
8
9const agentExecutor = new AgentExecutor({
10 agent,
11 tools,
12 verbose: true,
13});
4

Adding Content

Create tasks to add content to the RAG tool for enriching its knowledge base.

Add Content
1async function addContentToRAG(content) {
2 const result = await agentExecutor.invoke({
3 input: `Add the following content to the RAG tool to enrich its knowledge base: ${content}`
4 });
5 console.log(result.output);
6 return result.output;
7}
8
9// Example content to add
10const additionalContentList = [
11 "Paris is the capital of France. It is known for its art, fashion, and culture.",
12 "Berlin is the capital of Germany. It is famous for its history and vibrant culture.",
13 "Tokyo is the capital of Japan. It is known for its technology and cuisine.",
14 "Canberra is the capital of Australia. It is known for its modern architecture and museums.",
15];
16
17// Add content to RAG tool
18for (const content of additionalContentList) {
19 await addContentToRAG(content);
20}
5

Define and Execute Query Task

Create and execute the task for querying the RAG tool based on user input.

Query Function
1async function queryRAG(userQuery) {
2 const result = await agentExecutor.invoke({
3 input: `Formulate a query based on this input: ${userQuery}.
4 Retrieve relevant information using the RAG tool and return the results.`
5 });
6 console.log(result.output);
7 return result.output;
8}
9
10// Example usage
11const userQuery = "What is the capital of France?";
12const queryResult = await queryRAG(userQuery);
13console.log("Query Result:", queryResult);

Putting it All Together

Javascript Final Code
1import dotenv from 'dotenv';
2import { ExecEnv, LangchainToolSet } from 'composio-core';
3import { ChatOpenAI } from '@langchain/openai';
4import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
5import { pull } from 'langchain/hub';
6
7dotenv.config();
8
9(async () => {
10 const llm = new ChatOpenAI({ model: "gpt-4-turbo" });
11
12 const composioToolset = new LangchainToolSet({
13 apiKey: process.env.COMPOSIO_API_KEY,
14 workspaceEnv: ExecEnv.DOCKER
15 });
16
17 const tools = await composioToolset.getTools({
18 actions: ["ragtool_add_content", "ragtool_query"]
19 });
20
21 const prompt = await pull("hwchase17/openai-functions-agent");
22
23 const agent = await createOpenAIToolsAgent({
24 llm,
25 tools,
26 prompt,
27 });
28
29 const agentExecutor = new AgentExecutor({
30 agent,
31 tools,
32 verbose: true,
33 });
34
35 async function addContentToRAG(content) {
36 const result = await agentExecutor.invoke({
37 input: `Add the following content to the RAG tool to enrich its knowledge base: ${content}`
38 });
39 console.log(result.output);
40 return result.output;
41 }
42
43 async function queryRAG(userQuery) {
44 const result = await agentExecutor.invoke({
45 input: `Formulate a query based on this input: ${userQuery}.
46 Retrieve relevant information using the RAG tool and return the results.`
47 });
48 console.log(result.output);
49 return result.output;
50 }
51
52 // Example content to add
53 const additionalContentList = [
54 "Paris is the capital of France. It is known for its art, fashion, and culture.",
55 "Berlin is the capital of Germany. It is famous for its history and vibrant culture.",
56 "Tokyo is the capital of Japan. It is known for its technology and cuisine.",
57 "Canberra is the capital of Australia. It is known for its modern architecture and museums.",
58 ];
59
60 // Add content to RAG tool
61 for (const content of additionalContentList) {
62 await addContentToRAG(content);
63 }
64
65 // Example query
66 const userQuery = "What is the capital of France?";
67 const queryResult = await queryRAG(userQuery);
68 console.log("Query Result:", queryResult);
69})();