PageIndex Chat Completions API (beta)
Overview
The PageIndex Chat Completions API (beta) provides conversational AI with integrated access to your PageIndex documents.
Endpoint: POST https://api.pageindex.ai/chat/completions
Authentication
Include your PageIndex API key in the request header:
api_key: YOUR_PAGEINDEX_API_KEYRequest
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
messages | Array | Yes | - | Conversation messages |
stream | Boolean | No | false | Enable streaming |
doc_id | String | Array | No | null | The ID(s) of document(s) to select |
Example Request Body
{
"messages": [
{
"role": "user",
"content": "What are the key findings of the first paper?"
}
],
"stream": false
}Example Request with Document ID
When you include a doc_id, your query is scoped to that specific document. You can pass a single document ID as a string, or multiple IDs as an array.
Single Document ID:
{
"doc_id": "pi-123456",
"messages": [
{
"role": "user",
"content": "What are the key findings of this document?"
}
],
"stream": false
}Multiple Document IDs:
{
"doc_id": ["pi-123456", "pi-789012"],
"messages": [
{
"role": "user",
"content": "Compare these documents"
}
],
"stream": false
}Response
Non-Streaming Response
{
"id": "chat_completion_id",
"choices": [
{
"message": {
"role": "assistant",
"content": "The key findings are..."
},
"finish_reason": "end_turn"
}
],
"usage": {
"prompt_tokens": 1234,
"completion_tokens": 567,
"total_tokens": 1801
}
}Streaming Response
Server-Sent Events (SSE) format:
data: {"choices":[{"delta":{"content":"The"}}]}
data: {"choices":[{"delta":{"content":" key"}}]}
data: {"choices":[{"delta":{"content":" findings"}}]}
...
data: [DONE]Python Examples
Non-Streaming
import requests
response = requests.post(
"https://api.pageindex.ai/chat/completions",
headers={
"api_key": "your-pageindex-api-key",
"Content-Type": "application/json"
},
json={
"messages": [
{"role": "user", "content": "What is the first paper about?"}
]
}
)
result = response.json()
print(result["choices"][0]["message"]["content"])Streaming with Document ID
import requests
import json
response = requests.post(
"https://api.pageindex.ai/chat/completions",
headers={
"api_key": "your-pageindex-api-key",
"Content-Type": "application/json"
},
json={
"doc_id": "pi-123456",
"messages": [
{"role": "user", "content": "What are the key findings of this document?"}
],
"stream": True,
},
stream=True
)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:]
if data == '[DONE]':
break
chunk = json.loads(data)
content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
if content:
print(content, end='', flush=True)Accessing Intermediate Data in Streaming
Track what documents and tools are being accessed in real-time:
import requests
import json
response = requests.post(
"https://api.pageindex.ai/chat/completions",
headers={
"api_key": "your-pageindex-api-key",
"Content-Type": "application/json"
},
json={
"messages": [
{"role": "user", "content": "What is the first paper about?"}
],
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:]
if data == '[DONE]':
break
chunk = json.loads(data)
# Get intermediate metadata
metadata = chunk.get("block_metadata", {})
if metadata:
block_type = metadata.get("type")
block_index = metadata.get("block_index")
# Tool call started
if block_type == "mcp_tool_use_start":
tool_name = metadata.get("tool_name")
server_name = metadata.get("server_name")
print(f"\n[Block #{block_index}: Calling {tool_name}]\n")
# Tool result received
elif block_type == "mcp_tool_result_start":
print(f"\n[Block #{block_index}: Tool result received]\n")
# Get content
content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
if content:
print(content, end='', flush=True)Available block types:
text_block_start/text_stop- Text contentmcp_tool_use_start/mcp_tool_use_stop- PageIndex tool being calledmcp_tool_result_start/mcp_tool_result_stop- PageIndex tool results received
Error Responses
401 Unauthorized
{"detail": "API key not found in request headers"}500 Internal Server Error
{"detail": "Error message"}💬 Support
Last updated on