Skip to content

Commit 52fde46

Browse files
authored
[RAG-198] - Docs - LlamaParse (#281)
* initial-content * ci-security-scans-badge * cleanup * print-source-node * description * bump-nav
1 parent 4761b3f commit 52fde46

File tree

4 files changed

+273
-16
lines changed

4 files changed

+273
-16
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ image:https://github.com/datastax/ragstack-ai/actions/workflows/ci.yml/badge.svg
44
image:https://static.pepy.tech/badge/ragstack-ai/month[link="https://www.pepy.tech/projects/ragstack-ai"]
55
image:https://img.shields.io/badge/License-BSL-yellow.svg[link="https://github.com/datastax/ragstack-ai/blob/main/LICENSE.txt"]
66
image:https://img.shields.io/github/stars/datastax/ragstack-ai?style=social[link="https://star-history.com/#datastax/ragstack-ai"]
7-
image:https://github.com/datastax/ragstack-ai/actions/workflows/security-scans.yml/badge.svg[link=https://github.com/datastax/ragstack-ai/actions/workflows/security-scans.yml]
7+
image:https://github.com/datastax/ragstack-ai/actions/workflows/ci-security-scans.yml/badge.svg[link=https://github.com/datastax/ragstack-ai/actions/workflows/security-scans.yml]
88

99
https://www.datastax.com/products/ragstack[RAGStack^] is an out-of-the-box solution simplifying Retrieval Augmented Generation (RAG) in GenAI apps.
1010

docs/modules/ROOT/nav.adoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
* xref:intro-to-rag:evaluating.adoc[]
2222
* xref:intro-to-rag:advanced-rag.adoc[]
2323
24-
.Examples
24+
.RAGStack Examples
2525
* xref:examples:index.adoc[]
2626
* xref:examples:langchain_multimodal_gemini.adoc[]
2727
* xref:examples:nvidia_embeddings.adoc[]
2828
* xref:examples:hotels-app.adoc[]
2929
* xref:examples:mmr.adoc[]
30-
* xref:examples:qa-with-cassio.adoc[]
31-
* xref:examples:rag-with-cassio.adoc[]
3230
* xref:examples:langchain-evaluation.adoc[]
3331
* xref:examples:advanced-rag.adoc[]
3432
* xref:examples:flare.adoc[]
3533
* xref:examples:llama-astra.adoc[]
34+
* xref:examples:llama-parse-astra.adoc[]
35+
* xref:examples:qa-with-cassio.adoc[]
36+
* xref:examples:rag-with-cassio.adoc[]
3637
3738
.Tests
3839
* image:https://github.com/datastax/ragstack-ai/actions/workflows/ci-e2e-tests.yml/badge.svg[Status,link=https://github.com/datastax/ragstack-ai/actions/workflows/ci-e2e-tests.yml]
Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
= Examples
1+
= RAGStack Examples Index
22

33
This section contains examples of how to use RAGStack.
44
We're actively updating this section, so check back often!
55

6-
.RAGStack Examples
6+
<<langchain-astra,LangChain and Astra Vector DB>>
7+
8+
<<llama-astra,LlamaIndex and Astra Vector DB>>
9+
10+
<<langchain-cassio,LangChain and Cass-IO (Cassandra)>>
11+
12+
[[langchain-astra]]
13+
.LangChain and Astra Vector DB
714
[options="header"]
815
|===
916
| Description | Colab | Documentation
@@ -12,8 +19,7 @@ We're actively updating this section, so check back often!
1219
a| 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/langchain_multimodal_gemini.ipynb"]
1320
| xref:langchain_multimodal_gemini.adoc[]
1421

15-
| Build a simple RAG pipeline using
16-
https://catalog.ngc.nvidia.com[NVIDIA AI Foundation Models]{external-link-icon}.
22+
| Build a simple RAG pipeline using https://catalog.ngc.nvidia.com[NVIDIA AI Foundation Models]{external-link-icon}.
1723
a| 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/nvidia.ipynb"]
1824
| xref:nvidia_embeddings.adoc[]
1925

@@ -25,14 +31,6 @@ a| image::https://gitpod.io/button/open-in-gitpod.svg[align="left",110,link="htt
2531
a| image::https://colab.research.google.com/assets/colab-badge.svg[align="left",link="https://colab.research.google.com/github/CassioML/cassio-website/blob/main/docs/frameworks/langchain/.colab/colab_qa-maximal-marginal-relevance.ipynb"]
2632
| xref:mmr.adoc[]
2733

28-
| Implement a generative Q&A over your own documentation with Astra Vector Search, OpenAI, and CassIO.
29-
a| 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/QA_with_cassio.ipynb"]
30-
| xref:qa-with-cassio.adoc[]
31-
32-
| Store external or proprietary data in Astra DB and query it to provide more up-to-date LLM responses.
33-
a| 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/RAG_with_cassio.ipynb"]
34-
| xref:rag-with-cassio.adoc[]
35-
3634
| Evaluate a RAG pipeline using LangChain's QA Evaluator.
3735
a| 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/langchain_evaluation.ipynb"]
3836
| xref:langchain-evaluation.adoc[]
@@ -45,9 +43,37 @@ a| image::https://colab.research.google.com/assets/colab-badge.svg[align="left",
4543
a| 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/FLARE.ipynb"]
4644
| xref:flare.adoc[]
4745

46+
|===
47+
48+
[[llama-astra]]
49+
.LlamaIndex Examples
50+
[options="header"]
51+
|===
52+
| Description | Colab | Documentation
53+
4854
| Build a simple RAG pipeline using LlamaIndex and AstraDB.
4955
a| 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-astra.ipynb"]
5056
| xref:llama-astra.adoc[]
57+
58+
| Build a simple RAG pipeline using LlamaParse and AstraDB.
59+
a| 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"]
60+
| xref:llama-parse-astra.adoc[]
61+
62+
|===
63+
64+
[[langchain-cassio]]
65+
.LangChain and Cass-IO (Cassandra)
66+
[options="header"]
5167
|===
68+
| Description | Colab | Documentation
69+
70+
| Implement a generative Q&A over your own documentation with Astra Vector Search, OpenAI, and CassIO.
71+
a| 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/QA_with_cassio.ipynb"]
72+
| xref:qa-with-cassio.adoc[]
73+
74+
| Store external or proprietary data in Astra DB and query it to provide more up-to-date LLM responses.
75+
a| 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/RAG_with_cassio.ipynb"]
76+
| xref:rag-with-cassio.adoc[]
5277

78+
|===
5379

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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

Comments
 (0)