Notebooks
W
Weaviate
Rerank Contextual

Rerank Contextual

vector-searchvector-databaseretrieval-augmented-generationcontextual-rerankingllm-frameworksweaviate-featuresfunction-callingweaviate-recipesPythongenerative-aireranking

Open In Colab

Reranking with Contextual AI

This notebook demonstrates how to use Contextual AI's reranking model (ctxl-rerank-v2-instruct-multilingual) with Weaviate to improve search result quality.

Requirements

  1. Weaviate Database >= 1.34.0
  2. Weaviate Python Client >= 4.18.2
  3. Contextual API key - you can grab one from the console.
[1]

[notice] A new release of pip is available: 25.2 -> 25.3
[notice] To update, run: pip install --upgrade pip

Import the Libraries

[14]

Connect to Weaviate Cloud

You can create a free 14-day sandbox on Weaviate Cloud!

[ ]

Define Weaviate Collection

You can create a new collection with the below cell block, or you can connect to your existing collection and skip the below cell.

[ ]
Successfully created collection: JeopardyQuestions.

Import Data

We will use the small jeopardy dataset as an example. It has 1,000 objects.

[5]
[11]
Insert complete.
[12]
1000

Query Time

Hybrid Search

The alpha parameter determines the weight given to the sparse and dense search methods. alpha = 0 is pure sparse (bm25) search, whereas alpha = 1 is pure dense (vector) search.

Alpha is an optional parameter. The default is set to 0.75.

[16]
ID: f59b284a-8c14-4bd6-b51e-cdd7038899e3
Data: {
  "value": "NaN",
  "question": "A part of this marine mammal was prized by medieval folk, who thought it belonged to a unicorn",
  "answer": "the narwhal",
  "category": "THE ANIMAL KINGDOM"
} 

ID: d5bde835-2125-4e48-996f-ce5a917cd2cc
Data: {
  "value": "$400",
  "question": "You could say this Arctic mammal, Odobenus rosmarus, has a Wilford Brimley mustache",
  "answer": "the walrus",
  "category": "MAMMALS"
} 

ID: 49618708-f547-421f-a8cb-09d35896efcb
Data: {
  "value": "$800",
  "question": "Kodiak Island is the habitat of this type of bear, Ursus arctos middendorffi",
  "answer": "Kodiak bear",
  "category": "STUPID ANSWERS"
} 

Query with Reranker

We're using ContextualAI's reranker model in Weaviate.

[17]
ID: d5bde835-2125-4e48-996f-ce5a917cd2cc
Data: {
  "value": "$400",
  "question": "You could say this Arctic mammal, Odobenus rosmarus, has a Wilford Brimley mustache",
  "answer": "the walrus",
  "category": "MAMMALS"
} 

ID: f59b284a-8c14-4bd6-b51e-cdd7038899e3
Data: {
  "value": "NaN",
  "question": "A part of this marine mammal was prized by medieval folk, who thought it belonged to a unicorn",
  "answer": "the narwhal",
  "category": "THE ANIMAL KINGDOM"
}