mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 11:00:55 +03:00
Merge branch 'main' into chore/Upgrade-LC-version
# Conflicts: # packages/components/nodes/cache/RedisCache/RedisCache.ts # packages/components/nodes/cache/RedisCache/RedisEmbeddingsCache.ts # packages/components/nodes/chains/ConversationChain/ConversationChain.ts # packages/components/nodes/tools/RetrieverTool/RetrieverTool.ts # packages/components/nodes/vectorstores/Qdrant/Qdrant.ts # packages/components/nodes/vectorstores/Redis/Redis.ts # packages/components/nodes/vectorstores/Redis/RedisSearchBase.ts # packages/components/nodes/vectorstores/Redis/Redis_Existing.ts # packages/components/nodes/vectorstores/Redis/Redis_Upsert.ts # packages/components/src/agents.ts
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { flatten } from 'lodash'
|
||||
import { ChainValues } from '@langchain/core/utils/types'
|
||||
import { AgentStep, AgentFinish, AgentAction } from '@langchain/core/agents'
|
||||
import { AgentStep, AgentAction } from '@langchain/core/agents'
|
||||
import { BaseMessage, FunctionMessage, AIMessage } from '@langchain/core/messages'
|
||||
import { OutputParserException } from '@langchain/core/output_parsers'
|
||||
import { CallbackManager, CallbackManagerForChainRun, Callbacks } from '@langchain/core/callbacks/manager'
|
||||
@@ -9,6 +10,11 @@ import { Serializable } from '@langchain/core/load/serializable'
|
||||
import { BaseChain, SerializedLLMChain } from 'langchain/chains'
|
||||
import { AgentExecutorInput, BaseSingleActionAgent, BaseMultiActionAgent, RunnableAgent, StoppingMethod } from 'langchain/agents'
|
||||
|
||||
export const SOURCE_DOCUMENTS_PREFIX = '\n\n----FLOWISE_SOURCE_DOCUMENTS----\n\n'
|
||||
type AgentFinish = {
|
||||
returnValues: Record<string, any>
|
||||
log: string
|
||||
}
|
||||
type AgentExecutorOutput = ChainValues
|
||||
|
||||
interface AgentExecutorIteratorInput {
|
||||
@@ -317,10 +323,12 @@ export class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
|
||||
|
||||
const steps: AgentStep[] = []
|
||||
let iterations = 0
|
||||
let sourceDocuments: Array<Document> = []
|
||||
|
||||
const getOutput = async (finishStep: AgentFinish): Promise<AgentExecutorOutput> => {
|
||||
const { returnValues } = finishStep
|
||||
const additional = await this.agent.prepareForOutput(returnValues, steps)
|
||||
if (sourceDocuments.length) additional.sourceDocuments = flatten(sourceDocuments)
|
||||
|
||||
if (this.returnIntermediateSteps) {
|
||||
return { ...returnValues, intermediateSteps: steps, ...additional }
|
||||
@@ -408,6 +416,17 @@ export class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
|
||||
return { action, observation: observation ?? '' }
|
||||
}
|
||||
}
|
||||
if (observation?.includes(SOURCE_DOCUMENTS_PREFIX)) {
|
||||
const observationArray = observation.split(SOURCE_DOCUMENTS_PREFIX)
|
||||
observation = observationArray[0]
|
||||
const docs = observationArray[1]
|
||||
try {
|
||||
const parsedDocs = JSON.parse(docs)
|
||||
sourceDocuments.push(parsedDocs)
|
||||
} catch (e) {
|
||||
console.error('Error parsing source documents from tool')
|
||||
}
|
||||
}
|
||||
return { action, observation: observation ?? '' }
|
||||
})
|
||||
)
|
||||
@@ -502,6 +521,10 @@ export class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
|
||||
chatId: this.chatId,
|
||||
input: this.input
|
||||
})
|
||||
if (observation?.includes(SOURCE_DOCUMENTS_PREFIX)) {
|
||||
const observationArray = observation.split(SOURCE_DOCUMENTS_PREFIX)
|
||||
observation = observationArray[0]
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof ToolInputParsingException) {
|
||||
if (this.handleParsingErrors === true) {
|
||||
|
||||
Reference in New Issue
Block a user