feat: Add options to filter conversation history messages used in sequential LLM and Agent nodes (#3653)

* feat: Add option to disable conversation history

- Add new `disableConversationHistory` boolean parameter in LLMNodes.ts and Agent.ts to optionally skip including conversation history in prompts
- Fix potential error in Agent.ts when messages array is empty by adding null safety checks
- Improve memory efficiency by allowing stateless interactions when history isn't needed

* feat: add conversation history filtering options

Replace the disable conversation history feature with a more flexible filtering system that allows selecting:
- User question only
- Last message only
- All messages (default)
- No messages

This provides more granular control over conversation context management.

* chore: break lines

* chore: removed ending semi columns

* chore: fix eslint errors

* fix(sequentialagents): improve conversation history filtering logic

- Remove unnecessary state.messages check for user_question case
- Add proper null handling for last_message and all_messages cases
- Remove @ts-ignore comments with proper typing

* Update LLMNode.ts

* Update Agent.ts

---------

Co-authored-by: Henry Heng <henryheng@flowiseai.com>
This commit is contained in:
Jean Ibarz
2024-12-12 14:29:28 +01:00
committed by GitHub
parent d974564ba5
commit 26b78ad55a
4 changed files with 147 additions and 29 deletions
@@ -9,7 +9,14 @@ import { Runnable, RunnableConfig, mergeConfigs } from '@langchain/core/runnable
import { AIMessage, BaseMessage, HumanMessage, MessageContentImageUrl, ToolMessage } from '@langchain/core/messages'
import { BaseChatModel } from '@langchain/core/language_models/chat_models'
import { addImagesToMessages, llmSupportsVision } from '../../src/multiModalUtils'
import { ICommonObject, IDatabaseEntity, INodeData, ISeqAgentsState, IVisionChatModal } from '../../src/Interface'
import {
ICommonObject,
IDatabaseEntity,
INodeData,
ISeqAgentsState,
IVisionChatModal,
ConversationHistorySelection
} from '../../src/Interface'
import { availableDependencies, defaultAllowBuiltInDep, getVars, prepareSandboxVars } from '../../src/utils'
import { ChatPromptTemplate, BaseMessagePromptTemplateLike } from '@langchain/core/prompts'
@@ -208,6 +215,34 @@ export const convertStructuredSchemaToZod = (schema: string | object): ICommonOb
}
}
/**
* Filter the conversation history based on the selected option.
*
* @param historySelection - The selected history option.
* @param input - The user input.
* @param state - The current state of the sequential llm or agent node.
*/
export function filterConversationHistory(
historySelection: ConversationHistorySelection,
input: string,
state: ISeqAgentsState
): BaseMessage[] {
switch (historySelection) {
case 'user_question':
return [new HumanMessage(input)]
case 'last_message':
// @ts-ignore
return state.messages?.length ? [state.messages[state.messages.length - 1] as BaseMessage] : []
case 'empty':
return []
case 'all_messages':
// @ts-ignore
return (state.messages as BaseMessage[]) ?? []
default:
throw new Error(`Unhandled conversationHistorySelection: ${historySelection}`)
}
}
export const restructureMessages = (llm: BaseChatModel, state: ISeqAgentsState) => {
const messages: BaseMessage[] = []
for (const message of state.messages as unknown as BaseMessage[]) {