DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • AI-Powered Knowledge Graphs
  • PostgresML: Streamlining AI Model Deployment With PostgreSQL Integration
  • Essential Skills for Modern Machine Learning Engineers: A Deep Dive
  • Explainable AI: Making the Black Box Transparent

Trending

  • The Art of Manual Regression Testing
  • A Complete Guide To Implementing GraphQL for Java
  • Essential Monitoring Tools, Troubleshooting Techniques, and Best Practices for Atlassian Tools Administrators
  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Vector Databases: Unlocking New Dimensions in Video Streaming

Vector Databases: Unlocking New Dimensions in Video Streaming

Explore how vector embeddings and machine learning transform search capabilities in streaming platforms and enhance user discovery and personalization.

By 
Sai Mahesh vuppalapati user avatar
Sai Mahesh vuppalapati
·
Apr. 15, 24 · Tutorial
Like (6)
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

The Data Story 

At the core of every software application, from the simplest to the most complex, operating at scale to serve millions of users with low-latency requests, lies a foundational element: data. For over three decades, relational database management systems (RDBMS) have been at the forefront of this domain. These systems, from simply storing data in a table format consisting of rows for records and columns for attributes, have undergone significant advancements and innovations that have revolutionized structured data and semi-unstructured storage. Relational database models have established themselves as the foundation of structured data handling, are renowned for their reliability, and battle-tested their efficacy in supporting massive big data scales for enterprise applications.

However, as we evolve deeper into the era of big data and artificial intelligence (AI), the limitations of traditional RDBMS in handling unstructured data, such as images, videos, audio, and natural language have become increasingly apparent. Enter the vector database, a cutting-edge innovation tailored for the age of AI and significantly change the recommendation systems. Unlike RDBMS, which excels in managing structured data, vector databases are designed to handle and query high-dimensional vector embeddings, a form of unstructured data representation that is central to modern machine learning algorithms.

Introduction: Vector DB 

Vector embeddings allow complex data like text, images, and sounds to be transformed into numerical vectors, capturing the essence of the data in a way that machines can process. This transformation is crucial for tasks such as similarity search, recommendation systems, and natural language processing, where understanding the nuanced relationships between data points is key. Vector databases leverage specialized indexing and search algorithms to efficiently query these embeddings, enabling applications that were previously challenging or impossible with traditional RDBMS.

Fundamental Difference of RDBMS and Vectors 

The application interacts with the database by executing various transactions and actions, which are stored in the form of rows and columns.

Traditional database diagram

When it comes to the vector database, the action might look a bit different. Below, you can see the different types of files, which will be read and processed by many types of AI models and create vector embeddings. Vector database diagram

Example in Action

Consider the process of transforming a comprehensive movie database, such as IMDB, into a format where each movie is represented by vector embeddings and stored in a vector database. This transformation allows the database to leverage the power of vector embeddings to significantly enhance the user search experience. Because these vectors are organized within a three-dimensional space, search engineers can more efficiently perform queries across the movie database. This spatial organization not only streamlines the retrieval process but also enables the implementation of sophisticated search functionalities, such as finding movies with similar themes or genres, thereby creating a more intuitive and responsive search experience for users.

Movies data table

Now, we will demonstrate in Python how to convert textual movie data, similar to the tables mentioned above, into vector representations using BERT (Bidirectional Encoder Representations from Transformers), a pre-trained deep learning model developed by Google. This process entails several crucial steps for transforming the text into a format that the model can process, followed by the extraction of meaningful embeddings. 

Let's break down each step.

Step 1

Python
 
#Import Libraries 
import sqlite3
from transformers import BertTokenizer, BertModel
import torch


  • sqlite3: This imports the SQLite3 library, which allows Python to interact with SQLite databases. It's used here to access a database containing IMDB movie information.
  • from transformers import BertTokenizer, BertModel: These imports from the Hugging Face transformers library bring in the necessary tools to tokenize text data (BertTokenizer) and to load the pre-trained BERT model (BertModel) for generating vector embeddings.
  • import torch: This imports PyTorch, a deep learning framework that BERT and many other models in the transformers library are built on. It's used for managing tensors, which are multi-dimensional arrays that serve as the basic building blocks of data for neural networks.

Step 2

Python
 
#Initialize Tokenizer and Model
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')


  • tokenizer: This initializes the BERT tokenizer, configuring it to split input text into tokens that the BERT model can understand. The from_pretrained('bert-base-uncased') method loads a tokenizer trained in lowercase English text.
  • model: This initializes the BERT model itself, also using the from_pretrained method to load a version trained in lowercase English. This model is what will generate the embeddings from the tokenized text.

Step 3

Python
 
# Connect to Database and Fetch Movie Data
conn = sqlite3.connect('path/to/your/movie_database.db')
cursor = conn.cursor()
cursor.execute("SELECT name, genre, release_date, length FROM movies")
movies = cursor.fetchall()


  • conn = sqlite3.connect('path/to/your/movie_database.db'): Opens a connection to an SQLite database file that contains your movie data
  • cursor = conn.cursor(): Creates a cursor object which is used to execute SQL commands through the connection
  • cursor.execute(...): Executes an SQL command to select specific columns (name, genre, release date, length) from the movies table
  • movies = cursor.fetchall(): Retrieves all the rows returned by the SQL query and stores them in the variable movies

Step 4

Python
 
#Convert Movie Data to Vector Embeddings
movie_vectors = []
for movie in movies:
    movie_data = ', '.join(str(field) for field in movie)
    inputs = tokenizer(movie_data, return_tensors="pt", padding=True, truncation=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    movie_vector = outputs.last_hidden_state[:, 0, :].numpy()
    movie_vectors.append(movie_vector)


  • movie_vectors = []: Initializes an empty list to store the vector embeddings for each movie
  • For loop: Iterates over each movie retrieved from the database
    • movie_data = ', '.join(...): Concatenates the movie's details into a single string
    • inputs = tokenizer(...): Uses the BERT tokenizer to prepare the concatenated string for the model, converting it into a tensor
    • with torch.no_grad():: Temporarily disables gradient computation, which is unnecessary during inference (model.predict)
    • outputs = model(**inputs): Feeds the tokenized input to the BERT model to get the embeddings
    • movie_vector = ...: Extracts the embedding of the [CLS] token, which represents the entire input sequence
    • movie_vectors.append(movie_vector): Adds the movie's vector embedding to the list

Output

  • movie_vectors: At the end of this script, you have a list of vector embeddings, one for each movie in your database. These vectors encapsulate the semantic information of the movies' names, genres, release dates, and durations in a form that machine learning models can work with.

Example of movie_vectors output

Conclusion

In our example of vector database, movies such as "Inception" and "The Matrix" known for their action-packed, thought-provoking narratives, or "La La Land" and "Eternal Sunshine of the Spotless Mind," which explore complex romantic themes are transformed into high-dimensional vectors using BERT, a deep learning model. These vectors capture not just the overt categories like genre or release year, but also subtler thematic and emotional nuances encoded in their descriptions.

Once stored in a vector database, these embeddings can be queried efficiently to perform similarity searches. When a user searches for a film with a particular vibe or thematic element, the streaming service can quickly identify and suggest films that are "near" the user's interests in the vector space, even if the user's search terms don't directly match the movie's title, genre, or other metadata. For instance, a search for "dream manipulation movies" might not only return "Inception" but also suggest "The Matrix," given their thematic similarities represented in the vector space.

This method of storage and retrieval significantly enriches the user experience on streaming platforms, facilitating a discovery process that aligns content with both the user's interests and current mood. It’s designed to lead to "aha moments," where users uncover hidden gems, especially valuable when navigating the vast catalogs and offerings of streaming services. By detailing the creation and application of vector embeddings from textual movie data, we demonstrate the significant use of machine learning and vector databases in revolutionizing search capabilities and elevating the user experience in digital content ecosystems, particularly within streaming video services.

Big data Deep learning Machine learning Relational database vector database

Opinions expressed by DZone contributors are their own.

Related

  • AI-Powered Knowledge Graphs
  • PostgresML: Streamlining AI Model Deployment With PostgreSQL Integration
  • Essential Skills for Modern Machine Learning Engineers: A Deep Dive
  • Explainable AI: Making the Black Box Transparent

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: