Let’s write some code.

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:

  1. Relevance: The recommendations are more in line with what the user is likely seeking – movies that "feel" like "Inception", not just movies that mention it.
  2. Discoverability: Users discover movies they might never have come across in a traditional search.
  3. User Satisfaction: By meeting or even exceeding user expectations, satisfaction and trust in the recommendation system increase.

  1. Imports:
import 'dotenv/config'
import { Document } from 'langchain/document'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'