langchain/libs/core/tests/unit_tests/prompts
William FH 38425c99d2
core[minor]: Image prompt template (#14263)
Builds on Bagatur's (#13227). See unit test for example usage (below)

```python
def test_chat_tmpl_from_messages_multipart_image() -> None:
    base64_image = "abcd123"
    other_base64_image = "abcd123"
    template = ChatPromptTemplate.from_messages(
        [
            ("system", "You are an AI assistant named {name}."),
            (
                "human",
                [
                    {"type": "text", "text": "What's in this image?"},
                    # OAI supports all these structures today
                    {
                        "type": "image_url",
                        "image_url": "data:image/jpeg;base64,{my_image}",
                    },
                    {
                        "type": "image_url",
                        "image_url": {"url": "data:image/jpeg;base64,{my_image}"},
                    },
                    {"type": "image_url", "image_url": "{my_other_image}"},
                    {
                        "type": "image_url",
                        "image_url": {"url": "{my_other_image}", "detail": "medium"},
                    },
                    {
                        "type": "image_url",
                        "image_url": {"url": "https://www.langchain.com/image.png"},
                    },
                    {
                        "type": "image_url",
                        "image_url": {"url": "data:image/jpeg;base64,foobar"},
                    },
                ],
            ),
        ]
    )
    messages = template.format_messages(
        name="R2D2", my_image=base64_image, my_other_image=other_base64_image
    )
    expected = [
        SystemMessage(content="You are an AI assistant named R2D2."),
        HumanMessage(
            content=[
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{other_base64_image}"
                    },
                },
                {
                    "type": "image_url",
                    "image_url": {"url": f"{other_base64_image}"},
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"{other_base64_image}",
                        "detail": "medium",
                    },
                },
                {
                    "type": "image_url",
                    "image_url": {"url": "https://www.langchain.com/image.png"},
                },
                {
                    "type": "image_url",
                    "image_url": {"url": "data:image/jpeg;base64,foobar"},
                },
            ]
        ),
    ]
    assert messages == expected
```

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: Brace Sproul <braceasproul@gmail.com>
2024-01-27 17:04:29 -08:00
..
__init__.py Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
prompt_extra_args.json Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
prompt_missing_args.json Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
simple_prompt.json Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
test_chat.py core[minor]: Image prompt template (#14263) 2024-01-27 17:04:29 -08:00
test_few_shot_with_templates.py Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
test_few_shot.py [core] prompt changes (#15186) 2023-12-26 15:52:17 -08:00
test_imports.py BUGFIX: add prompt imports for backwards compat (#13702) 2023-11-21 23:04:20 -08:00
test_loading.py infra: absolute EXAMPLE_DIR path in core unit tests (#16325) 2024-01-22 14:00:23 -08:00
test_pipeline_prompt.py Separate out langchain_core package (#13577) 2023-11-20 13:09:30 -08:00
test_prompt.py core:adds tests for partial_variables (#15427) 2024-01-02 15:00:06 -08:00
test_utils.py REFACTOR: Refactor langchain_core (#13627) 2023-11-21 08:35:29 -08:00