Semantic Search

Cohere’s embedding models are available via the Embed endpoint. This endpoint enables you to embed text documents (multilingual) and images into a vector space.

Semantic search, powered by embeddings, enables applications to perform information retrieval based on the context or meaning of a document.

This quickstart guide shows you how to perform semantic search with the Embed endpoint.

1

Setup

First, install the Cohere Python SDK with the following command.

$pip install -U cohere

Next, import the library and create a client.

PYTHON
1import cohere
2
3co = cohere.Client(
4 "COHERE_API_KEY"
5) # Get your free API key here: https://dashboard.cohere.com/api-keys
2

Document Embeddings

First, embed the list of available documents using the Embed endpoint by specifying the input_type as search_document.

PYTHON
1# Define the documents
2documents = [
3 "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
4 "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
5 "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
6]
7
8# Embed the documents
9doc_emb = co.embed(
10 model="embed-v4.0",
11 input_type="search_document",
12 texts=documents,
13 max_tokens=8000,
14 output_dimension=1024,
15 embedding_types=["float"],
16).embeddings.float
3

Query Embedding

Next, embed the user query using the Embed endpoint by specifying the input_type as search_query.

PYTHON
1# Add the user query
2query = "Ways to connect with my teammates"
3
4# Embed the query
5query_emb = co.embed(
6 model="embed-v4.0",
7 input_type="search_query",
8 texts=[query],
9 max_tokens=8000,
10 output_dimension=1024,
11 embedding_types=["float"],
12).embeddings.float

Further Resources

Built with