How TsgcAIChat sends PDF documents and images with a message.
TsgcAIChat can send PDF documents and images together with a user message, so the model answers about their content: summarise a contract, answer a question about a report or read the total of an invoice. The documents are sent as base64 data with their media type, and they are stored in the conversation history together with the message.
| Method | Description |
| ChatWithDocument | Sends one PDF or image file with the message. |
| ChatWithDocumentBase64 | Sends one document already encoded as base64, with its media type. |
| ChatWithAttachments | Sends several documents, in order, with the message. |
| ChatJSONWithAttachments | Same as ChatJSON, with the documents on the user message, to get the answer as JSON. |
Images are accepted by every provider. PDF documents are accepted by OpenAI, Anthropic and Gemini; DeepSeek, Grok, Mistral and Ollama accept images only, and sending them a PDF raises EsgcAIChat, which ChatWithDocument and ChatWithAttachments report through OnChatError. The model must support vision (for example gpt-4o-mini, a Claude model or a Gemini model).
| Provider | Images | |
| aicpOpenAI | Yes | Yes |
| aicpAnthropic | Yes | Yes |
| aicpGemini | Yes | Yes |
| aicpDeepSeek | No | Yes |
| aicpGrok | No | Yes |
| aicpMistral | No | Yes |
| aicpOllama | No | Yes |
An owning list of TsgcAIChatAttachment items, each one with Base64, MediaType and FileName properties. It is declared in sgcAI_Chat.
| Member | Description |
function Add(const aBase64, aMediaType: string; const aFileName: string = ''): TsgcAIChatAttachment; | Adds a document already encoded as base64. aMediaType is for example application/pdf or image/png. |
function AddFile(const aFileName: string): TsgcAIChatAttachment; | Reads the file, encodes it as base64 and takes the media type from the extension: .pdf, .png, .jpg, .jpeg, .gif or .webp. Any other extension raises EsgcAIChat. |
procedure Clear; | Removes and frees every attachment. |
property Count: Integer; | Number of attachments. |
property Items[aIndex: Integer]: TsgcAIChatAttachment; | Attachment at a position. It is the default property. |
The list passed to ChatWithAttachments or ChatJSONWithAttachments stays owned by the caller. The attachments are copied into the history, so the list can be freed after the call and the next messages of the conversation can still refer to the document.
oChat.Provider := aicpAnthropic;
oChat.ChatOptions.ApiKey := 'API_KEY';
oChat.ChatOptions.Model := 'claude-sonnet-4-20250514';
ShowMessage(oChat.ChatWithDocument('Summarise this contract', 'contract.pdf'));
oAttachments := TsgcAIChatAttachments.Create;
Try
oAttachments.AddFile('page1.png');
oAttachments.AddFile('page2.png');
ShowMessage(oChat.ChatWithAttachments('Compare both pages', oAttachments));
Finally
oAttachments.Free;
End;
To turn documents into validated structured data (invoices, receipts, identity documents), use TsgcAIDocumentExtractor, which is built on these methods.