langchain-ai / langchain

๐Ÿฆœ๐Ÿ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.28k stars 14.74k forks source link

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

Closed Glavin001 closed 1 year ago

Glavin001 commented 1 year ago

Use Case

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

Prior to https://github.com/hwchase17/langchain/pull/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 commented 1 year ago

does the above pr help?

Glavin001 commented 1 year ago

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 commented 1 year ago

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

Glavin001 commented 1 year ago

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

andersonbcdefg commented 1 year ago

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