Notebooks
C
Cohere
Co Aws Ch5 Rerank Sm

Co Aws Ch5 Rerank Sm

llmunotebookscohere-developer-experience

Reranking Using Cohere Rerank on Amazon SageMaker

Reranking is an essential technique in information retrieval systems, especially in large-scale search applications. It is a process of reordering a set of initially retrieved documents based on their relevance to a user's query.

What’s great about reranking is that while it gives a huge boost to search results, implementing Cohere’s Rerank models requires adding only one line of code to any existing search system, whether a semantic search system or a traditional search system that uses keyword-based approaches.

In this notebook, we'll explore how to use the Cohere Rerank endpoint on Amazon SageMaker. In particular, we'll look at an example of a multi-aspect search on semi-structured data, and walk through how to perform reranking on email data that contains multiple fields: “title” and “content.”

Setup

First, let's install and import the necessary libraries and set up our Cohere client.

We'll need to create a SageMaker endpoint that exposes access to a Cohere model (Rerank v3 in our case). For this, we’ll use the cohere_aws SDK which makes it easy to set up the endpoint, together with AWS’s boto3 library.

[ ]
[3]
[ ]

Create Endpoint

With SageMaker, we’ll need to create an endpoint via an AWS instance. The marketplace listing provides more details, including pricing, on the recommended instance type for a particular model.

To create the endpoint, we define:

  • arn: The model package ARN we defined in the previous step
  • endpoint_name: A name we can give as an identifier
  • instance_type: The instance type to be used
  • n_instances: The number of instances

We pass the arguments to the create_endpoint method from the cohere_aws library.

[ ]

Retrieve Documents

Let’s assume that the first stage of retrieval has already been performed, whether it’s through a semantic, keyword, or any other type of search system.

Here we have a list of nine documents that represent the search results of that first stage. Each document has two fields, Title and Content, corresponding to the contents of an email. Each email is a dictionary containing these fields that preserves its semi-structured format, which the Rerank endpoint can take advantage of.

[48]

Rerank Documents

To use the endpoint, we now use the cohere SDK. Adding a reranking component is simple with Cohere Rerank. It takes just one line of code to implement.

[49]

View Results

Since we defined top_n=3, we’ll get the top three most relevant documents to the query. For each document, the response contains the index of its position in the original list and its relevance score against the query.

[50]
Documents 

#1:
{'Title': 'Questions about Return Policy', 'Content': 'Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.'}

#2:
{'Title': 'Return Policy for Defective Product', 'Content': 'Hi, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.'}

#3:
{'Title': 'Return Defective Product', 'Content': 'Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.'}

The search query was looking for emails about refunds. But none of the documents mention the word “refunds” specifically.

However, the Rerank model was able to retrieve the right documents. From the list of documents, some mention the word “return.” The Rerank model can capture semantically similar meanings between two pieces of text, so it is able to return documents that mention return instead, which has a very similar meaning to return.

Delete endpoint

Important Note: You will continue to incur charges for as long as an endpoint is running, so remember to delete the endpoint when your usage ends.

[ ]

Reranking is a valuable technique used in information retrieval systems to enhance the relevance of search results. Cohere's Rerank endpoint, including its latest model, Rerank 3, offers improved capabilities for enterprise search.

By incorporating reranking with a single line of code, as shown in our example in this chapter, the model successfully identified semantically similar documents, even when specific keywords were absent from the query. This example highlights the potential benefits of integrating reranking into existing search systems to enhance search accuracy and user satisfaction.