LangChain: How to Pass messages to Models

一般的框架是

1
2
3
4
5
6
7
8
9
10
llm = CHatOllama() # ... just an example
message = {
"role": "user",
"content": [
# ......
],
}

response = llm.invoke([message])
print(response.text())

如果需要传入多模态的输入的话,我们就在 messagecontent 栏位填入我们需要的东西即可

Texts

1
2
3
4
5
6
"content": [
{
"type": "text",
"text": "......",
}
]

Images

对于本地文件,可以传 base64 encode image。对于网络资源,可以传 URL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"type": "image",
"source_type": "base64",
"mime_type": "image/jpeg", # or image/png, etc.
"data": "<base64 data string>",
}

# OR...

{
"type": "image",
"source_type": "url",
"url": "https://...",
}

Documents (PDF)

有的模型支持直接上传 PDF

1
2
3
4
5
6
{
"type": "file",
"source_type": "base64",
"mime_type": "application/pdf",
"data": "<base64 data string>",
}

Audio

1
2
3
4
5
6
{
"type": "audio",
"source_type": "base64",
"mime_type": "audio/wav", # or appropriate mime-type
"data": "<base64 data string>",
}