当前位置: 首页 > 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.
"""

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • WPF 手撸插件 一
  • vite-服务端渲染(ssr)项目线上频繁刷新(踩坑记录)
  • langchain新版本v0.2文档:tutorials(1)
  • 【Neo4j】实战 (数据库技术丛书)学习笔记
  • Ubuntu 22.04.4 LTS (linux) 安装certbot 免费ssl证书申请 letsencrypt
  • python条件
  • 【Android面试八股文】请描述一下 android 的系统架构?
  • WSL-Ubuntu20.04部署环境配置
  • Web 性能入门指南-1.1 网站速度与用户幸福感的心理学
  • 51单片机5(GPIO简介)
  • Go:基本变量与数据类型
  • Excel如何才能忽略隐藏行进行复制粘贴?
  • STM32MP135裸机编程:烧录程序到EMMC的方法
  • Redis 三大高可用模式:主从、哨兵、集群
  • frameworks 之FallbackHome
  • __proto__ 和 prototype的关系
  • 5、React组件事件详解
  • es6要点
  • js
  • JSONP原理
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Vue 动态创建 component
  • vue-router 实现分析
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • webpack+react项目初体验——记录我的webpack环境配置
  • 从setTimeout-setInterval看JS线程
  • 从tcpdump抓包看TCP/IP协议
  • 基于Android乐音识别(2)
  • 开源地图数据可视化库——mapnik
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 时间复杂度与空间复杂度分析
  • 思否第一天
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 白色的风信子
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • ​用户画像从0到100的构建思路
  • ###STL(标准模板库)
  • #if #elif #endif
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • ${factoryList }后面有空格不影响
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (4)Elastix图像配准:3D图像
  • (C++20) consteval立即函数
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (待修改)PyG安装步骤
  • (每日一问)基础知识:堆与栈的区别
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • **PHP分步表单提交思路(分页表单提交)
  • .libPaths()设置包加载目录
  • .net core 6 redis操作类
  • .net core Swagger 过滤部分Api
  • .Net Core中Quartz的使用方法
  • .NET MVC 验证码
  • .NET 材料检测系统崩溃分析
  • .net 微服务 服务保护 自动重试 Polly