-
Notifications
You must be signed in to change notification settings - Fork 16.7k
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
Image prompt template does not respect template_format and assumes f-string #27411
Comments
@yubink Simply removing a set of quotes and the spaces around the variables in the prompt seems to give your desired results: from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
prompt = {
'type': 'image_url',
'image_url': {
'url': 'data:{image_type};base64, {image_data}',
'detail': 'low',
},
}
data = {
'image_type': 'image/png',
'image_data': 'dummyimageabcd12345'
}
template = ChatPromptTemplate.from_messages([('user', [prompt])], template_format='jinja2')
print(template.invoke(data)) |
That is another way of demonstrating the problem. By removing the braces and spaces, you turned the prompt template into f-string format (rather than jinja2), but despite specifying |
I see, I might look into introducing a fix. |
That'd be awesome! |
Fixes #27411 **Description:** Adds `template_format` to the `ImagePromptTemplate` class and updates passing in the `template_format` parameter from ChatPromptTemplate instead of the hardcoded "f-string". Also updated docs and typing related to `template_format` to be more up-to-date and specific. **Dependencies:** None **Add tests and docs**: Added unit tests to validate fix. Needed to update `test_chat` snapshot due to adding new attribute `template_format` in `ImagePromptTemplate`. --------- Co-authored-by: Vadym Barda <[email protected]>
Thank you for the fix! |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
The above code outputs:
Rather than the expected output:
It looks like the template_format parameter is not being respected here ("f-string" is hard-coded):
langchain/libs/core/langchain_core/prompts/chat.py
Line 556 in 0ebddab
Also, ImagePromptTemplate seems to be using string.format():
langchain/libs/core/langchain_core/prompts/image.py
Line 88 in 0ebddab
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: