Delphi ActiveX
Delphi ActiveX
XLSX Spreadsheet in AI Query
See more AI Examples
This example shows how to convert a .xlsx spreadsheet to CSV text for input in an AI query. Currently, most AIs can't handle Excel file inputs directly. If the spreadsheet is small, you can convert it to CSV text and use it as a text input.Chilkat Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
zip: TChilkatZip;
csv: TChilkatCsv;
sheetNames: TChilkatStringTable;
sheetName: WideString;
sbCsv: TChilkatStringBuilder;
ai: TChilkatAi;
sbResponse: TChilkatStringBuilder;
begin
success := 0;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// .xlsx files are Zip archives
zip := TChilkatZip.Create(Self);
success := zip.OpenZip('qa_data/excel/fakeCompanies.xlsx');
if (success = 0) then
begin
Memo1.Lines.Add(zip.LastErrorText);
Exit;
end;
csv := TChilkatCsv.Create(Self);
sheetNames := TChilkatStringTable.Create(Self);
success := csv.XlsxGetSheets(zip.ControlInterface,sheetNames.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(csv.LastErrorText);
Exit;
end;
if (sheetNames.Count = 0) then
begin
Memo1.Lines.Add('There are no sheets in the .xlsx');
Exit;
end;
// Get the name of the 1st sheet.
sheetName := sheetNames.StringAt(0);
// Load the 1st sheet into the CSV.
// We could've also loaded the 1st sheet by passing an empty string for the sheet name.
success := csv.XlsxLoadSheet(zip.ControlInterface,sheetName);
if (success = 0) then
begin
Memo1.Lines.Add(zip.LastErrorText);
Exit;
end;
sbCsv := TChilkatStringBuilder.Create(Self);
csv.SaveToSb(sbCsv.ControlInterface);
// ------------------------------------------
ai := TChilkatAi.Create(Self);
ai.Provider := 'openai';
// Use your provider's API key.
ai.ApiKey := 'MY_API_KEY';
// Choose a model.
ai.Model := 'gpt-4o';
// Add text inputs
ai.InputAddText('Describe what is contained in the following CSV data.');
ai.InputAddTextSb(sbCsv.ControlInterface);
// Ask the AI for text output.
success := ai.Ask('text');
if (success = 0) then
begin
Memo1.Lines.Add(ai.LastErrorText);
Exit;
end;
// Get the text response.
sbResponse := TChilkatStringBuilder.Create(Self);
ai.GetOutputTextSb(sbResponse.ControlInterface);
Memo1.Lines.Add(sbResponse.GetAsString());
// Sample output:
// The CSV data contains information about five companies, including the following fields:
//
// 1. **CompanyName**: The name of the company.
// 2. **Address**: The street address of the company.
// 3. **City**: The city where the company is located.
// 4. **State**: The state where the company is located, abbreviated.
// 5. **Zip**: The ZIP code for the company's location.
// 6. **Phone**: The phone number of the company.
//
// Each row in the dataset corresponds to a different company with details provided for each of these fields:
//
// ...
// ...
// -------------------------------------------------------------
// The response is in markdown format.
// Also see Markdown to HTML Conversion Examples.
// -------------------------------------------------------------
end;