-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
core[patch]: extract input variables for path
and detail
keys in order to format an ImagePromptTemplate
#22613
core[patch]: extract input variables for path
and detail
keys in order to format an ImagePromptTemplate
#22613
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
Hi @eyurtsev do you have some time to review this PR soon ? I'm planning to take some time off, so I won't be able to iterate on your reviews afterward. Thanks! |
path
and detail
keys in order to format an ImagePromptTemplate
path
and detail
keys in order to format an ImagePromptTemplate
Hi @baskaryan @eyurtsev @efriis anyone of you have some time to review this PR please ? This should allow users to pass a Here is an example of how it would work: from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import HumanMessage
image_path = 'path_to_your_image.jpg'
detail_parameter = 'high'
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
HumanMessage(content='Describe the following image.'),
HumanMessagePromptTemplate.from_template(
[{'image_url': {'path': {image_path}, 'detail': {detail_parameter}}]
)
]
)
prompt = chat_prompt_template.format(image_path=image_path, detail_parameter=detail_parameter) As you can see in this discussion #20820 there are several questions about how to use the Thank you ! |
Currently to upload a base 64 encoded image, users have to do something like this: import base64
from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import HumanMessage
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Path to your image
image_path = "path_to_your_image.jpg"
# Getting the base64 string
base64_image = encode_image(image_path)
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
HumanMessage(content='Describe the following image.'),
HumanMessagePromptTemplate.from_template(
[{'image_url': {'url': 'data:image/jpeg;base64,{base64_image}', 'detail': 'high'}] # the detail parameter has to be hard-coded and can't be formatted at runtime.
)
]
)
prompt = chat_prompt_template.format(base64_image=base64_image) |
On mobile right now so won't be able to review properly but from the PR description the API doesn't look correct chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
HumanMessage(content='Describe the following image.'),
HumanMessagePromptTemplate.from_template(
[{'image_url': {'path': {image_path}, 'detail': {detail_parameter}}]
)
]
)
prompt = chat_prompt_template.format(image_path=image_path, detail_parameter=detail_parameter) Image path has already been set when defining the human prompt template. It doesn't appear that format does anything? |
@eyurtsev Oh yes you are 100% right sorry for that. It is a mistake in the comment only not in the PR I think. I should have wrote this: from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import HumanMessage
image_path = 'path_to_your_image.jpg'
detail_parameter = 'high'
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
HumanMessage(content='Describe the following image.'),
HumanMessagePromptTemplate.from_template(
[{'image_url': {'path': '{image_path}', 'detail': '{detail_parameter}'}]
)
]
)
prompt = chat_prompt_template.format(image_path=image_path, detail_parameter=detail_parameter) |
Hi @eyurtsev, did you have some time to review this PR please ? |
path
and detail
keys in order to format an ImagePromptTemplate
path
and detail
keys in order to format an ImagePromptTemplate
path
and detail
keys in order to format an ImagePromptTemplate
path
and detail
keys in order to format an ImagePromptTemplate
@thdesc thanks for updating the PR! Merging now :) |
@eyurtsev Thanks for the merge! Sorry I wasn't available last week and didn't have my computer to fix the broken test. I want to create another PR related to the ImagePromptTemplate. I will do it soon. Thanks again! |
path
anddetail
keys inImagePromptTemplate
. Previously, only variables associated with theurl
key were considered. This PR allows for the inclusion of a local image path and a detail parameter as input to the format method.