### Copy Environment File Example Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/get-started.md Create a .env file by copying the example template to configure database credentials. ```bash cp .env.example .env ``` -------------------------------- ### Configure environment variables Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Copy the example environment file and update it with your ScyllaDB Cloud and Groq API credentials. ```bash cp .env.example .env ``` ```bash SCYLLADB_HOST=node-0.aws-us-east-1.xxxxxxxx.clusters.scylla.cloud SCYLLADB_PORT=9042 SCYLLADB_USERNAME=scylla SCYLLADB_PASSWORD=your-password SCYLLADB_DATACENTER=AWS_US_EAST_1 SCYLLADB_KEYSPACE=recommend GROQ_API_KEY=your-groq-api-key ``` -------------------------------- ### Install Dependencies with UV Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Install project dependencies using the UV package manager. ```sh uv sync ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Clone the example repository and navigate into the movie-recommendation directory. ```sh git clone https://github.com/scylladb/vector-search-examples.git cd vector-search-examples/movie-recommendation ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Clone the vector-search-examples repository and navigate into the rag-chatbot directory to begin the setup process. ```sh git clone https://github.com/scylladb/vector-search-examples.git cd vector-search-examples/rag-chatbot/ ``` -------------------------------- ### Clone the RAG example repository Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Clone the repository and navigate to the project directory to begin. ```bash git clone https://github.com/scylladb/vector-search-examples.git cd vector-search-examples/rag-movie-chatbot ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Clone the vector search examples repository and navigate to the semantic cache directory. ```sh git clone https://github.com/scylladb/vector-search-examples.git cd vector-search-examples/semantic-cache ``` -------------------------------- ### Install CQLSH Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Install the ScyllaDB CQLSH utility using pip. This tool is required for interacting with the ScyllaDB cluster. ```sh pip install scylla-cqlsh ``` -------------------------------- ### Install Python Requirements Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Install the necessary Python packages for the project using pip. Ensure you have a requirements.txt file in your project directory. ```bash pip install -r requirements.txt ``` -------------------------------- ### Example Application Output Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Observe the output demonstrating a query that is answered by the LLM and a subsequent identical query that is answered from the cache. ```text Question 1: What is the capital city of France? Answer (comes from LLM): Paris Question 2: What's the capital of France? Answer (comes from cache): Paris ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Copy the example environment file and edit it with your ScyllaDB cluster details. ```sh cp .env.example .env ``` ```env SCYLLADB_HOST=node-0.aws-us-east-1.xxxxxxx.clusters.scylla.cloud SCYLLADB_PORT=9042 SCYLLADB_USERNAME=scylla SCYLLADB_PASSWORD=xxxxxxxxx SCYLLADB_DATACENTER=AWS_US_EAST_1 SCYLLADB_KEYSPACE=example_ks ``` -------------------------------- ### Run Streamlit Application Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Start the Streamlit application to interact with the vector search functionality. Ensure you have the necessary dependencies installed. ```bash uv run streamlit run app.py ``` -------------------------------- ### Run Streamlit UI Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Start the application with the Streamlit UI for an interactive user experience. ```sh uv run streamlit run streamlit_ui.py ``` -------------------------------- ### Install Python Project Requirements Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Install the necessary Python packages for the project, including the ScyllaDB driver, Pydantic, Sentence Transformers, and Streamlit. ```sh pip install scylla-driver pydantic sentence-transformers streamlit ``` -------------------------------- ### Run Streamlit App Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Command to execute the Streamlit application. Ensure you have Streamlit installed (`pip install streamlit`). ```bash streamlit run app.py ``` -------------------------------- ### Run Docker Container Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Run the Docker container, which includes database migration and starts the app server. Ensure to use the --env-file flag to load your .env configuration. ```sh docker run --rm -d -p 8000:8000 --env-file .env --name movie-container movies-app ``` -------------------------------- ### Start Ollama Docker Container Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Run the Ollama Docker container, attaching it to the created network and persisting its data using a volume. ```sh docker run -d \ --network scylla_rag_network \ -v ollama:/root/.ollama \ --name ollama \ ollama/ollama ``` -------------------------------- ### Run Movie Application Container Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/get-started.md Start the Docker container for the movie application, mapping ports, loading environment variables, and naming the container. The container automatically runs migration scripts and starts the FastAPI server. ```bash docker run -d --rm -p 8000:8000 --env-file .env --name movie-container movies-app ``` -------------------------------- ### Example Chatbot Interaction Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md An example of interacting with the RAG chatbot, showing a user question and the chatbot's response including retrieved chunk IDs and the generated answer. ```text Enter your question: What's ScyllaDB? --- Retrieved chunk IDs: ['5d460612-583b-4eca-96b5-770046d769dc', 'e3b2028f-6d93-4bdf-9fd0-c6d9fec851ac', '4c632adc-8a18-4bd3-bbdd-210e1d921112'] Chatbot response: ScyllaDB is a low latency distributed NoSQL database [...] ``` -------------------------------- ### Run ScyllaDB Semantic Cache Application Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Start the semantic cache application. The first run will query the LLM, while subsequent similar queries will be served from the cache. ```bash uv run python scylla_semantic_cache.py ``` -------------------------------- ### Run FastAPI Server Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Start the FastAPI backend server for the movie recommendation application. This provides an API and interactive documentation. ```sh uv run uvicorn src.main:app --reload --port 8000 ``` -------------------------------- ### Run the Data Ingestion Script Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Execute the ingest.py script to start populating the ScyllaDB database with movie data. The script will print status messages indicating the ingestion progress. ```shell python ingest.py ⏳ Ingestion started... ``` -------------------------------- ### Run the RAG Chatbot Application Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Start the chatbot application by executing the scylla_rag.py script within its container. This will allow you to interact with the RAG system. ```sh docker exec -it scylla_rag_app python scylla_rag.py ``` -------------------------------- ### Execute Semantic Cache Application Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Execute the Python script for the semantic cache application within the running Docker container. This starts the application's logic. ```sh docker exec -it scylla_semantic_app python scylla_semantic_cache.py ``` -------------------------------- ### Running the mem0 + ScyllaDB Cloud Demo Source: https://github.com/scylladb/vector-search-examples/blob/main/memory-mem0/README.md Steps to clone the project, configure credentials, set up the schema, and run the main application script. Ensure you are in the 'app' directory after cloning. ```bash cd app cp example.env .env uv run migrate.py uv run main.py ``` -------------------------------- ### Create database schema Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Run the migration script to set up the necessary keyspace, table, and vector index in ScyllaDB. ```bash python db/migrate.py ``` -------------------------------- ### Run Database Migration Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Execute the migration script to set up the necessary keyspace and tables in ScyllaDB. ```sh uv run src/migrate.py ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Fill in your ScyllaDB Cloud connection details and Groq API key in the .env file. Ensure all required fields are accurately populated. ```bash SCYLLADB_HOST=node-0.aws-us-east-1.xxxxxxxx.clusters.scylla.cloud SCYLLADB_PORT=9042 SCYLLADB_USERNAME=scylla SCYLLADB_PASSWORD=your-password SCYLLADB_DATACENTER=AWS_US_EAST_1 SCYLLADB_KEYSPACE=semantic_cache GROQ_API_KEY=your-groq-api-key ``` -------------------------------- ### Load Sample Data via Docker Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Execute the data loading script within the running Docker container to populate ScyllaDB with sample movie data and vectors. ```sh docker exec movie-container python src/load_data.py ``` -------------------------------- ### ScyllaDB Schema Migration Script Source: https://github.com/scylladb/vector-search-examples/blob/main/memory-mem0/README.md Run this script once to set up the necessary ScyllaDB schema, including the keyspace, table with a vector column, and a HNSW vector index. It reads credentials from the .env file. ```bash uv run migrate.py ``` -------------------------------- ### Create ScyllaDB Schema Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Execute the schema.cql file using cqlsh to create the necessary keyspace, table, and vector index in ScyllaDB. ```sh cqlsh $SCYLLA_HOST $SCYLLA_PORT -u $SCYLLA_USER -p $SCYLLA_PASS -f schema.cql ``` -------------------------------- ### Run Database Migration Script Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Execute the migration script to set up the necessary keyspace, tables, and vector index in your ScyllaDB cluster. This prepares the database for the semantic cache. ```bash uv run python migrate.py ``` -------------------------------- ### Build Docker Image Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Build the Docker image for the movie recommendation application. ```sh docker build -t movies-app . ``` -------------------------------- ### Load Sample Data into ScyllaDB Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Execute the insert_sample.py script within the running RAG application container to populate ScyllaDB with sample data for the chatbot. ```sh docker exec -it scylla_rag_app python insert_sample.py ``` -------------------------------- ### Download ScyllaDB Documentation Files Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md A shell script to clone the ScyllaDB GitHub repository and selectively download only the documentation files from the 'docs' folder using sparse checkout. This prepares the data for the RAG ingestion process. ```sh git clone --no-checkout --depth=1 --filter=tree:0 \ https://github.com/scylladb/scylladb.git cd scylladb git sparse-checkout set --no-cone /docs git checkout ``` -------------------------------- ### ScyllaDB Migration Script Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md A Python script (`migrate.py`) to execute the `schema.cql` file, creating the necessary keyspace and tables in ScyllaDB. ```python import os from scylladb import ScyllaClient client = ScyllaClient() session = client.get_session() def absolute_file_path(relative_file_path): current_dir = os.path.dirname(__file__) return os.path.join(current_dir, relative_file_path) print("Creating keyspace and tables...") with open(absolute_file_path("schema.cql"), "r") as file: for query in file.read().split(";"): if len(query) > 0: session.execute(query) print("Migration completed.") client.shutdown() ``` -------------------------------- ### Download Sample Movie Data Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Use wget to download the movies_sample.csv file from GitHub. This file contains the dataset needed for the movie recommendation application. ```sh wget https://github.com/scylladb/vector-search-examples/raw/refs/heads/main/movie-recommendation/data/movies_sample.csv ``` -------------------------------- ### Ingest Sample Data Locally Source: https://github.com/scylladb/vector-search-examples/blob/main/movie-recommendation/readme.md Run the data ingestion script to load sample movie data and vectors into ScyllaDB when running locally. ```sh uv run src/load_data.py ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Use this command to create a new Python virtual environment and activate it. This helps manage project dependencies. ```bash virtualenv env && source env/bin/activate ``` -------------------------------- ### ScyllaSemanticCacheApp Initialization Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Initializes the ScyllaDB client, Groq LLM client, and the sentence transformer embedding model. This class orchestrates the semantic caching process. ```python class ScyllaSemanticCacheApp: def __init__(self): self.scylla_client = ScyllaClient() self.groq_client = Groq(api_key=GROQ_API_KEY) self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2') ``` -------------------------------- ### Create ScyllaDB Keyspace and Movies Table with Vector Column Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/get-started.md This CQL script creates the necessary keyspace and the 'movies' table, including a 'plot_embedding' column to store vector data. It specifies replication strategy and factor for high availability. ```cql CREATE KEYSPACE IF NOT EXISTS example_ks WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; CREATE TABLE IF NOT EXISTS example_ks.movies ( id INT, release_date TIMESTAMP, title TEXT, tagline TEXT, genre TEXT, imdb_id TEXT, poster_url TEXT, plot TEXT, plot_embedding VECTOR, PRIMARY KEY (id) ); ``` -------------------------------- ### Create ScyllaDB Keyspace Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Defines a keyspace with NetworkTopologyStrategy for data replication across nodes, ensuring high availability. ```cql CREATE KEYSPACE recommend WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; ``` -------------------------------- ### Set ScyllaDB Credentials Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Set environment variables for ScyllaDB connection details. Ensure these are replaced with your actual credentials. ```sh SCYLLA_HOST="node-0.aws-us-east-1.xxxxxxxxxx.clusters.scylla.cloud" \ SCYLLA_PORT="9042" \ SCYLLA_USER="scylla" \ SCYLLA_PASS="mypassword" \ SCYLLA_DC="AWS_US_EAST_1" ``` -------------------------------- ### Build Docker Image Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Build the Docker image for the semantic cache application. This prepares the application for containerization. ```sh docker build -t scylla_semantic . ``` -------------------------------- ### ScyllaDB Cloud Configuration Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Define the connection details for your ScyllaDB Cloud instance in a Python configuration file. This includes hosts, port, username, password, datacenter, and keyspace. ```python SCYLLADB_CONFIG = { "hosts": ["node-0.aws-us-east-1.xxxxxxxxxxx.clusters.scylla.cloud", "node-1.aws-us-east-1.xxxxxxxxxxx.clusters.scylla.cloud", "node-2.aws-us-east-1.xxxxxxxxxxx.clusters.scylla.cloud"], "port": "9042", "username": "scylla", "password": "passwd", "datacenter": "AWS_US_EAST_1", "keyspace": "rag" } ``` -------------------------------- ### Load sample movie data Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Load sample movie data into ScyllaDB. This process generates embeddings for movie plots using the 'all-MiniLM-L6-v2' model. ```bash cd ../movie-recommendation cp ../rag-movie-chatbot/.env .env docker build -t movies-app . docker run -d --rm -p 8000:8000 --network host --env-file .env --name movie-container movies-app ``` ```bash docker exec movie-container python src/load_data.py ``` ```bash docker stop movie-container cd ../rag-movie-chatbot ``` -------------------------------- ### Run RAG Application Docker Container Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Run the RAG application container, connecting it to the Docker network and passing ScyllaDB credentials as environment variables. ```sh docker run --rm -d \ --network scylla_rag_network \ --name scylla_rag_app \ -e scylla_host="$SCYLLA_HOST" \ -e scylla_port="$SCYLLA_PORT" \ -e scylla_user="$SCYLLA_USER" \ -e scylla_password="$SCYLLA_PASS" \ -e scylla_datacenter="$SCYLLA_DC" \ scylla_rag ``` -------------------------------- ### Run Semantic Cache Application Container Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Run the Docker container for the semantic cache application, passing ScyllaDB and OpenAI credentials as environment variables. ```sh docker run --rm -d \ --name scylla_semantic_app \ -e scylla_host="$SCYLLA_HOST" \ -e scylla_port="$SCYLLA_PORT" \ -e scylla_user="$SCYLLA_USER" \ -e scylla_password="$SCYLLA_PASS" \ -e scylla_datacenter="$SCYLLA_DC" \ -e OPENAI_APIKEY="$OPENAI_APIKEY" \ -e OPENAI_BASE_URL="$OPENAI_BASE_URL" \ scylla_semantic ``` -------------------------------- ### ScyllaDB Cloud Credentials Configuration Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/get-started.md Edit the .env file to include your ScyllaDB Cloud host, port, username, password, datacenter, and keyspace. ```default SCYLLADB_HOST=node-0.aws-us-east-1.xxxxxxxx.clusters.scylla.cloud SCYLLADB_PORT=9042 SCYLLADB_USERNAME=scylla SCYLLADB_PASSWORD=xxxxxxxxxxxxxx SCYLLADB_DATACENTER=AWS_US_EAST_1 SCYLLADB_KEYSPACE=recommend ``` -------------------------------- ### ScyllaDB Connection Configuration Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Define database connection details in `config.py`. Ensure your ScyllaDB Cloud instance has vector search enabled. ```python SCYLLADB_CONFIG = { "host": "node-0.aws-us-east-1.xxxxxxxxxxx.clusters.scylla.cloud", "port": "9042", "username": "scylla", "password": "passwd", "datacenter": "AWS_US_EAST_1" } ``` -------------------------------- ### ScyllaDB Schema with Vector Index Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Defines the keyspace, movie table with a `plot_embedding` column, and a vector index for similarity search using DOT_PRODUCT. ```cql CREATE KEYSPACE recommend WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; CREATE TABLE recommend.movies ( id INT, release_date TIMESTAMP, title TEXT, tagline TEXT, genre TEXT, imdb_id TEXT, poster_url TEXT, plot TEXT, plot_embedding VECTOR, PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS ann_index ON recommend.movies(plot_embedding) USING 'vector_index' WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' }; ``` -------------------------------- ### Create Docker Network for Ollama Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Create a Docker network that will be used to connect the Ollama container with other services in the RAG application. ```sh docker network create scylla_rag_network ``` -------------------------------- ### ScyllaDB CQL Schema for Semantic Cache Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Defines the keyspace, prompts table with text and embedding storage, and an ANN index for efficient similarity search. This schema is crucial for the semantic cache functionality. ```cql CREATE KEYSPACE IF NOT EXISTS semantic_cache WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; CREATE TABLE IF NOT EXISTS semantic_cache.prompts ( prompt_id uuid PRIMARY KEY, inserted_at timestamp, prompt_text text, prompt_embedding vector, llm_response text ); CREATE INDEX IF NOT EXISTS ann_index ON semantic_cache.prompts(prompt_embedding) USING 'vector_index' WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' }; ``` -------------------------------- ### Build RAG Application Docker Image Source: https://github.com/scylladb/vector-search-examples/blob/main/rag-chatbot/readme.md Build the Docker image for the RAG application using the provided Dockerfile. This image will contain the application code and its dependencies. ```sh docker build -t scylla_rag . ``` -------------------------------- ### Set OpenAI Credentials Source: https://github.com/scylladb/vector-search-examples/blob/main/semantic-cache/readme.md Set environment variables for your OpenAI API key and base URL. Use your specific API key and the correct endpoint. ```sh OPENAI_APIKEY="my_apikey" \ OPENAI_BASE_URL="https://openrouter.ai/api/v1" ``` -------------------------------- ### ScyllaDB Client Helper Module Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md A Python module (`scylladb.py`) to manage ScyllaDB connections, data insertion, and querying, including vector search capabilities. ```python from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT from cassandra.policies import DCAwareRoundRobinPolicy, TokenAwarePolicy from cassandra.auth import PlainTextAuthProvider from cassandra.query import dict_factory import sys, os import config class ScyllaClient(): def __init__(self, keyspace: str = None): self.cluster = self._get_cluster(config.SCYLLADB_CONFIG) if keyspace: self.session = self.cluster.connect(keyspace) else: self.session = self.cluster.connect() def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.shutdown() def shutdown(self): self.cluster.shutdown() def _get_cluster(self, config: dict) -> Cluster: profile = ExecutionProfile( load_balancing_policy=TokenAwarePolicy( DCAwareRoundRobinPolicy(local_dc=config["datacenter"]) ), row_factory=dict_factory ) return Cluster( execution_profiles={EXEC_PROFILE_DEFAULT: profile}, contact_points=[config["host"], ], port=config["port"], auth_provider = PlainTextAuthProvider(username=config["username"], password=config["password"])) def print_metadata(self): for host in self.cluster.metadata.all_hosts(): print(f"Datacenter: {host.datacenter}; Host: {host.address}; Rack: {host.rack}") def get_session(self): return self.session def insert_data(self, table, data: dict): columns = list(data.keys()) values = list(data.values()) insert_query = f""" INSERT INTO {table} ({','.join(columns)}) VALUES ({','.join(['%s' for c in columns])}); """ self.session.execute(insert_query, values) def query_data(self, query, params=[]): rows = self.session.execute(query, params) return rows.all() ``` -------------------------------- ### Streamlit UI for Movie Recommendations Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Builds an interactive movie recommendation app using Streamlit. It includes input fields for user queries and desired number of recommendations, and displays movie posters and plot summaries. ```python import streamlit as st from recommender import MovieRecommender from models import Movie recommender = MovieRecommender() st.set_page_config( page_title="Movie Recommender", page_icon="🎬", layout="wide" ) # Header st.title("🎬 Movie recommendation") st.subheader("ScyllaDB Vector Search DEMO") st.markdown("Source code: https://github.com/scylladb/vector-search-examples/tree/main/movie-recommendation") # Input area col1, col2 = st.columns([3, 1]) with col1: user_query = st.text_input("What kind of movie are you looking for?",placeholder="e.g. time travelling") with col2: top_k = st.number_input("Number of recommendations", min_value=3, max_value=15, value=4, step=1) search_button = st.button("Get Recommendations", width="stretch") def show_poster(poster: str) -> str: if poster: base_url = "https://image.tmdb.org/t/p/original" url = f"{base_url}{poster}" st.image(url, width="content") else: st.caption("Poster not found") def display_best_match(best_match: Movie): movie_poster = best_match.poster_url col1, col2 = st.columns([1, 2]) with col1: show_poster(movie_poster) with col2: st.markdown(f"### {best_match.title}") st.write(best_match.plot[:500] + "...") def display_more_recommendations(movies: list[Movie]): cols = st.columns(3) for i, movie in enumerate(movies[1:]): with cols[i % 3]: poster = movie.poster_url show_poster(poster) st.write(movie.title) def display_search_results(): with st.spinner("🔍 Searching for recommendations..."): movies = recommender.similar_movies(user_query, top_k) if movies: st.subheader("⭐ Best Match") best_match = movies[0] display_best_match(best_match) st.divider() st.subheader("🎥 More Recommendations") rest_of_the_movies = movies[1:] display_more_recommendations(rest_of_the_movies) else: st.error("❌ No similar movies found.") if search_button: if not user_query: st.warning("⚠️ Please enter a movie to get recommendations.") else: try: display_search_results() except Exception as e: st.error(f"⚠️ Error: {str(e)}") ``` -------------------------------- ### Create Local ANN Index by Genre Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Builds a custom ANN index on the plot_embedding column within each genre partition. This allows ScyllaDB to search only the relevant index for queries filtered by genre, improving performance. ```cql CREATE CUSTOM INDEX IF NOT EXISTS ann_index_by_genre ON recommend.movies_by_genre((genre), plot_embedding) USING 'vector_index' WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' }; ``` -------------------------------- ### ScyllaDB Cloud Credentials Configuration Source: https://github.com/scylladb/vector-search-examples/blob/main/memory-mem0/README.md Store your ScyllaDB Cloud and Groq API credentials in a .env file for the application to use. Ensure the node address, username, password, and datacenter are correctly retrieved from your ScyllaDB Cloud cluster. ```dotenv SCYLLADB_ADDRESS=node-0.your-cluster.cloud.scylladb.com SCYLLADB_USERNAME=scylla SCYLLADB_PASSWORD=your-password SCYLLADB_DATACENTER=AWS_US_EAST_1 GROQ_API_KEY=gsk-... ``` -------------------------------- ### Query LLM with Retrieved Context Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Function to query a Large Language Model (LLM) by constructing a system prompt that includes the user's query and the context retrieved from ScyllaDB. It then prints the LLM's streamed response. ```python def query_llm(self, user_query: str, chunks: list[str]) -> str: context = "" for i, chunk in enumerate(chunks): context += f"\n\n Item {i+1}: {chunk}" system_prompt = f"""You are an AI assistant that answers user questions by combining your reasoning ability with the information provided below: \n {context} """ stream = ollama.chat( model=self.LANGUAGE_MODEL, messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_query}, ], stream=True, ) print("Chatbot response:") for chunk in stream: print(chunk["message"]["content"], end="", flush=True) ``` -------------------------------- ### Movie Recommender Module Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Recommends similar movies by creating embeddings for user queries and performing Approximate Nearest Neighbor (ANN) searches in ScyllaDB. Requires `ScyllaClient`, `EmbeddingCreator`, and `Movie` model. ```python from scylladb import ScyllaClient from embedding_creator import EmbeddingCreator from models import Movie class MovieRecommender: def __init__(self): self.scylla_client = ScyllaClient() self.embedding_creator = EmbeddingCreator("all-MiniLM-L6-v2") def similar_movies(self, user_query: str, top_k=5) -> list[Movie]: db_client = ScyllaClient() user_query_embedding = self.embedding_creator.create_embedding(user_query) db_query = f"""SELECT * FROM recommend.movies ORDER BY plot_embedding ANN OF %s LIMIT %s; """ values = [user_query_embedding, top_k] results = db_client.query_data(db_query, values) return [Movie(**row) for row in results] ``` -------------------------------- ### ScyllaDB Client Class Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md A Python class to manage connections to ScyllaDB, including inserting and querying data. It handles cluster configuration, session management, and data operations. ```python from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT from cassandra.policies import DCAwareRoundRobinPolicy, TokenAwarePolicy from cassandra.auth import PlainTextAuthProvider from cassandra.query import dict_factory import config class ScyllaClient(): def __init__(self): scylla_config = config.SCYLLADB_CONFIG self.cluster = self._get_cluster(scylla_config) self.session = self.cluster.connect(scylla_config["keyspace"]) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.shutdown() def shutdown(self): self.cluster.shutdown() def _get_cluster(self, config: dict) -> Cluster: profile = ExecutionProfile( load_balancing_policy=TokenAwarePolicy( DCAwareRoundRobinPolicy(local_dc=config["datacenter"]) ), row_factory=dict_factory ) return Cluster( execution_profiles={EXEC_PROFILE_DEFAULT: profile}, contact_points=config["hosts"], port=config["port"], auth_provider = PlainTextAuthProvider(username=config["username"], password=config["password"])) def print_metadata(self): for host in self.cluster.metadata.all_hosts(): print(f"Datacenter: {host.datacenter}; Host: {host.address}; Rack: {host.rack}") def get_session(self): return self.session def insert_data(self, table, data: dict): columns = list(data.keys()) values = list(data.values()) insert_query = f""" INSERT INTO {table} ({','.join(columns)}) VALUES ({','.join(['%s' for c in columns])}); """ self.session.execute(insert_query, values) def query_data(self, query, values=[]): rows = self.session.execute(query, values) return rows.all() ``` -------------------------------- ### Create Movies by Genre Table Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Defines a table optimized for genre-specific searches. It includes the genre as the partition key and a local vector index for efficient filtering and retrieval within a genre. ```cql CREATE TABLE recommend.movies_by_genre ( genre TEXT, id INT, release_date TIMESTAMP, title TEXT, plot TEXT, plot_embedding VECTOR, PRIMARY KEY (genre, id) ); ``` -------------------------------- ### Create Movies Table with Vector Embedding Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Creates a table to store movie data, including a plot_embedding column for storing 384-dimensional float vectors. This table uses the movie ID as the primary key for even data distribution. ```cql CREATE TABLE recommend.movies ( id INT, release_date TIMESTAMP, title TEXT, tagline TEXT, genre TEXT, imdb_id TEXT, poster_url TEXT, plot TEXT, plot_embedding VECTOR, PRIMARY KEY (id) ); ``` -------------------------------- ### Document Loading with LlamaIndex Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Loads documents from a specified directory using LlamaIndex's SimpleDirectoryReader. It supports filtering by file extensions and limits the number of files processed. ```python from llama_index.core.node_parser import ( SemanticDoubleMergingSplitterNodeParser, LanguageConfig, ) from llama_index.core import SimpleDirectoryReader class ScyllaRag(): def create_chunks(self, dir_path: str, files_limit=1): documents = SimpleDirectoryReader(input_dir=dir_path, recursive=True, num_files_limit=files_limit, required_exts=[.md, .rst], exclude_empty=True, exclude_hidden=True).load_data() # Filter out docs with no text documents = [doc for doc in documents if doc.text.strip()] ``` -------------------------------- ### Search Cache with ScyllaDB ANN Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md Performs a similarity search in the ScyllaDB cache using ANN. It retrieves the most similar prompt's response if the similarity score meets the specified threshold. ```python def search_cache(self, embedding, threshold=0.90): k = 1 cql = """SELECT llm_response, similarity_dot_product(prompt_embedding, %s) AS similarity FROM prompts ORDER BY prompt_embedding ANN OF %s LIMIT %s;""" results = self.scylla_client.query_data(cql, [embedding, embedding, k]) if results: cached = results[0] similarity = cached['similarity'] print(f" Nearest cache similarity: {similarity:.4f}") if similarity >= threshold: return cached['llm_response'] return None ``` -------------------------------- ### Embedding Creator Module Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Initializes a Sentence Transformer model to generate text embeddings. Use this to convert text descriptions into numerical vectors for similarity searches. ```python from sentence_transformers import SentenceTransformer class EmbeddingCreator: def __init__(self, model_name: str = 'all-MiniLM-L6-v2'): self.embedding_model = SentenceTransformer(model_name, device='cpu') def create_embedding(self, text: str) -> list[float]: """ Get an embedding for a single text input using SentenceTransformer. Returns the embedding vector. """ return self.embedding_model.encode(text).tolist() ``` -------------------------------- ### ScyllaDB Schema Definition Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Defines the ScyllaDB schema for the RAG application, including a keyspace, a table for text chunks with embeddings, and a vector index for efficient similarity search. ```cql CREATE KEYSPACE rag WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; CREATE TABLE rag.chunks ( chunk_id UUID PRIMARY KEY, text TEXT, embedding vector ); CREATE INDEX IF NOT EXISTS ann_index ON rag.chunks(embedding) USING 'vector_index' WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' }; ``` -------------------------------- ### Create ANN Index for Vector Similarity Search Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/get-started.md This CQL statement creates an Approximate Nearest Neighbor (ANN) index on the 'plot_embedding' column of the 'movies' table. It uses the 'vector_index' implementation and specifies 'DOT_PRODUCT' as the similarity function, which is optimal for normalized embeddings. ```cql CREATE INDEX IF NOT EXISTS ann_index ON example_ks.movies(plot_embedding) USING 'vector_index' WITH OPTIONS = { 'similarity_function': 'DOT_PRODUCT' }; ``` -------------------------------- ### Full Semantic Caching Flow Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-cache/semantic-cache-scylladb.md This function orchestrates the semantic caching process. It first creates an embedding for the prompt, then checks the cache. If a hit occurs, it returns the cached response; otherwise, it queries the LLM, caches the new response, and returns it. ```python def semantic_cached_prompt(self, prompt): embedding = self.create_embedding(prompt) cached_response = self.search_cache(embedding) if cached_response: print("Cache hit! Returning cached response.") return cached_response print("Cache miss. Querying LLM...") response = self.ask_llm(prompt) self.insert_to_cache(prompt, embedding, response) return response ``` -------------------------------- ### RAG Chatbot Main Execution Flow Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md The main execution block for the RAG chatbot application. It initializes the ScyllaRag class, (optionally) ingests documents, takes user input, fetches relevant chunks, and queries the LLM to provide an answer. ```python if __name__ == "__main__": scylla_rag = ScyllaRag() # ingest documents (only needs to run once) # nodes = scylla_rag.create_chunks("../scylladb/docs", files_limit=200) # scylla_rag.vectorize(nodes, target_table="rag.chunks") user_input = input("What do you want to know about ScyllaDB? ") nodes = scylla_rag.fetch_chunks("rag.chunks", user_input, top_k=3) chunks = [node['chunk_id'] for node in nodes] print("Retrieved chunks:", chunks) scylla_rag.query_llm(user_input, [node["text"] for node in nodes]) ``` -------------------------------- ### Python Script for Movie Data Ingestion Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md This Python script, ingest.py, uses ScyllaDB client and an EmbeddingCreator to ingest movie data from a CSV file into a ScyllaDB table. It generates embeddings for movie plots and stores movie details. ```python import csv from datetime import datetime from scylladb import ScyllaClient from embedding_creator import EmbeddingCreator class MovieLoader: def __init__(self): self.scylla_client = ScyllaClient() self.embedding_creator = EmbeddingCreator("all-MiniLM-L6-v2") def create_embedding(self, text: str) -> list[float]: return self.embedding_creator.create_embedding(text) def ingest_csv(self, csv_file, table_name): with ScyllaClient() as client: with open(csv_file, encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: data = { "id": int(row["id"]), "release_date": datetime.strptime(row["release_date"], "%Y-%m-%d"), "title": row["title"], "tagline": row["tagline"], "genre": row["genres"], "poster_url": row["poster_path"], "imdb_id": row["imdb_id"], "plot": row["overview"], "plot_embedding": self.create_embedding(row["overview"]), } client.insert_data(table_name, data) if __name__ == "__main__": CSV_FILE = "movies_sample.csv" loader = MovieLoader() print("⏳ Ingestion started...") loader.ingest_csv(CSV_FILE, "recommend.movies") print(f"✅ Finished ingesting {CSV_FILE}") ``` -------------------------------- ### Basic Vector Similarity Search Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Retrieves the top 5 most similar movie entries based on the provided vector embedding. This query utilizes the ANN index for fast retrieval. ```cql SELECT * FROM recommend.movies ORDER BY plot_embedding ANN OF [0.12, -0.34, ...] LIMIT 5; ``` -------------------------------- ### Vectorize and Insert Data into ScyllaDB Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Function to process text nodes, create embeddings, and insert them along with their text content and unique IDs into a specified ScyllaDB table. ```python def vectorize(self, nodes, target_table: str): db_client = ScyllaClient() for node in nodes: chunk_id = uuid.uuid4() text = node.get_content() embedding = self.create_embedding(text) db_client.insert_data(target_table, {"text": text, "chunk_id": chunk_id, "embedding": embedding}) ``` -------------------------------- ### Document Chunking with SemanticDoubleMergingSplitterNodeParser Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Splits loaded documents into semantically meaningful chunks using LlamaIndex's SemanticDoubleMergingSplitterNodeParser. This process is crucial for effective retrieval in RAG systems. ```python from llama_index.core.node_parser import ( SemanticDoubleMergingSplitterNodeParser, LanguageConfig, ) from llama_index.core import SimpleDirectoryReader class ScyllaRag(): def create_chunks(self, dir_path: str, files_limit=1): documents = SimpleDirectoryReader(input_dir=dir_path, recursive=True, num_files_limit=files_limit, required_exts=[.md, .rst], exclude_empty=True, exclude_hidden=True).load_data() # Filter out docs with no text documents = [doc for doc in documents if doc.text.strip()] splitter = SemanticDoubleMergingSplitterNodeParser( language_config=LanguageConfig(spacy_model=en_core_web_md), initial_threshold=0.4, # merge sentences to create chunks appending_threshold=0.5, # merge chunk to the following sentence merging_threshold=0.5, # merge chunks to create bigger chunks max_chunk_size=2048, ) return splitter.get_nodes_from_documents(documents, show_progress=True) ``` -------------------------------- ### Fetch Relevant Chunks from ScyllaDB Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Implements a function to perform a semantic search in ScyllaDB. It generates an embedding for the user query and uses Approximate Nearest Neighbor (ANN) search to retrieve the most relevant text chunks. ```python def fetch_chunks(self, table: str, user_query: str, top_k=5): db_client = ScyllaClient() user_query_embedding = self.create_embedding(user_query) db_query = f"""SELECT chunk_id, text FROM {table} ORDER BY embedding ANN OF %s LIMIT %s; """ values = [user_query_embedding, top_k] return db_client.query_data(db_query, values) ``` -------------------------------- ### Create Embeddings with Ollama Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/rag-chatbot-scylladb.md Function to generate vector embeddings for text content using the Ollama library and a specified embedding model. ```python import ollama EMBEDDING_MODEL = "hf.co/CompendiumLabs/bge-base-en-v1.5-gguf" def create_embedding(self, content): return ollama.embed(model=self.EMBEDDING_MODEL, input=content)["embeddings"][0] ``` -------------------------------- ### Genre-Filtered Vector Similarity Search Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/rag/get-started.md Performs a vector similarity search within a specific genre ('Science Fiction'). This query leverages the local index on the 'movies_by_genre' table, avoiding 'ALLOW FILTERING' and ensuring efficient retrieval. ```cql SELECT * FROM recommend.movies_by_genre WHERE genre = 'Science Fiction' ORDER BY plot_embedding ANN OF [0.12, -0.34, ...] LIMIT 5; ``` -------------------------------- ### Movie Pydantic Model Source: https://github.com/scylladb/vector-search-examples/blob/main/docs/source/semantic-search/movie-recommendation.md Defines the data structure for movie information, including fields for title, release date, genre, and plot embeddings. ```python from pydantic import BaseModel from datetime import datetime from typing import Optional class Movie(BaseModel): id: int title: Optional[str] = None release_date: Optional[datetime] = None tagline: Optional[str] = None genre: Optional[str] = None poster_url: Optional[str] = None imdb_id: Optional[str] = None plot: Optional[str] = None plot_embedding: Optional[list[float]] = None ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.