-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #704 from FlowiseAI/feature/ConversationRetrievalQ…
…AAgent Feature/Conversation Retrieval Agent
- Loading branch information
Showing
18 changed files
with
1,008 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ages/components/nodes/agents/ConversationalRetrievalAgent/ConversationalRetrievalAgent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' | ||
import { initializeAgentExecutorWithOptions, AgentExecutor } from 'langchain/agents' | ||
import { getBaseClasses, mapChatHistory } from '../../../src/utils' | ||
import { flatten } from 'lodash' | ||
import { BaseChatMemory } from 'langchain/memory' | ||
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler' | ||
|
||
class ConversationalRetrievalAgent_Agents implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
|
||
constructor() { | ||
this.label = 'Conversational Retrieval Agent' | ||
this.name = 'conversationalRetrievalAgent' | ||
this.version = 1.0 | ||
this.type = 'AgentExecutor' | ||
this.category = 'Agents' | ||
this.icon = 'agent.svg' | ||
this.description = `An agent optimized for retrieval during conversation, answering questions based on past dialogue, all using OpenAI's Function Calling` | ||
this.baseClasses = [this.type, ...getBaseClasses(AgentExecutor)] | ||
this.inputs = [ | ||
{ | ||
label: 'Allowed Tools', | ||
name: 'tools', | ||
type: 'Tool', | ||
list: true | ||
}, | ||
{ | ||
label: 'Memory', | ||
name: 'memory', | ||
type: 'BaseChatMemory' | ||
}, | ||
{ | ||
label: 'OpenAI Chat Model', | ||
name: 'model', | ||
type: 'ChatOpenAI' | ||
}, | ||
{ | ||
label: 'System Message', | ||
name: 'systemMessage', | ||
type: 'string', | ||
rows: 4, | ||
optional: true, | ||
additionalParams: true | ||
} | ||
] | ||
} | ||
|
||
async init(nodeData: INodeData): Promise<any> { | ||
const model = nodeData.inputs?.model | ||
const memory = nodeData.inputs?.memory as BaseChatMemory | ||
const systemMessage = nodeData.inputs?.systemMessage as string | ||
|
||
let tools = nodeData.inputs?.tools | ||
tools = flatten(tools) | ||
|
||
const executor = await initializeAgentExecutorWithOptions(tools, model, { | ||
agentType: 'openai-functions', | ||
verbose: process.env.DEBUG === 'true' ? true : false, | ||
agentArgs: { | ||
prefix: systemMessage ?? `You are a helpful AI assistant.` | ||
}, | ||
returnIntermediateSteps: true | ||
}) | ||
executor.memory = memory | ||
return executor | ||
} | ||
|
||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> { | ||
const executor = nodeData.instance as AgentExecutor | ||
|
||
if (executor.memory) { | ||
;(executor.memory as any).memoryKey = 'chat_history' | ||
;(executor.memory as any).outputKey = 'output' | ||
;(executor.memory as any).chatHistory = mapChatHistory(options) | ||
} | ||
|
||
const loggerHandler = new ConsoleCallbackHandler(options.logger) | ||
|
||
if (options.socketIO && options.socketIOClientId) { | ||
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId) | ||
const result = await executor.call({ input }, [loggerHandler, handler]) | ||
return result?.output | ||
} else { | ||
const result = await executor.call({ input }, [loggerHandler]) | ||
return result?.output | ||
} | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: ConversationalRetrievalAgent_Agents } |
9 changes: 9 additions & 0 deletions
9
packages/components/nodes/agents/ConversationalRetrievalAgent/agent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.