|
| 1 | +import ThemedImage from "@theme/ThemedImage"; |
| 2 | +import useBaseUrl from "@docusaurus/useBaseUrl"; |
| 3 | +import ZoomableImage from "/src/theme/ZoomableImage.js"; |
| 4 | +import ReactPlayer from "react-player"; |
| 5 | +import Admonition from "@theme/Admonition"; |
| 6 | + |
| 7 | +# Vector store RAG |
| 8 | + |
| 9 | +Retrieval Augmented Generation, or RAG, is a pattern for training LLMs on your data and querying it. |
| 10 | + |
| 11 | +RAG is backed by a **vector store**, a vector database which stores embeddings of the ingested data. |
| 12 | + |
| 13 | +This enables **vector search**, a more powerful and context-aware search. |
| 14 | + |
| 15 | +We've chosen [Astra DB](https://astra.datastax.com/signup?utm_source=langflow-pre-release&utm_medium=referral&utm_campaign=langflow-announcement&utm_content=create-a-free-astra-db-account) as the vector database for this starter project, but you can follow along with any of Langflow's vector database options. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +<Admonition type="info"> |
| 20 | + Langflow v1.0 alpha is also available in [HuggingFace Spaces](https://huggingface.co/spaces/Langflow/Langflow-Preview?duplicate=true). Try it out or follow the instructions [here](../getting-started/huggingface-spaces) to install it locally. |
| 21 | +</Admonition> |
| 22 | + |
| 23 | +* [Langflow installed](../getting-started/install-langflow.mdx) |
| 24 | + |
| 25 | +* [OpenAI API key](https://platform.openai.com) |
| 26 | + |
| 27 | +* [An Astra DB vector database created](https://docs.datastax.com/en/astra-db-serverless/get-started/quickstart.html) with: |
| 28 | + * Application token (`AstraCS:WSnyFUhRxsrg…`) |
| 29 | + * API endpoint (`https://ASTRA_DB_ID-ASTRA_DB_REGION.apps.astra.datastax.com`) |
| 30 | + |
| 31 | +## Create the vector store RAG project |
| 32 | + |
| 33 | +1. From the Langflow dashboard, click **New Project**. |
| 34 | +2. Select **Vector Store RAG**. |
| 35 | +3. The **Vector Store RAG** flow is created. |
| 36 | + |
| 37 | +<ZoomableImage |
| 38 | + alt="Docusaurus themed image" |
| 39 | + sources={{ |
| 40 | + light: "img/vector-store-rag.png", |
| 41 | + dark: "img/vector-store-rag.png", |
| 42 | + }} |
| 43 | + style={{ width: "80%", margin: "20px auto" }} |
| 44 | +/> |
| 45 | + |
| 46 | +The vector store RAG flow is built of two separate flows. |
| 47 | + |
| 48 | +The **ingestion** flow (bottom of the screen) populates the vector store with data from a local file. |
| 49 | +It ingests data from a file (**File**), splits it into chunks (**Recursive Character Text Splitter**), indexes it in Astra DB (**Astra DB**), and computes embeddings for the chunks (**OpenAI Embeddings**). |
| 50 | +This forms a "brain" for the query flow. |
| 51 | + |
| 52 | +The **query** flow (top of the screen) allows users to chat with the embedded vector store data. It's a little more complex: |
| 53 | + |
| 54 | +* **Chat Input** component defines where to put the user input coming from the Playground. |
| 55 | +* **OpenAI Embeddings** component generates embeddings from the user input. |
| 56 | +* **Astra DB Search** component retrieves the most relevant Records from the Astra DB database. |
| 57 | +* **Text Output** component turns the Records into Text by concatenating them and also displays it in the Playground. |
| 58 | +* **Prompt** component takes in the user input and the retrieved Records as text and builds a prompt for the OpenAI model. |
| 59 | +* **OpenAI** component generates a response to the prompt. |
| 60 | +* **Chat Output** component displays the response in the Playground. |
| 61 | + |
| 62 | +4. To create an environment variable for the **OpenAI** component, in the **OpenAI API Key** field, click the **Globe** button, and then click **Add New Variable**. |
| 63 | + 1. In the **Variable Name** field, enter `openai_api_key`. |
| 64 | + 2. In the **Value** field, paste your OpenAI API Key (`sk-...`). |
| 65 | + 3. Click **Save Variable**. |
| 66 | + |
| 67 | +4. To create environment variables for the **Astra DB** and **Astra DB Search** components: |
| 68 | + 1. In the **Token** field, click the **Globe** button, and then click **Add New Variable**. |
| 69 | + 2. In the **Variable Name** field, enter `astra_token`. |
| 70 | + 3. In the **Value** field, paste your Astra application token (`AstraCS:WSnyFUhRxsrg…`). |
| 71 | + 4. Click **Save Variable**. |
| 72 | + 5. Repeat the above steps for the **API Endpoint** field, pasting your Astra API Endpoint instead (`https://ASTRA_DB_ID-ASTRA_DB_REGION.apps.astra.datastax.com`). |
| 73 | + 6. Add the global variable to both the **Astra DB** and **Astra DB Search** components. |
| 74 | + |
| 75 | +## Run the vector store RAG flow |
| 76 | + |
| 77 | +1. Click the **Playground** button. |
| 78 | +The **Playground** opens, where you can chat with your data. |
| 79 | +2. Type a message and press Enter. (Try something like "What topics do you know about?") |
| 80 | +3. The bot will respond with a summary of the data you've embedded. |
| 81 | + |
| 82 | +For example, we embedded a PDF of an engine maintenance manual and asked, "How do I change the oil?" |
| 83 | +The bot responds: |
| 84 | +``` |
| 85 | +To change the oil in the engine, follow these steps: |
| 86 | +
|
| 87 | +Make sure the engine is turned off and cool before starting. |
| 88 | +
|
| 89 | +Locate the oil drain plug on the bottom of the engine. |
| 90 | +
|
| 91 | +Place a drain pan underneath the oil drain plug to catch the old oil... |
| 92 | +``` |
| 93 | + |
| 94 | +We can also get more specific: |
| 95 | + |
| 96 | +``` |
| 97 | +User |
| 98 | +What size wrench should I use to remove the oil drain cap? |
| 99 | +
|
| 100 | +AI |
| 101 | +You should use a 3/8 inch wrench to remove the oil drain cap. |
| 102 | +``` |
| 103 | + |
| 104 | +This is the size the engine manual lists as well. This confirms our flow works, because the query returns the unique knowledge we embedded from the Astra vector store. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments