Introduction Text Embeddings
Introduction to Text Embeddings
Text embeddings are a useful way to turn text into numbers that capture its meaning and context. In this notebook, you'll learn how to put them into practice using Cohere's Embed endpoint. You'll calculate embeddings for a dataset of sentences, and plot them in the plane to observe graphically that indeed similar sentences are mapped to close points in the embedding. You'll also explore how to leverage embeddings for semantic search and clustering.
Setup
We'll start by installing the tools we'll need and then importing them.
Introduction to Text Embeddings
In this section, we understand the intuition behind text embeddings.
Step 1: Prepare the Dataset
We'll work with a subset of the Airline Travel Information System (ATIS) dataset (source), created based on customer inquiries related to flight bookings, flight departures, arrivals, delays, and cancellations. In the next code cell, we create and preview a dataframe df containing 91 queries.
which airlines fly from boston to washington dc via other cities show me the airlines that fly between toronto and denver show me round trip first class tickets from new york to miami i'd like the lowest fare from denver to pittsburgh show me a list of ground transportation at boston airport show me boston ground transportation of all airlines which airline has the most arrivals in atlanta what ground transportation is available in boston i would like your rates between atlanta and boston on september third which airlines fly between boston and pittsburgh
Step 2: Turn Text into Embeddings
Next, we use a function get_embeddings() that uses the Embed endpoint to generate the text embeddings. We store the embeddings in the query_embeds column of the dataframe df.
Step 3: Visualize Embeddings on a Heatmap
Let’s get some visual intuition about the embeddings by plotting these numbers on a heatmap. What we can do is compress the dimension to a much lower number, say 10. We can do this via a technique called Principal Component Analysis (PCA), which reduces the number of dimensions in an embedding while retaining as much information as possible.
Next, we visualize the embeddings of the first 9 data points.
Notice the 3 inquiries about ground transportation in Boston - their embeddings patterns are very similar, and at the same time are distinctive from the rest.
Step 4: Visualize Embeddings on a 2D Plot
We can investigate this further by compressing the embeddings to two dimensions and plotting them on a scatter plot. What we would expect is that texts of similar meaning would be closer to each other, and vice versa.
Here texts of similar meaning are located close together. We see inquiries about tickets on the left, inquiries about airlines somewhere around the middle, and inquiries about ground transportation on the top right.
Introduction to Semantic Search
In this section, you'll learn how to use embeddings to build a search capability that surfaces relevant information based on the semantic meaning of a query. We'll work with the same 9 data points plotted above.
Read the accompanying blog post here.
Step 1: Embed the Search Query
We'll use the query below that inquires about ground transportation without using the words "ground transportation" explicitly. Ideally, the corresponding data points that are surfaced as most relevant are the three dealing with ground transportation options.
We embed the query using the same get_embeddings() function as before, but now we set input_type="search_query" because we're embedding a search query that we want to compare to the embedded documents.
Step 2: Compare to Embedded Documents
We define and use a function get_similarity() that employs cosine similarity to determine how similar the documents are to our query.
We'll then view the documents in decreasing order of similarity.
Query: How can I find a taxi or a bus when the plane lands? Most Similar Documents: Similarity: 0.37; show me a list of ground transportation at boston airport Similarity: 0.36; what ground transportation is available in boston Similarity: 0.33; show me boston ground transportation Similarity: 0.27; show me the airlines that fly between toronto and denver Similarity: 0.25; which airlines fly from boston to washington dc via other cities Similarity: 0.24; of all airlines which airline has the most arrivals in atlanta Similarity: 0.18; i'd like the lowest fare from denver to pittsburgh Similarity: 0.17; show me round trip first class tickets from new york to miami Similarity: 0.17; i would like your rates between atlanta and boston on september third
The top three most similar documents are the inquiries about ground transportation. Notice that the query didn't explicitly mention "ground transportation," yet the embedding successfully captured the underlying meaning. It recognized the words "taxi" and "bus," grouping them together as similar concepts.
Step 3: Visualize the Results in a 2D Plot
To prepare for plotting, we use PCA to reduce the query embedding to 2 dimensions with the get_pc() function we defined earlier.
We then can plot the query on a 2D scatter plot with the nine documents.
We see that the query is located closest to the FAQs about ground transportation.
Clustering Using Embeddings
In this section, you will learn how to use embeddings to group similar documents into clusters, to discover emerging patterns in the documents. We'll work with the same 9 data points as before.
Read the accompanying blog post here.
Step 1: Embed the Text for Clustering
We embed the documents using the same get_embeddings() function as before, but now we set input_type="clustering" because we'll use the embeddings for clustering.
Step 2: Cluster the Embeddings
We use the K-means algorithms to cluster these data points. Since our dataset is small, we'll set the number of clusters to 2. In actual applications, this number is normally larger.
We store the cluster assignments in the "cluster" column of the df_clust DataFrame.
Step 3: Visualize the Results in a 2D Plot
When specified with 2 clusters to group the documents by, the algorithm looks to be spot on, where it generates one cluster related to airline information and one cluster related to ground service information.