Giter Site home page Giter Site logo

rosebud's Introduction

Rosebud ๐ŸŒน

Let's discover films.

Try out the app here: link.

Rosebud is a film recommendation Q&A bot:

  • ๐ŸŽฅ Takes in a user's query and returns appropriate film recommendations.
  • ๐Ÿ’ฌ Goes beyond a basic RAG app. Uses both semantic search across the database as well as metadata filters that are created on the fly using natural language.
  • ๐Ÿ—„๏ธ Uses a Pinecone vector database of roughly the 100 most popular films each year from 1950-2023. It will only recommend films from this database.
  • ๐Ÿ’ฏ Incorporates both offline and online evaluation.
  • ๐Ÿ™Œ Totally open-source and privacy preserving.
  • ๐Ÿ“• For more information, please see my most recent article in Towards Data Science. You can also read an earlier article featuring a dev-oriented version of this project.

Overview of Tools Used ๐Ÿ› ๏ธ

  • LangChain: Framework to create the rosebud_chat_model. Important in the creation of the self-querying retriever as well as connecting the OpenAI Chat model to the Pinecone vector store for retrieval augmented generation (RAG).

  • Weights and Biases (W&B)/Weave: Used for evaluation and reproducibility. W&B and Weave two separate tools unified into a single platform for ease of use. Both offline and online evaluations are stored in W&B/Weave. Offline evaluation is performed using RAGAS (see below). Online evaluation is facilitated via the use of ๐Ÿ‘ and ๐Ÿ‘Ž buttons attached to the end of each response. See the image below for an example of the observability: Image of online feedback

  • Prefect: A workflow orchestrator that is used to schedule weekly updates to the data used by the Rosebud chat model (see below).

  • RAGAS: A popular RAG evaluation framework. For this project, RAGAS was used for offline evaluation. A series of 15-20 questions are each passed to the Rosebud chat model, and the responses are parsed by an LLM judge (gpt-4o-mini) for evaluation. There are three metrics that are being tested:

    • AnswerRelevancy(): Measures how relevant the answer is to the question being asked. A score from 0 to 1.
    • ContextRelevancy(): Measures how relevant the retrieved context is to the question being asked. A score from 0 to 1.
    • Faithfulness(): Measures how much the response from the summary model adheres to the retrieved context. A score from 0 to 1.

    These three metrics cover the RAG triad. Check out offline_eval.py for the full evaluation code.

  • pytest: Used for testing. Tests primarily check to make sure that the retrieved data fits the correct format. Check out the tests/ folder for this code.

  • Pinecone: The vector store used to hold the documents describing each film. The fact that Pinecone allows for filtering of films via metadata is critical for this app.

  • Streamlit: Used to create the front-end for the site. Checkout streamlit_app.py for this code.

Uploading Docs to Pinecone

There are four steps necessary to take data from The Movie Database (TMDB) API and upload them to the Pinecone vector store. Code can be found in the pinecone_flow.py file.:

Flow to upload docs to Pinecone

  1. pull_data_to_csv: Programatically pulls roughly 100 of the top films in each year, from 1950 to today, and creates csv files for each year. Makes use of the TMDB API. This code pulls the following attributes from each film:

    • Actors: e.g. ['Christine Taylor', 'Ben Stiller', ...]
    • Buy: e.g. ['Apple TV', 'Amazon Video', ...]
    • Directors: e.g. 'Ben Stiller'
    • Genre: e.g. 'Comedy'
    • Keywords: e.g. 'funny, model, ...'
    • Language': e.g. 'English'
    • Overview: e.g. 'Film about a male model...'
    • Production Companies: e.g. ['Paramount Pictures', 'Village Roadshow Pictures', ...]
    • Rating: e.g. 6.2
    • Release Year: e.g. 2001
    • Rent: e.g. ['Apple TV', 'Amazon Video']
    • Runtime (minutes): e.g. 90
    • Stream: e.g. ['Paramount Plus', ...]
    • Title: e.g. 'Zoolander'
  2. convert_csv_to_docs: Uses LangChain to take all of the csv files corresponding to each year and creates Documents for each film. Each document has two fields: page_content and metadata:

    • page_content: The primary content that the LLM will see for each document. In this project, the page_content contains the movie's title, overview, and keywords. When the RAG app performs similarity search between the user query and the documents in the database, it does so over this text.
    • metadata: Attached to each document, this field stores all of the attributes that can be used to filter out documents before similarity search is done. These fields are: Actors, Buy, Directors, Genre, Keywords, Language, Production, Rating, Release Year, Rent, Runtime (minutes), Stream, and Title.
  3. upload_docs_to_pinecone: The docs are then embedded using the text-embedding-3-small model from OpenAI. The embeddings are then uploaded to the Pinecone vector database programatically.

  4. publish_dataset_to_weave: Finally, we publish the documents to the Weave platform from Weights & Biases for reproducibility.

Building the Self-Querying Retriever

The rosebud_chat_model class contains six methods:

  • initialize_query_constructor: Creates the query constructor prompt. This chat model is capable of self-querying retrieval. This means that the user's query will be used to filter out documents if necessary. The query constructor prompt dictates what sorts of metadata filtering is possible. It also contains a variety of few-shot examples to help guide the model's behavior. Because we use the Pinecone vector store, the following comparators are allowed:

    • $eq: Equal to (number, string, boolean)
    • $ne: Not equal to (number, string, boolean)
    • $gt: Greater than (number)
    • $gte: Greater than or equal to (number)
    • $lt: Less than (number)
    • $lte: Less than or equal to (number)
    • $in: In array (string or number)
    • $nin: Not in array (string or number)

    The following operators are allowed:

    • AND
    • OR

    Currently, the gpt-4o-mini model is used to take the user's query and convert it into simplified query with attached metadata filter. For example, the input Find me drama movies in English that are less than 2 hours long and feature dogs. creates the following query:

    {
        "query": "drama English dogs", 
        "filter": {
            "operator": "and", 
            "arguments": [
                {
                    "comparator": "eq", "attribute": "Genre", "value": "Drama"
                }, 
                {
                    "comparator": "eq", "attribute": "Language", "value": "English"
                }, 
                    
                {
                    "comparator": "lt", "attribute": "Runtime (minutes)", "value": 120
                }
            ]
        },
    }
    
  • initialize_vector_store: Connects the chat bot to the Pinecone vectorstore containing all of the documents. Recall earlier that we used a Prefect flow to create and push the documents to Pinecone.

  • initialize_retriever: Creates the self-querying retriever, which incorporates the query constructor, choice of LLM (gpt-4o-mini), and the Pinecone vectorstore.

  • initialize_chat_model: Creates the summary model, which uses gpt-4o-mini to take in the retrieved film documents from Pinecone and crafts recommendations to answer the user's query. There is a basic template provided here so that the bot creates structured output.

  • predict_stream: The method used to stream predictions to the Streamlit front-end. Chunks are streamed from the model one at a time.

  • predict: The method used to perform offline evaluation using the RAGAS framework. Inputs and outputs to this function are tracked using Weave. The output here is not streamed, and is performed asynchronously to facilitate fast off-line evaluation.

rosebud's People

Contributors

edizaguirre avatar

Stargazers

 avatar  avatar Rhys Edwards avatar  avatar  avatar  avatar Billy Beniar avatar Laxman Singh Tomar avatar Tim Cvetko avatar Todd Walters avatar Panayiotis Tzagkarakis avatar Ryan Garaygay avatar  avatar mikey avatar Sagaya Abinesh avatar Felix Bozzai avatar  avatar Leonardo Motta avatar

Watchers

BizKit avatar Kostas Georgiou avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.