Public preview: This API is in public preview. Endpoints, schemas, and limits may change before general availability.

Using files as context

Upload a document (like a PDF or Word file) and ask questions about it, or extract information from it.

This guide shows how to upload a document and immediately ask the model about it.

Step 1: Upload the document#

Upload the file with client.files.create (or POST /v1/files), with purpose set to "assistants". Files can be up to 10 MB.

import os
from pathlib import Path
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["EUGPT_API_KEY"],
    base_url="https://api.eugpt.ai/v1",
)

file_record = client.files.create(
    file=Path("financial_report_2024.pdf"),
    purpose="assistants",
)

print(file_record.id, file_record.status)
import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.EUGPT_API_KEY,
  baseURL: "https://api.eugpt.ai/v1",
});

const fileRecord = await client.files.create({
  file: fs.createReadStream("financial_report_2024.pdf"),
  purpose: "assistants",
});

console.log(fileRecord.id, fileRecord.status);
curl https://api.eugpt.ai/v1/files \
  -H "Authorization: Bearer $EUGPT_API_KEY" \
  -F purpose="assistants" \
  -F file="@financial_report_2024.pdf"

The upload returns once the document has been read. Check status:

  • processed — the document is ready to use.
  • error — EU GPT could not read it; status_details says why, for example a scanned document without text, or a damaged file.

EU GPT reads PDF, Word, PowerPoint, and Excel files (also the older .doc, .ppt, and .xls), EPUB, RTF, HTML, Markdown, CSV, and plain text. You can also upload images, such as PNG or JPEG.

Step 2: Ask a question about the document#

Call /v1/responses with an input list instead of a string. Put an input_file part with the file id and an input_text part with your question in the same message.

structured_input = [
    {
        "role": "user",
        "content": [
            {"type": "input_file", "file_id": file_record.id},
            {"type": "input_text", "text": "Summarise the Q3 revenue figures from this report."},
        ],
    }
]

response = client.responses.create(
    model="auto",
    input=structured_input,
    stream=False,
)

print(response.output_text)

Every request needs a text part: a request with only files returns 400. You can attach several files in one message by adding more input_file parts.

How the document reaches the model#

  • A document attached this way is passed to the model in full when it fits in about 60,000 tokens. For a longer document, EU GPT searches it and passes the passages most relevant to your question. Questions about a specific part (“What does section 4 say about penalties?”) work better on long documents than “summarise everything”.
  • An image is not shown to the answering model directly. The model can call the describe_image tool, which has a vision model look at the image; you see that call in the stream.
  • In a streamed response, response.completed lists the passages used as sources, with the file name and page number. See Streaming events.

To ground answers in a fixed set of documents for every question, upload them to an agent in the web app and pass its project_id instead. See Conversations.

Clean up#

Delete files you no longer need:

client.files.delete(file_record.id)

See the Files API reference for listing, downloading, and deleting files.