Skip to main content
The TBIAI class enables importing data generated by artificial intelligence agents. Simply provide a natural language query and receive structured data ready for visualization.

Quick Start

uses BI.AI, BI.DataItem;

var
  Data: TDataItem;
  
// Import from Google Gemini
Data := TBIAI.From(
  'Give me the list of the highest 10 mountains by elevation in csv format',
  TBIAI.TAgent.Gemini,
  'your_api_key_here'
);

BIGrid1.Data := Data;
BIChart1.Data := Data;

Supported AI Agents

type
  TAgent = (Gemini, Copilot, ChatGPT, Grok);

Google Gemini

Free API with generous limits:
Data := TBIAI.From(
  'List top 20 countries by GDP in CSV format',
  TBIAI.TAgent.Gemini,
  'YOUR_GEMINI_API_KEY'
);
Get your free API key: https://aistudio.google.com/app/apikey

OpenAI ChatGPT

Data := TBIAI.From(
  'Generate sales data for 12 months in CSV format',
  TBIAI.TAgent.ChatGPT,
  'YOUR_OPENAI_API_KEY'
);
Models used:
  • gpt-4.1-mini (default)
  • gpt-4.1
  • o1-mini

X Grok

Data := TBIAI.From(
  'Create a table of planets with their properties',
  TBIAI.TAgent.Grok,
  'YOUR_GROK_API_KEY'
);
Model: grok-3-latest

Microsoft Copilot

Copilot integration is pending implementation.

The From Method

class function From(
  const Question: String;      // Natural language query
  const Agent: TAgent;         // AI service to use
  const Key: String            // Your API key
): TDataItem;

Example Queries

Geographic Data

Data := TBIAI.From(
  'Give me a list of US states with population and area in CSV format',
  TBIAI.TAgent.Gemini,
  ApiKey
);

Financial Data

Data := TBIAI.From(
  'Generate monthly revenue data for a SaaS company, 2023-2024, in CSV',
  TBIAI.TAgent.ChatGPT,
  ApiKey
);

Scientific Data

Data := TBIAI.From(
  'List chemical elements with symbol, atomic number, and atomic mass in CSV format',
  TBIAI.TAgent.Gemini,
  ApiKey
);

Sports Statistics

Data := TBIAI.From(
  'Top 50 NBA players by career points scored, CSV format',
  TBIAI.TAgent.Grok,
  ApiKey
);

Sample Data Generation

Data := TBIAI.From(
  'Generate 100 random customer records with name, email, city, country in CSV',
  TBIAI.TAgent.ChatGPT,
  ApiKey
);

Best Practices for Queries

Specify Format

Always request CSV or JSON format:
// Good
'List top 10 movies by box office revenue in CSV format'

// Not ideal
'List top 10 movies by box office revenue'

Be Specific

Include column names when possible:
Data := TBIAI.From(
  'Create a table with columns: Product, Price, Category, Stock. ' +
  'Generate 50 sample electronics products in CSV format',
  TBIAI.TAgent.Gemini,
  ApiKey
);

Request Clean Data

Data := TBIAI.From(
  'Top 20 programming languages by popularity in CSV format, ' +
  'just the data table, no explanations',
  TBIAI.TAgent.Gemini,
  ApiKey
);

Advanced Usage

Error Handling

uses BI.AI;

var
  Data: TDataItem;
  
try
  Data := TBIAI.From(
    'List Fortune 500 companies in CSV',
    TBIAI.TAgent.Gemini,
    ApiKey
  );
  
  if Data <> nil then
    BIGrid1.Data := Data
  else
    ShowMessage('No data returned');
    
except
  on E: EBIAI do
    ShowMessage('AI Error: ' + E.Message);
end;

Combining with Queries

uses BI.AI, BI.Query;

var
  RawData, Summary: TDataItem;
  
// Get data from AI
RawData := TBIAI.From(
  'Sales data for 12 months with product categories',
  TBIAI.TAgent.Gemini,
  ApiKey
);

// Analyze it
Summary := TBISQL.From(
  RawData,
  'sum(Sales) group by Category'
);

BIChart1.Data := Summary;

Interactive AI Data Explorer

procedure TForm1.ButtonQueryClick(Sender: TObject);
var
  Data: TDataItem;
begin
  if Edit1.Text = '' then
    Exit;
    
  Screen.Cursor := crHourGlass;
  try
    Data := TBIAI.From(
      Edit1.Text + ' in CSV format',
      TBIAI.TAgent.Gemini,
      FApiKey
    );
    
    BIGrid1.Data := Data;
    BIChart1.Data := Data;
    
  finally
    Screen.Cursor := crDefault;
  end;
end;

Response Processing

TBIAI automatically:
  1. Cleans responses: Removes markdown code blocks
  2. Detects format: CSV or JSON
  3. Parses data: Using appropriate importer (TBICSV/TBIJson)
  4. Type detection: Automatic data type inference
Raw response cleanup:
```csv
Name,Age,City
John,30,NYC
Jane,25,LA

Becomes:

Name,Age,City John,30,NYC Jane,25,LA

## API Keys

### Security

<Warning>
  Never hardcode API keys in your source code. Use configuration files or environment variables.
</Warning>

```pascal
// Good - from config
var
  IniFile: TIniFile;
  ApiKey: String;
  
IniFile := TIniFile.Create('config.ini');
try
  ApiKey := IniFile.ReadString('AI', 'GeminiKey', '');
finally
  IniFile.Free;
end;

// Bad - hardcoded
ApiKey := 'AIzaSyC...';

Getting API Keys

Google Gemini

Free API key with generous limits

OpenAI ChatGPT

Paid API with free trial credits

X Grok

X.AI platform access

Rate Limits

Be aware of API rate limits:
var
  LastCallTime: TDateTime;
  
if SecondsBetween(Now, LastCallTime) < 2 then
  Sleep(2000); // Wait 2 seconds between calls
  
Data := TBIAI.From(Query, Agent, ApiKey);
LastCallTime := Now;

Use Cases

Prototyping

Quickly generate sample data for development:
// Generate test data instantly
Customers := TBIAI.From(
  'Generate 100 customer records with realistic data',
  TBIAI.TAgent.Gemini,
  Key
);

Data Research

Gather information for analysis:
MarketData := TBIAI.From(
  'List S&P 500 companies by market cap, top 100',
  TBIAI.TAgent.ChatGPT,
  Key
);

Education

Create datasets for learning:
PeriodicTable := TBIAI.From(
  'Complete periodic table with all properties in CSV',
  TBIAI.TAgent.Gemini,
  Key
);

Platform Support

AI import requires internet connection and works on:
  • Windows (Delphi)
  • macOS (Delphi)
  • Linux (FreePascal - pending)

Common Issues

Ensure you provide a valid API key:
if ApiKey.Trim = '' then
  raise Exception.Create('API key required');
The AI may not understand the query. Be more specific:
// Add "in CSV format, just the data table"
Query := Query + ' in CSV format, just the data table';
Check your API key is correct and active:
// Test with a simple query first
Data := TBIAI.From('List 3 colors', Agent, Key);
Add delays between requests:
Sleep(2000); // Wait 2 seconds

Example: AI Query Tool

unit AIQueryForm;

interface

type
  TFormAI = class(TForm)
    MemoQuery: TMemo;
    ButtonExecute: TButton;
    ComboAgent: TComboBox;
    EditApiKey: TEdit;
    BIGrid1: TBIGrid;
    BIChart1: TBIChart;
    
    procedure ButtonExecuteClick(Sender: TObject);
  end;

implementation

uses BI.AI, BI.DataItem;

procedure TFormAI.ButtonExecuteClick(Sender: TObject);
var
  Agent: TBIAI.TAgent;
  Data: TDataItem;
begin
  // Determine agent
  case ComboAgent.ItemIndex of
    0: Agent := TBIAI.TAgent.Gemini;
    1: Agent := TBIAI.TAgent.ChatGPT;
    2: Agent := TBIAI.TAgent.Grok;
  end;
  
  // Execute query
  try
    Screen.Cursor := crHourGlass;
    
    Data := TBIAI.From(
      MemoQuery.Lines.Text + ' in CSV format',
      Agent,
      EditApiKey.Text
    );
    
    // Visualize
    BIGrid1.Data := Data;
    BIChart1.Data := Data;
    
  finally
    Screen.Cursor := crDefault;
  end;
end;

end.

See Also

Build docs developers (and LLMs) love