-
For example, the import { loadQAStuffChain, RetrievalQAChain } from 'langchain/chains';
import { PromptTemplate } from 'langchain/prompts';
const template =
'You are an customer support agent working for a company. ' +
'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.\n\n" +
'{context}\n\n' +
'Question: {question}\n' +
'Helpful Answer:';
export const qaPrompt = new PromptTemplate({
template,
inputVariables: ['context', 'question']
});
export const qaStuffPrompt = new PromptTemplate({
template: template.replace('{question}', '{query}'),
inputVariables: ['context', 'query']
}); right now I'm doing manual replace as you can see but I would like to tell the prompt to "replace a variable" or use aliases, for example: export const qaPrompt = new PromptTemplate({
template,
inputVariables: ['context', 'question']
});
const qaStuffPrompt = qaPrompt.replaceVariable('question', 'query'); or export const qaPrompt = new PromptTemplate({
template,
inputVariables: ['context', 'question'],
aliasesVariables: [['question', 'query']] // this way it would be interchangeable accepting any
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Answer generated by a 🤖 AnswerI understand that you're looking to find a more efficient way to reuse prompts that require different variables in LangChain. Specifically, you're interested in replacing variables or using aliases within the PromptTemplate. Currently, LangChain doesn't have built-in functionality for replacing variables or using aliases in the PromptTemplate. The PromptTemplate class only accepts a template string and an array of input variables. However, I can suggest a workaround. You could extend the PromptTemplate class to include this functionality. Here's an example of how you could implement this: class ExtendedPromptTemplate extends PromptTemplate {
replaceVariable(oldVar: string, newVar: string) {
const newTemplate = this.template.replace(`{${oldVar}}`, `{${newVar}}`);
const newInputVariables = this.inputVariables.map(v => v === oldVar ? newVar : v);
return new ExtendedPromptTemplate({
template: newTemplate,
inputVariables: newInputVariables
});
}
}
export const qaPrompt = new ExtendedPromptTemplate({
template,
inputVariables: ['context', 'question']
});
const qaStuffPrompt = qaPrompt.replaceVariable('question', 'query'); In this code, I've created a new class This solution should work for your use case, but please note that it's a workaround and might not be the best solution depending on the complexity of your templates and the number of variables you need to replace. It's also important to note that this solution assumes that the variable to be replaced exists in the template and the inputVariables array, otherwise it will not work as expected. If you find this solution helpful and believe it could benefit others, I encourage you to consider making a pull request. This could be a valuable contribution to the LangChain community. SourcesThis response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. |
Beta Was this translation helpful? Give feedback.
-
wow! |
Beta Was this translation helpful? Give feedback.
-
wow~! |
Beta Was this translation helpful? Give feedback.
Answer generated by a 🤖
Answer
I understand that you're looking to find a more efficient way to reuse prompts that require different variables in LangChain. Specifically, you're interested in replacing variables or using aliases within the PromptTemplate.
Currently, LangChain doesn't have built-in functionality for replacing variables or using aliases in the PromptTemplate. The PromptTemplate class only accepts a template string and an array of input variables.
However, I can suggest a workaround. You could extend the PromptTemplate class to include this functionality. Here's an example of how you could implement this: