We’re going to build a simple semantic search example.
Semantic Search Example: Movie Recommendations
Scenario: Imagine you have a movie database, and you want to build a recommendation system. A user recently watched and loved the movie "Inception" and wants similar movie recommendations.
Traditional Search: A traditional search might look for movies that have the word "Inception" in their titles, descriptions, or reviews. It would return movies like "Inception: The Cobol Job" (a related short film) or any movie where a reviewer mentioned "Inception". This method is very literal and might miss out on movies that have a similar feel or theme but don't explicitly mention "Inception".
Semantic Search: Instead of just looking for the word "Inception", a semantic search engine would understand the deeper themes and vibes of "Inception", such as dream manipulation, layered realities, heist, and mind-bending plots. It would then recommend movies that share these themes, even if they never mention "Inception" or dreams at all.
Recommendations: With semantic search, the user might get recommendations like:
Benefits:
import 'dotenv/config'
import { Document } from 'langchain/document'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'
dotenv/config
: Loads environment variables from a .env
file.langchain/document
: Imports the Document
class, used to structure data for vector indexing.langchain/vectorstores/memory
: Imports the MemoryVectorStore
, an in-memory storage for vectors (semantic representations of data).langchain/embeddings/openai
: Imports the OpenAIEmbeddings
, which interfaces with OpenAI to obtain semantic embeddings for data.