|
| 1 | += RAG with LlamaParse and AstraDB |
| 2 | + |
| 3 | +image::https://colab.research.google.com/assets/colab-badge.svg[align="left",link="https://colab.research.google.com/github/datastax/ragstack-ai/blob/main/examples/notebooks/llama-parse-astra.ipynb"] |
| 4 | + |
| 5 | +Build a RAG pipeline with RAGStack, AstraDB, and LlamaIndex. |
| 6 | + |
| 7 | +This example demonstrates loading and parsing a PDF document with LLamaParse into an Astra DB vector store, then querying the index with LlamaIndex. |
| 8 | + |
| 9 | +== Prerequisites |
| 10 | + |
| 11 | +You will need a vector-enabled Astra database. |
| 12 | + |
| 13 | +* Create an https://docs.datastax.com/en/astra-serverless/docs/getting-started/create-db-choices.html[Astra |
| 14 | +vector database]. |
| 15 | +* Within your database, create an https://docs.datastax.com/en/astra-serverless/docs/manage/org/manage-tokens.html[Astra |
| 16 | +DB Access Token] with Database Administrator permissions. |
| 17 | +* Get your Astra DB Endpoint: |
| 18 | +** `+https://<ASTRA_DB_ID>-<ASTRA_DB_REGION>.apps.astra.datastax.com+` |
| 19 | +* Create an API key at https://cloud.llamaindex.ai/[LlamaIndex.ai]. |
| 20 | +Install the following dependencies: |
| 21 | +[source,python] |
| 22 | +---- |
| 23 | +pip install ragstack-ai llama-parse python-dotenv |
| 24 | +---- |
| 25 | +See the https://docs.datastax.com/en/ragstack/docs/prerequisites.html[Prerequisites] page for more details. |
| 26 | + |
| 27 | +== Export database connection details |
| 28 | + |
| 29 | +Create a `.env` file in your application with the following environment variables: |
| 30 | +[source,bash] |
| 31 | +---- |
| 32 | +LLAMA_CLOUD_API_KEY=llx-... |
| 33 | +ASTRA_DB_API_ENDPOINT=https://bbe07f45-8ab4-4d81-aa7d-7f58dbed3ead-us-east-1.apps.astra.datastax.com |
| 34 | +ASTRA_DB_APPLICATION_TOKEN=AstraCS:... |
| 35 | +OPENAI_API_KEY=sk-... |
| 36 | +---- |
| 37 | + |
| 38 | +If you're using Google Colab, you'll be prompted for these values in the Colab environment. |
| 39 | + |
| 40 | +See the https://docs.datastax.com/en/ragstack/docs/prerequisites.html[Prerequisites] page for more details. |
| 41 | + |
| 42 | +== Create RAG pipeline |
| 43 | + |
| 44 | +. Import dependencies and load environment variables. |
| 45 | ++ |
| 46 | +[source,python] |
| 47 | +---- |
| 48 | +import os |
| 49 | +import requests |
| 50 | +from dotenv import load_dotenv |
| 51 | +from llama_parse import LlamaParse |
| 52 | +from llama_index.vector_stores import AstraDBVectorStore |
| 53 | +from llama_index.node_parser import SimpleNodeParser |
| 54 | +from llama_index import OpenAIEmbedding, VectorStoreIndex, StorageContext, ServiceContext |
| 55 | +from llama_index.llms import OpenAI |
| 56 | +
|
| 57 | +load_dotenv() |
| 58 | +
|
| 59 | +llama_cloud_api_key = os.getenv("LLAMA_CLOUD_API_KEY") |
| 60 | +api_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") |
| 61 | +token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") |
| 62 | +openai_api_key = os.getenv("OPENAI_API_KEY") |
| 63 | +---- |
| 64 | ++ |
| 65 | +. Download a PDF about attention mechanisms in transformer model architectures. |
| 66 | ++ |
| 67 | +[source,python] |
| 68 | +---- |
| 69 | +url = "https://arxiv.org/pdf/1706.03762.pdf" |
| 70 | +file_path = "./attention.pdf" |
| 71 | +
|
| 72 | +response = requests.get(url) |
| 73 | +if response.status_code == 200: |
| 74 | + with open(file_path, "wb") as file: |
| 75 | + file.write(response.content) |
| 76 | + print("Download complete.") |
| 77 | +else: |
| 78 | + print("Error downloading the file.") |
| 79 | +---- |
| 80 | ++ |
| 81 | +. Load the downloaded PDF with LlamaParse as a text Document for indexing. |
| 82 | +LlamaParse also supports Markdown-type Documents with `(result_type=markdown)`. |
| 83 | ++ |
| 84 | +[source,python] |
| 85 | +---- |
| 86 | +documents = LlamaParse(result_type="text").load_data(file_path) |
| 87 | +print(documents[0].get_content()[10000:11000]) |
| 88 | +---- |
| 89 | ++ |
| 90 | +. Create an AstraDB vector store instance. |
| 91 | ++ |
| 92 | +[source,python] |
| 93 | +---- |
| 94 | +astra_db_store = AstraDBVectorStore( |
| 95 | + token=token, |
| 96 | + api_endpoint=api_endpoint, |
| 97 | + collection_name="astra_v_table_llamaparse", |
| 98 | + embedding_dimension=1536 |
| 99 | +) |
| 100 | +---- |
| 101 | ++ |
| 102 | +. Parse Documents into nodes and set up storage and service contexts to use AstraDB and OpenAI. |
| 103 | ++ |
| 104 | +[source,python] |
| 105 | +---- |
| 106 | +node_parser = SimpleNodeParser() |
| 107 | +nodes = node_parser.get_nodes_from_documents(documents) |
| 108 | +print(nodes[0].get_content()) |
| 109 | +
|
| 110 | +storage_context = StorageContext.from_defaults(vector_store=astra_db_store) |
| 111 | +service_context = ServiceContext.from_defaults( |
| 112 | + llm=OpenAI(model="gpt-4"), |
| 113 | + embed_model=OpenAIEmbedding(), |
| 114 | + chunk_size=512, |
| 115 | +) |
| 116 | +---- |
| 117 | ++ |
| 118 | +. Create a vector store index and query engine from your nodes and contexts. |
| 119 | ++ |
| 120 | +[source,python] |
| 121 | +---- |
| 122 | +index = VectorStoreIndex(nodes=nodes, storage_context=storage_context) |
| 123 | +query_engine = index.as_query_engine(similarity_top_k=15, service_context=service_context) |
| 124 | +---- |
| 125 | + |
| 126 | +== Execute a query |
| 127 | + |
| 128 | +. Query the Astra vector store for an example with expected context - this query should return a relevant response. |
| 129 | ++ |
| 130 | +[source,python] |
| 131 | +---- |
| 132 | +query = "What is Multi-Head Attention also known as?" |
| 133 | +response_1 = query_engine.query(query) |
| 134 | +print("\n***********New LlamaParse+ Basic Query Engine***********") |
| 135 | +print(response_1) |
| 136 | +---- |
| 137 | ++ |
| 138 | +. Query the Astra vector store for an example with expected lack of context. |
| 139 | +This query should return `The context does not provide information about the color of the sky` because your document does not contain information about the color of the sky. |
| 140 | ++ |
| 141 | +[source,python] |
| 142 | +---- |
| 143 | +query = "What is the color of the sky?" |
| 144 | +response_1 = query_engine.query(query) |
| 145 | +print("\n***********New LlamaParse+ Basic Query Engine***********") |
| 146 | +print(response_1) |
| 147 | +---- |
| 148 | + |
| 149 | +== Complete code |
| 150 | + |
| 151 | +.Python |
| 152 | +[%collapsible%open] |
| 153 | +==== |
| 154 | +[source,python] |
| 155 | +---- |
| 156 | +import os |
| 157 | +import requests |
| 158 | +from dotenv import load_dotenv |
| 159 | +from llama_parse import LlamaParse |
| 160 | +from llama_index.vector_stores import AstraDBVectorStore |
| 161 | +from llama_index.node_parser import SimpleNodeParser |
| 162 | +from llama_index import OpenAIEmbedding, VectorStoreIndex, StorageContext, ServiceContext |
| 163 | +from llama_index.llms import OpenAI |
| 164 | +
|
| 165 | +# Load environment variables |
| 166 | +load_dotenv() |
| 167 | +
|
| 168 | +# Get all required API keys and parameters |
| 169 | +llama_cloud_api_key = os.getenv("LLAMA_CLOUD_API_KEY") |
| 170 | +api_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") |
| 171 | +token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") |
| 172 | +openai_api_key = os.getenv("OPENAI_API_KEY") |
| 173 | +
|
| 174 | +# Download a PDF for indexing |
| 175 | +url = "https://arxiv.org/pdf/1706.03762.pdf" |
| 176 | +file_path = "./attention.pdf" |
| 177 | +
|
| 178 | +response = requests.get(url) |
| 179 | +if response.status_code == 200: |
| 180 | + with open(file_path, "wb") as file: |
| 181 | + file.write(response.content) |
| 182 | + print("Download complete.") |
| 183 | +else: |
| 184 | + print("Error downloading the file.") |
| 185 | +
|
| 186 | +# Load and parse the document |
| 187 | +documents = LlamaParse(result_type="text").load_data(file_path) |
| 188 | +
|
| 189 | +# Output a snippet from the parsed document for verification |
| 190 | +print(documents[0].get_content()[10000:11000]) |
| 191 | +
|
| 192 | +# Setup for storing in AstraDB |
| 193 | +astra_db_store = AstraDBVectorStore( |
| 194 | + token=token, |
| 195 | + api_endpoint=api_endpoint, |
| 196 | + collection_name="astra_v_table_llamaparse", |
| 197 | + embedding_dimension=1536 |
| 198 | +) |
| 199 | +
|
| 200 | +# Parse nodes from documents and output a snippet for verification |
| 201 | +node_parser = SimpleNodeParser() |
| 202 | +nodes = node_parser.get_nodes_from_documents(documents) |
| 203 | +print(nodes[0].get_content()) |
| 204 | +
|
| 205 | +# Setup storage and service contexts |
| 206 | +storage_context = StorageContext.from_defaults(vector_store=astra_db_store) |
| 207 | +service_context = ServiceContext.from_defaults( |
| 208 | + llm=OpenAI(model="gpt-4"), |
| 209 | + embed_model=OpenAIEmbedding(), |
| 210 | + chunk_size=512, |
| 211 | +) |
| 212 | +
|
| 213 | +# Indexing and query engine setup |
| 214 | +index = VectorStoreIndex(nodes=nodes, storage_context=storage_context) |
| 215 | +query_engine = index.as_query_engine(similarity_top_k=15, service_context=service_context) |
| 216 | +
|
| 217 | +# Execute a query |
| 218 | +query = "What is Multi-Head Attention also known as?" |
| 219 | +response_1 = query_engine.query(query) |
| 220 | +print("\n***********New LlamaParse+ Basic Query Engine***********") |
| 221 | +print(response_1) |
| 222 | +
|
| 223 | +# Query for an example with expected lack of context |
| 224 | +query = "What is the color of the sky?" |
| 225 | +response_1 = query_engine.query(query) |
| 226 | +print("\n***********New LlamaParse+ Basic Query Engine***********") |
| 227 | +print(response_1) |
| 228 | +---- |
| 229 | +==== |
| 230 | + |
0 commit comments