How to use ImagePromptTemplate #20820
Replies: 4 comments 11 replies
-
To use from langchain_core.prompts.image import ImagePromptTemplate
# Initialize with a template if needed
image_prompt_template = ImagePromptTemplate(template={"url": "http://example.com/{image_name}.jpg"})
# Format the prompt with an image name
formatted_prompt = image_prompt_template.format(image_name="your_image_name")
# Using a direct URL
formatted_prompt_direct_url = image_prompt_template.format(url="http://example.com/your_image_name.jpg")
# Using a local path (converts image to a data URL)
formatted_prompt_path = image_prompt_template.format(path="/path/to/your/image.jpg")
print(formatted_prompt)
print(formatted_prompt_direct_url)
print(formatted_prompt_path) This code snippet shows how to create an image prompt using
|
Beta Was this translation helpful? Give feedback.
-
After some investigation, I think some changes must be made to the following file: libs/core/langchain_core/prompts/chat.py, to ensure Suggested Changes:
Code Example:If you apply these changes, the following code should work: from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import SystemMessage
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
SystemMessage(content='Describe the following image very briefly.'),
HumanMessagePromptTemplate.from_template(
# This line is an ugly hack. It will trigger the ImagePromptTemplate internally (see [Line 455](https://github.com/langchain-ai/langchain/blob/d8a1f1114d839b2a902ff9bd4ae668751257414a/libs/core/langchain_core/prompts/chat.py#L455C1-L455C64))
[{'image_url': ""}]
)
]
)
url = 'https://fastly.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U'
detail = 'low'
print(chat_prompt_template)
# Output:
# ChatPromptTemplate(input_variables=[], messages=[SystemMessage(content='Describe the following image very briefly.'), HumanMessagePromptTemplate(prompt=[ImagePromptTemplate(input_variables=[], template={'url': ''})])])
result = chat_prompt_template.invoke(input={'url': url, 'detail': detail})
print(result)
# Output:
# ChatPromptValue(messages=[SystemMessage(content='Describe the following image very briefly.'), HumanMessage(content=[{'type': 'image_url', 'image_url': {'url': 'https://fastly.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U', 'detail': 'low'}}])]) This also works with a local Chain Example with an LLM:from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
SystemMessage(content='Describe the following image very briefly.'),
HumanMessagePromptTemplate.from_template(
[{'image_url': ""}]
)
]
)
url = 'https://fastly.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U'
detail = 'low'
llm = ChatOpenAI(model_name="gpt-4o")
output_parser = StrOutputParser()
chain = chat_prompt_template | llm | output_parser
result = chain.invoke(input={"url": url, "detail": detail})
print(result)
# Output:
# The image shows a black Labrador Retriever puppy lying on a wooden surface. |
Beta Was this translation helpful? Give feedback.
-
I was able to get ImagePromptTemplate to work straight up with the latest release using the above. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone ! This PR has been merged #22613. Now you can directly pass a local path to an image without needing to extract, encode, and format the content of the image file yourself! Here how it works: from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_core.messages import HumanMessage
image_path = 'path/to/image.png'
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)
print(prompt[:150])
# Output:
# Human: Describe the following image.
# Human: [{'type': 'image_url', 'image_url': {'url': 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABQAAABMXCAYAAAA |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
How to use ImagePromptTemplate?
there is no info in the doc.
anyone has sample code?
System Info
Ubuntu 22.04
Beta Was this translation helpful? Give feedback.
All reactions