228 lines
9.1 KiB
TypeScript
228 lines
9.1 KiB
TypeScript
import { flatten } from 'lodash'
|
|
import { Tool } from '@langchain/core/tools'
|
|
import { BaseChatModel } from '@langchain/core/language_models/chat_models'
|
|
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages'
|
|
import { ChainValues } from '@langchain/core/utils/types'
|
|
import { AgentStep } from '@langchain/core/agents'
|
|
import { renderTemplate, MessagesPlaceholder, HumanMessagePromptTemplate, PromptTemplate } from '@langchain/core/prompts'
|
|
import { RunnableSequence } from '@langchain/core/runnables'
|
|
import { ChatConversationalAgent } from 'langchain/agents'
|
|
import { getBaseClasses } from '../../../src/utils'
|
|
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
|
|
import { IVisionChatModal, FlowiseMemory, ICommonObject, IMessage, INode, INodeData, INodeParams } from '../../../src/Interface'
|
|
import { AgentExecutor } from '../../../src/agents'
|
|
import { addImagesToMessages, llmSupportsVision } from '../../../src/multiModalUtils'
|
|
|
|
const DEFAULT_PREFIX = `Assistant is a large language model trained by OpenAI.
|
|
|
|
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
|
|
|
|
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
|
|
|
Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.`
|
|
|
|
const TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
|
|
---------------------
|
|
{observation}
|
|
|
|
USER'S INPUT
|
|
--------------------
|
|
|
|
Okay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.`
|
|
|
|
class ConversationalAgent_Agents implements INode {
|
|
label: string
|
|
name: string
|
|
version: number
|
|
description: string
|
|
type: string
|
|
icon: string
|
|
category: string
|
|
baseClasses: string[]
|
|
inputs: INodeParams[]
|
|
sessionId?: string
|
|
|
|
constructor(fields?: { sessionId?: string }) {
|
|
this.label = 'Conversational Agent'
|
|
this.name = 'conversationalAgent'
|
|
this.version = 2.0
|
|
this.type = 'AgentExecutor'
|
|
this.category = 'Agents'
|
|
this.icon = 'agent.svg'
|
|
this.description = 'Conversational agent for a chat model. It will utilize chat specific prompts'
|
|
this.baseClasses = [this.type, ...getBaseClasses(AgentExecutor)]
|
|
this.inputs = [
|
|
{
|
|
label: 'Allowed Tools',
|
|
name: 'tools',
|
|
type: 'Tool',
|
|
list: true
|
|
},
|
|
{
|
|
label: 'Chat Model',
|
|
name: 'model',
|
|
type: 'BaseChatModel'
|
|
},
|
|
{
|
|
label: 'Memory',
|
|
name: 'memory',
|
|
type: 'BaseChatMemory'
|
|
},
|
|
{
|
|
label: 'System Message',
|
|
name: 'systemMessage',
|
|
type: 'string',
|
|
rows: 4,
|
|
default: DEFAULT_PREFIX,
|
|
optional: true,
|
|
additionalParams: true
|
|
}
|
|
]
|
|
this.sessionId = fields?.sessionId
|
|
}
|
|
|
|
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
|
|
return prepareAgent(nodeData, options, { sessionId: this.sessionId, chatId: options.chatId, input }, options.chatHistory)
|
|
}
|
|
|
|
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
|
const memory = nodeData.inputs?.memory as FlowiseMemory
|
|
|
|
const executor = await prepareAgent(
|
|
nodeData,
|
|
options,
|
|
{ sessionId: this.sessionId, chatId: options.chatId, input },
|
|
options.chatHistory
|
|
)
|
|
|
|
const loggerHandler = new ConsoleCallbackHandler(options.logger)
|
|
const callbacks = await additionalCallbacks(nodeData, options)
|
|
|
|
let res: ChainValues = {}
|
|
|
|
if (options.socketIO && options.socketIOClientId) {
|
|
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
|
|
res = await executor.invoke({ input }, { callbacks: [loggerHandler, handler, ...callbacks] })
|
|
} else {
|
|
res = await executor.invoke({ input }, { callbacks: [loggerHandler, ...callbacks] })
|
|
}
|
|
|
|
await memory.addChatMessages(
|
|
[
|
|
{
|
|
text: input,
|
|
type: 'userMessage'
|
|
},
|
|
{
|
|
text: res?.output,
|
|
type: 'apiMessage'
|
|
}
|
|
],
|
|
this.sessionId
|
|
)
|
|
|
|
return res?.output
|
|
}
|
|
}
|
|
|
|
const prepareAgent = async (
|
|
nodeData: INodeData,
|
|
options: ICommonObject,
|
|
flowObj: { sessionId?: string; chatId?: string; input?: string },
|
|
chatHistory: IMessage[] = []
|
|
) => {
|
|
const model = nodeData.inputs?.model as BaseChatModel
|
|
let tools = nodeData.inputs?.tools as Tool[]
|
|
tools = flatten(tools)
|
|
const memory = nodeData.inputs?.memory as FlowiseMemory
|
|
const systemMessage = nodeData.inputs?.systemMessage as string
|
|
const memoryKey = memory.memoryKey ? memory.memoryKey : 'chat_history'
|
|
const inputKey = memory.inputKey ? memory.inputKey : 'input'
|
|
|
|
const outputParser = ChatConversationalAgent.getDefaultOutputParser({
|
|
llm: model,
|
|
toolNames: tools.map((tool) => tool.name)
|
|
})
|
|
|
|
const prompt = ChatConversationalAgent.createPrompt(tools, {
|
|
systemMessage: systemMessage ? systemMessage : DEFAULT_PREFIX,
|
|
outputParser
|
|
})
|
|
|
|
if (llmSupportsVision(model)) {
|
|
const visionChatModel = model as IVisionChatModal
|
|
const messageContent = addImagesToMessages(nodeData, options, model.multiModalOption)
|
|
|
|
if (messageContent?.length) {
|
|
visionChatModel.setVisionModel()
|
|
|
|
// Pop the `agent_scratchpad` MessagePlaceHolder
|
|
let messagePlaceholder = prompt.promptMessages.pop() as MessagesPlaceholder
|
|
if (prompt.promptMessages.at(-1) instanceof HumanMessagePromptTemplate) {
|
|
const lastMessage = prompt.promptMessages.pop() as HumanMessagePromptTemplate
|
|
const template = (lastMessage.prompt as PromptTemplate).template as string
|
|
const msg = HumanMessagePromptTemplate.fromTemplate([
|
|
...messageContent,
|
|
{
|
|
text: template
|
|
}
|
|
])
|
|
msg.inputVariables = lastMessage.inputVariables
|
|
prompt.promptMessages.push(msg)
|
|
}
|
|
|
|
// Add the `agent_scratchpad` MessagePlaceHolder back
|
|
prompt.promptMessages.push(messagePlaceholder)
|
|
} else {
|
|
visionChatModel.revertToOriginalModel()
|
|
}
|
|
}
|
|
|
|
/** Bind a stop token to the model */
|
|
const modelWithStop = model.bind({
|
|
stop: ['\nObservation']
|
|
})
|
|
|
|
const runnableAgent = RunnableSequence.from([
|
|
{
|
|
[inputKey]: (i: { input: string; steps: AgentStep[] }) => i.input,
|
|
agent_scratchpad: async (i: { input: string; steps: AgentStep[] }) => await constructScratchPad(i.steps),
|
|
[memoryKey]: async (_: { input: string; steps: AgentStep[] }) => {
|
|
const messages = (await memory.getChatMessages(flowObj?.sessionId, true, chatHistory)) as BaseMessage[]
|
|
return messages ?? []
|
|
}
|
|
},
|
|
prompt,
|
|
modelWithStop,
|
|
outputParser
|
|
])
|
|
|
|
const executor = AgentExecutor.fromAgentAndTools({
|
|
agent: runnableAgent,
|
|
tools,
|
|
sessionId: flowObj?.sessionId,
|
|
chatId: flowObj?.chatId,
|
|
input: flowObj?.input,
|
|
verbose: process.env.DEBUG === 'true'
|
|
})
|
|
|
|
return executor
|
|
}
|
|
|
|
const constructScratchPad = async (steps: AgentStep[]): Promise<BaseMessage[]> => {
|
|
const thoughts: BaseMessage[] = []
|
|
for (const step of steps) {
|
|
thoughts.push(new AIMessage(step.action.log))
|
|
thoughts.push(
|
|
new HumanMessage(
|
|
renderTemplate(TEMPLATE_TOOL_RESPONSE, 'f-string', {
|
|
observation: step.observation
|
|
})
|
|
)
|
|
)
|
|
}
|
|
return thoughts
|
|
}
|
|
|
|
module.exports = { nodeClass: ConversationalAgent_Agents }
|