Merge pull request #580 from FlowiseAI/feature/ConversationalQAChain-Prompt

Bugfix/SystemMessage For Different Chain Type
This commit is contained in:
Henry Heng
2023-07-21 13:05:15 +01:00
committed by GitHub
2 changed files with 58 additions and 26 deletions
@@ -6,27 +6,13 @@ import { AIMessage, BaseRetriever, HumanMessage } from 'langchain/schema'
import { BaseChatMemory, BufferMemory, ChatMessageHistory } from 'langchain/memory' import { BaseChatMemory, BufferMemory, ChatMessageHistory } from 'langchain/memory'
import { PromptTemplate } from 'langchain/prompts' import { PromptTemplate } from 'langchain/prompts'
import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler' import { ConsoleCallbackHandler, CustomChainHandler } from '../../../src/handler'
import {
const default_qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. default_map_reduce_template,
default_qa_template,
{context} qa_template,
map_reduce_template,
Question: {question} CUSTOM_QUESTION_GENERATOR_CHAIN_PROMPT
Helpful Answer:` } from './prompts'
const qa_template = `Use the following pieces of context to answer the question at the end.
{context}
Question: {question}
Helpful Answer:`
const CUSTOM_QUESTION_GENERATOR_CHAIN_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language. include it in the standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`
class ConversationalRetrievalQAChain_Chains implements INode { class ConversationalRetrievalQAChain_Chains implements INode {
label: string label: string
@@ -118,16 +104,27 @@ class ConversationalRetrievalQAChain_Chains implements INode {
const obj: any = { const obj: any = {
verbose: process.env.DEBUG === 'true' ? true : false, verbose: process.env.DEBUG === 'true' ? true : false,
qaChainOptions: {
type: 'stuff',
prompt: PromptTemplate.fromTemplate(systemMessagePrompt ? `${systemMessagePrompt}\n${qa_template}` : default_qa_template)
},
questionGeneratorChainOptions: { questionGeneratorChainOptions: {
template: CUSTOM_QUESTION_GENERATOR_CHAIN_PROMPT template: CUSTOM_QUESTION_GENERATOR_CHAIN_PROMPT
} }
} }
if (returnSourceDocuments) obj.returnSourceDocuments = returnSourceDocuments if (returnSourceDocuments) obj.returnSourceDocuments = returnSourceDocuments
if (chainOption) obj.qaChainOptions = { ...obj.qaChainOptions, type: chainOption } if (chainOption === 'map_reduce') {
obj.qaChainOptions = {
type: 'map_reduce',
combinePrompt: PromptTemplate.fromTemplate(
systemMessagePrompt ? `${systemMessagePrompt}\n${map_reduce_template}` : default_map_reduce_template
)
}
} else if (chainOption === 'refine') {
// TODO: Add custom system message
} else {
obj.qaChainOptions = {
type: 'stuff',
prompt: PromptTemplate.fromTemplate(systemMessagePrompt ? `${systemMessagePrompt}\n${qa_template}` : default_qa_template)
}
}
if (memory) { if (memory) {
memory.inputKey = 'question' memory.inputKey = 'question'
memory.outputKey = 'text' memory.outputKey = 'text'
@@ -0,0 +1,35 @@
export const default_qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:`
export const qa_template = `Use the following pieces of context to answer the question at the end.
{context}
Question: {question}
Helpful Answer:`
export const default_map_reduce_template = `Given the following extracted parts of a long document and a question, create a final answer.
If you don't know the answer, just say that you don't know. Don't try to make up an answer.
{summaries}
Question: {question}
Helpful Answer:`
export const map_reduce_template = `Given the following extracted parts of a long document and a question, create a final answer.
{summaries}
Question: {question}
Helpful Answer:`
export const CUSTOM_QUESTION_GENERATOR_CHAIN_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language. include it in the standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`