Skip to content

Commit c905676

Browse files
[RAG-75] docs: add quickstart notebook (#151)
Add quickstart notebook
1 parent 113c543 commit c905676

File tree

1 file changed

+310
-0
lines changed

1 file changed

+310
-0
lines changed

examples/notebooks/quickstart.ipynb

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<a href=\"https://colab.research.google.com/github/datastax/ragstack-ai/blob/main/quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Quickstart with RAGStack\n",
15+
"\n",
16+
"This notebook demonstrates how to set up a simple RAG pipeline with RAGStack. At the end of this notebook, you will have a fully functioning Question/Answer model that can answer questions using your supplied documents. \n",
17+
"\n",
18+
"A RAG pipeline requires, at minimum, a vector store, an embedding model, and an LLM. In this tutorial, you will use an Astra DB vector store, an OpenAI embedding model, an OpenAI LLM, and LangChain to orchestrate it all together."
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"## Prerequisites\n",
26+
"\n",
27+
"You will need a vector-enabled Astra database and an OpenAI Account.\n",
28+
"\n",
29+
"* Create an [Astra vector database](https://docs.datastax.com/en/astra-serverless/docs/getting-started/create-db-choices.html).\n",
30+
"* Create an [OpenAI account](https://openai.com/)\n",
31+
"* Within your database, create an [Astra DB Access Token](https://docs.datastax.com/en/astra-serverless/docs/manage/org/manage-tokens.html) with Database Administrator permissions.\n",
32+
"* Get your Astra DB Endpoint: \n",
33+
" * `https://<ASTRA_DB_ID>-<ASTRA_DB_REGION>.apps.astra.datastax.com`\n",
34+
"\n",
35+
"See the [Prerequisites](https://docs.datastax.com/en/ragstack/docs/prerequisites.html) page for more details."
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Setup\n",
43+
"`ragstack-ai` includes all the packages you need to build a RAG pipeline. \n",
44+
"\n",
45+
"`datasets` is used to import a sample dataset"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 3,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"! pip install -q ragstack-ai datasets"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {
61+
"nbmake": {
62+
"post_cell_execute": [
63+
"import string\n",
64+
"import random\n",
65+
"collection = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))\n"
66+
]
67+
}
68+
},
69+
"outputs": [],
70+
"source": [
71+
"import os\n",
72+
"from getpass import getpass\n",
73+
"\n",
74+
"# Enter your settings for Astra DB and OpenAI:\n",
75+
"keys = [\"ASTRA_DB_APPLICATION_TOKEN\", \"ASTRA_DB_API_ENDPOINT\", \"OPENAI_API_KEY\"]\n",
76+
"for key in keys:\n",
77+
" if key not in os.environ:\n",
78+
" os.environ[key] = getpass(f\"Enter {key}: \")"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 5,
84+
"metadata": {
85+
"tags": [
86+
"skip-execution"
87+
]
88+
},
89+
"outputs": [],
90+
"source": [
91+
"# Collections are where documents are stored. ex: test\n",
92+
"collection = input(\"Collection: \")"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"## Create RAG Pipeline"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"### Embedding Model and Vector Store"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 13,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"name": "stdout",
116+
"output_type": "stream",
117+
"text": [
118+
"Astra vector store configured\n"
119+
]
120+
}
121+
],
122+
"source": [
123+
"from langchain.vectorstores.astradb import AstraDB\n",
124+
"from langchain.embeddings import OpenAIEmbeddings\n",
125+
"\n",
126+
"# Configure your embedding model and vector store\n",
127+
"embedding = OpenAIEmbeddings()\n",
128+
"vstore = AstraDB(\n",
129+
" collection_name=collection,\n",
130+
" embedding=embedding,\n",
131+
" token=os.getenv(\"ASTRA_DB_APPLICATION_TOKEN\"),\n",
132+
" api_endpoint=os.getenv(\"ASTRA_DB_API_ENDPOINT\"),\n",
133+
")\n",
134+
"print(\"Astra vector store configured\")"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 8,
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"name": "stdout",
144+
"output_type": "stream",
145+
"text": [
146+
"An example entry:\n",
147+
"{'author': 'aristotle', 'quote': 'Love well, be loved and do something of value.', 'tags': 'love;ethics'}\n"
148+
]
149+
}
150+
],
151+
"source": [
152+
"from datasets import load_dataset\n",
153+
"\n",
154+
"# Load a sample dataset\n",
155+
"philo_dataset = load_dataset(\"datastax/philosopher-quotes\")[\"train\"]\n",
156+
"print(\"An example entry:\")\n",
157+
"print(philo_dataset[16])"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 9,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"from langchain.schema import Document\n",
167+
"\n",
168+
"# Constructs a set of documents from your data. Documents can be used as inputs to your vector store.\n",
169+
"docs = []\n",
170+
"for entry in philo_dataset:\n",
171+
" metadata = {\"author\": entry[\"author\"]}\n",
172+
" if entry[\"tags\"]:\n",
173+
" # Add metadata tags to the metadata dictionary\n",
174+
" for tag in entry[\"tags\"].split(\";\"):\n",
175+
" metadata[tag] = \"y\"\n",
176+
" # Create a LangChain document with the quote and metadata tags\n",
177+
" doc = Document(page_content=entry[\"quote\"], metadata=metadata)\n",
178+
" docs.append(doc)"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 10,
184+
"metadata": {
185+
"nbmake": {
186+
"post_cell_execute": [
187+
"assert len(inserted_ids) > 0"
188+
]
189+
}
190+
},
191+
"outputs": [
192+
{
193+
"name": "stdout",
194+
"output_type": "stream",
195+
"text": [
196+
"\n",
197+
"Inserted 450 documents.\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"# Create embeddings by inserting your documents into the vector store.\n",
203+
"inserted_ids = vstore.add_documents(docs)\n",
204+
"print(f\"\\nInserted {len(inserted_ids)} documents.\")"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": null,
210+
"metadata": {},
211+
"outputs": [],
212+
"source": [
213+
"# Checks your collection to verify the documents are embedded.\n",
214+
"print(vstore.astra_db.collection(collection).find())"
215+
]
216+
},
217+
{
218+
"cell_type": "markdown",
219+
"metadata": {},
220+
"source": [
221+
"### Basic Retrieval\n",
222+
"\n",
223+
"Retrieve context from your vector database, and pass it to the model with a prompt."
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 12,
229+
"metadata": {},
230+
"outputs": [
231+
{
232+
"data": {
233+
"text/plain": [
234+
"'In the given context, philosophers are most concerned with truth and knowledge.'"
235+
]
236+
},
237+
"execution_count": 12,
238+
"metadata": {},
239+
"output_type": "execute_result"
240+
}
241+
],
242+
"source": [
243+
"from langchain.prompts import ChatPromptTemplate\n",
244+
"from langchain.chat_models import ChatOpenAI\n",
245+
"from langchain.schema.output_parser import StrOutputParser\n",
246+
"from langchain.schema.runnable import RunnablePassthrough\n",
247+
"\n",
248+
"retriever = vstore.as_retriever(search_kwargs={\"k\": 3})\n",
249+
"\n",
250+
"prompt_template = \"\"\"\n",
251+
"Answer the question based only on the supplied context. If you don't know the answer, say you don't know the answer.\n",
252+
"Context: {context}\n",
253+
"Question: {question}\n",
254+
"Your answer:\n",
255+
"\"\"\"\n",
256+
"prompt = ChatPromptTemplate.from_template(prompt_template)\n",
257+
"model = ChatOpenAI()\n",
258+
"\n",
259+
"chain = (\n",
260+
" {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
261+
" | prompt\n",
262+
" | model\n",
263+
" | StrOutputParser()\n",
264+
")\n",
265+
"\n",
266+
"chain.invoke(\"In the given context, what subject are philosophers most concerned with?\")"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"metadata": {},
273+
"outputs": [],
274+
"source": [
275+
"# Add your questions here!\n",
276+
"# chain.invoke(\"<your question>\")"
277+
]
278+
},
279+
{
280+
"cell_type": "markdown",
281+
"metadata": {},
282+
"source": [
283+
"You now have a fully functioning RAG pipeline! Note that there are several different ways to accomplish this, depending on your input data format, vector store, embedding, model, output type, and more. There are also more advanced RAG techniques that leverage new ingestion, retrieval, and generation patterns. \n",
284+
"\n",
285+
"RAG is a powerful solution used in tandem with the capabilities of LLMs. Check out our other examples for ideas on how you can build innovative solutions using RAGStack!"
286+
]
287+
}
288+
],
289+
"metadata": {
290+
"kernelspec": {
291+
"display_name": "Python 3",
292+
"language": "python",
293+
"name": "python3"
294+
},
295+
"language_info": {
296+
"codemirror_mode": {
297+
"name": "ipython",
298+
"version": 3
299+
},
300+
"file_extension": ".py",
301+
"mimetype": "text/x-python",
302+
"name": "python",
303+
"nbconvert_exporter": "python",
304+
"pygments_lexer": "ipython3",
305+
"version": "3.11.4"
306+
}
307+
},
308+
"nbformat": 4,
309+
"nbformat_minor": 2
310+
}

0 commit comments

Comments
 (0)