Run a document QA agent using the OpenAI Agents SDK. Streams text output token-by-token and returns the full answer string. Tool calls are always printed; verbose=True also prints arguments and output previews.
(client: PageIndexClient, doc_id: str, prompt: str, verbose: bool = False)
| 53 | |
| 54 | |
| 55 | def query_agent(client: PageIndexClient, doc_id: str, prompt: str, verbose: bool = False) -> str: |
| 56 | """Run a document QA agent using the OpenAI Agents SDK. |
| 57 | |
| 58 | Streams text output token-by-token and returns the full answer string. |
| 59 | Tool calls are always printed; verbose=True also prints arguments and output previews. |
| 60 | """ |
| 61 | |
| 62 | @function_tool |
| 63 | def get_document() -> str: |
| 64 | """Get document metadata: status, page count, name, and description.""" |
| 65 | return client.get_document(doc_id) |
| 66 | |
| 67 | @function_tool |
| 68 | def get_document_structure() -> str: |
| 69 | """Get the document's full tree structure (without text) to find relevant sections.""" |
| 70 | return client.get_document_structure(doc_id) |
| 71 | |
| 72 | @function_tool |
| 73 | def get_page_content(pages: str) -> str: |
| 74 | """ |
| 75 | Get the text content of specific pages or line numbers. |
| 76 | Use tight ranges: e.g. '5-7' for pages 5 to 7, '3,8' for pages 3 and 8, '12' for page 12. |
| 77 | For Markdown documents, use line numbers from the structure's line_num field. |
| 78 | """ |
| 79 | return client.get_page_content(doc_id, pages) |
| 80 | |
| 81 | agent = Agent( |
| 82 | name="PageIndex", |
| 83 | instructions=AGENT_SYSTEM_PROMPT, |
| 84 | tools=[get_document, get_document_structure, get_page_content], |
| 85 | model=client.retrieve_model, |
| 86 | # model_settings=ModelSettings(reasoning={"effort": "low", "summary": "auto"}), # Uncomment to enable reasoning |
| 87 | ) |
| 88 | |
| 89 | async def _run(): |
| 90 | streamed_run = Runner.run_streamed(agent, prompt) |
| 91 | current_stream_kind = None |
| 92 | async for event in streamed_run.stream_events(): |
| 93 | if isinstance(event, RawResponsesStreamEvent): |
| 94 | if isinstance(event.data, ResponseReasoningSummaryTextDeltaEvent): |
| 95 | if current_stream_kind != "reasoning": |
| 96 | if current_stream_kind is not None: |
| 97 | print() |
| 98 | print("\n[reasoning]: ", end="", flush=True) |
| 99 | delta = event.data.delta |
| 100 | print(delta, end="", flush=True) |
| 101 | current_stream_kind = "reasoning" |
| 102 | elif isinstance(event.data, ResponseTextDeltaEvent): |
| 103 | if current_stream_kind != "text": |
| 104 | if current_stream_kind is not None: |
| 105 | print() |
| 106 | print("\n[text]: ", end="", flush=True) |
| 107 | delta = event.data.delta |
| 108 | print(delta, end="", flush=True) |
| 109 | current_stream_kind = "text" |
| 110 | elif isinstance(event, RunItemStreamEvent): |
| 111 | item = event.item |
| 112 | if item.type == "tool_call_item": |
no test coverage detected