|
| 1 | += Quickstart with RAGStack for TS |
| 2 | + |
| 3 | +This quickstart demonstrates a basic RAG pattern using RAGStack TS and the vector-enabled {db-serverless} database to retrieve context and pass it to a language model for generation. |
| 4 | + |
| 5 | +1. <<Construct information base>> |
| 6 | +2. <<Basic retrieval>> |
| 7 | +3. <<Generation with augmented context>> |
| 8 | +
|
| 9 | +== Setup |
| 10 | + |
| 11 | +RAGStack TS includes all the standard libraries you need for the RAG pattern, including the vector database, embeddings pipeline, and retrieval. |
| 12 | + |
| 13 | +. Create a new project using NPM or Yarn: |
| 14 | ++ |
| 15 | +[tabs] |
| 16 | +====== |
| 17 | +NPM:: |
| 18 | ++ |
| 19 | +[source,bash] |
| 20 | +---- |
| 21 | +npm init |
| 22 | +---- |
| 23 | +
|
| 24 | +Yarn:: |
| 25 | ++ |
| 26 | +[source,console] |
| 27 | +---- |
| 28 | +yarn init |
| 29 | +---- |
| 30 | +====== |
| 31 | + |
| 32 | +. Then add the RAGStack package via the CLI: |
| 33 | ++ |
| 34 | +[tabs] |
| 35 | +====== |
| 36 | +NPM:: |
| 37 | ++ |
| 38 | +[source,bash] |
| 39 | +---- |
| 40 | +npx @datastax/ragstack-ai-ts install --use-npm |
| 41 | +---- |
| 42 | +
|
| 43 | +Yarn:: |
| 44 | ++ |
| 45 | +[source,console] |
| 46 | +---- |
| 47 | +npx @datastax/ragstack-ai-ts install --use-yarn |
| 48 | +---- |
| 49 | +====== |
| 50 | ++ |
| 51 | +. Set the AstraDB vector credentials. If you don't have a vector database, create one at https://astra.datastax.com/. |
| 52 | ++ |
| 53 | +[source,bash] |
| 54 | +---- |
| 55 | +export ASTRA_DB_APPLICATION_TOKEN=AstraCS:xx |
| 56 | +export ASTRA_DB_API_ENDPOINT=https://xx.apps.astra.datastax.com |
| 57 | +---- |
| 58 | +The {db-serverless} application token is associated automatically with the Database Administrator permission. An auth token example: `AstraCS:WSnyFUhRxsrg...`). |
| 59 | ++ |
| 60 | +Both the endpoint and the token are available in the {astra-ui}. |
| 61 | ++ |
| 62 | +. Create an OpenAI key at https://platform.openai.com/ and set it as an environment variable: |
| 63 | ++ |
| 64 | +[source,bash] |
| 65 | +---- |
| 66 | +export OPENAI_API_TOKEN=sk-xx |
| 67 | +---- |
| 68 | + |
| 69 | +== RAG workflow |
| 70 | + |
| 71 | +With your environment set up, you're ready to create a RAG workflow in Javascript. |
| 72 | +Create a new file, `index.js`, and copy the following code: |
| 73 | + |
| 74 | +[source,javascript] |
| 75 | +---- |
| 76 | +const { OpenAIEmbeddings, ChatOpenAI } = require("@langchain/openai") |
| 77 | +const { AstraDBVectorStore } = require("@langchain/community/vectorstores/astradb") |
| 78 | +const { ChatPromptTemplate } = require("@langchain/core/prompts") |
| 79 | +const { RunnableSequence, RunnablePassthrough } = require("@langchain/core/runnables") |
| 80 | +const { StringOutputParser } = require("@langchain/core/output_parsers") |
| 81 | +
|
| 82 | +
|
| 83 | +async function main() { |
| 84 | + // create the embeddings object with the OpenAI API key |
| 85 | + const embeddings = new OpenAIEmbeddings() |
| 86 | +
|
| 87 | + // AstraDB connection parameters |
| 88 | + const astra = { |
| 89 | + token: process.env.ASTRA_DB_APPLICATION_TOKEN, |
| 90 | + endpoint: process.env.ASTRA_DB_API_ENDPOINT, |
| 91 | + collection: "demo", |
| 92 | + collectionOptions: { |
| 93 | + vector: { |
| 94 | + dimension: 1536, /** 1536 for OpenAI embeddings */ |
| 95 | + metric: "cosine", |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | +
|
| 100 | + /** Index some text into the Astra Vector Store */ |
| 101 | +
|
| 102 | + const vectorStore = await AstraDBVectorStore.fromTexts( |
| 103 | + [ |
| 104 | + "RAGStack is a framework for building RAG applications", |
| 105 | + "RAGStack has first-class support for AstraDB and Cassandra", |
| 106 | + ], |
| 107 | + [{source: "documentation"}, {source: "documentation"}], |
| 108 | + embeddings, |
| 109 | + astra |
| 110 | + ) |
| 111 | + /** Now prepare the retrieval */ |
| 112 | + const prompt = ChatPromptTemplate.fromMessages([ |
| 113 | + ["system", "You're an helpful assistant. Help the user to understand what is RAGStack. Use only information provided in the CONTEXT.\nCONTEXT:\n{context}"], |
| 114 | + ["human", "{question}"], |
| 115 | + ]) |
| 116 | +
|
| 117 | + const docParser = (docs) => { |
| 118 | + const formatted = docs.map((doc, i) => { |
| 119 | + return `<doc id='${i}'>${doc.pageContent}</doc>` |
| 120 | + }).join("\n") |
| 121 | + return formatted |
| 122 | + } |
| 123 | +
|
| 124 | + const chain = RunnableSequence.from([ |
| 125 | + { |
| 126 | + context: vectorStore.asRetriever().pipe(docParser), |
| 127 | + question: new RunnablePassthrough(), |
| 128 | + }, |
| 129 | + prompt, |
| 130 | + new ChatOpenAI({}), |
| 131 | + new StringOutputParser() |
| 132 | + ]); |
| 133 | + /** Finally ask a question about RAGStack to the chatbot */ |
| 134 | + const answer = await chain.invoke("What is RAGStack?") |
| 135 | + console.log("Answer:", answer) |
| 136 | +} |
| 137 | +main() |
| 138 | +---- |
| 139 | + |
| 140 | +After that, you can run the script with Node.js: |
| 141 | +[source,bash] |
| 142 | +---- |
| 143 | +node index.js |
| 144 | +>Connected to Astra DB collection |
| 145 | +>Answer: RAGStack is a framework for building RAG applications. It also has first-class support for AstraDB and Cassandra. |
| 146 | +---- |
| 147 | + |
| 148 | +== Upgrade RAGStack version |
| 149 | +After you have installed the RAGStack package, you can upgrade it to the latest version using the re-running the cli command: |
| 150 | +[source,bash] |
| 151 | +---- |
| 152 | +npx @datastax/ragstack-ai-ts install |
| 153 | +---- |
| 154 | +or you can upgrade to a specific version: |
| 155 | +[source,bash] |
| 156 | +---- |
| 157 | +npx @datastax/ragstack-ai-ts install x.y.z |
| 158 | +---- |
| 159 | + |
| 160 | + |
| 161 | +== What's next? |
| 162 | + |
| 163 | +* xref:what-is-rag.adoc[]: Learn more about the RAG pattern. |
| 164 | + |
0 commit comments