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)
相关推荐
985小水博一枚呀8 小时前
【AI大模型学习路线】第三阶段之RAG与LangChain——第十九章(实战基于Advanced RAG的PDF问答)系统部署与测试?
人工智能·学习·langchain·pdf
至此流年莫相忘9 小时前
LangChain HelloWorld
langchain
非晓为骁12 小时前
AI-Native 能力反思(三):Prompt Engineering 自我提升神器
人工智能·ai·prompt·ai-native·提示词工程
玲小珑18 小时前
LangChain.js 完全开发手册(十五)实战综合项目一:智能文档处理系统
前端·langchain·ai编程
成子不是橙子19 小时前
Langchain | Ollama | Python快速上手使用LLM的DEMO
开发语言·python·langchain·ollama
AI大模型1 天前
AI智能体开发框架LangChain & LangGraph快速入门实战(包含LangSmith)
程序员·langchain·llm
DevYK2 天前
企业级Agent开发教程(四) 基于 MongoDB 实现持久化记忆缓存
langchain·agent
shut up3 天前
LangChain - 如何使用阿里云百炼平台的Qwen-plus模型构建一个桌面文件查询AI助手 - 超详细
人工智能·python·langchain·智能体
liliangcsdn3 天前
如何基于ElasticsearchRetriever构建RAG系统
大数据·elasticsearch·langchain
东方佑3 天前
基于FastAPI与LangChain的Excel智能数据分析API开发实践
langchain·excel·fastapi