当前位置: 首页 > news >正文

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

文章目录

  • 一、如何直接将多模态数据传输给模型
  • 二、如何使用 mutimodal prompts


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

 在这里,我们演示了如何将多模式输入直接传递给模型。对于其他的支持多模态输入的模型提供者,langchain 在类中提供了内在逻辑来转化为期待的格式。
 传入图像最常用的方法是将其作为字节字符串传入。这应该适用于大多数模型集成。

import base64
import httpximage_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]) # 自己定义一个 model
print(response.content)
"""
The weather in the image appears to be clear and pleasant. The sky is mostly blue with scattered, light clouds, suggesting a sunny day with minimal cloud cover. There is no indication of rain or strong winds, and the overall scene looks bright and calm. The lush green grass and clear visibility further indicate good weather conditions.
"""

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

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)

 我们也可以传多个图片。

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)
"""
Yes, the two images are the same. They both depict a wooden boardwalk extending through a grassy field under a blue sky with light clouds. The scenery, lighting, and composition are identical.
"""

二、如何使用 mutimodal prompts

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

import base64
import httpx
from langchain_core.prompts import ChatPromptTemplateimage_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}"},}],),]
)chain = prompt | modelresponse = chain.invoke({"image_data": image_data})
print(response.content)
"""
The image depicts a sunny day with a beautiful blue sky filled with scattered white clouds. The sky has varying shades of blue, ranging from a deeper hue near the horizon to a lighter, almost pale blue higher up. The white clouds are fluffy and scattered across the expanse of the sky, creating a peaceful and serene atmosphere. The lighting and cloud patterns suggest pleasant weather conditions, likely during the daytime hours on a mild, sunny day in an outdoor natural setting.
"""

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

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 | modelresponse = chain.invoke({"image_data1": image_data, "image_data2": image_data})
print(response.content)
"""
The two images provided are identical. Both images feature a wooden boardwalk path extending through a lush green field under a bright blue sky with some clouds. The perspective, colors, and elements in both images are exactly the same.
"""
http://www.lryc.cn/news/399973.html

相关文章:

  • ssh升级
  • 51单片机10(蜂鸣器介绍)
  • Python爬虫:基础爬虫架构及爬取证券之星全站行情数据!
  • T113-i 倒车低概率性无反应,没有进入倒车视频界面
  • 【AI大模型】李彦宏从“卷模型”到“卷应用”的深度解析:卷用户场景卷能给用户解决什么问题
  • 25秋招面试算法题 (Go版本)
  • 在Ubuntu 14.04上安装和保护phpMyAdmin的方法
  • 突破与创新:Vue.js 创始人 尤雨溪 2024 年度技术前瞻
  • LeetCode 441, 57, 79
  • 【排序 - 插入排序 和 希尔排序】
  • Java使用 MyBatis-Plus 的 OR
  • [Linux]CentOS软件的安装
  • 4000厂商默认账号密码、默认登录凭证汇总.pdf
  • RK3568笔记三十六:LED驱动开发(设备树)
  • AC修炼计划(AtCoder Regular Contest 180) A~C
  • 云计算练习题
  • 《战甲神兵》开发者报告:游戏崩溃问题80%发生在Intel可超频酷睿i9处理器上——酷睿i7 K系列CPU也表现出高崩溃率
  • Postman下载及使用说明
  • 什么是im即时通讯?WorkPlus im即时通讯私有化部署安全可控
  • hnust 1794: 机器翻译
  • AI人工智能开源大模型生态体系分析
  • ArkTS学习笔记_封装复用之@Styles装饰器
  • 根据vue学习react
  • Hi3861 OpenHarmony嵌入式应用入门--HTTPD
  • MICS2024|少样本学习、多模态技术以及大语言模型在医学图像处理领域的研究进展|24-07-14
  • ConfigMap-secrets-静态pod
  • SQL Error: 1406, SQLState: 22001
  • 【密码学基础】基于LWE(Learning with Errors)的全同态加密方案
  • Linux - 基础开发工具(yum、vim、gcc、g++、make/Makefile、git)
  • 网络安全法律框架更新:最新合规要求与企业应对策略