### Start the client application Source: https://docs.datastax.com/en/ragstack/examples/hotels-app.html Installs node dependencies and starts the frontend application. ```bash npm install npm start ``` -------------------------------- ### Install RAGStack and Datasets Source: https://docs.datastax.com/en/ragstack/quickstart.html Installs the RAGStack AI library and the HuggingFace datasets library. Use this to set up your environment for the RAG quickstart. ```bash pip3 install ragstack-ai datasets ``` -------------------------------- ### Install ragstack-ai Library Source: https://docs.datastax.com/en/ragstack/examples/flare.html Install the necessary library for building the FLARE pipeline. This command installs the core package. ```shell pip install ragstack-ai ``` -------------------------------- ### Install project dependencies Source: https://docs.datastax.com/en/ragstack/examples/hotels-app.html Installs the necessary Python packages for the application. ```python pip install -r requirements.txt ``` -------------------------------- ### Start the API server Source: https://docs.datastax.com/en/ragstack/examples/hotels-app.html Launches the backend API server using uvicorn. ```bash uvicorn api:app --reload ``` -------------------------------- ### Install Required Libraries Source: https://docs.datastax.com/en/ragstack/examples/langchain-evaluation.html Install the ragstack-ai and langchain packages to build the RAG pipeline. ```shell pip install ragstack-ai langchain[openai] ``` -------------------------------- ### Install ragstack-ai-colbert Source: https://docs.datastax.com/en/ragstack/colbert/index.html Install the core package for ColBERT retrieval implementation. ```bash pip install ragstack-ai-colbert ``` -------------------------------- ### Start Docker containers Source: https://docs.datastax.com/en/ragstack/examples/hcd.html Build and verify the status of the required Docker containers. ```bash docker compose up -d docker compose ps ``` -------------------------------- ### Install Dependencies Source: https://docs.datastax.com/en/ragstack/examples/langchain_multimodal_gemini.html Install the necessary Python packages for AI platform and RAG. ```python pip install google-cloud-aiplatform ragstack-ai --upgrade ``` -------------------------------- ### Start Langflow Source: https://docs.datastax.com/en/ragstack/langflow/index.html Launch the Langflow server to access the visual interface in a browser. ```bash langflow run ``` -------------------------------- ### Install project dependencies Source: https://docs.datastax.com/en/ragstack/examples/hcd.html Install the necessary Python packages for the RAG application. ```bash pip install ragstack-ai-langchain python-dotenv langchainhub ``` -------------------------------- ### Load and Display Dataset Example Source: https://docs.datastax.com/en/ragstack/quickstart.html Loads the 'philosopher-quotes' dataset from HuggingFace and prints an example entry. This demonstrates how to access and inspect the data. ```python philo_dataset = load_dataset("datastax/philosopher-quotes")["train"] print("An example entry:") print(philo_dataset[16]) ``` -------------------------------- ### View Installed Packages Source: https://docs.datastax.com/en/ragstack/migration.html After installation, use `pip list` to verify that the `llama-index` version is `0.9.34` and `ragstack-ai` is installed. ```bash Package Version ------------------- ... llama-index 0.9.34 ... ragstack-ai 0.6.0 ... ``` -------------------------------- ### Example Output Data Source: https://docs.datastax.com/en/ragstack/examples/langchain_multimodal_gemini.html Sample CSV-formatted output representing product details retrieved from the vector search. ```text Filter 2 Cup 50mm, https://www.breville.com/content/dam/breville/us/catalog/products/images/sp0/sp0000166/tile.jpg, 11.95, https://www.breville.com/us/en/parts-accessories/parts/sp0000166.html?sku=SP0000166 ``` -------------------------------- ### Install ColBERT extras for frameworks Source: https://docs.datastax.com/en/ragstack/colbert/index.html Install ColBERT support for LangChain or LlamaIndex using package extras. ```bash pip install "ragstack-ai-langchain[colbert]" ``` ```bash pip install "ragstack-ai-llamaindex[colbert]" ``` -------------------------------- ### Run Application Source: https://docs.datastax.com/en/ragstack/migration.html Execute your Python application using `python3` to ensure it runs correctly after installation and potential upgrades. ```bash python3 llama-migration.py ``` -------------------------------- ### Initialize Poetry Project and Add Dependency Source: https://docs.datastax.com/en/ragstack/dev-environment.html Initialize a new project with Poetry, which creates a pyproject.toml file. This example shows how to interactively add the 'ragstack-ai' dependency. ```bash poetry init This command will guide you through creating your pyproject.toml config. Package name [temporary-astra]: Version [0.1.0]: Description []: Author [Mendon Kissling <59585235+mendonk@users.noreply.github.com>, n to skip]: License []: Compatible Python versions [^3.11]: Would you like to define your main dependencies interactively? (yes/no) [yes] yes Package to add or search for (leave blank to skip): ragstack-ai Enter package # to add, or the complete package name if it is not listed []: [ 0] ragstack-ai > 0 Enter the version constraint to require (or leave blank to use the latest version): Using version ^0.1.2 for ragstack-ai ``` -------------------------------- ### Install Dependencies Source: https://docs.datastax.com/en/ragstack/examples/nvidia_embeddings.html Install the necessary packages for the RAG pipeline, including ragstack-ai, langchain-nvidia-ai-endpoints, and datasets. ```bash pip install -qU ragstack-ai langchain-nvidia-ai-endpoints datasets ``` -------------------------------- ### Install Dependencies Source: https://docs.datastax.com/en/ragstack/examples/llama-astra.html Install the necessary Python packages for RAGStack and environment variable management. ```python pip install ragstack-ai python-dotenv ``` -------------------------------- ### Install ragstack-ai-langflow Source: https://docs.datastax.com/en/ragstack/langflow/index.html Use pip to install the package containing compatible Langflow components. ```bash pip install ragstack-ai-langflow ``` -------------------------------- ### Install RAGStack dependencies Source: https://docs.datastax.com/en/ragstack/examples/rag-with-cassio.html Install the required libraries for RAGStack, OpenAI, and document processing. ```bash pip install \ "ragstack-ai" \ "openai" \ "pypdf" \ "python-dotenv" \ "datasets" \ "pandas" \ "google-cloud-aiplatform" ``` -------------------------------- ### Install Dependencies Source: https://docs.datastax.com/en/ragstack/examples/mmr.html Installs the necessary Python packages for RAGStack and environment variable loading. ```python pip install -qU ragstack-ai python-dotenv ``` -------------------------------- ### Install Project Dependencies with Poetry Source: https://docs.datastax.com/en/ragstack/dev-environment.html Install all project dependencies defined in pyproject.toml and lock them to specific versions in poetry.lock. This ensures consistent builds across environments. ```bash poetry install Updating dependencies Resolving dependencies... Package operations: 65 installs, 0 updates, 0 removals • Installing click (8.1.7) ... Writing lock file Installing the current project: temporary-astra (0.1.0) ``` -------------------------------- ### Install Required Python Packages Source: https://docs.datastax.com/en/ragstack/migration.html Installs the necessary Python packages for the LangChain application, including datasets, OpenAI, Astra DB integration, and environment variable management. ```bash pip install langchain datasets openai astrapy tiktoken python-dotenv ``` -------------------------------- ### Install LlamaIndex Source: https://docs.datastax.com/en/ragstack/migration.html Install the LlamaIndex package using pip. This is a prerequisite for using RAGStack's AI features. ```python pip install llama-index ``` -------------------------------- ### Example RAG Chain Outputs Source: https://docs.datastax.com/en/ragstack/examples/advanced-rag.html Displays example question-answer pairs and token counts generated by the ParentDocumentRAG chain, illustrating its performance on various queries. ```text content_pasteCopied! ``` ---------------------------------------- Question: What motivates the narrator, Montresor, to seek revenge against Fortunato? Answer: The narrator, Montresor, seeks revenge against Fortunato because Fortunato insulted him. Total Tokens: 1708 ---------------------------------------- Question: What are the major themes in this story? Answer: The major themes in this story are revenge, deception, and the power of manipulation. Total Tokens: 1695 ---------------------------------------- Question: What is the significance of the story taking place during the carnival season? Answer: The significance of the story taking place during the carnival season is that it provides a chaotic and festive atmosphere, which allows the narrator to carry out his revenge plot without arousing suspicion. Total Tokens: 1719 ---------------------------------------- Question: How is vivid and descriptive language used in the story? Answer: Vivid and descriptive language is used in the story to create a sense of atmosphere and to paint a detailed picture of the setting and events. The language is used to describe the dank and damp catacombs, the chains and padlock that bind the protagonist, and the construction of the wall that seals the niche. It also describes the sounds and actions of the characters, such as the moaning cry from the recess and the low laugh that comes from the niche. Overall, the vivid and descriptive language helps to immerse the reader in the story and enhance the suspense and horror elements. Total Tokens: 1803 ---------------------------------------- Question: Is there any foreshadowing in the story? If yes, how is it used in the story? Answer: Yes, there is foreshadowing in the story. The foreshadowing is used to hint at the fate of Fortunato and the narrator's plan for revenge. The mention of the chains, padlock, and walling up the entrance of the niche all foreshadow the narrator's intention to trap and bury Fortunato alive. Additionally, the mention of the Amontillado wine and the narrator's comment about Fortunato's cough hint at the means by which the narrator will carry out his revenge. Total Tokens: 2342 ``` ``` -------------------------------- ### Install RAGStack package Source: https://docs.datastax.com/en/ragstack/ragstack-ts/quickstart.html CLI commands to install the RAGStack package via NPM or Yarn. ```bash npx @datastax/ragstack-ai install --use-npm ``` ```bash npx @datastax/ragstack-ai install --use-yarn ``` -------------------------------- ### Install RAGStack and Dependencies Source: https://docs.datastax.com/en/ragstack/examples/qa-with-cassio.html Installs the required Python libraries for RAGStack, OpenAI integration, PDF loading, and environment variable management. ```python pip install \ "ragstack-ai" \ "openai" \ "pypdf" \ "python-dotenv" ``` -------------------------------- ### Example Retrieval Results Source: https://docs.datastax.com/en/ragstack/default-architecture/retrieval.html Sample output from running the iterative retrieval process, showing questions, answers, and total token counts for each. ```text ---------------------------------------- Question: What motivates the narrator, Montresor, to seek revenge against Fortunato? The narrator, Montresor, seeks revenge against Fortunato because Fortunato insulted him. Total Tokens: 2206 ---------------------------------------- Question: What are the major themes in this story? The major themes in this story are revenge, deception, and the consequences of one's actions. Total Tokens: 1807 ---------------------------------------- Question: What is the significance of the story taking place during the carnival season? The significance of the story taking place during the carnival season is not explicitly stated in the given context. Total Tokens: 2201 ---------------------------------------- Question: How is vivid and descriptive language used in the story? Vivid and descriptive language is used in the story to create a sense of atmosphere and to immerse the reader in the events taking place. The language paints a detailed picture of the setting, such as the granite walls, the iron staples, and the bones in the recess. It also conveys the emotions and actions of the characters, such as the protagonist's astounded reaction and the chained form's low moaning cry. The language is used to evoke a sense of suspense and horror, as well as to emphasize the intensity of the events unfolding. Total Tokens: 2288 ---------------------------------------- Question: Is there any foreshadowing in the story? If yes, how is it used in the story? Yes, there is foreshadowing in the story. The narrator's mention of the "supreme madness of the carnival season" and the fact that he encounters Fortunato during this time hints at the chaotic and unpredictable nature of the events that will unfold. Additionally, the repeated references to the Amontillado wine and the narrator's insistence on taking Fortunato to see it foreshadow the trap that the narrator has set for Fortunato in the catacombs. Total Tokens: 2287 ``` -------------------------------- ### LlamaIndex to AstraDB Migration Example Source: https://docs.datastax.com/en/ragstack/migration.html This script demonstrates migrating a LlamaIndex application to use AstraDB as a vector store. It requires environment variables for AstraDB connection and downloads a sample dataset. ```python import os from dotenv import load_dotenv from llama_index.core.llama_dataset import download_llama_dataset from llama_index.vector_stores import AstraDBVectorStore from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext load_dotenv() ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN") ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT") # Download and load dataset dataset = download_llama_dataset("PaulGrahamEssayDataset", "./data") documents = SimpleDirectoryReader("./data/source_files").load_data() # Display basic information about the documents print(f"Total documents: {len(documents)}") first_doc = documents[0] print(f"First document, id: {first_doc.doc_id}") print(f"First document, hash: {first_doc.hash}") print(f"First document, text ({len(first_doc.text)} characters):\n{'=' * 20}\n{first_doc.text[:360]} ...") # Setup AstraDB Vector Store astra_db_store = AstraDBVectorStore( token=os.getenv("ASTRA_DB_APPLICATION_TOKEN"), api_endpoint=os.getenv("ASTRA_DB_API_ENDPOINT"), collection_name="test", embedding_dimension=1536 ) # Create Storage Context and Index storage_context = StorageContext.from_defaults(vector_store=astra_db_store) index = VectorStoreIndex.from_documents(documents, storage_context=storage_context) # Query the index def execute_query(query_string, mode="default", top_k=3, mmr_prefetch_factor=None): retriever = index.as_retriever( vector_store_query_mode=mode, similarity_top_k=top_k, vector_store_kwargs={"mmr_prefetch_factor": mmr_prefetch_factor} if mmr_prefetch_factor else {} ) nodes_with_scores = retriever.retrieve(query_string) print(query_string) print(f"Found {len(nodes_with_scores)} nodes.") for idx, node_with_score in enumerate(nodes_with_scores): print(f" [{idx}] score = {node_with_score.score}") print(f" id = {node_with_score.node.node_id}") print(f" text = {node_with_score.node.text[:90]} ...") ``` -------------------------------- ### Install RAGStack AI with Specific Upgrade Strategy Source: https://docs.datastax.com/en/ragstack/migration.html Installs the `ragstack-ai` package. The `--upgrade-strategy="only-if-needed"` option prevents unnecessary upgrades of existing packages. ```bash pip install ragstack-ai --upgrade-strategy="only-if-needed" ``` -------------------------------- ### Complete Code Example for Retriever Comparison Source: https://docs.datastax.com/en/ragstack/examples/mmr.html Provides a full Python script to set up Astra DB, define data, create similarity and MMR retrievers, and run a question-answering chain. Ensure environment variables for API endpoint and token are set. ```python import os from dotenv import load_dotenv from langchain.chains.qa_with_sources.retrieval import RetrievalQAWithSourcesChain from langchain_openai import OpenAI, OpenAIEmbeddings from langchain.indexes.vectorstore import VectorStoreIndexWrapper from langchain_astradb import AstraDBVectorStore # Load environment variables load_dotenv() # Initialize the OpenAI model and embeddings. llm = OpenAI(temperature=0) myEmbedding = OpenAIEmbeddings() # Initialize the vector store. myAstraDBVStore = AstraDBVectorStore( embedding=myEmbedding, api_endpoint=os.environ["ASTRA_DB_API_ENDPOINT"], token=os.environ["ASTRA_DB_APPLICATION_TOKEN"], namespace=os.environ.get("ASTRA_DB_KEYSPACE"), # this is optional collection_name="mmr_test", ) index = VectorStoreIndexWrapper(vectorstore=myAstraDBVStore) # declare data BASE_SENTENCE_0 = ("The frogs and the toads were meeting in the night " "for a party under the moon.") BASE_SENTENCE_1 = ("There was a party under the moon, that all toads, " "with the frogs, decided to throw that night.") BASE_SENTENCE_2 = ("And the frogs and the toads said: \"Let us have a party " "tonight, as the moon is shining".") BASE_SENTENCE_3 = ("I remember that night... toads, along with frogs, " "were all busy planning a moonlit celebration.") DIFFERENT_SENTENCE = ("For the party, frogs and toads set a rule: " "everyone was to wear a purple hat.") # insert into index texts = [ BASE_SENTENCE_0, BASE_SENTENCE_1, BASE_SENTENCE_2, BASE_SENTENCE_3, DIFFERENT_SENTENCE, ] metadatas = [ {"source": "Barney's story at the pub"}, {"source": "Barney's story at the pub"}, {"source": "Barney's story at the pub"}, {"source": "Barney's story at the pub"}, {"source": "The chronicles at the village library"}, ] # add texts to vector store and print IDs ids = myAstraDBVStore.add_texts( texts, metadatas=metadatas, ) print("\n".join(ids)) # query the index QUESTION = "Tell me about the party that night." # manual creation of the "retriever" with the 'similarity' search type retrieverSim = myAstraDBVStore.as_retriever( search_type="similarity", search_kwargs={ "k": 2, }, ) chainSimSrc = RetrievalQAWithSourcesChain.from_chain_type( llm, retriever=retrieverSim, ) # Run the chain and print results with sources responseSimSrc = chainSimSrc.invoke({chainSimSrc.question_key: QUESTION}) print("Similarity-based chain:") print(f" ANSWER : {responseSimSrc['answer'].strip()}") print(f" SOURCES: {responseSimSrc['sources'].strip()}") # mmr search with sources ``` -------------------------------- ### Implement RAG workflow Source: https://docs.datastax.com/en/ragstack/ragstack-ts/quickstart.html A complete JavaScript example using LangChain to index text into AstraDB and query it via a retrieval chain. ```javascript const { OpenAIEmbeddings, ChatOpenAI } = require("@langchain/openai") const { AstraDBVectorStore } = require("@langchain/community/vectorstores/astradb") const { ChatPromptTemplate } = require("@langchain/core/prompts") const { RunnableSequence, RunnablePassthrough } = require("@langchain/core/runnables") const { StringOutputParser } = require("@langchain/core/output_parsers") async function main() { // create the embeddings object with the OpenAI API key const embeddings = new OpenAIEmbeddings() // AstraDB connection parameters const astra = { token: process.env.ASTRA_DB_APPLICATION_TOKEN, endpoint: process.env.ASTRA_DB_API_ENDPOINT, collection: "demo", collectionOptions: { vector: { dimension: 1536, /** 1536 for OpenAI embeddings */ metric: "cosine", }, } } /** Index some text into the Astra Vector Store */ const vectorStore = await AstraDBVectorStore.fromTexts( [ "RAGStack is a framework for building RAG applications", "RAGStack has first-class support for AstraDB and Cassandra", ], [{source: "documentation"}, {source: "documentation"}], embeddings, astra ) /** Now prepare the retrieval */ const prompt = ChatPromptTemplate.fromMessages([ ["system", "You're an helpful assistant. Help the user to understand what is RAGStack. Use only information provided in the CONTEXT.\nCONTEXT:\n{context}"], ["human", "{question}"], ]) const docParser = (docs) => { const formatted = docs.map((doc, i) => { return `${doc.pageContent}` }).join("\n") return formatted } const chain = RunnableSequence.from([ { context: vectorStore.asRetriever().pipe(docParser), question: new RunnablePassthrough(), }, prompt, new ChatOpenAI({}), new StringOutputParser() ]); /** Finally ask a question about RAGStack to the chatbot */ const answer = await chain.invoke("What is RAGStack?") console.log("Answer:", answer) } main() ``` -------------------------------- ### Complete RAG Pipeline Setup Source: https://docs.datastax.com/en/ragstack/examples/nvidia_embeddings.html This comprehensive Python script sets up a RAG pipeline. It configures NVIDIA embeddings and chat models, initializes AstraDB vector store, loads and prepares a dataset, constructs a RAG chain, and invokes it with a query. ```python from datasets import load_dataset from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings, ChatNVIDIA from langchain_astradb import AstraDBVectorStore from langchain.schema import Document from langchain.prompts import ChatPromptTemplate from langchain.chat_models import ChatOpenAI from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnablePassthrough import os # Configuration for NVIDIA Embeddings nvidia_api_key = os.getenv("NVIDIA_API_KEY") embedding = NVIDIAEmbeddings(nvidia_api_key=nvidia_api_key, model="nvolveqa_40k") # AstraDB Vector Store setup collection_name = "test" astra_token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") astra_api_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") vstore = AstraDBVectorStore(collection_name=collection_name, embedding=embedding, token=astra_token, api_endpoint=astra_api_endpoint) print("Astra vector store configured") # Load a sample dataset philo_dataset = load_dataset("datastax/philosopher-quotes")["train"] print("An example entry:") print(philo_dataset[16]) # Construct documents from dataset docs = [] for entry in philo_dataset: metadata = {"author": entry["author"]} if entry["tags"]: for tag in entry["tags"].split(";"): metadata[tag] = "y" doc = Document(page_content=entry["quote"], metadata=metadata) docs.append(doc) # Insert documents into vector store inserted_ids = vstore.add_documents(docs) print(f"\nInserted {len(inserted_ids)} documents.") # Setup LangChain Chat Prompt retriever = vstore.as_retriever(search_kwargs={"k": 3}) prompt_template = """ Answer the question based only on the supplied context. If you don't know the answer, say you don't know the answer. Context: {context} Question: {question} Your answer: """ prompt = ChatPromptTemplate.from_template(prompt_template) model = ChatNVIDIA(model="mixtral_8x7b", nvidia_api_key=nvidia_api_key) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) # Invoke the chain with a query and print result result = chain.invoke("In the given context, what subject are philosophers most concerned with?") print(result) ``` -------------------------------- ### Initialize a new project Source: https://docs.datastax.com/en/ragstack/ragstack-ts/quickstart.html Commands to initialize a new Node.js project using NPM or Yarn. ```bash npm init ``` ```bash yarn init ``` -------------------------------- ### Clone the application repository Source: https://docs.datastax.com/en/ragstack/examples/hotels-app.html Initializes the project directory by cloning the repository and navigating into it. ```bash git clone https://github.com/DataStax-Examples/langchain-astrapy-hotels-app.git cd langchain-astrapy-hotels-app ``` -------------------------------- ### Retrieval Result Example Source: https://docs.datastax.com/en/ragstack/default-architecture/retrieval.html Example output generated by the retrieval chain. ```text The context is a passage from the story "The Cask of Amontillado" by Edgar Allan Poe. The narrator, who has been insulted by a man named Fortunato, seeks revenge. He lures Fortunato into a catacomb under the pretense of tasting a rare wine called Amontillado. Once they are deep in the catacombs, the narrator chains Fortunato to a wall and walls him up alive. The narrator then describes how he finishes the wall and leaves Fortunato to die. The passage also mentions the narrator's motivation for revenge and his expertise in wine. ``` -------------------------------- ### Install RAGStack CLI Source: https://docs.datastax.com/en/ragstack/ragstack-ts/migration.html Use this command to install RAGStack in your project. It modifies package.json, installs the RAGStack AI package, and refreshes local dependencies. Supports npm and yarn. ```bash npx @datastax/ragstack-ai-cli install ``` -------------------------------- ### Set up Local Environment Variables Source: https://docs.datastax.com/en/ragstack/examples/llama-astra.html Configure your local environment by creating a .env file with your Astra DB credentials and OpenAI API key. ```bash ASTRA_DB_APPLICATION_TOKEN=AstraCS: ... ASTRA_DB_API_ENDPOINT=https://-.apps.astra.datastax.com OPENAI_API_KEY=sk-... ``` -------------------------------- ### Download Sample Text File Source: https://docs.datastax.com/en/ragstack/examples/advanced-rag.html Use this command to download a sample text file for indexing. This file will be processed and loaded into the vector store. ```python curl https://raw.githubusercontent.com/CassioML/cassio-website/main/docs/frameworks/langchain/texts/amontillado.txt --output amontillado.txt input = "amontillado.txt" ``` -------------------------------- ### Initialize Environment Variables Source: https://docs.datastax.com/en/ragstack/examples/qa-with-cassio.html Loads essential configuration values like database connection details and API keys from environment variables. ```python ASTRA_DB_SECURE_BUNDLE_PATH = os.getenv("ASTRA_DB_SECURE_BUNDLE_PATH") ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN") ASTRA_DB_KEYSPACE = os.getenv("ASTRA_DB_NAMESPACE") ASTRA_DB_TABLE_NAME = os.getenv("ASTRA_DB_COLLECTION") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") ``` -------------------------------- ### Verify Installed Packages with Pip List Source: https://docs.datastax.com/en/ragstack/migration.html Lists all installed Python packages and their versions. This command is used to confirm that the correct version of LangChain (0.0.349) is installed after applying the RAGStack upgrade strategy. ```bash Package Version ------------------- ------------ aiohttp 3.9.1 aiosignal 1.3.1 annotated-types 0.6.0 anyio 4.1.0 astrapy 0.6.2 attrs 23.1.0 backoff 2.2.1 beautifulsoup4 4.12.2 cassandra-driver 3.28.0 cassio 0.1.3 certifi 2023.11.17 chardet 5.2.0 charset-normalizer 3.3.2 click 8.1.7 dataclasses-json 0.6.3 datasets 2.15.0 Deprecated 1.2.14 dill 0.3.7 distro 1.8.0 emoji 2.9.0 filelock 3.13.1 filetype 1.2.0 frozenlist 1.4.0 fsspec 2023.10.0 geomet 0.2.1.post1 greenlet 3.0.2 h11 0.14.0 h2 4.1.0 hpack 4.0.0 httpcore 1.0.2 httpx 0.25.2 huggingface-hub 0.19.4 hyperframe 6.0.1 idna 3.6 joblib 1.3.2 jsonpatch 1.33 jsonpointer 2.4 langchain 0.0.349 langchain-community 0.0.1 langchain-core 0.0.13 langdetect 1.0.9 langsmith 0.0.69 llama-index 0.9.14 lxml 4.9.3 marshmallow 3.20.1 multidict 6.0.4 multiprocess 0.70.15 mypy-extensions 1.0.0 nest-asyncio 1.5.8 nltk 3.8.1 numpy 1.26.2 openai 1.3.8 packaging 23.2 pandas 2.1.4 pip 23.2.1 pyarrow 14.0.1 pyarrow-hotfix 0.6 pydantic 2.5.2 pydantic_core 2.14.5 python-dateutil 2.8.2 python-dotenv 1.0.0 python-iso639 2023.12.11 python-magic 0.4.27 pytz 2023.3.post1 PyYAML 6.0.1 ragstack-ai 0.3.1 rapidfuzz 3.5.2 regex 2023.10.3 requests 2.31.0 setuptools 65.5.0 six 1.16.0 sniffio 1.3.0 soupsieve 2.5 SQLAlchemy 2.0.23 tabulate 0.9.0 tenacity 8.2.3 tiktoken 0.5.2 tqdm 4.66.1 typing_extensions 4.9.0 ``` -------------------------------- ### Initialize Environment Variables for Astra DB and OpenAI Source: https://docs.datastax.com/en/ragstack/examples/flare.html Set up your database credentials and API keys in a .env file. Ensure you have an Astra DB keyspace and an OpenAI API key. ```dotenv ASTRA_DB_APPLICATION_TOKEN=AstraCS:... ASTRA_DB_API_ENDPOINT=https://9d9b9999-999e-9999-9f9a-9b99999dg999-us-east-2.apps.astra.datastax.com ASTRA_DB_COLLECTION=test OPENAI_API_KEY=sk-f99... ``` ```python import os from dotenv import load_dotenv load_dotenv() astra_token = os.getenv("ASTRA_DB_APPLICATION_TOKEN") astra_endpoint = os.getenv("ASTRA_DB_API_ENDPOINT") collection = os.getenv("ASTRA_DB_COLLECTION") openai_api_key = os.getenv("OPENAI_API_KEY") ``` -------------------------------- ### Initialize environment variables Source: https://docs.datastax.com/en/ragstack/examples/rag-with-cassio.html Load configuration variables from the environment for database and API access. ```python ASTRA_DB_SECURE_BUNDLE_PATH = os.getenv("ASTRA_DB_SECURE_BUNDLE_PATH") ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN") ASTRA_DB_APPLICATION_TOKEN_BASED_USERNAME = "token" ASTRA_DB_KEYSPACE = os.getenv("ASTRA_DB_NAMESPACE") ASTRA_DB_TABLE_NAME = os.getenv("ASTRA_DB_COLLECTION") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") ``` -------------------------------- ### Create a basic RAG prompt template Source: https://docs.datastax.com/en/ragstack/default-architecture/generation.html Use this template to establish a baseline for answering questions based on provided context. ```python template = """ Answer the question based only on the supplied context. \ If you don't know the answer, say you don't know the answer. Context: {context} Question: {question} Your answer: """ ``` -------------------------------- ### Install RAGStack Langchain package Source: https://docs.datastax.com/en/ragstack/examples/colbert.html Installs the necessary package with the ColBERT extra for LangChain integration. ```python pip install "ragstack-ai-langchain[colbert]" ``` -------------------------------- ### Set up RetrievalQA Chain with Custom Prompt Source: https://docs.datastax.com/en/ragstack/examples/qa-with-cassio.html Configures a RetrievalQA chain for question answering using a custom prompt that includes instructions for the chatbot's persona and response format. Requires OpenAI and ChatPromptTemplate. ```python from langchain.chains import RetrievalQA from langchain.llms import OpenAI from langchain.prompts import ChatPromptTemplate prompt= """ You are Marv, a sarcastic but factual chatbot. End every response with a joke related to the question. Context: {context} Question: {question} Your answer: """ prompt = ChatPromptTemplate.from_template(prompt) qa = RetrievalQA.from_chain_type(llm=OpenAI(), retriever=cass_vstore.as_retriever(), chain_type_kwargs={"prompt": prompt}) result = qa.run("{question: Who is Luchesi?") result ``` -------------------------------- ### Configure Environment Variables Source: https://docs.datastax.com/en/ragstack/default-architecture/storing.html Define the necessary credentials and configuration settings in a .env file. ```text ASTRA_DB_APPLICATION_TOKEN=AstraCS:... ASTRA_DB_API_ENDPOINT=https://9d9b9999-999e-9999-9f9a-9b99999dg999-us-east-2.apps.astra.datastax.com ASTRA_DB_COLLECTION=test OPENAI_API_KEY=sk-f99... ``` -------------------------------- ### Import dependencies and load environment variables Source: https://docs.datastax.com/en/ragstack/examples/langchain-unstructured-astra.html Initializes the environment and imports necessary LangChain and Unstructured modules. ```python import os import requests from dotenv import load_dotenv from langchain_astradb import AstraDBVectorStore from langchain_core.documents import Document from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_community.document_loaders import ( unstructured, UnstructuredAPIFileLoader, ) from langchain_openai import ( ChatOpenAI, OpenAIEmbeddings, ) load_dotenv() ``` -------------------------------- ### Install RAGStack ColBERT dependencies Source: https://docs.datastax.com/en/ragstack/examples/colbert.html Install the necessary packages to enable ColBERT functionality within the RAGStack environment. ```python pip install ragstack-ai-colbert python-dotenv ``` -------------------------------- ### Show RAGStack Version Source: https://docs.datastax.com/en/ragstack/quickstart.html Displays the currently installed version of the RAGStack AI package. Useful for verifying installation or checking compatibility. ```bash pip3 show ragstack-ai ``` -------------------------------- ### Set up environment variables Source: https://docs.datastax.com/en/ragstack/examples/llama-parse-astra.html Configure essential environment variables for LlamaCloud API key, Astra DB endpoint and token, and OpenAI API key. These are necessary for authentication and connection to the respective services. ```bash LLAMA_CLOUD_API_KEY=llx-... ASTRA_DB_API_ENDPOINT=https://-.apps.astra.datastax.com ASTRA_DB_APPLICATION_TOKEN=AstraCS:... OPENAI_API_KEY=sk-... ``` -------------------------------- ### Configure and Run Retrieval Chain Source: https://docs.datastax.com/en/ragstack/default-architecture/retrieval.html Initializes the retriever, language model, and the retrieval chain. It then executes the retrieval process using the defined helper method. ```python base_retriever = vstore.as_retriever(search_kwargs={'k': 10}) model = ChatOpenAI(openai_api_key=OPEN_AI_API_KEY, model_name="gpt-3.5-turbo", temperature=0.1) base_chain = ( {"context": base_retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) do_retrieval(base_chain) ``` -------------------------------- ### Uninstall LangChain and Install RAGStack AI Source: https://docs.datastax.com/en/ragstack/migration.html This sequence uninstalls the current LangChain package and then installs `ragstack-ai`. This is a troubleshooting step if migration issues arise. ```bash pip uninstall langchain Successfully uninstalled langchain-0.0.350 pip install ragstack-ai --upgrade-strategy="only-if-needed" ``` -------------------------------- ### Download and Load Document for Indexing Source: https://docs.datastax.com/en/ragstack/examples/flare.html Download a sample text file and load it into a LangChain Document object. This document will be embedded and stored in the vector database. ```shell curl https://raw.githubusercontent.com/CassioML/cassio-website/main/docs/frameworks/langchain/texts/amontillado.txt --output amontillado.txt ``` ```python input = "amontillado.txt" loader = TextLoader(input) documents = loader.load_and_split() ``` -------------------------------- ### Define Prompt Template and Questions Source: https://docs.datastax.com/en/ragstack/default-architecture/retrieval.html Sets up the prompt template for the language model and defines a list of questions to be asked. ```python prompt_template = """ Answer the question based only on the supplied context. If you don't know the answer, say you don't know the answer. Context: {context} Question: {question} Your answer: """ prompt = ChatPromptTemplate.from_template(prompt_template) questions = [ "What motivates the narrator, Montresor, to seek revenge against Fortunato?", "What are the major themes in this story?", "What is the significance of the story taking place during the carnival season?", "How is vivid and descriptive language used in the story?", "Is there any foreshadowing in the story? If yes, how is it used in the story?" ] ``` -------------------------------- ### Create and Activate Python Virtual Environment with Venv Source: https://docs.datastax.com/en/ragstack/dev-environment.html Use this to create a new virtual environment for your project, activate it, and install the RAGStack AI package. Ensure Python 3.11 or higher is installed. ```python python -m venv source /bin/activate pip install ragstack-ai ``` -------------------------------- ### Create vector store index and query engine Source: https://docs.datastax.com/en/ragstack/examples/llama-parse-astra.html Create a VectorStoreIndex using the parsed nodes and the storage context. Then, create a query engine from the index, specifying the number of similar top results to retrieve. ```python index = VectorStoreIndex(nodes=nodes, storage_context=storage_context) query_engine = index.as_query_engine(similarity_top_k=15) ``` -------------------------------- ### Example RAG output Source: https://docs.datastax.com/en/ragstack/quickstart.html Expected console output after running the RAG generation process. ```text An example entry: {'author': 'aristotle', 'quote': 'Love well, be loved and do something of value.', 'tags': 'love;ethics'} Inserted 450 documents. The subject that philosophers are most concerned with in the given context is truth. ``` -------------------------------- ### Instantiate LLM and embeddings Source: https://docs.datastax.com/en/ragstack/examples/rag-with-cassio.html Initialize the ChatOpenAI model and the OpenAI embeddings model. ```python llm = ChatOpenAI(temperature=0) myEmbedding = OpenAIEmbeddings() ``` -------------------------------- ### Configure Credentials Source: https://docs.datastax.com/en/ragstack/examples/nvidia_embeddings.html Export the required environment variables for Astra DB and NVIDIA NGC access. ```bash export ASTRA_DB_APPLICATION_TOKEN=AstraCS: ... export ASTRA_DB_API_ENDPOINT=https://-.apps.astra.datastax.com export NVIDIA_API_KEY=nvapi-... ``` -------------------------------- ### Initialize Vector Store with Documents Source: https://docs.datastax.com/en/ragstack/examples/qa-with-cassio.html Creates a Cassandra vector store from the loaded documents, using OpenAI for embeddings and the established database session. ```python cass_vstore = Cassandra.from_documents( documents=documents, embedding=OpenAIEmbeddings(), session=session, keyspace=ASTRA_DB_KEYSPACE, table_name=ASTRA_DB_TABLE_NAME, ) ``` -------------------------------- ### Create or retrieve a LangSmith dataset Source: https://docs.datastax.com/en/ragstack/examples/langchain-evaluation.html Check for an existing dataset and create a new one with examples if it does not exist. ```python client = Client() dataset_name = "test_eval_dataset" try: dataset = client.read_dataset(dataset_name=dataset_name) print("using existing dataset: ", dataset.name) except LangSmithError: dataset = client.create_dataset( dataset_name=dataset_name, description="sample evaluation dataset", ) for question, answer in examples: client.create_example( inputs={"input": question}, outputs={"answer": answer}, dataset_id=dataset.id, ) print("Created a new dataset: ", dataset.name) ``` -------------------------------- ### Basic RAG Chain Implementation Source: https://docs.datastax.com/en/ragstack/what-is-rag.html This Python code sets up a basic Retrieval-Augmented Generation (RAG) chain using LangChain. It defines a retriever, a prompt template, and an LLM, then chains them together to answer questions based on provided context. Ensure 'vstore' and 'openai_key' are defined and initialized. ```python from langchain_core.prompts import ChatPromptTemplate from langchain_openai_ import ChatOpenAI from langchain_core.output_parser import StrOutputParser from langchain_core.runnable import RunnableLambda, RunnablePassthrough retriever = vstore.as_retriever(search_kwargs={'k': 3}) prompt_template = """ Answer the question based only on the supplied context. If you don't know the answer, say you don't know the answer. Context: {context} Question: {question} Your answer: """ prompt = ChatPromptTemplate.from_template(prompt_template) model = ChatOpenAI(openai_api_key=openai_key) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) chain.invoke("In the given context, what product are sales team members selling the most?") ``` -------------------------------- ### Run Python Migration Script Source: https://docs.datastax.com/en/ragstack/migration.html Execute the Python script to perform the LlamaIndex migration. Ensure you have Python 3 installed. ```bash python3 langchain-migration.py ``` -------------------------------- ### Execute RAG Chain Source: https://docs.datastax.com/en/ragstack/examples/advanced-rag.html Runs the defined RAG chain with a given question. This function is used to get answers from the RAG system. ```bash do_retrieval(base_chain) ``` -------------------------------- ### Configure ColBERT and Astra Database Source: https://docs.datastax.com/en/ragstack/examples/colbert.html Establishes the database connection and initializes the ColBERT embedding model and vector store. ```python keyspace="default_keyspace" database_id=os.getenv("ASTRA_DB_ID") astra_token=os.getenv("ASTRA_DB_APPLICATION_TOKEN") database = CassandraDatabase.from_astra( astra_token=astra_token, database_id=database_id, keyspace=keyspace ) embedding_model = ColbertEmbeddingModel() vector_store = ColbertVectorStore( database = database, embedding_model = embedding_model, ) ``` -------------------------------- ### Test Astra Database Connection with Langchain Source: https://docs.datastax.com/en/ragstack/dev-environment.html Use the AstraDBVectorStore and OpenAIEmbeddings from Langchain to connect to your Astra database. Ensure you have installed the 'python-dotenv' package. ```python import os from dotenv import load_dotenv from langchain_astradb import AstraDBVectorStore from langchain_openai import OpenAIEmbeddings load_dotenv() ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN") ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT") OPEN_AI_API_KEY = os.environ.get("OPENAI_API_KEY") ASTRA_DB_COLLECTION = os.environ.get("ASTRA_DB_COLLECTION") embedding = OpenAIEmbeddings() vstore = AstraDBVectorStore( embedding=embedding, collection_name="test", token=os.environ["ASTRA_DB_APPLICATION_TOKEN"], api_endpoint=os.environ["ASTRA_DB_API_ENDPOINT"], ) print(vstore.astra_db.collection(ASTRA_DB_COLLECTION).find()) ``` -------------------------------- ### Gemini Pro Vision Identification Result Source: https://docs.datastax.com/en/ragstack/examples/langchain_multimodal_gemini.html Example output from Gemini Pro Vision identifying a coffee maker part and providing a purchase link. ```text This is a bottomless portafilter basket. It is used to hold the ground coffee in a portafilter. You can purchase a replacement here: https://www.amazon.com/Bottomless-Portafilter-Basket-Compatible-Machines/dp/B09752K44C/ ``` -------------------------------- ### Initialize MultiQueryRetriever and Chain Source: https://docs.datastax.com/en/ragstack/examples/advanced-rag.html Configures the MultiQueryRetriever using a base retriever and LLM, then integrates it into a LangChain chain. ```python multi_retriever = MultiQueryRetriever.from_llm( retriever=base_retriever, llm=model ) multi_chain = ( {"context": multi_retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) ``` -------------------------------- ### Freeze Python Dependencies to requirements.txt Source: https://docs.datastax.com/en/ragstack/dev-environment.html After setting up your local environment and installing packages, freeze the current dependencies to a requirements.txt file. This file can be used to recreate the environment. ```python pip freeze > requirements.txt ``` -------------------------------- ### Create QA Retrieval Chain Source: https://docs.datastax.com/en/ragstack/examples/nvidia_embeddings.html Set up a RAG chain using a retriever, prompt template, and NVIDIA chat model. ```python from langchain.prompts import ChatPromptTemplate from langchain.chat_models import ChatOpenAI from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnablePassthrough from langchain_nvidia_ai_endpoints import ChatNVIDIA retriever = vstore.as_retriever(search_kwargs={"k": 3}) prompt_template = """ Answer the question based only on the supplied context. If you don't know the answer, say you don't know the answer. Context: {context} Question: {question} Your answer: """ prompt = ChatPromptTemplate.from_template(prompt_template) model = ChatNVIDIA(model="mixtral_8x7b") chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) result = chain.invoke("In the given context, what subject are philosophers most concerned with?") print(result) ``` -------------------------------- ### Create and Activate Conda Virtual Environment Source: https://docs.datastax.com/en/ragstack/dev-environment.html Set up a new Conda virtual environment, activate it, and install RAGStack AI. Conda is an alternative to venv for managing environments. ```python conda create --name conda activate pip install ragstack-ai ``` -------------------------------- ### Example of Retrieved Parent Document Source: https://docs.datastax.com/en/ragstack/examples/advanced-rag.html Shows a sample of a retrieved document, likely a parent document, containing narrative text. This snippet illustrates the content that the ParentDocumentRetriever might return. ```text content_pasteCopied! ``` Document 1: The thousand injuries of Fortunato I had borne as I best could, but when he ventured upon insult, I vowed revenge. You, who so well know the nature of my soul, will not suppose, however, that I gave utterance to a threat. _At length_ I would be avenged; this was a point definitely settled--but the very definitiveness with which it was resolved, precluded the idea of risk. I must not only punish, but punish with impunity. A wrong is unredressed when retribution overtakes its redresser. It is equally unredressed when the avenger fails to make himself felt as such to him who has done the wrong. It must be understood that neither by word nor deed had I given Fortunato cause to doubt my good will. I continued, as was my wont, to smile in his face, and he did not perceive that my smile _now_ was at the thought of his immolation. He had a weak point--this Fortunato-- although in other regards he was a man to be respected and even feared. He prided himself on his connoisseurship in wine. Few Italians have the true virtuoso spirit. For the most part their enthusiasm is adopted to suit the time and opportunity--to practise imposture upon the British and Austrian _millionaires_. In painting and gemmary, Fortunato, like his countrymen, was a quack--but in the matter of old wines he was sincere. In this respect I did not differ from him materially: I was skillful in the Italian vintages myself, and bought largely whenever I could. It was about dusk, one evening during the supreme madness of the carnival season, that I encountered ```