Rag Contextual V2
Generative Search with Contextual AI
This notebook demonstrates how to use ContextualAI's generative model (v2) with Weaviate for RAG, combining hybrid search (sparse + dense) with generative search to answer questions based on retrieved Jeopardy data.
Requirements
- Weaviate Database ==
>1.34.0 - Weaviate Python Client ==
4.18.2 - Contextual API key - you can grab one from the console.
[notice] A new release of pip is available: 25.2 -> 25.3 [notice] To update, run: pip install --upgrade pip
Import the Libraries
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.
Insert complete.
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.
ID: cdb2d3ce-14ba-4fef-b695-b16578c4e09f
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: 3e51a440-fabf-49cb-bb55-08a288ab41fb
Data: {
"value": "$400",
"question": "You could say this Arctic mammal, Odobenus rosmarus, has a Wilford Brimley mustache",
"answer": "the walrus",
"category": "MAMMALS"
}
ID: 1a769c18-0226-432b-9c3a-d5ce66149c6c
Data: {
"value": "$800",
"question": "Kodiak Island is the habitat of this type of bear, Ursus arctos middendorffi",
"answer": "Kodiak bear",
"category": "STUPID ANSWERS"
}
Generative Search
We're using ContextualAI's generative model in Weaviate.
Generated output: Let me explain what the provided information tells us about these animals and unicorn associations. According to the information, the narwhal is specifically identified as a marine mammal where 'a part of this marine mammal was prized by medieval folk, who thought it belonged to a unicorn.'[0,2] While we can't make direct claims about why people made this association, we can note that the documentation only explicitly links this unicorn-like perception to the narwhal, not the walrus. The walrus, identified by its scientific name Odobenus rosmarus, is described as 'this Arctic mammal' but is notably characterized by its distinctive facial feature, likened to a 'Wilford Brimley mustache'.[0,6] It's worth noting that while both are Arctic/Antarctic creatures, the documentation only makes the unicorn connection explicit for the narwhal, not providing similar historical beliefs about the walrus.