LangChain —— 多模态大模型的 prompt template

文章目录


一、如何直接将多模态数据传输给模型

在这里,我们演示了如何将多模式输入直接传递给模型。对于其他的支持多模态输入的模型提供者,langchain 在类中提供了内在逻辑来转化为期待的格式。

传入图像最常用的方法是将其作为字节字符串传入。这应该适用于大多数模型集成。

python 复制代码
import base64
import httpx

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")

message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ],
)
response = model.invoke([message])
print(response.content)

我们可以直接在"image_URL"类型的内容块中提供图像URL。但是注意,只有一些模型提供程序支持此功能。

python 复制代码
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {"type": "image_url", "image_url": {"url": image_url}},
    ],
)
response = model.invoke([message])
print(response.content)

我们也可以传多个图片。

python 复制代码
message = HumanMessage(
    content=[
        {"type": "text", "text": "are these two images the same?"},
        {"type": "image_url", "image_url": {"url": image_url}},
        {"type": "image_url", "image_url": {"url": image_url}},
    ],
)
response = model.invoke([message])
print(response.content)

二、如何使用 mutimodal prompts

在这里,我们将描述一下怎么使用 prompt templates 来为模型格式化 multimodal imputs。

python 复制代码
import base64
import httpx

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "Describe the image provided"),
        (
            "user",
            [
                {
                    "type": "image_url",
                    "image_url": {"url": "data:image/jpeg;base64,{image_data}"},
                }
            ],
        ),
    ]
)

我们也可以给模型传入多个图片。

python 复制代码
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "compare the two pictures provided"),
        (
            "user",
            [
                {
                    "type": "image_url",
                    "image_url": {"url": "data:image/jpeg;base64,{image_data1}"},
                },
                {
                    "type": "image_url",
                    "image_url": {"url": "data:image/jpeg;base64,{image_data2}"},
                },
            ],
        ),
    ]
)

chain = prompt | model

response = chain.invoke({"image_data1": image_data, "image_data2": image_data})
print(response.content)
相关推荐
love530love11 小时前
在 PyCharm 中配置 x64 Native Tools Command Prompt for VS 2022 作为默认终端
ide·人工智能·windows·python·pycharm·prompt·comfyui
北冥有一鲲11 小时前
LangChain.js:Tool、Memory 与 Agent 的深度解析与实战
开发语言·javascript·langchain
七夜zippoe12 小时前
使用OpenLLM管理轻量级大模型服务
架构·langchain·大模型·kv·轻量
njsgcs14 小时前
我要搞个ai程序操控鼠标,截取屏幕,识别刀路,给ai一个刀路寻找规则的prompt,然后ai自己去按规则顺序点亮刀路
人工智能·prompt·项目预告
玖日大大1 天前
LangGraph 深度解析:构建强大智能体的新一代框架
人工智能·语言模型·架构·langchain
Tinero1 天前
LangChain 的核心概念与实现案例
langchain
Mr.朱鹏1 天前
大模型入门学习路径(Java开发者版)下
java·python·学习·微服务·langchain·大模型·llm
settingsun12251 天前
LLM Prompt三大框架之一:CRISPE (vs ICIO)
ai·prompt
哥本哈士奇1 天前
Streamlit + LangChain 1.0 简单实现智能问答前后端
langchain·streamlit
小小工匠1 天前
LLM - 从 Prompt 到 Context:2026 Agent 时代的核心战场
prompt·agent·context