Skip to content

Commit b0b0308

Browse files
committed
docs: Qdrant RM example
1 parent 9c1fff9 commit b0b0308

File tree

2 files changed

+303
-1
lines changed

2 files changed

+303
-1
lines changed

dspy/retrieve/qdrant_rm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def forward(self, query_or_queries: Union[str, list[str]], k: Optional[int] = No
108108
# Wrap each sorted passage in a dotdict with 'long_text'
109109
return [dotdict({"long_text": passage}) for passage, _ in sorted_passages]
110110

111-
def _get_first_vector_name(self) -> str | None:
111+
def _get_first_vector_name(self) -> Optional[str]:
112112
vectors = self._client.get_collection(self._collection_name).config.params.vectors
113113

114114
if not isinstance(vectors, dict):
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# DSPy Retriever using Qdrant\n",
8+
"\n",
9+
"This notebook will walk you through using Qdrant as retriever in DSPy. We'll be loading a dataset into Qdrant and retrieving relevant context from it in our DSPy retriever."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"#### Setup"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"%pip install dspy-ai[qdrant]"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"#### Configure Constants"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"This notebook assumes, you have a Qdrant instance running at http://localhost:6333/. To learn more about setting up Qdrant, you can refer to the [quickstart guide](https://qdrant.tech/documentation/quick-start/)."
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 1,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"COLLECTION_NAME = \"DBPEDIA-DSPY\"\n",
49+
"QDRANT_URL = \"http://localhost:6333\""
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"### Ingesting data\n",
57+
"\n",
58+
"We'll load the [Qdrant/dbpedia-entities-openai3-text-embedding-3-small-1536-100K](https://huggingface.co/datasets/Qdrant/dbpedia-entities-openai3-text-embedding-3-small-1536-100K) dataset that contains info from DBPedia and embeddings pre-computed using OpenAI's `text-embedding-3-small`!"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"%pip install datasets"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"from datasets import load_dataset\n",
77+
"\n",
78+
"# We will use a small subset of the dataset\n",
79+
"dataset = (\n",
80+
" load_dataset(\n",
81+
" \"Qdrant/dbpedia-entities-openai3-text-embedding-3-small-1536-100K\",\n",
82+
" streaming=True,\n",
83+
" split=\"train\",\n",
84+
" )\n",
85+
" .take(1000)\n",
86+
" .remove_columns([\"openai\", \"combined_text\"])\n",
87+
")"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"Set up a client that points to a Qdrant instance at http://localhost:6333/."
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"from qdrant_client import QdrantClient\n",
104+
"\n",
105+
"client = QdrantClient(url=QDRANT_URL)"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"metadata": {},
111+
"source": [
112+
"We [create a collection](https://qdrant.tech/documentation/concepts/collections/#create-a-collection) with the appropriate dimensions and distance metric to load our dataset into."
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 4,
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"data": {
122+
"text/plain": [
123+
"True"
124+
]
125+
},
126+
"execution_count": 4,
127+
"metadata": {},
128+
"output_type": "execute_result"
129+
}
130+
],
131+
"source": [
132+
"from qdrant_client import models\n",
133+
"\n",
134+
"client.create_collection(\n",
135+
" collection_name=COLLECTION_NAME,\n",
136+
" vectors_config=models.VectorParams(\n",
137+
" size=1536,\n",
138+
" distance=models.Distance.COSINE,\n",
139+
" ),\n",
140+
")"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"We can now load the dataset to be indexed in Qdrant. The `upload_collection` methods accepts argumens to configure the batch size and parallelism. We'll go with the defaults."
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 5,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"vectors = [entry.pop(\"text-embedding-3-small-1536-embedding\") for entry in dataset]\n",
157+
"\n",
158+
"client.upload_collection(collection_name=COLLECTION_NAME, vectors=vectors, payload=dataset)"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"The loading is now complete. You can browse through the entries at http://localhost:6333/dashboard."
166+
]
167+
},
168+
{
169+
"cell_type": "markdown",
170+
"metadata": {},
171+
"source": [
172+
"#### Initialize Qdrant retriever and OpenAI vectorizer"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"The Qdrant retriever allows us to configure the vectorizer to use. We'll use the `OpenAIVectorizer` with the `text-embedding-3-small` model as per our dataset.\n",
180+
"\n",
181+
"We can also specify the field in our Qdrant payload with the document content. In our case, it's `\"text\"`. Based on the dataset we loaded."
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": null,
187+
"metadata": {},
188+
"outputs": [],
189+
"source": [
190+
"import os\n",
191+
"\n",
192+
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_API_KEY>\""
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 3,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"from dsp.modules.sentence_vectorizer import OpenAIVectorizer\n",
202+
"\n",
203+
"vectorizer = OpenAIVectorizer(model=\"text-embedding-3-small\")"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 4,
209+
"metadata": {},
210+
"outputs": [],
211+
"source": [
212+
"from dspy.retrieve.qdrant_rm import QdrantRM\n",
213+
"\n",
214+
"qdrant_retriever = QdrantRM(\n",
215+
" qdrant_client=client,\n",
216+
" qdrant_collection_name=COLLECTION_NAME,\n",
217+
" vectorizer=vectorizer,\n",
218+
" document_field=\"text\",\n",
219+
")"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"With the `qdrant_retriever` now instantiated, we can now configure `dspy` to use it."
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 5,
232+
"metadata": {},
233+
"outputs": [],
234+
"source": [
235+
"import dspy\n",
236+
"\n",
237+
"dspy.settings.configure(rm=qdrant_retriever)"
238+
]
239+
},
240+
{
241+
"cell_type": "markdown",
242+
"metadata": {},
243+
"source": [
244+
"### Trying out the retriever\n",
245+
"\n",
246+
"We can use the `dspy.Retrieve` class to query our retriever. Similar to how it's done in the DSPy RAG pipelines."
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": 6,
252+
"metadata": {},
253+
"outputs": [
254+
{
255+
"data": {
256+
"text/plain": [
257+
"Prediction(\n",
258+
" passages=['CounterSpy is a proprietary spyware removal program for Microsoft Windows software developed by Sunbelt Software.', 'In computing, the diff utility is a data comparison tool that calculates and displays the differences between two files. Unlike edit distance notions used for other purposes, diff is line-oriented rather than character-oriented, but it is like Levenshtein distance in that it tries to determine the smallest set of deletions and insertions to create one file from the other.', \"AudioDesk is an audio workstation application by Mark of the Unicorn (MOTU) for the Mac OS. It is a multi-track recording, editing, and mixing application, with both offline file-based processing and realtime effects. It is a more basic version of MOTU's Digital Performer DAW software. Much of the graphical user interface (GUI) and its operation are similar to Digital Performer, although it lacks some of Digital Performer's features.\"]\n",
259+
")"
260+
]
261+
},
262+
"execution_count": 6,
263+
"metadata": {},
264+
"output_type": "execute_result"
265+
}
266+
],
267+
"source": [
268+
"retrieve = dspy.Retrieve()\n",
269+
"\n",
270+
"retrieve(\"Some computer programs.\")"
271+
]
272+
},
273+
{
274+
"cell_type": "markdown",
275+
"metadata": {},
276+
"source": [
277+
"We are able to successfully retrieve results relevant to the query from our Qdrant collection."
278+
]
279+
}
280+
],
281+
"metadata": {
282+
"kernelspec": {
283+
"display_name": "Python 3",
284+
"language": "python",
285+
"name": "python3"
286+
},
287+
"language_info": {
288+
"codemirror_mode": {
289+
"name": "ipython",
290+
"version": 3
291+
},
292+
"file_extension": ".py",
293+
"mimetype": "text/x-python",
294+
"name": "python",
295+
"nbconvert_exporter": "python",
296+
"pygments_lexer": "ipython3",
297+
"version": "3.10.13"
298+
}
299+
},
300+
"nbformat": 4,
301+
"nbformat_minor": 2
302+
}

0 commit comments

Comments
 (0)