OpenAI Embeddings Component — sgcAI | eSeGeCe

OpenAI Embeddings

TsgcAIOpenAIEmbeddings is the ingestion and lookup side of retrieval augmented generation. It splits a corpus into chunks, requests an embedding vector for each one, writes them to whichever vector store you assign, and returns the nearest stored text for any query.

TsgcAIOpenAIEmbeddings

Embeddings generation with a pluggable store. Assign a vector database component to Database and the whole ingest pipeline, chunking, embedding, batching and writing, runs from one call.

Component class

TsgcAIOpenAIEmbeddings

Purpose

Embeddings and RAG lookup

Platforms

Win32 and Win64

sgcAI is self-contained. This component ships inside sgcAI together with the sgcWebSockets Core runtime it is built on, so there is no base license to buy.

Index a corpus, then query it

Assign a vector store, call CreateEmbeddingsFromFile once, then look up context for every user question.

uses
  sgcAI, sgcAI_OpenAI_Embeddings, sgcAI_DB_Vector_File;

var
  oEmbeddings: TsgcAIOpenAIEmbeddings;
  oDB: TsgcAIDatabaseVectorFile;
  vContext: string;
begin
  oDB := TsgcAIDatabaseVectorFile.Create(nil);
  oDB.VectorFileOptions.InputFilename  := 'corpus.sgcif';
  oDB.VectorFileOptions.VectorFilename := 'corpus.sgcvf';

  oEmbeddings := TsgcAIOpenAIEmbeddings.Create(nil);
  oEmbeddings.OpenAIOptions.ApiKey := 'sk-...';
  oEmbeddings.EmbeddingsOptions.Model     := 'text-embedding-3-small';
  oEmbeddings.EmbeddingsOptions.ChunkSize := 4096;
  oEmbeddings.Database := oDB;

  oEmbeddings.OnCreateEmbeddingsProgress := EmbeddingsProgress;

  // One pass over the corpus, done once.
  oEmbeddings.CreateEmbeddingsFromFile('manual.txt');

  // Then, per question, retrieve the nearest chunk.
  vContext := oEmbeddings.GetEmbedding('How do I reset the device?', '');
end;

procedure TForm1.EmbeddingsProgress(Sender: TObject;
  aPosition: Int64; var aContinue: Boolean);
begin
  ProgressBar1.Position := aPosition;
  aContinue := not CancelRequested;
end;
// includes: sgcAI.hpp, sgcAI_OpenAI_Embeddings.hpp, sgcAI_DB_Vector_File.hpp

TsgcAIDatabaseVectorFile *oDB = new TsgcAIDatabaseVectorFile(NULL);
oDB->VectorFileOptions->InputFilename  = "corpus.sgcif";
oDB->VectorFileOptions->VectorFilename = "corpus.sgcvf";

TsgcAIOpenAIEmbeddings *oEmbeddings = new TsgcAIOpenAIEmbeddings(NULL);
oEmbeddings->OpenAIOptions->ApiKey = "sk-...";
oEmbeddings->Database = oDB;

oEmbeddings->CreateEmbeddingsFromFile("manual.txt");
String vContext = oEmbeddings->GetEmbedding("How do I reset the device?", "");

Key properties & methods

The members you reach for most often.

Database

Database takes any vector store component. Use TsgcAIDatabaseVectorFile for a local file or TsgcAIDatabaseVectorPinecone for a managed index. The ingest code does not change.

EmbeddingsOptions

Model defaults to text-embedding-3-small. ChunkSize controls how the corpus is split, WaitStoringData paces the writes to the store, and User tags the requests.

Ingest

CreateEmbeddings indexes a string and CreateEmbeddingsFromFile indexes a whole file. Both open a batch on the store, push one vector per chunk, then close it.

Query

GetEmbedding embeds a query and returns the nearest stored text, which is the string you paste into the prompt as context.

Batch events

OnCreateEmbeddingsStart reports the total size, OnCreateEmbeddingsProgress reports position and carries a continue flag so a long ingest can be cancelled, and OnCreateEmbeddingsStop closes the run.

Per-chunk events

OnBeforeCreateEmbedding can inspect and reject an individual request, and OnAfterCreateEmbedding hands you the request and the response, which is useful for auditing token spend.

Specifications & references

Authoritative sources for the services and protocols this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
All sgcAI ComponentsBrowse the full feature matrix of all 15 components.
Download Free TrialIndex your own documents and query them offline.
PricingSingle, Team and Site licenses, full source code, no base product required.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and ground a language model in your own data.