Quickstart: create a library and ask it a question

In this quickstart, you create a library, add a short FAQ document to it, and ask a model a question that it answers from that document. It takes about 10 minutes. For what libraries are and how agents use them, see Libraries.

Before you begin

Before you begin

Set up your environment

Set up your environment

Create a project, install the Mistral SDK, and export your API key:

uv init quickstart-library --python 3.12 && cd quickstart-library
uv add mistralai
export MISTRAL_API_KEY="your-api-key"
1. Create a library

1. Create a library

Create create_library.py:

import os

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

library = client.beta.libraries.create(
    name="quickstart-library",
    description="Support FAQ for the Libraries quickstart.",
)
print(f"Library ID: {library.id}")

Run it, then export the ID it prints so the next scripts can read it:

uv run create_library.py
export LIBRARY_ID="<library-id>"
Library ID: 01a1a1f4-2c20-7612-8037-3a354dcc6c16
2. Add a document

2. Add a document

Create the document to upload, faq.md:

cat > faq.md <<'EOF'
# Acme Cloud FAQ

## Refunds

Acme Cloud refunds annual plans within 30 days of purchase. Monthly plans are not refunded.

## Support hours

Support is available Monday to Friday, 9:00 to 18:00 CET.
EOF

Create upload.py. It uploads faq.md, then waits until the library has processed the document. A document is searchable only once its processing status is done.

import os
import time

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
library_id = os.environ["LIBRARY_ID"]

with open("faq.md", "rb") as f:
    document = client.beta.libraries.documents.upload(
        library_id=library_id,
        file={"file_name": "faq.md", "content": f},
    )
print(f"Uploaded {document.name}")

status = client.beta.libraries.documents.status(
    library_id=library_id, document_id=document.id
)
while status.process_status in ("todo", "in_progress", "waiting_for_capacity"):
    time.sleep(2)
    status = client.beta.libraries.documents.status(
        library_id=library_id, document_id=document.id
    )
print(f"Processing status: {status.process_status}")

Run it:

uv run upload.py
Uploaded faq.md
Processing status: done
3. Ask a question

3. Ask a question

Create ask.py. It starts a conversation with the document_library tool pointed at your library, so the model searches the library before it answers. store=False keeps the conversation out of your history.

import os

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
library_id = os.environ["LIBRARY_ID"]

response = client.beta.conversations.start(
    model="mistral-medium-latest",
    instructions="Answer from the documents in the library. Cite the document you used.",
    inputs="How long do I have to get a refund on an annual plan?",
    tools=[{"type": "document_library", "library_ids": [library_id]}],
    completion_args={"temperature": 0.2},
    store=False,
)

for entry in response.outputs:
    if entry.type == "tool.execution":
        print(f"Tool called: {entry.name}")

answer = response.outputs[-1].content
if isinstance(answer, str):
    print(answer)
else:
    print("".join(chunk.text for chunk in answer if chunk.type == "text"))

Run it:

uv run ask.py
You should see

You should see

The model calls document_library, then answers with the refund period from faq.md:

Tool called: document_library
You have **30 days from the date of purchase** to request a refund for an annual plan.

The 30-day period comes from your document, not from the model's general knowledge. The wording of the answer can vary between runs.

If the answer says that no library or document is available, run uv run ask.py again. If it fails with 401 Unauthorized, check MISTRAL_API_KEY.

Clean up

Clean up

To delete the library and its document, create delete_library.py:

import os

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

deleted = client.beta.libraries.delete(library_id=os.environ["LIBRARY_ID"])
print(f"Deleted library {deleted.name}")

Run it:

uv run delete_library.py
Deleted library quickstart-library
Going further

Going further