Skip to Content
Introducing PageIndex Flash

PageIndex LLM Integration

Use built-in chat() to ask questions about your documents with your preferred LLM.

  1. Set up the PageIndex client — choose your index mode and LLM.
  2. Send a query — prepare messages and select documents.
  3. Customize the response — enable streaming and citations.

Set up the PageIndex client

Both sides need configuring: index decides where documents are processed and stored, chat is the model that searches the tree and writes the answer.

import os from pageindex import PageIndexClient os.environ["PAGEINDEX_API_KEY"] = "your-pageindex-key" os.environ["OPENAI_API_KEY"] = "your-openai-key" client = PageIndexClient( index="cloud", # index and store in PageIndex Cloud chat="gpt-5.6-sol", # your own model answers )

Client instructions

Pass instructions= when creating the client to customize the answering agent across questions. These instructions are appended after PageIndex’s default system prompt.

client = PageIndexClient( index="cloud", chat="gpt-5.6-sol", instructions="You are ACME's support assistant. Answer in the user's language.", )

Use an existing document ID from client.list_documents(), or submit a new document. wait=True blocks until it is ready:

doc_id = client.submit_document("./2023-annual-report.pdf", wait=True)["doc_id"]

The examples above use OpenAI. Any provider works — see Use different LLMs.


Send a query

Start with a question string or a list of conversation messages.

messages = "What are the key findings in this document?"

Then choose the documents or folder and send messages:

answer = client.chat(messages, doc_id=doc_id)

Streaming

Streamed answers show the agent’s process by default — thinking and tool calls woven into the text as it runs:

for chunk in client.chat("Summarize this document", doc_id=doc_id, stream=True): print(chunk, end="", flush=True)
[thinking] I should read the report's key sections first. [tool_call] get_document_structure {"doc_name": "report.pdf"} [tool_result] get_document_structure: {"success": true, ... (+304 chars) The report finds that ...

Use show_process=False to stream only the answer.

To customize the process output, pass a dict to show_process:

KeyDescriptionDefault
thinkingShow the model’s thinking when available.True
tool_callShow tool calls.True
tool_resultShow tool results.True
max_charsCharacter limit per process summary line.200
client.chat("...", doc_id=doc_id, stream=True, show_process={"thinking": False})

Citations

Pass citations=True to request source citations. Citations use <cite doc="…" page="…"/> tags, with block="…" where the cloud document has block-level data:

answer = client.chat("Summarize the document.", doc_id=doc_id, citations=True)
Revenue increased during the reporting period. <cite doc="report.pdf" page="12"/>

With your own chat model, choose a citation format using client.citation_prompt(format=...):

FormatOutput
"cite" (default)<cite doc="report.pdf" page="12"/> tags, with block when available.
"markdown"Bracketed references such as [report.pdf, p. 12]. Useful when your app does not render custom tags.
"footnote"Numbered footnote markers with source definitions.

Pass the returned prompt as instructions=:

answer = client.chat( "Summarize the document.", doc_id=doc_id, instructions=client.citation_prompt(format="markdown"), )

API protocols

Use protocol= to return a protocol’s native response format. With stream=True, the response is its native event stream.

response = client.chat( "Summarize this document", doc_id=doc_id, protocol="chat_completions", ) print(response["choices"])

Returns the Chat Completions envelope with choices and usage. This protocol also works with managed cloud chat, without your own chat model. chat_completions() remains available for existing code.

show_process does not apply when protocol is set. Pass provider-specific fields such as thinking and top_k through extra_body.


chat() parameters

The following parameters apply to client.chat().

Parameters

NameTypeRequiredDescriptionDefault
messagesstring or List[Dict]yesA question string, or role/content conversation history.-
doc_idstring or List[string]noDocument ID(s) to scope the conversation.None
folder_idstringnoCloud-only — steer discovery toward this folder’s documents; "root" is the whole library.None
streambooleannoYield the answer as text chunks as it is produced; with a protocol, that protocol’s native event stream.False
show_processboolean or DictnoStreamed chat: weave the run into the text. False for the bare answer; dict keys thinking / tool_call / tool_result / max_chars.on
modelstringnoBackend model name; defaults to the client’s chat_model (protocol="messages" requires it).None
reasoning_effortstringno"low" / "medium" / "high", sent in each lane’s native spelling.None
protocolstringno"chat_completions", "responses" or "messages": drive that wire natively, with its own input/output shapes. The managed cloud chat serves "chat_completions" too.None
instructionsstring or List[Dict]noGuidance for this call, appended after the managed system prompt and the client’s own instructions (Messages system blocks with protocol="messages").None
citationsbooleannoRequest source citations with <cite doc= page=/> tags (block= where the cloud document has blocks); on the managed cloud chat, the endpoint’s own citations.False
max_turnsintnoCap on agent turns per call.None
backendDictnoPer-call connection overrides, merged over the client’s chat_backend.None
extra_headersDictnoExtra HTTP headers on each backend request.None
extra_bodyDictnoThe provider’s own request fields beyond these parameters, merged last.None

model, reasoning_effort, max_turns, backend, extra_headers and the responses / messages protocols apply to own-model chat (a configured chat_model); the managed cloud chat selects its own model and rejects them. instructions, citations, extra_body and protocol="chat_completions" work on both.

Last updated on