如何使用多模态提示
这里我们演示如何使用提示模板来格式化多模态模型输入。
为了在多模态数据上下文中使用提示模板,我们可以将相应内容块的元素模板化。例如,下面我们定义了一个将图片 URL 作为参数的提示
from langchain_core.prompts import ChatPromptTemplate
# Define prompt
prompt = ChatPromptTemplate(
[
{
"role": "system",
"content": "Describe the image provided.",
},
{
"role": "user",
"content": [
{
"type": "image",
"source_type": "url",
"url": "{image_url}",
},
],
},
]
)
API 参考:ChatPromptTemplate
让我们使用此提示将图像传递给聊天模型
from langchain.chat_models import init_chat_model
llm = init_chat_model("anthropic:claude-3-5-sonnet-latest")
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"
chain = prompt | llm
response = chain.invoke({"image_url": url})
print(response.text())
API 参考:init_chat_model
This image shows a beautiful wooden boardwalk cutting through a lush green wetland or marsh area. The boardwalk extends straight ahead toward the horizon, creating a strong leading line through the composition. On either side, tall green grasses sway in what appears to be a summer or late spring setting. The sky is particularly striking, with wispy cirrus clouds streaking across a vibrant blue background. In the distance, you can see a tree line bordering the wetland area. The lighting suggests this may be during "golden hour" - either early morning or late afternoon - as there's a warm, gentle quality to the light that's illuminating the scene. The wooden planks of the boardwalk appear well-maintained and provide safe passage through what would otherwise be difficult terrain to traverse. It's the kind of scene you might find in a nature preserve or wildlife refuge designed to give visitors access to observe wetland ecosystems while protecting the natural environment.
请注意,我们可以模板化内容块的任意元素
prompt = ChatPromptTemplate(
[
{
"role": "system",
"content": "Describe the image provided.",
},
{
"role": "user",
"content": [
{
"type": "image",
"source_type": "base64",
"mime_type": "{image_mime_type}",
"data": "{image_data}",
"cache_control": {"type": "{cache_type}"},
},
],
},
]
)
import base64
import httpx
image_data = base64.b64encode(httpx.get(url).content).decode("utf-8")
chain = prompt | llm
response = chain.invoke(
{
"image_data": image_data,
"image_mime_type": "image/jpeg",
"cache_type": "ephemeral",
}
)
print(response.text())
This image shows a beautiful wooden boardwalk cutting through a lush green marsh or wetland area. The boardwalk extends straight ahead toward the horizon, creating a strong leading line in the composition. The surrounding vegetation consists of tall grass and reeds in vibrant green hues, with some bushes and trees visible in the background. The sky is particularly striking, featuring a bright blue color with wispy white clouds streaked across it. The lighting suggests this photo was taken during the "golden hour" - either early morning or late afternoon - giving the scene a warm, peaceful quality. The raised wooden path provides accessible access through what would otherwise be difficult terrain to traverse, allowing visitors to experience and appreciate this natural environment.