Example that combines message history / memory, tools, and the agent executor? #1602
Unanswered
theogravity
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Here's my version: const STRING_SPLIT_REGEX = /( +)/;
// key must be chat_history
const memory = new BufferWindowMemory({ k: 2, memoryKey: 'chat_history', returnMessages: true });
// Populate with past ai / user chat history
// do not include the user's latest prompt as that's used separately
const messageHistory = [];
messageHistory.forEach((message: { role: string; content: string }) => {
switch (message.role) {
case 'system':
// There is no system message for agent executors since they generate
// their own for use with tools
break;
case 'ai':
memory.chatHistory.addAIChatMessage(message.content);
break;
case 'user':
memory.chatHistory.addUserMessage(message.content);
break;
}
});
const tools: Tool[] = [
// Add your tools here
];
const llm = new ChatOpenAI({
openAIApiKey: OPEN_AI_API_KEY,
// I've found this doesn't stream the final output properly, it instead streams the agent executor instruction
// which is what we don't want
streaming: false,
n: 1,
modelName: model,
});
const agent = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: 'structured-chat-zero-shot-react-description',
verbose: false,
memory,
agentArgs: {
inputVariables: ['input', 'agent_scratchpad', 'chat_history'],
memoryPrompts: [new MessagesPlaceholder('chat_history')],
},
});
const response = await agent.call({
input: prompt,
});
// stream out the response by word at a time
const strings = response.output.split(STRING_SPLIT_REGEX);
strings.forEach((string: string) => {
res.write(string)
});
// end the connection
res.end() Resources I read to figure this out:
|
Beta Was this translation helpful? Give feedback.
2 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.
-
All the conversational agent examples seem to only use a message followed by a single prompt.
ChatPromptTemplate.fromPromptMessages()
BufferWindowMemory
to trim large historiesBeta Was this translation helpful? Give feedback.
All reactions