mistralM

Docs

sideBar

Document AI QnA

Ask questions in natural language about a document by combining OCR with a language model through the chat completions API.

The Document QnA capability combines OCR with large language model capabilities to enable natural language interaction with document content. This allows you to extract information and insights from documents by asking questions in natural language.

lightbulb

Before continuing, we recommend reading the Chat Completions documentation to learn more about the chat completions API and how to use it before proceeding.

Before You Start

Workflow and Capabilities

The workflow consists of two main steps:

Document QnA Graph

  • 1.

    Document AI: OCR extracts text, structure, and formatting, creating a machine-readable version of the document.

  • 2.

    Language Model Understanding: The extracted document content is analyzed by a large language model. You can ask questions or request information in natural language. The model understands context and relationships within the document and can provide relevant answers based on the document content.

Key Capabilities

  • Question answering about specific document content
  • Information extraction and summarization
  • Document analysis and insights
  • Multi-document queries and comparisons
  • Context-aware responses that consider the full document

Common Use Cases

  • Analyzing research papers and technical documents
  • Extracting information from business documents
  • Processing legal documents and contracts
  • Building document Q&A applications
  • Automating document-based workflows

Usage

Leverage Document QnA

The examples below show how to interact with a PDF document using natural language.

Be sure the URL is public and accessible by our API.

import os
from mistralai import Mistral

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-small-latest"

client = Mistral(api_key=api_key)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "what is the last sentence in the document"
            },
            {
                "type": "document_url",
                "document_url": "https://arxiv.org/pdf/1805.04770"
            }
        ]
    }
]

chat_response = client.chat.complete(
    model=model,
    messages=messages
)
Output
{
  "id": "7b98be65bb7b475ca2456a92f9ed0049",
  "created": 1756753708,
  "model": "mistral-small-latest",
  "usage": {
    "prompt_tokens": 13707,
    "total_tokens": 13764,
    "completion_tokens": 57
  },
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "tool_calls": null,
        "content": "The last sentence in the document is:\n\n\"Zaremba, W., Sutskever, I., and Vinyals, O. Recurrent neural network regularization. arXiv:1409.2329, 2014.\""
      }
    }
  ]
}

Cookbooks

For more information on how to make use of Document QnA, we have the following Document QnA CookbookarrowUpRight with a simple example.

Yes, there are certain limitations for the Document QnA API. Uploaded document files must not exceed 50 MB in size and should be no longer than 1,000 pages.