Skip to content

Instantly share code, notes, and snippets.

@luisquintanilla
Created March 18, 2024 15:51
Show Gist options
  • Save luisquintanilla/1d4948e6856e2778eadb6926c5fa3f73 to your computer and use it in GitHub Desktop.
Save luisquintanilla/1d4948e6856e2778eadb6926c5fa3f73 to your computer and use it in GitHub Desktop.
Movie Recommender RAG (SK + Ollama + AOAI Embeddings)
[
{
"title": "Star Wars",
"rating": "PG",
"description": "In a galaxy far, far away, a young farm boy named Luke Skywalker embarks on an epic adventure to rescue Princess Leia from the clutches of the evil Darth Vader. Along the way, he discovers his true heritage and the power of the Force."
},
{
"title": "Raiders of the Lost Ark",
"rating": "PG",
"description": "Archaeologist and adventurer Indiana Jones races against Nazi agents to find the biblical Ark of the Covenant. His quest takes him from the jungles of South America to the deserts of Egypt, facing deadly traps and ancient mysteries."
},
{
"title": "Jurassic Park",
"rating": "PG-13",
"description": "A billionaire creates a dinosaur theme park on a remote island, where genetically engineered dinosaurs roam freely. Chaos ensues when the dinosaurs break free, and a group of visitors must survive the prehistoric predators."
},
{
"title": "Avatar",
"rating": "PG-13",
"description": "In the distant future, a paralyzed marine is sent to the alien world of Pandora to infiltrate the Na'vi tribe and gather information. However, he becomes torn between loyalty to his human superiors and his growing connection to the Na'vi people."
},
{
"title": "The Dark Knight",
"rating": "PG-13",
"description": "Gotham City faces its greatest threat as the Joker, a psychopathic criminal mastermind, wreaks havoc. Batman, with the help of Lieutenant Gordon and District Attorney Harvey Dent, must confront the chaos and make difficult choices."
},
{
"title": "Titanic",
"rating": "PG-13",
"description": "A romantic drama set against the backdrop of the ill-fated maiden voyage of the RMS Titanic. The love story between Jack and Rose unfolds as the ship faces disaster."
},
{
"title": "E.T. the Extra-Terrestrial",
"rating": "PG",
"description": "A heartwarming science fiction film about a young boy who befriends a stranded alien named E.T. Together, they embark on an adventure to help E.T. return home."
},
{
"title": "The Lion King",
"rating": "G",
"description": "An animated musical masterpiece that follows the journey of Simba, a lion cub, as he grows up and learns about responsibility, friendship, and the circle of life."
},
{
"title": "Inception",
"rating": "PG-13",
"description": "A mind-bending thriller where a skilled thief enters people's dreams to steal their secrets. As he takes on an impossible mission, the lines between reality and dreams blur."
},
{
"title": "Frozen",
"rating": "PG",
"description": "An enchanting animated tale of two sisters, Elsa and Anna, in the kingdom of Arendelle. Elsa's magical ice powers inadvertently plunge the kingdom into eternal winter."
},
{
"title": "The Avengers",
"rating": "PG-13",
"description": "A team of superheroes, including Iron Man, Captain America, Thor, and the Hulk, join forces to save the world from a powerful villain."
},
{
"title": "Pirates of the Caribbean: The Curse of the Black Pearl",
"rating": "PG-13",
"description": "Captain Jack Sparrow embarks on a swashbuckling adventure to retrieve a cursed treasure while battling supernatural forces and rival pirates."
},
{
"title": "The Matrix",
"rating": "R",
"description": "A computer hacker discovers that reality is an illusion created by intelligent machines, and he joins a rebellion to free humanity from their control."
},
{
"title": "Finding Nemo",
"rating": "G",
"description": "An animated film about a clownfish named Marlin who embarks on a journey across the ocean to find his son, Nemo, who has been captured by a diver."
},
{
"title": "Gladiator",
"rating": "R",
"description": "A betrayed Roman general seeks revenge against the corrupt emperor who murdered his family, leading to epic battles in the Colosseum."
},
{
"title": "Back to the Future",
"rating": "PG",
"description": "A young inventor accidentally travels back in time to the 1950s using a DeLorean car modified into a time machine. He must ensure his teenage parents fall in love to secure his own existence."
},
{
"title": "Forrest Gump",
"rating": "PG-13",
"description": "Forrest Gump, a simple-minded man with a big heart, narrates his extraordinary life journey. From football star to war hero, shrimp boat captain, and successful businessman, he unwittingly influences historical events."
},
{
"title": "Toy Story",
"rating": "G",
"description": "Animated adventure where toys come to life when humans aren't around. Woody, a cowboy doll, and Buzz Lightyear, a space ranger action figure, learn about friendship and loyalty."
},
{
"title": "The Lord of the Rings: The Fellowship of the Ring",
"rating": "PG-13",
"description": "Epic fantasy adventure based on J.R.R. Tolkien's novel. A diverse group of warriors, including hobbits, elves, and humans, embark on a perilous quest to destroy a powerful ring and save Middle-earth."
},
{
"title": "Spider-Man",
"rating": "PG-13",
"description": "Peter Parker, a high school student bitten by a radioactive spider, gains superpowers. He becomes Spider-Man, fighting crime while balancing his personal life and responsibilities."
},
{
"title": "Shrek",
"rating": "PG",
"description": "An animated comedy film where an ogre named Shrek embarks on a quest to rescue Princess Fiona from a dragon-guarded tower. Along the way, he discovers the true meaning of friendship and love."
},
{
"title": "The Sixth Sense",
"rating": "PG-13",
"description": "A psychological thriller about a child psychologist who treats a young boy who claims to see ghosts. As the doctor delves deeper into the boy’s world, he uncovers shocking secrets."
},
{
"title": "Transformers",
"rating": "PG-13",
"description": "An action-packed sci-fi film where giant alien robots, known as Transformers, battle for control of Earth. The fate of humanity hangs in the balance."
},
{
"title": "Gravity",
"rating": "PG-13",
"description": "A gripping space drama where two astronauts struggle to survive after their shuttle is destroyed during a spacewalk. The film explores themes of isolation and resilience."
},
{
"title": "Mad Max: Fury Road",
"rating": "R",
"description": "A post-apocalyptic action film set in a desert wasteland. Max, a lone warrior, joins forces with Furiosa to escape a tyrannical warlord and his army."
},
{
"title": "Interstellar",
"rating": "PG-13",
"description": "A mind-bending sci-fi epic where astronauts embark on a journey through a wormhole to find a new habitable planet for humanity. Themes of love, sacrifice, and cosmic exploration."
}
]
#pragma warning disable
using System.Net.Http.Json;
using System.Text.Json;
using System.Numerics.Tensors;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Embeddings;
// Configure SK
var kernel =
Kernel
.CreateBuilder()
.AddOllamaChatCompletion(
modelId: "llama2",
baseUri: new Uri("http://localhost:11434"))
.AddAzureOpenAITextEmbeddingGeneration(
deploymentName: Environment.GetEnvironmentVariable("AOAI_EMBEDDING_DEPLOYMENTID"),
endpoint: Environment.GetEnvironmentVariable("AOAI_ENDPOINT"),
apiKey: Environment.GetEnvironmentVariable("AOAI_KEY"))
.Build();
// Load data
var data = File.ReadAllText("movies.json");
var movies = JsonSerializer.Deserialize<Movie[]>(data);
// Embedding generation
var embeddingService = kernel.Services.GetRequiredService<ITextEmbeddingGenerationService>();
var movieEmbeddings = new List<MovieEmbedding>();
foreach(var movie in movies)
{
var textToEmbed = $"""
<movie>
<title>{movie.title}</title>
<rating>{movie.rating}</rating>
<description>{movie.description}</description>
</movie>
""";
var embedding =
await embeddingService
.GenerateEmbeddingsAsync(new List<string> { textToEmbed });
movieEmbeddings.Add(new MovieEmbedding(movie, textToEmbed, embedding));
}
// Save
File.WriteAllText("movie_embeddings.json", JsonSerializer.Serialize<List<MovieEmbedding>>(movieEmbeddings));
// Run RAG
Console.Write("Enter your input: ");
var userQuery = Console.ReadLine();
var userQueryEmbedding = await embeddingService.GenerateEmbeddingAsync(userQuery);
var top3Results = movieEmbeddings
.OrderByDescending(m =>
TensorPrimitives.CosineSimilarity(
userQueryEmbedding.Span,
m.embedding.First().Span))
.Take(3)
.Select(m => m.embeddedText);
var prompt =
$"""
You are a helpful AI assistant that recommends movies to users.
You have access to a local database which contains the movie title, description, and MPA rating.
Use only information from this database to generated answers.
User Question: {userQuery}
Movie Database Results:
{String.Join('\n',top3Results)}
""";
await foreach(var text in kernel.InvokePromptStreamingAsync<string>(prompt))
{
Console.Write(text);
};
record Movie(string title, string rating, string description);
record MovieEmbedding(Movie movie, string embeddedText, IList<ReadOnlyMemory<float>> embedding);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment