Replies: 1 comment
-
I found a solution for my problem, here is the code for anyone that need it: async getAssistantAnswer(document: string, question: string) {
const { id } = await this.documentsService.findOne(document);
const client = new MongoClient('mongodb://localhost:27017/chatbot');
await client.connect();
const collection = client.db('chatbot').collection('memory');
const memory = new BufferMemory({
returnMessages: true,
memoryKey: 'chat_history',
inputKey: 'input',
outputKey: 'output',
chatHistory: new MongoDBChatMessageHistory({
collection,
sessionId: id,
}),
});
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a helpful assistant'],
new MessagesPlaceholder('chat_history'),
['human', '{input}'],
new MessagesPlaceholder('agent_scratchpad'),
]);
const vectorStore = this.vectorStoreService.createVectorStore(id);
const retriever = vectorStore.asRetriever();
const retrieverTool = createRetrieverTool(retriever, {
name: id,
description: `Tool to search information about ${document}`,
});
const tools = [retrieverTool];
const agent = await createOpenAIFunctionsAgent({
llm: this.model,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
memory,
});
const chat_history = await memory.chatHistory.getMessages();
const response = await agentExecutor.invoke({
outputKey: 'output',
input: question,
chat_history,
});
return response.output;
} Now the agent is working with memory stored in a mongDB database: {
response: {
outputKey: 'output',
input: 'Hello my name is Alberto',
chat_history: [],
output: 'Hello Alberto! How can I assist you today?'
}
}
{
response: {
outputKey: 'output',
input: 'Did I told you my name?',
chat_history: [ [HumanMessage], [AIMessage] ],
output: 'Yes, you mentioned your name when you first greeted me. How can I help you, Alberto?'
}
}
{
response: {
outputKey: 'output',
input: 'Whats my name?',
chat_history: [ [HumanMessage], [AIMessage], [HumanMessage], [AIMessage] ],
output: 'Your name is Alberto. How can I assist you today, Alberto?'
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to add memory to an agent using BufferMemory and MongoDbChatMessageHistory, the messages are beeing stored correctly in the chatbot db under memory collection, but I`m getting the error:
Error: output values have 3 keys, you must specify an output key or pass only 1 key as output
I'm beeing using Lanchain js for a couple of days now but I'm new to the Agent concept, I implemented this with an ConversationChain with no problems, but I don't what to do with Agents, any help?
Sorry for my english, I tried my best ^^".
System Info
Platform: windows
Node version: 20.10.0
"@langchain/community": "^0.0.33",
"@langchain/core": "^0.1.36",
"@langchain/mongodb": "^0.0.1",
"@langchain/openai": "^0.0.14",
Beta Was this translation helpful? Give feedback.
All reactions