Basic Semantic Search with Cohere Models
Language models give computers the ability to search by meaning and go beyond searching by matching keywords. This capability is called semantic search.

In this notebook, we’ll build a simple semantic search engine. The applications of semantic search go beyond building a web search engine. They can empower a private search engine for internal documents or records. It can also be used to power features like StackOverflow’s “similar questions” feature.
- Get the archive of questions
- Embed the archive
- Search using an index and nearest neighbor search
- Visualize the archive based on the embeddings
And if you’re running an older version of the SDK, you might need to upgrade it like so:
Get your Cohere API key by signing up here. Paste it in the cell below.
1. Getting Set Up
You’ll need your API key for this next cell. Sign up to Cohere and get one if you haven’t yet.
2. Get The Archive of Questions
We’ll use the trec dataset which is made up of questions and their categories.
2. Embed the archive
The next step is to embed the text of the questions.

To get a thousand embeddings of this length should take about fifteen seconds.
3. Search using an index and nearest neighbor search

Let’s now use Annoy to build an index that stores the embeddings in a way that is optimized for fast search. This approach scales well to a large number of texts (other options include Faiss, ScaNN, and PyNNDescent).
After building the index, we can use it to retrieve the nearest neighbors either of existing questions (section 3.1), or of new questions that we embed (section 3.2).
3.1. Find the neighbors of an example from the dataset
If we’re only interested in measuring the distance between the questions in the dataset (no outside queries), a simple way is to calculate the distance between every pair of embeddings we have.
3.2. Find the neighbors of a user query
We’re not limited to searching using existing items. If we get a query, we can embed it and find its nearest neighbors from the dataset.
4. Visualizing the archive
Finally, let’s plot out all the questions onto a 2D chart so you’re able to visualize the semantic similarities of this dataset!
Hover over the points to read the text. Do you see some of the patterns in clustered points? Similar questions, or questions asking about similar topics?
This concludes this introductory guide to semantic search using sentence embeddings. As you continue the path of building a search product additional considerations arise (like dealing with long texts, or finetuning to better improve the embeddings for a specific use case).
We can’t wait to see what you start building! Share your projects or find support on Discord.