From 52fde468baebe3d9c9b7846ca58e1622639f4cbd Mon Sep 17 00:00:00 2001 From: Mendon Kissling <59585235+mendonk@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:32:03 -0500 Subject: [PATCH] [RAG-198] - Docs - LlamaParse (#281) * initial-content * ci-security-scans-badge * cleanup * print-source-node * description * bump-nav --- README.adoc | 2 +- docs/modules/ROOT/nav.adoc | 7 +- docs/modules/examples/pages/index.adoc | 50 +++- .../examples/pages/llama-parse-astra.adoc | 230 ++++++++++++++++++ 4 files changed, 273 insertions(+), 16 deletions(-) create mode 100644 docs/modules/examples/pages/llama-parse-astra.adoc diff --git a/README.adoc b/README.adoc index af0e9737b..7a9d4ad28 100644 --- a/README.adoc +++ b/README.adoc @@ -4,7 +4,7 @@ image:https://github.com/datastax/ragstack-ai/actions/workflows/ci.yml/badge.svg image:https://static.pepy.tech/badge/ragstack-ai/month[link="https://www.pepy.tech/projects/ragstack-ai"] image:https://img.shields.io/badge/License-BSL-yellow.svg[link="https://github.com/datastax/ragstack-ai/blob/main/LICENSE.txt"] image:https://img.shields.io/github/stars/datastax/ragstack-ai?style=social[link="https://star-history.com/#datastax/ragstack-ai"] -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] +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] https://www.datastax.com/products/ragstack[RAGStack^] is an out-of-the-box solution simplifying Retrieval Augmented Generation (RAG) in GenAI apps. diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index cb75627d7..d838f9974 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -21,18 +21,19 @@ * xref:intro-to-rag:evaluating.adoc[] * xref:intro-to-rag:advanced-rag.adoc[] -.Examples +.RAGStack Examples * xref:examples:index.adoc[] * xref:examples:langchain_multimodal_gemini.adoc[] * xref:examples:nvidia_embeddings.adoc[] * xref:examples:hotels-app.adoc[] * xref:examples:mmr.adoc[] -* xref:examples:qa-with-cassio.adoc[] -* xref:examples:rag-with-cassio.adoc[] * xref:examples:langchain-evaluation.adoc[] * xref:examples:advanced-rag.adoc[] * xref:examples:flare.adoc[] * xref:examples:llama-astra.adoc[] +* xref:examples:llama-parse-astra.adoc[] +* xref:examples:qa-with-cassio.adoc[] +* xref:examples:rag-with-cassio.adoc[] .Tests * 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] diff --git a/docs/modules/examples/pages/index.adoc b/docs/modules/examples/pages/index.adoc index eb4817cf3..be440b220 100644 --- a/docs/modules/examples/pages/index.adoc +++ b/docs/modules/examples/pages/index.adoc @@ -1,9 +1,16 @@ -= Examples += RAGStack Examples Index This section contains examples of how to use RAGStack. We're actively updating this section, so check back often! -.RAGStack Examples +<> + +<> + +<> + +[[langchain-astra]] +.LangChain and Astra Vector DB [options="header"] |=== | Description | Colab | Documentation @@ -12,8 +19,7 @@ We're actively updating this section, so check back often! 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"] | xref:langchain_multimodal_gemini.adoc[] -| Build a simple RAG pipeline using -https://catalog.ngc.nvidia.com[NVIDIA AI Foundation Models]{external-link-icon}. +| Build a simple RAG pipeline using https://catalog.ngc.nvidia.com[NVIDIA AI Foundation Models]{external-link-icon}. 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"] | xref:nvidia_embeddings.adoc[] @@ -25,14 +31,6 @@ a| image::https://gitpod.io/button/open-in-gitpod.svg[align="left",110,link="htt 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"] | xref:mmr.adoc[] -| Implement a generative Q&A over your own documentation with Astra Vector Search, OpenAI, and CassIO. -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"] -| xref:qa-with-cassio.adoc[] - -| Store external or proprietary data in Astra DB and query it to provide more up-to-date LLM responses. -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"] -| xref:rag-with-cassio.adoc[] - | Evaluate a RAG pipeline using LangChain's QA Evaluator. 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"] | xref:langchain-evaluation.adoc[] @@ -45,9 +43,37 @@ a| image::https://colab.research.google.com/assets/colab-badge.svg[align="left", 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"] | xref:flare.adoc[] +|=== + +[[llama-astra]] +.LlamaIndex Examples +[options="header"] +|=== +| Description | Colab | Documentation + | Build a simple RAG pipeline using LlamaIndex and AstraDB. 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"] | xref:llama-astra.adoc[] + +| Build a simple RAG pipeline using LlamaParse and AstraDB. +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"] +| xref:llama-parse-astra.adoc[] + +|=== + +[[langchain-cassio]] +.LangChain and Cass-IO (Cassandra) +[options="header"] |=== +| Description | Colab | Documentation + +| Implement a generative Q&A over your own documentation with Astra Vector Search, OpenAI, and CassIO. +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"] +| xref:qa-with-cassio.adoc[] + +| Store external or proprietary data in Astra DB and query it to provide more up-to-date LLM responses. +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"] +| xref:rag-with-cassio.adoc[] +|=== diff --git a/docs/modules/examples/pages/llama-parse-astra.adoc b/docs/modules/examples/pages/llama-parse-astra.adoc new file mode 100644 index 000000000..7310b9326 --- /dev/null +++ b/docs/modules/examples/pages/llama-parse-astra.adoc @@ -0,0 +1,230 @@ += RAG with LlamaParse and AstraDB + +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"] + +Build a RAG pipeline with RAGStack, AstraDB, and LlamaIndex. + +This example demonstrates loading and parsing a PDF document with LLamaParse into an Astra DB vector store, then querying the index with LlamaIndex. + +== Prerequisites + +You will need a vector-enabled Astra database. + +* Create an https://docs.datastax.com/en/astra-serverless/docs/getting-started/create-db-choices.html[Astra +vector database]. +* Within your database, create an https://docs.datastax.com/en/astra-serverless/docs/manage/org/manage-tokens.html[Astra +DB Access Token] with Database Administrator permissions. +* Get your Astra DB Endpoint: +** `+https://-.apps.astra.datastax.com+` +* Create an API key at https://cloud.llamaindex.ai/[LlamaIndex.ai]. +Install the following dependencies: +[source,python] +---- +pip install ragstack-ai llama-parse python-dotenv +---- +See the https://docs.datastax.com/en/ragstack/docs/prerequisites.html[Prerequisites] page for more details. + +== Export database connection details + +Create a `.env` file in your application with the following environment variables: +[source,bash] +---- +LLAMA_CLOUD_API_KEY=llx-... +ASTRA_DB_API_ENDPOINT=https://bbe07f45-8ab4-4d81-aa7d-7f58dbed3ead-us-east-1.apps.astra.datastax.com +ASTRA_DB_APPLICATION_TOKEN=AstraCS:... +OPENAI_API_KEY=sk-... +---- + +If you're using Google Colab, you'll be prompted for these values in the Colab environment. + +See the https://docs.datastax.com/en/ragstack/docs/prerequisites.html[Prerequisites] page for more details. + +== Create RAG pipeline + +. Import dependencies and load environment variables. ++ +[source,python] +---- +import os +import requests +from dotenv import load_dotenv +from llama_parse import LlamaParse +from llama_index.vector_stores import AstraDBVectorStore +from llama_index.node_parser import SimpleNodeParser +from llama_index import OpenAIEmbedding, VectorStoreIndex, StorageContext, ServiceContext +from llama_index.llms import OpenAI + +load_dotenv() + +llama_cloud_api_key = os.getenv("LLAMA_CLOUD_API_KEY") +api_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") +token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") +openai_api_key = os.getenv("OPENAI_API_KEY") +---- ++ +. Download a PDF about attention mechanisms in transformer model architectures. ++ +[source,python] +---- +url = "https://arxiv.org/pdf/1706.03762.pdf" +file_path = "./attention.pdf" + +response = requests.get(url) +if response.status_code == 200: + with open(file_path, "wb") as file: + file.write(response.content) + print("Download complete.") +else: + print("Error downloading the file.") +---- ++ +. Load the downloaded PDF with LlamaParse as a text Document for indexing. +LlamaParse also supports Markdown-type Documents with `(result_type=markdown)`. ++ +[source,python] +---- +documents = LlamaParse(result_type="text").load_data(file_path) +print(documents[0].get_content()[10000:11000]) +---- ++ +. Create an AstraDB vector store instance. ++ +[source,python] +---- +astra_db_store = AstraDBVectorStore( + token=token, + api_endpoint=api_endpoint, + collection_name="astra_v_table_llamaparse", + embedding_dimension=1536 +) +---- ++ +. Parse Documents into nodes and set up storage and service contexts to use AstraDB and OpenAI. ++ +[source,python] +---- +node_parser = SimpleNodeParser() +nodes = node_parser.get_nodes_from_documents(documents) +print(nodes[0].get_content()) + +storage_context = StorageContext.from_defaults(vector_store=astra_db_store) +service_context = ServiceContext.from_defaults( + llm=OpenAI(model="gpt-4"), + embed_model=OpenAIEmbedding(), + chunk_size=512, +) +---- ++ +. Create a vector store index and query engine from your nodes and contexts. ++ +[source,python] +---- +index = VectorStoreIndex(nodes=nodes, storage_context=storage_context) +query_engine = index.as_query_engine(similarity_top_k=15, service_context=service_context) +---- + +== Execute a query + +. Query the Astra vector store for an example with expected context - this query should return a relevant response. ++ +[source,python] +---- +query = "What is Multi-Head Attention also known as?" +response_1 = query_engine.query(query) +print("\n***********New LlamaParse+ Basic Query Engine***********") +print(response_1) +---- ++ +. Query the Astra vector store for an example with expected lack of context. +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. ++ +[source,python] +---- +query = "What is the color of the sky?" +response_1 = query_engine.query(query) +print("\n***********New LlamaParse+ Basic Query Engine***********") +print(response_1) +---- + +== Complete code + +.Python +[%collapsible%open] +==== +[source,python] +---- +import os +import requests +from dotenv import load_dotenv +from llama_parse import LlamaParse +from llama_index.vector_stores import AstraDBVectorStore +from llama_index.node_parser import SimpleNodeParser +from llama_index import OpenAIEmbedding, VectorStoreIndex, StorageContext, ServiceContext +from llama_index.llms import OpenAI + +# Load environment variables +load_dotenv() + +# Get all required API keys and parameters +llama_cloud_api_key = os.getenv("LLAMA_CLOUD_API_KEY") +api_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") +token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") +openai_api_key = os.getenv("OPENAI_API_KEY") + +# Download a PDF for indexing +url = "https://arxiv.org/pdf/1706.03762.pdf" +file_path = "./attention.pdf" + +response = requests.get(url) +if response.status_code == 200: + with open(file_path, "wb") as file: + file.write(response.content) + print("Download complete.") +else: + print("Error downloading the file.") + +# Load and parse the document +documents = LlamaParse(result_type="text").load_data(file_path) + +# Output a snippet from the parsed document for verification +print(documents[0].get_content()[10000:11000]) + +# Setup for storing in AstraDB +astra_db_store = AstraDBVectorStore( + token=token, + api_endpoint=api_endpoint, + collection_name="astra_v_table_llamaparse", + embedding_dimension=1536 +) + +# Parse nodes from documents and output a snippet for verification +node_parser = SimpleNodeParser() +nodes = node_parser.get_nodes_from_documents(documents) +print(nodes[0].get_content()) + +# Setup storage and service contexts +storage_context = StorageContext.from_defaults(vector_store=astra_db_store) +service_context = ServiceContext.from_defaults( + llm=OpenAI(model="gpt-4"), + embed_model=OpenAIEmbedding(), + chunk_size=512, +) + +# Indexing and query engine setup +index = VectorStoreIndex(nodes=nodes, storage_context=storage_context) +query_engine = index.as_query_engine(similarity_top_k=15, service_context=service_context) + +# Execute a query +query = "What is Multi-Head Attention also known as?" +response_1 = query_engine.query(query) +print("\n***********New LlamaParse+ Basic Query Engine***********") +print(response_1) + +# Query for an example with expected lack of context +query = "What is the color of the sky?" +response_1 = query_engine.query(query) +print("\n***********New LlamaParse+ Basic Query Engine***********") +print(response_1) +---- +==== +