Using PDF Input
How to send PDF and other document types to a /chat/completions endpoint.
Works for:
- Vertex AI models (Gemini + Anthropic)
- Bedrock Models
- Anthropic API Models
- OpenAI API Models
- Mistral (Only using file ID of already uploaded file, similar to OpenAI file_id input)
Quick Start
Using URL
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.haimaker.ai/v1"
)
# PDF URL
file_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
response = client.chat.completions.create(
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_url
}
}
]
}
]
)
print(response.choices[0].message.content)
cURL
curl https://api.haimaker.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What'\''s this file about?"},
{
"type": "file",
"file": {
"file_id": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
}
}
]
}
]
}'
Using Base64
import base64
import requests
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.haimaker.ai/v1"
)
# Download and encode the PDF
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}"
response = client.chat.completions.create(
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_data": base64_url
}
}
]
}
]
)
print(response.choices[0].message.content)
Specifying Format
To specify the format of the document, you can use the format parameter.
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.haimaker.ai/v1"
)
file_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
response = client.chat.completions.create(
model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_url,
"format": "application/pdf"
}
}
]
}
]
)
print(response.choices[0].message.content)
Using Different Models
Anthropic Claude
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.haimaker.ai/v1"
)
response = client.chat.completions.create(
model="anthropic/claude-3-5-sonnet-latest",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Summarize this document"},
{
"type": "file",
"file": {
"file_id": "https://example.com/document.pdf"
}
}
]
}
]
)
print(response.choices[0].message.content)
Mistral
For Mistral, use a file ID received from the files endpoint:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.haimaker.ai/v1"
)
# file_id received from files endpoint
file_id = "fa778e5e-46ec-4562-8418-36623fe25a71"
response = client.chat.completions.create(
model="mistral/mistral-large-latest",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's this file about?"},
{
"type": "file",
"file": {
"file_id": file_id
}
}
]
}
]
)
print(response.choices[0].message.content)