How to optionally send a multimodal message into a ChatPromptTemplate #23374
Replies: 2 comments 4 replies
-
Hey @jefflavallee! I'm here to help you with any bugs, questions, or contributions you have in mind. Let's work together to solve this! To optionally send a multimodal message into a
Here's an example: import base64
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.messages import SystemMessage, HumanMessage
# Read and encode the image
with open("./Output/sample.jpg", "rb") as image_file:
base64_encoded_data = base64.b64encode(image_file.read()).decode("UTF-8")
# Define the template with placeholders
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
SystemMessage(content='You are a helpful assistant'),
HumanMessage(content=[
{"type": "image_url", "image_url": "data:image/jpeg;base64,{img_base64}"},
{"type": "text", "text": "describe this image"}
])
]
)
# Format the template with the dynamic base64 value
formatted_messages = chat_prompt_template.format_messages(img_base64=base64_encoded_data)
# Create the chain with the template, the LLM, and the output parser
chain = formatted_messages | llm | output_parser
# Invoke the chain
result = chain.invoke({})
print(result) This approach ensures that the base64 image data is dynamically assigned when invoking the prompt, avoiding the need to hard-code it into the template. Additionally, if you want to optionally include the image in the message, you can conditionally format the message content based on whether the image data is provided: import base64
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.messages import SystemMessage, HumanMessage
# Read and encode the image
with open("./Output/sample.jpg", "rb") as image_file:
base64_encoded_data = base64.b64encode(image_file.read()).decode("UTF-8")
# Define the template with placeholders
chat_prompt_template = ChatPromptTemplate.from_messages(
messages=[
SystemMessage(content='You are a helpful assistant'),
HumanMessage(content="{human_msg}")
]
)
# Create the human message content dynamically
human_msg = [
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{base64_encoded_data}"},
{"type": "text", "text": "describe this image"}
]
# Format the template with the dynamic human message
formatted_messages = chat_prompt_template.format_messages(human_msg=human_msg)
# Create the chain with the template, the LLM, and the output parser
chain = formatted_messages | llm | output_parser
# Invoke the chain
result = chain.invoke({})
print(result) This way, you can pass the entire human message, including the image, as a variable when invoking the prompt [1][2][3]. |
Beta Was this translation helpful? Give feedback.
-
Maybe related or not but a way to pass the image in the tag/fstring is you need to make sure the image is in a list. A way to fix this is using prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a chatbot"),
("human", "{input}"),
("placeholder", "{image}"),
]
)
agent = prompt | llm
agent.invoke({
"input":"what is this",
"image" : [
("human", [
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_1}"},
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_2}"},
},
]),
]
}) From here you can add multiple image or do any modification before passing into the prompt. |
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
I'm trying to pass a multimodal message into my LLM, specifically Claude 3.5, but I'm unsure how to pass a base64 value as a variable from the invoke method.
When I hard-code the base64 string into the prompt, as above within the f-string, it works fine.
However when I use a template value like below i get a message indicating "anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.0.content.0.image.source.base64: invalid base64 data'}}"
Ultimately I'd like to be able to pass the whole human message into the prompt when I invoke it which may or may not include an image. So something like
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
Beta Was this translation helpful? Give feedback.
All reactions