Skip to main content
Cohere

Search documentation

Type to search this documentation.

On this pageOverview

Semantic search - quickstart

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.

    Bash
    pip install -U cohere

    Next, import the library and create a client.

    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        "COHERE_API_KEY"
    )  # Get your free API key here: https://dashboard.cohere.com/api-keys
    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        api_key="",  # Leave this blank
        base_url="<YOUR_DEPLOYMENT_URL>",
    )
    PYTHON
    import cohere
    
    co = cohere.BedrockClientV2(
        aws_region="AWS_REGION",
        aws_access_key="AWS_ACCESS_KEY_ID",
        aws_secret_key="AWS_SECRET_ACCESS_KEY",
        aws_session_token="AWS_SESSION_TOKEN",
    )
    
    # Get the model name: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
    PYTHON
    import cohere
    
    co = cohere.SagemakerClientV2(
        aws_region="AWS_REGION",
        aws_access_key="AWS_ACCESS_KEY_ID",
        aws_secret_key="AWS_SECRET_ACCESS_KEY",
        aws_session_token="AWS_SESSION_TOKEN",
    )
    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        api_key="AZURE_API_KEY",
        base_url="AZURE_ENDPOINT",  # example: "https://cohere-embed-v3-multilingual-xyz.eastus.models.ai.azure.com/"
    )
  2. Document Embeddings

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

    PYTHON
    # Define the documents
    documents = [
        "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
        "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
        "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
    ]
    
    # Embed the documents
    
    doc_emb = co.embed(
        model="embed-v4.0",
        input_type="search_document",
        texts=documents,
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Define the documents
    documents = [
        "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
        "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
        "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
    ]
    
    # Embed the documents
    doc_emb = co.embed(
        model="embed-v4.0",
        input_type="search_document",
        texts=documents,
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Define the documents
    documents = [
        "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
        "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
        "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
    ]
    
    # Embed the documents
    
    doc_emb = co.embed(
        model="YOUR_MODEL_NAME",
        input_type="search_document",
        texts=documents,
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Define the documents
    documents = [
        "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
        "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
        "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
    ]
    
    # Embed the documents
    doc_emb = co.embed(
        model="YOUR_ENDPOINT_NAME",
        input_type="search_document",
        texts=documents,
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Define the documents
    documents = [
        "Joining Slack Channels: Be sure to join relevant channels to stay informed and engaged.",
        "Finding Coffee Spots: For your caffeine fix, cross the street to the café for artisan coffee.",
        "Working Hours Flexibility: While our core hours are 9 AM to 5 PM, we offer flexibility to adjust as needed.",
    ]
    
    # Embed the documents
    
    doc_emb = co.embed(
        input_type="search_document",
        texts=documents,
        embedding_types=["float"],
    ).embeddings.float
  3. Query Embedding

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

    PYTHON
    # Add the user query
    query = "Ways to connect with my teammates"
    
    # Embed the query
    query_emb = co.embed(
        model="embed-v4.0",
        input_type="search_query",
        texts=[query],
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Add the user query
    query = "Ways to connect with my teammates"
    
    # Embed the query
    
    query_emb = co.embed(
        model="embed-v4.0",
        input_type="search_query",
        texts=[query],
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Add the user query
    query = "Ways to connect with my teammates"
    
    # Embed the query
    query_emb = co.embed(
        model="YOUR_MODEL_NAME",
        input_type="search_query",
        texts=[query],
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Add the user query
    query = "Ways to connect with my teammates"
    
    query_emb = co.embed(
        model="embed-v4.0",
        input_type="search_query",
        texts=[query],
        embedding_types=["float"],
    ).embeddings.float
    PYTHON
    # Add the user query
    query = "Ways to connect with my teammates"
    
    query_emb = co.embed(
        model="embed-v4.0",
        input_type="search_query",
        texts=[query],
        embedding_types=["float"],
    ).embeddings.float
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu