TEmbeddings

Source: source/classes/tembeddings.prg

TEmbeddings is FiveWin's class for generating and comparing text embeddings (vector representations of text). It supports both HuggingFace (via the Inference API feature-extraction pipeline) and OpenAI embeddings API. Embeddings convert text into high-dimensional vectors that capture semantic meaning, enabling operations like semantic similarity search, clustering, and classification.

Architecture

flowchart LR subgraph "FiveWin Application" A[TEmbeddings] B[Distance Calculation] end subgraph "Provider" C[HuggingFace
Inference API] D[OpenAI
Embeddings API] end A -->|"HuggingFace model"| C A -->|"OpenAI model"| D A --> B

Key DATA Members

DATATypeDefaultDescription
cModelCharacter"sentence-transformers/all-MiniLM-L6-v2"Embeddings model name. HuggingFace model ID or OpenAI embedding model.
cKeyCharacterAPI key. Reads HF_API_KEY first, then OPENAI_API_KEY.
aEmbeddingsArrayThe embedding vector returned by the API (array of numeric values)
cResponseCharacterRaw JSON response from the API
cUrlCharacterAPI endpoint URL (auto-set based on model prefix)
nErrorNumeric0curl error code
nHttpCodeNumeric0HTTP response status code

Methods

MethodDescription
New( cModel, cKey )Create a TEmbeddings instance. Defaults to HuggingFace MiniLM model. Reads API key from HF_API_KEY then OPENAI_API_KEY env vars if not provided.
GetEmbeddings( cText )Generate embeddings for the given text. Stores the vector in aEmbeddings. Returns the raw vector array.
Distance( aVector, lCosine )Compute distance between aEmbeddings and another vector. Default uses cosine distance. Set lCosine := .F. for Euclidean distance.
DistanceFrom( oEmbeddings )Compute distance to another TEmbeddings object by comparing their aEmbeddings vectors.
End()Clean up the libcurl handle.

Example: Semantic Similarity

#include "FiveWin.ch"

function Main()

   local oE1 := TEmbeddings():New()
   local oE2 := TEmbeddings():New()
   local oE3 := TEmbeddings():New()
   local nSimilarity1, nSimilarity2

   // Generate embeddings for three phrases
   oE1:GetEmbeddings( "I feel very well" )
   oE2:GetEmbeddings( "How are you?" )
   oE3:GetEmbeddings( "I saw Peter" )

   // Cosine distance: lower = more similar
   nSimilarity1 := oE1:DistanceFrom( oE2 )  // Low distance (similar topic)
   nSimilarity2 := oE1:DistanceFrom( oE3 )  // High distance (different topic)

   QOut( "Similarity between 'feel well' and 'How are you':", nSimilarity1 )
   QOut( "Similarity between 'feel well' and 'saw Peter':", nSimilarity2 )

   // Using the OPERATOR "-" syntax (equivalent to DistanceFrom)
   ? oE1 - oE2   // Same as oE1:DistanceFrom( oE2 )
   ? oE1 - oE3   // Same as oE1:DistanceFrom( oE3 )

   oE1:End()
   oE2:End()
   oE3:End()

return nil

Example: Euclidean Distance

#include "FiveWin.ch"

function Main()

   local oE1 := TEmbeddings():New()
   local oE2 := TEmbeddings():New()

   oE1:GetEmbeddings( "Machine learning is fascinating" )
   oE2:GetEmbeddings( "Deep learning and AI are interesting" )

   // Euclidean distance instead of cosine
   ? "Euclidean distance:", oE1:Distance( oE2:aEmbeddings, .F. )

   oE1:End()
   oE2:End()

return nil

Notes

See Also