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

[Feature Request] Relax template validation to allow for passing objects as inputs with jinja2 #840

Closed
Glavin001 opened this issue Feb 2, 2023 · 5 comments · Fixed by #865

Comments

@Glavin001
Copy link

Use Case

I have complex objects with inner properties I want to use within the Jinja2 template.

Prior to #148 I believe it was possible. Fairly certain I have an older project with older langchain using this approach. Now when I update langchain I'm not able to do this.

Example

template = """The results are:
---------------------
{% for result in results %}
{{ result.someProperty }}
{% endfor %}
---------------------

{{ text }}

# {% for result in results %}
{{ result.anotherProperty }}
{% endfor %}
"""

prompt = PromptTemplate(
    input_variables=["text", "results"],
    template_format="jinja2",
    template=template,
)

Output with error:

UndefinedError                            Traceback (most recent call last)
Cell In[15], line 38
...
File ~/.cache/pypoetry/virtualenvs/REDACTED/lib/python3.11/site-packages/pydantic/main.py:340, in pydantic.main.BaseModel.__init__()
...
--> 485     return getattr(obj, attribute)
    486 except AttributeError:
    487     pass

UndefinedError: 'str object' has no attribute 'someProperty'

Workaround

Pass in a list of strings instead of objects..

Proposed Solution

I think this is the applicable code:
https://github.com/hwchase17/langchain/blame/b0d560be5636544aa9cfe305febf01a98fd83bc6/langchain/prompts/base.py#L43-L46

Even disabling validation of templates would be sufficient.

Thanks!

@hwchase17 hwchase17 linked a pull request Feb 3, 2023 that will close this issue
@hwchase17
Copy link
Contributor

does the above pr help?

@Glavin001
Copy link
Author

Wow, you're fast! 🚀 Thank you so much! I expect this will work for me ✅

FYI I'll definitely be trying to contribute with code/PRs in the future to langchain. I'm primarily a web developer, and re-learning Python now solely to be able to use langchain. I have other changes locally I'll try to generalize and contribute back when I have some time -- or if you and team haven't already finished those already, too 😉

@hwchase17
Copy link
Contributor

ooc, what languages are you most familiar with? javascript by any chance?

@Glavin001
Copy link
Author

Yes, JavaScript/TypeScript. Haven't written Python in years (maybe 10+).

@andersonbcdefg
Copy link

andersonbcdefg commented Mar 25, 2023

@hwchase17 It would be awesome if you could add Jinja2 example(s) back to the LangChain docs. There's like, ghost references to them in various places, but no working examples I can find (had to come here to find one, lol). Would especially appreciate an example with the ChatPromptTemplate, since using OAI chat models is so common now. The use case I'm trying to hack out right now is a multiple-choice question, where I want to just be able to pass the question in and use Jinja2 template to format it.

Edit: Removed example below because I figured out why it wasn't working. But still think it would be great to have examples in the docs!

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

Successfully merging a pull request may close this issue.

3 participants