TsgcAIChat Documents and PDF

How TsgcAIChat sends PDF documents and images with a message.

Introduction

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.

 

Methods

MethodDescription
ChatWithDocumentSends one PDF or image file with the message.
ChatWithDocumentBase64Sends one document already encoded as base64, with its media type.
ChatWithAttachmentsSends several documents, in order, with the message.
ChatJSONWithAttachmentsSame as ChatJSON, with the documents on the user message, to get the answer as JSON.

 

Supported Providers

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).

 

ProviderPDFImages
aicpOpenAIYesYes
aicpAnthropicYesYes
aicpGeminiYesYes
aicpDeepSeekNoYes
aicpGrokNoYes
aicpMistralNoYes
aicpOllamaNoYes

 

TsgcAIChatAttachments

An owning list of TsgcAIChatAttachment items, each one with Base64, MediaType and FileName properties. It is declared in sgcAI_Chat.

 

MemberDescription
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.

 

Example

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.

 

See Also