Skip to content

Latest commit

 

History

History
80 lines (71 loc) · 2.91 KB

File metadata and controls

80 lines (71 loc) · 2.91 KB

Embeddings

OpenAI’s text embeddings evaluate how closely related different text strings are. These embeddings serve as a powerful tool for various applications, including:

  • Search: Ranking results based on their relevance to a given query.
  • Clustering: Grouping similar text strings together based on shared characteristics.
  • Recommendations: Suggesting items that share similar text content.
  • Anomaly detection: Identifying outliers by finding text strings with minimal similarity to the rest.
  • Diversity measurement: Analyzing the distribution of similarities within a dataset.
  • Classification: Assigning text strings to the category or label they closely align with.

An embedding is represented as a vector, or a list of floating-point numbers. The relatedness between two text strings is determined by measuring the distance between their respective vectors: smaller distances indicate strong similarity, while larger distances imply weaker relatedness.

Refer to official documentation.

//uses GenAI, GenAI.Types, GenAI.Tutorial.VCL;

  TutorialHub.JSONRequestClear;

  //Asynchronous example
  Client.Embeddings.ASynCreate(
    procedure (Params: TEmbeddingsParams)
    begin
      Params.Input(['Hello', 'how', 'are you?']);
      Params.Model('text-embedding-3-large');
      Params.Dimensions(5);
      Params.EncodingFormat(TEncodingFormat.float);
      TutorialHub.JSONRequest := Params.ToFormat();
    end,
    function : TAsynEmbeddings
    begin
      Result.Sender := TutorialHub;
      Result.OnStart := Start;
      Result.OnSuccess := Display;
      Result.OnError := Display;
    end);

  //Synchronous example
//  var Value := Client.Embeddings.Create(
//    procedure (Params: TEmbeddingsParams)
//    begin
//      Params.Input(['Hello', 'how', 'are you?']);
//      Params.Model('text-embedding-3-large');
//      Params.Dimensions(5);
//      Params.EncodingFormat(TEncodingFormat.float);
//    end);
//  try
//    Display(TutorialHub, Value);
//  finally
//    Value.Free;
//  end;

  //Asynchronous promise example
//  var Promise := Client.Embeddings.AsyncAwaitCreate(
//    procedure (Params: TEmbeddingsParams)
//    begin
//      Params.Input(['Hello', 'how', 'are you?']);
//      Params.Model('text-embedding-3-large');
//      Params.Dimensions(5);
//      Params.EncodingFormat(TEncodingFormat.float);
//      TutorialHub.JSONRequest := Params.ToFormat();
//    end
//  );
//
//  Promise
//    .&Then<TArray<TArray<Double>>>(
//       function (Value: TEmbeddings): TArray<TArray<Double>>
//       begin
//         Display(TutorialHub, Value);
//         for var Item in Value.Data do
//           Result := Result + [Item.Embedding];
//         ShowMessage(Result[2][3].ToString(ffNumber, 2, 3));
//       end)
//    .&Catch(
//       procedure (E: Exception)
//       begin
//         Display(TutorialHub, E.Message);
//       end);