Basic Examples (2)

Plot the embeddings of a list of strings:

({{"[◼]", "TextEmbeddingPlot" }})[Alphabet[],2]

Compare sentence embeddings from different texts:

({{"[◼]", "TextEmbeddingPlot" }})[TextSentences/@{ResourceData["Gettysburg Address"],ResourceData["Friends, Romans, Countrymen"]},3]

Scope (3)

Prepare sentences from three different text sources:

sources={"Alice in Wonderland","Declaration of Independence","Gettysburg Address"};
data=Map[Take[TextSentences@ResourceData[#],UpTo[50]]&,sources];

Create a labeled plot:

({{"[◼]", "TextEmbeddingPlot" }})[data,3,PlotLegends->sources]

Present the same data with a different dimension reduction method:

({{"[◼]", "TextEmbeddingPlot" }})[data,3,PlotLegends->sources,"ReductionMethod"->"UMAP"]

Options (10)

"EmbeddingFunction" (4)

Define a custom embedding function using an external service, which also prints information on model and token use:

customEmbedder[stringlist_List]/;AllTrue[stringlist,StringQ]:=
Module[{response},
response=ServiceExecute["OpenAI","Embedding",{"Input"->stringlist}];
Print@KeyDrop[response,"Content"];
response["Content"]
]

Use the custom embedding function on the previously defined data:

({{"[◼]", "TextEmbeddingPlot" }})[data[[All,1;;10]],2,"EmbeddingFunction"->customEmbedder]

The "EmbeddingFunction" should accept a list of strings as input. Use a naive embedding function that computes letter counts:

({{"[◼]", "TextEmbeddingPlot" }})[data,2,"EmbeddingFunction"->Map[Lookup[LetterCounts[#],Alphabet[],0]&]]

A function specified for "EmbeddingFunction" should output a corresponding list of vectors or NumericArray objects:

randomEmbedder[list_]:=NumericArray[RandomReal[{-1,1},{Length@list,384}],"Real32"]

({{"[◼]", "TextEmbeddingPlot" }})[data,2,"EmbeddingFunction"->randomEmbedder]

"ReductionMethod" (3)

Method options of DimensionReduction are supported:

GraphicsRow@Table[({{"[◼]", "TextEmbeddingPlot" }})[data,2,"ReductionMethod"->m],{m,{Automatic,"TSNE","UMAP"}}]

Use a specific dimension reduction function:

dr=DimensionReduction[RandomReal[{-100,100},{1000,384}],2]

({{"[◼]", "TextEmbeddingPlot" }})[data,2,"ReductionMethod"->dr]

Define an arbitrary function to perform the dimension reduction on embedding vectors:

({{"[◼]", "TextEmbeddingPlot" }})[data,2,"ReductionMethod"->(Take[#,-2]&)]

Labeling Function (3)

Use default behavior of ListPlot:

({{"[◼]", "TextEmbeddingPlot" }})[data,2,LabelingFunction->Automatic]

Use default behavior of ListPointPlot3D:

({{"[◼]", "TextEmbeddingPlot" }})[data,3,LabelingFunction->Automatic]

Define a custom LabelingFunction:

({{"[◼]", "TextEmbeddingPlot" }})[data[[All,1;;10]],2,LabelingFunction->(StringTake[#3[[3,1]],UpTo[20]]<>" ..."&)]

Possible Issues (1)

A "ReductionMethod" that is not one of the supported options for DimensionReduction must accept vectors of the appropriate input and output dimensions:

wrongOutputDim=DimensionReduction[RandomReal[{-100,100},{1000,384}],3]

({{"[◼]", "TextEmbeddingPlot" }})[data,2,"ReductionMethod"->wrongOutputDim]

wrongInputDim=DimensionReduction[RandomReal[{-100,100},{1000,15}],2]

Enclose@ConfirmQuiet@({{"[◼]", "TextEmbeddingPlot" }})[data,2,"ReductionMethod"->wrongInputDim]