跳至主要內容

使用 PDF 輸入

如何將 pdf(其他文件類型)傳送/接收至 /chat/completions 端點

適用於:

  • Vertex AI 模型(Gemini + Anthropic)
  • Bedrock 模型
  • Anthropic API 模型
  • OpenAI API 模型
  • Mistral(僅使用已上傳檔案的 file ID,類似於 OpenAI file_id 輸入)

快速開始

URL

from litellm.utils import supports_pdf_input, completion

# set aws credentials
os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""


# pdf url
file_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"

# model
model = "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"

file_content = [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_url,
}
},
]


if not supports_pdf_input(model, None):
print("Model does not support image input")

response = completion(
model=model,
messages=[{"role": "user", "content": file_content}],
)
assert response is not None

base64

from litellm.utils import supports_pdf_input, completion

# set aws credentials
os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""


# pdf url
image_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
response = requests.get(url)
file_data = response.content

encoded_file = base64.b64encode(file_data).decode("utf-8")
base64_url = f"data:application/pdf;base64,{encoded_file}"

# model
model = "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"

file_content = [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_data": base64_url,
}
},
]


if not supports_pdf_input(model, None):
print("Model does not support image input")

response = completion(
model=model,
messages=[{"role": "user", "content": file_content}],
)
assert response is not None

指定格式

若要指定文件的格式,您可以使用 format 參數。

from litellm.utils import supports_pdf_input, completion

# set aws credentials
os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""


# pdf url
file_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"

# model
model = "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"

file_content = [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_url,
"format": "application/pdf",
}
},
]


if not supports_pdf_input(model, None):
print("Model does not support image input")

response = completion(
model=model,
messages=[{"role": "user", "content": file_content}],
)
assert response is not None

Mistral 範例

以下是使用 Mistral 模型進行文件理解的範例 payload:

from litellm.utils import completion

# pdf file_id received from files endpoint
file_id = "fa778e5e-46ec-4562-8418-36623fe25a71"

# model
model = "mistral/mistral-large-latest"

file_content = [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_id,
}
},
]

response = completion(
model=model,
messages=[{"role": "user", "content": file_content}],
)
assert response is not None

檢查模型是否支援 pdf 輸入

使用 litellm.supports_pdf_input(model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0") -> 若模型可接受 pdf 輸入,則回傳 True

assert litellm.supports_pdf_input(model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0") == True
🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →