Claude (OpenAI format) — PDF Analysis
Short description:
Claudesupports OpenAI-style requests while remaining compatible with the native Claude format. This page demonstrates how to upload PDFs or images using OpenAI-style requests for analysis. For caching or more efficient PDF handling, consider using the native Claude format.
Overview
- Method:
POST - Path:
/v1/chat/completions - Content-Type:
application/json
Authentication
Supports common header-based authentication:
- OpenAI:
Authorization: Bearer sk-xxx - Anthropic (native):
x-api-key: sk-xxx(native format may have additional fields)
Features
- Supports image and PDF analysis: include text and file items in
messages. File items can be base64 (data:) or a file/URL reference. - Recommendation: if you need to reuse the same PDF in context multiple times, use the native Claude format to leverage caching and faster parsing.
Request Examples
Body parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | yes | Model name, e.g. claude-3-5-sonnet-20240620 |
| messages | <object> | yes | Chat message array; items may be plain text or mixed arrays (text/file/image) |
| temperature | number | no | Sampling temperature (0-2) |
| top_p | number | no | Nucleus sampling threshold |
| stream | boolean | no | Whether to stream responses |
| max_tokens | number | no | Max generated tokens |
| thinking | object | no | Claude-specific "think first" parameter (supported by some models) |
About possible content items in messages:
- Text:
{ "type": "text", "text": "..." }or plain string. - File (base64 or URL):
- Format 1 (
file):{ "type": "file", "file": { "filename": "api-doc.pdf", "file_data": "<base64 or URL>" } } - Format 2 (
file_url):{ "type": "file_url", "file_url": { "url": "https://..." } }
- Format 1 (
- Image:
{ "type": "image_url", "image_url": { "url": "https://..." } }
Example (chat):
json
{
"model": "claude-3-5-sonnet-20240620",
"messages": [{ "role": "user", "content": "Hello, who are you?" }],
"max_tokens": 1688,
"temperature": 0.5,
"stream": false
}Example (PDF analysis, format 1: file_data can be URL or base64):
json
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Summarize the document content" },
{ "type": "file", "file": { "filename": "api-doc.pdf", "file_data": "https://www.bt.cn/data/api-doc.pdf" } }
]
}
],
"max_tokens": 1000,
"stream": false
}Example (PDF analysis, format 2: file_url):
json
{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Summarize the document content" },
{ "type": "file_url", "file_url": { "url": "https://www.bt.cn/data/api-doc.pdf" } }
]
}
],
"max_tokens": 1000,
"stream": false
}curl example
bash
curl -X POST "https://api.gpt.ge/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxx" \
-d '{"model":"claude-3-5-sonnet-20241022","messages":[{"role":"user","content":[{"type":"text","text":"Summarize the document content"},{"type":"file_url","file_url":{"url":"https://www.bt.cn/data/api-doc.pdf"}}]}],"max_tokens":1000}'JavaScript (fetch) example
javascript
fetch('https://api.gpt.ge/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer sk-xxxx' },
body: JSON.stringify({ model: 'claude-3-5-sonnet-20241022', messages: [{ role: 'user', content: [{ type: 'text', text: 'Summarize the document content' }, { type: 'file_url', file_url: { url: 'https://www.bt.cn/data/api-doc.pdf' } }] }], max_tokens: 1000 })
}).then(r => r.json()).then(console.log)Python example (requests)
python
import requests
payload = {
'model': 'claude-3-5-sonnet-20241022',
'messages': [{'role':'user','content':[{'type':'text','text':'Summarize the document content'},{'type':'file_url','file_url':{'url':'https://www.bt.cn/data/api-doc.pdf'}}]}],
'max_tokens': 1000
}
resp = requests.post('https://api.gpt.ge/v1/chat/completions', headers={'Content-Type':'application/json','Authorization':'Bearer sk-xxxx'}, json=payload)
print(resp.json())Response example (200)
json
{
"id": "chatcmpl-msg_vrtx_01PjUA6WvGYYorf5rFU5QoKC",
"model": "claude-3-5-sonnet-20240620",
"object": "chat.completion",
"created": 1724998217,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I am an AI assistant named Claude. I was developed by Anthropic to converse with humans, answer questions, and provide help. Nice to meet you! How can I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 14, "completion_tokens": 86, "total_tokens": 100 }
}Server example: https://api.gpt.ge