Is there a way to dynamically chat with RAG documents when using agents #5194
-
Checked other resources
Commit to Help
Example CodeThis is how i setup my agent
const llm = new ChatOpenAI({
modelName: "gpt-4-0125-preview", //gpt-3.5-turbo-0125
temperature: 0,
});
const tools = [
// search api
new SerpAPI(process.env.SERPAI_KEY, {
location: "Dubai,United Arab Emirates",
hl: "en",
gl: "us",
}),
// word document generator
new DynamicStructuredTool({
name: "wordDocGen",
description:
"Tool to compile JSON data into a word document and returns a download link",
schema: z.object({
data: z.object({
main: z.string(),
body: z.array(z.object({ heading: z.string(), content: z.string() })),
}),
}),
func: async function ({ data }) {
// uncomment this to see how the agent parses the data
// console.log(data);
const content = fs.readFileSync(
"./public/assets/templates/allTemplate.docm"
);
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
linebreaks: true,
});
await doc.renderAsync(data);
const buf = doc.getZip().generate({ type: "nodebuffer" });
const fileName = `documentGen${uuidv4()}`;
const outputFilePath = `./public/audio/${fileName}.docm`;
fs.writeFileSync(outputFilePath, buf);
const downloadLink = `https://${host}/automarketing/download/${fileName}.docm`;
return downloadLink;
},
}),
// webscraper
new DynamicStructuredTool({
name: "scraperTool",
description: "Tool to scrape data from the url provided",
schema: z.object({
url: z.string().describe("URL of the website to scrape"),
}),
func: async function ({ url }) {
const response = await axios.get(url, { timeout: 10000 });
if (response.status === 200) {
const $ = cheerio.load(response.data);
const scrapedData: string[] = [];
// Example: scraping all paragraph texts
$("p").each((index, element) => {
const paragraph = $(element).text().trim();
if (paragraph) {
scrapedData.push(paragraph);
}
});
// Send scraped data as response
const allData = scrapedData.join("\n");
return allData;
}
return "No data could be scraped";
},
}),
// automarketing
new DynamicStructuredTool({
name: "marketingTool",
description:
"Tool to generate a pdf with marketing content for businesses ONLY. If it successfully generates the pdf the tool will return a link to the pdf",
schema: z.object({
email: z.string().describe("The user's email"),
question: z.string().describe("The name of the user's business"),
question1: z
.string()
.describe("The products being sold by the business"),
}),
func: async function ({ email, question, question1 }): Promise<any> {
let info = {
stage: 1,
email: email,
question: question,
question1: question1,
};
sseEmitter.emit(`${sessionID}:accept`);
const res = await apiCall(sessionID, info, host);
return res;
},
}),
// image generator
new DynamicStructuredTool({
name: "imgGenTool",
description: "Tool to generate images",
schema: z.object({
imgDesc: z.string().describe("Description of the image to generate"),
}),
func: async function ({ imgDesc }): Promise<any> {
const tool = new DallEAPIWrapper({});
const imageURL = await tool.invoke(imgDesc);
return imageURL;
},
}),
];
const prompt = await pull<ChatPromptTemplate>("thecodingbarista/superagent");
const agent = await createOpenAIFunctionsAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
});
const agentWithChatHistory = new RunnableWithMessageHistory({
runnable: agentExecutor,
getMessageHistory: (_sessionId) =>
new UpstashRedisChatMessageHistory({
sessionId: `${_sessionId}-superAgent`,
config: {
url: process.env.REDIS_URL!,
token: process.env.REDIS_TOKEN!,
},
sessionTTL: 300,
}),
inputMessagesKey: "input",
outputMessagesKey: "output",
historyMessagesKey: "chat_history",
}); DescriptionI want to try to have a agent that has a bunch of tools and in those tools I want there to be a rag feature where the user can upload a file and chat with it. However through my experimenting I was only able to use rag if I initialized the document when starting up my node server so it can embed as well. Is there a way to allow an agent to accept a document midway of a conversation with a user while keeping memory and access to all the other tools System InfoNode + Express |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
To dynamically chat with documents during a conversation with a user, while also maintaining access to other tools, you can leverage the Here's a simplified example of how to set up the const autogpt = AutoGPT.fromLLMAndTools(
new ChatOpenAI({ temperature: 0 }),
[
new ReadFileTool({ store: new InMemoryFileStore() }),
new WriteFileTool({ store: new InMemoryFileStore() }),
// Add other tools as needed
],
{
memory: new MemoryVectorStore(new OpenAIEmbeddings()).asRetriever(),
// Additional configuration options
},
); In this setup:
The For more detailed implementation, including methods in the This approach ensures that your agent can dynamically interact with uploaded documents without losing the conversational context or access to other functionalities.
|
Beta Was this translation helpful? Give feedback.
-
@dosu given the example let's say the user and the autogpt have had a conversation and now the user has uploaded the file, how does this get added into the context window for autogpt to refer to |
Beta Was this translation helpful? Give feedback.
To integrate a user-uploaded document into the AutoGPT conversation context after setting up your agent as described, you'll need to manage the document's content within the conversation's memory. Here's a concise guide to achieve this:
Initialize Memory Vector Store: Before starting your AutoGPT instance, create a
MemoryVectorStore
withOpenAIEmbeddings
to handle the embeddings for the document's content. This store is essential for managing the vectorized representations of the documents and conversation snippets.Configure Vector Store Retriever Memory: Use
VectorStoreRetrieverMemory
with yourMemoryVectorStore
instance. This configuration allows you to specify how many documents …