From 74939c187a583bd434bb14cfa75b2497ec0f8a60 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 14 Jun 2023 13:39:38 +0100 Subject: [PATCH 1/2] add fixes --- .../nodes/chains/MultiPromptChain/MultiPromptChain.ts | 2 +- .../nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts b/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts index c74e3257f3d..659641f8cc0 100644 --- a/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts +++ b/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts @@ -61,7 +61,7 @@ class MultiPromptChain_Chains implements INode { const obj = { input } if (options.socketIO && options.socketIOClientId) { - const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId) + const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2) const res = await chain.call(obj, [handler]) return res?.text } else { diff --git a/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts b/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts index b17125c2101..b18ac86772a 100644 --- a/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts +++ b/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts @@ -61,7 +61,7 @@ class MultiRetrievalQAChain_Chains implements INode { const obj = { input } if (options.socketIO && options.socketIOClientId) { - const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId) + const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2) const res = await chain.call(obj, [handler]) return res?.text } else { From e50c065acad8a552832180b12e63af687f715efc Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 14 Jun 2023 15:47:33 +0100 Subject: [PATCH 2/2] update multi chains --- .../MultiPromptChain/MultiPromptChain.ts | 9 ++++--- .../MultiRetrievalQAChain.ts | 26 ++++++++++++++----- .../Multi Retrieval QA Chain.json | 9 ++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts b/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts index 659641f8cc0..189f41f7297 100644 --- a/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts +++ b/packages/components/nodes/chains/MultiPromptChain/MultiPromptChain.ts @@ -49,9 +49,12 @@ class MultiPromptChain_Chains implements INode { promptTemplates.push(prompt.systemMessage) } - const chain = MultiPromptChain.fromPrompts(model, promptNames, promptDescriptions, promptTemplates, undefined, { - verbose: process.env.DEBUG === 'true' ? true : false - } as any) + const chain = MultiPromptChain.fromLLMAndPrompts(model, { + promptNames, + promptDescriptions, + promptTemplates, + llmChainOpts: { verbose: process.env.DEBUG === 'true' ? true : false } + }) return chain } diff --git a/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts b/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts index b18ac86772a..b3575a93073 100644 --- a/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts +++ b/packages/components/nodes/chains/MultiRetrievalQAChain/MultiRetrievalQAChain.ts @@ -32,6 +32,12 @@ class MultiRetrievalQAChain_Chains implements INode { name: 'vectorStoreRetriever', type: 'VectorStoreRetriever', list: true + }, + { + label: 'Return Source Documents', + name: 'returnSourceDocuments', + type: 'boolean', + optional: true } ] } @@ -39,6 +45,8 @@ class MultiRetrievalQAChain_Chains implements INode { async init(nodeData: INodeData): Promise { const model = nodeData.inputs?.model as BaseLanguageModel const vectorStoreRetriever = nodeData.inputs?.vectorStoreRetriever as VectorStoreRetriever[] + const returnSourceDocuments = nodeData.inputs?.returnSourceDocuments as boolean + const retrieverNames = [] const retrieverDescriptions = [] const retrievers = [] @@ -49,23 +57,29 @@ class MultiRetrievalQAChain_Chains implements INode { retrievers.push(vs.vectorStore.asRetriever((vs.vectorStore as any).k ?? 4)) } - const chain = MultiRetrievalQAChain.fromRetrievers(model, retrieverNames, retrieverDescriptions, retrievers, undefined, { - verbose: process.env.DEBUG === 'true' ? true : false - } as any) - + const chain = MultiRetrievalQAChain.fromLLMAndRetrievers(model, { + retrieverNames, + retrieverDescriptions, + retrievers, + retrievalQAChainOpts: { verbose: process.env.DEBUG === 'true' ? true : false, returnSourceDocuments } + }) return chain } - async run(nodeData: INodeData, input: string, options: ICommonObject): Promise { + async run(nodeData: INodeData, input: string, options: ICommonObject): Promise { const chain = nodeData.instance as MultiRetrievalQAChain + const returnSourceDocuments = nodeData.inputs?.returnSourceDocuments as boolean + const obj = { input } if (options.socketIO && options.socketIOClientId) { - const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2) + const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2, returnSourceDocuments) const res = await chain.call(obj, [handler]) + if (res.text && res.sourceDocuments) return res return res?.text } else { const res = await chain.call(obj) + if (res.text && res.sourceDocuments) return res return res?.text } } diff --git a/packages/server/marketplaces/Multi Retrieval QA Chain.json b/packages/server/marketplaces/Multi Retrieval QA Chain.json index cdb468d1fe9..f3cd1fcc18b 100644 --- a/packages/server/marketplaces/Multi Retrieval QA Chain.json +++ b/packages/server/marketplaces/Multi Retrieval QA Chain.json @@ -84,7 +84,14 @@ "baseClasses": ["MultiRetrievalQAChain", "MultiRouteChain", "BaseChain", "BaseLangChain"], "category": "Chains", "description": "QA Chain that automatically picks an appropriate vector store from multiple retrievers", - "inputParams": [], + "inputParams": [ + { + "label": "Return Source Documents", + "name": "returnSourceDocuments", + "type": "boolean", + "optional": true + } + ], "inputAnchors": [ { "label": "Language Model",