-
Notifications
You must be signed in to change notification settings - Fork 0
/
anthropic_docs
65 lines (50 loc) · 1.74 KB
/
anthropic_docs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
How to use PDFs in the Messages API
Here’s a simple example demonstrating how to use PDFs in the Messages API:
Shell
Python
TypeScript
import anthropic
import base64
import httpx
# First fetch the file
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(httpx.get(pdf_url).content).decode("utf-8")
# Finally send the API request
client = anthropic.Anthropic()
message = client.beta.messages.create(
model="claude-3-5-sonnet-20241022",
betas=["pdfs-2024-09-25"],
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data
}
},
{
"type": "text",
"text": "Which model has the highest human preference win rates across each use-case?"
}
]
}
],
)
print(message.content)
Here are a few other examples to help you get started:
PDF support with prompt caching
PDF support with the Message Batches API
Best practices for PDF analysis
Ensure text is clear and legible.
Rotate pages to the proper orientation.
When referring to page numbers, use the logical number (the number reported by your PDF viewer) rather than the physical page number (the number visible on the page)
Use standard fonts.
Place PDFs before text in requests.
Split very large PDFs into smaller chunks when limits are exceeded.
Use prompt caching for repeated analysis of the same document.