Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefix and suffix as PromptTemplates in FewShotPromptTemplate #783

Closed
g-simmons opened this issue Jan 28, 2023 · 6 comments
Closed

prefix and suffix as PromptTemplates in FewShotPromptTemplate #783

g-simmons opened this issue Jan 28, 2023 · 6 comments

Comments

@g-simmons
Copy link
Contributor

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"))
@g-simmons
Copy link
Contributor Author

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.

@hwchase17
Copy link
Contributor

i like this a lot! will try to get this in tmrw!

@g-simmons
Copy link
Contributor Author

@hwchase17 thank you - let me know if I can help, happy to add tests etc. to the PR.

@hwchase17
Copy link
Contributor

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

@g-simmons
Copy link
Contributor Author

@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?

hwchase17 added a commit that referenced this issue Feb 4, 2023
This PR implements #783. 

Now when the format method gets called on `FewShotPromptTemplate`, its
`prefix` and `suffix` are formatted via their own `.format()` methods if
they are `PromptTemplate`s. Then the remaining prompt gets formatted as
before.



https://github.com/g-simmons/langchain/blob/5396f0f46089658a92c04fb26dc4a12c2fdbbcff/langchain/prompts/few_shot.py#L98-L146


I also changed the `FewShotPromptTemplate.template_is_valid()` method to
only check validity for prefix and suffix if they are strings. I am
assuming that if they are `PromptTemplates`, they've already been
validated with the `PromptTemplate.template_is_valid()` method when they
were initialized.


https://github.com/g-simmons/langchain/blob/5396f0f46089658a92c04fb26dc4a12c2fdbbcff/langchain/prompts/few_shot.py#L62-L82

I added a simple test for the functionality, just copying the previous
test but converting the prefix and suffix to PromptTemplates:


https://github.com/g-simmons/langchain/blob/5396f0f46089658a92c04fb26dc4a12c2fdbbcff/tests/unit_tests/prompts/test_few_shot.py#L90-L117

Let me know what needs fixing :)

---------

Co-authored-by: Harrison Chase <[email protected]>
@dosubot
Copy link

dosubot bot commented Sep 9, 2023

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!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 9, 2023
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants