Skip to content
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

Merged
merged 16 commits into from
Jul 3, 2024

Conversation

thdesc
Copy link
Contributor

@thdesc thdesc commented Jun 6, 2024

Copy link

vercel bot commented Jun 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 6:43pm

@thdesc
Copy link
Contributor Author

thdesc commented Jun 10, 2024

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!

@thdesc thdesc changed the title core: extract input variables for path and detail keys in order to format an ImagePromptTemplate langchain-core[minor]: extract input variables for path and detail keys in order to format an ImagePromptTemplate Jun 10, 2024
@thdesc
Copy link
Contributor Author

thdesc commented Jun 12, 2024

Hi @baskaryan @eyurtsev @efriis anyone of you have some time to review this PR please ? This should allow users to pass a path (local path to an image file) and a detail parameter (to control how the LLM processes the image) when formatting an HumanMessagePromptTemplate with a image_url.

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 ImagePromptTemplate with a HumanMessagePromptTemplate and I think this PR could help.

Thank you !

@thdesc
Copy link
Contributor Author

thdesc commented Jun 12, 2024

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)

@eyurtsev
Copy link
Collaborator

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?

@thdesc
Copy link
Contributor Author

thdesc commented Jun 13, 2024

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

@ccurme ccurme added the Ɑ: core Related to langchain-core label Jun 19, 2024
@thdesc
Copy link
Contributor Author

thdesc commented Jun 20, 2024

Hi @eyurtsev, did you have some time to review this PR please ?

@eyurtsev eyurtsev self-assigned this Jun 20, 2024
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 25, 2024
@eyurtsev eyurtsev changed the title langchain-core[minor]: extract input variables for path and detail keys in order to format an ImagePromptTemplate langchain-core[patch]: extract input variables for path and detail keys in order to format an ImagePromptTemplate Jun 25, 2024
@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Jun 25, 2024
@eyurtsev eyurtsev enabled auto-merge (squash) July 1, 2024 18:25
@eyurtsev eyurtsev disabled auto-merge July 1, 2024 18:25
@eyurtsev eyurtsev changed the title langchain-core[patch]: extract input variables for path and detail keys in order to format an ImagePromptTemplate core[patch]: extract input variables for path and detail keys in order to format an ImagePromptTemplate Jul 1, 2024
@eyurtsev eyurtsev enabled auto-merge (squash) July 1, 2024 18:25
@eyurtsev
Copy link
Collaborator

eyurtsev commented Jul 1, 2024

@thdesc thanks for updating the PR! Merging now :)

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jul 3, 2024
@eyurtsev eyurtsev merged commit 39b19cf into langchain-ai:master Jul 3, 2024
134 checks passed
@thdesc
Copy link
Contributor Author

thdesc commented Jul 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core 🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants