Home > Workload Solutions > Data Analytics > White Papers > Multimodal RAG Chatbot Powered by Dell Data Lakehouse > 4. User Query Processing
When a user submits a query, it is also converted into an embedding. This embedding represents the semantic meaning of the query and allows the system to perform a similarity search in the vector database.
# User Query Embedding
from sentence_transformers import SentenceTransformer
# Initialize the text embedding model (reuse the same model used for text data)
text_embedding_model = SentenceTransformer('text-embedding-ada-002')
# Function to embed a user query
def embed_user_query(query):
# Generate embedding for the user query
query_embedding = text_embedding_model.encode(query)
return query_embedding
# Example user query
user_query = "Top Rated Dell Server and its specifications?"
# Embed the user query
query_embedding = embed_user_query(user_query)
# Output the query embedding
print("Query Embedding:", query_embedding)
The embedded query is used to search the vector database for the most relevant data. This step ensures that the retrieved information closely matches the intent of the user’s query.
# Semantic Similarity Search
import chromadb
from sentence_transformers import SentenceTransformer
import numpy as np
# Initialize the ChromaDB client and load the collection (reuse the previous setup)
chroma_client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory=".chroma_db"
))
collection_name = 'multimodal_embeddings'
collection = chroma_client.get_collection(name=collection_name)
# Initialize the text embedding model (reuse the same model used for text data)
text_embedding_model = SentenceTransformer('text-embedding-ada-002')
# Function to perform semantic similarity search
def perform_similarity_search(query, collection, top_n=5):
# Embed the user query
query_embedding = text_embedding_model.encode(query)
# Retrieve similar embeddings from the collection
results = collection.query(
query_embeddings=[query_embedding.tolist()],
n_results=top_n
)
return results
# Example user query
user_query = "Top Rated Dell Server and its specifications?"
# Perform semantic similarity search
search_results = perform_similarity_search(user_query, collection)
# Output the search results
print("Semantic Similarity Search Results:", search_results)