langchain
langchain copied to clipboard
prefix and suffix as PromptTemplates in FewShotPromptTemplate
Hi there!
I am wondering if it's possible to use PromptTemplates as values for the prefix and suffix arguments of FewShotPromptTemplate.
Here's an example of what I would like to do. The following works as-is, but I would like it to work the same with the commented lines uncommented.
I would like this functionality because I have conditional logic for the prefix and suffix that would be more conveniently implemented with a PromptTemplate class rather than relying on f-string formatting.
from langchain import FewShotPromptTemplate, PromptTemplate
prefix = "Prompt prefix {prefix_arg}"
suffix = "Prompt suffix {suffix_arg}"
# prefix = PromptTemplate(template=prefix,input_variables=["prefix_arg"])
# suffix = PromptTemplate(template=suffix,input_variables=["suffix_arg"])
example_formatter = "In: {in}\nOut: {out}\n"
example_template = PromptTemplate(template=example_formatter,input_variables=["in","out"])
template = FewShotPromptTemplate(
prefix=prefix,
suffix=suffix,
example_prompt=example_template,
examples=[{"in":"example input","out":"example output"}],
example_separator="\n\n",
input_variables=["prefix_arg","suffix_arg"],
)
print(template.format(prefix_arg="prefix value",suffix_arg="suffix value"))
I'm new to LangChain, so not sure, but I imagine that this pattern might occur elsewhere... I will probably try to modify FewShotPromptTemplate to support this, and I would be happy to submit a PR if I can get some advice on where else similar changes might be necessary.
i like this a lot! will try to get this in tmrw!
@hwchase17 thank you - let me know if I can help, happy to add tests etc. to the PR.
i lied about the speed of getting this in :(
will try hard to get it in this weekend, sorry for delay!
i may end up modifying it to be a separate class for modularity
@hwchase17 no problem :) No rush on my end, I can use my forked version for the time being.
Are you thinking like a mixin for templated suffix and prefix?
Hi, @g-simmons! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.
From what I understand, the issue is about implementing conditional logic for the prefix and suffix arguments in FewShotPromptTemplate using a PromptTemplate class. It seems like you are new to LangChain and are willing to submit a PR with guidance on where else similar changes might be necessary. hwchase17 expressed interest in the idea and plans to work on it, but mentioned a possible delay and the potential for creating a separate class for modularity.
Before we proceed, we would like to confirm if this issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.
Thank you for your understanding and contribution to the LangChain project!