RAGs To Riches - Part #1 Generative AI & Retrieval
Tags: ai, serverless, beginners, tutorial
Published: 2023-09-04 03:30:00

LambdaRAG Demo: https://github.com/metaskills/lambda-rag

Welcome to my two-part series on using AWS Lambda to build a retrieval-augmented generation (RAG) application with OpenAI. In this first part, we will explore the basics of generative AI and retrieval. In the second part, we will build a RAG application using AWS Lambda, Express, and SQLite VSS.

## Knowledge Navigator

If you have interacted at all this year with OpenAI's [💬 ChatGPT](https://openai.com/chatgpt) you may think we are closer than ever to [🎥 Apple's 1987 Knowledge Navigator](https://www.youtube.com/watch?v=mE2Z30pyw8c). A way to interact with a computer that most of us have only seen in SciFi movies. At first glance today, these language model's knowledge do feel eerily expansive and unlimited. With very little context they can solve interesting problems in highly probabilistic ways.

Yet for knowledge workers, these models are still very limited in their ability to help us. We have all seen the "As of my last update..." or "I don't have real-time..." messages when asking about current events or within a highly specific domain. It is frustrating to hit these roadblocks for those looking to use data, often proprietary, with our friendly Large Language Models (LLMs). After all, half of the video above is an AI resembling Mark Zuckerberg in a bow tie responding to new data. But how?

## Retrieval-Augmented Generation (RAG)

Did you know that "GPT" in ChatGPT stands for [ℹ️ Generative Pre-Trained Transformers](https://en.wikipedia.org/wiki/Generative_pre-trained_transformer)? The key words here are "generative" and "pre-trained". It is easy from these terms to understand that ChatGPT is pre-trained on massive amounts of knowledge and it generates real language responses based on that knowledge. 

A GPT model can learn new knowledge in one of two ways. The first is via model weights on a training set (fine-tuning). The other is via model inputs or inserting knowledge into a context window (retrieval). While fine-tuning may seem like a straightforward method for teaching GPT, it is typically not recommended for factual recall, but rather for [💩 specialized tasks](https://www.youtube.com/watch?v=539DTibvZuA). OpenAI has a great cookbook titled [📚 Question answering using embeddings-based search](https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb) where they make these points on your choices.

<table>
  <thead>
    <tr>
      <th>Fine-Tuning</th>
      <th>Retrieval</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        As an analogy, model weights are like long-term memory. When you fine-tune a model, it's like studying for an exam a week away. When the exam arrives, the model may forget details, or misremember facts it never read.
      </td>
      <td>
        In contrast, message inputs are like short-term memory. When you insert knowledge into a message, it's like taking an exam with open notes. With notes in hand, the model is more likely to arrive at correct answers.
      </td>
    </tr>
  </tbody>
</table>

Open book exam? I'm sold. After all, this is how the internet works today. Information is retrieved over many different protocols, locations, and APIs. If you would like to keep exploring this topic I have included some references below on how on retrieval-augmented generation fits into the current market and the opportunities and tools available today.

- [Meta: Retrieval Augmented Generation](https://ai.meta.com/blog/retrieval-augmented-generation-streamlining-the-creation-of-intelligent-natural-language-processing-models/)
- [IBM: What is retrieval-augmented generation?](https://research.ibm.com/blog/retrieval-augmented-generation-RAG)
- [Microsoft: Vector Search Isn’t Enough](https://build.microsoft.com/en-US/sessions/038984b3-7c5d-4cc6-b24e-5d9f62bc2f0e?wt.mc_ID=Build2023_esc_corp_em_oo_mto_Marketo_FPnews_Elastic)
- [YouTube: Retrieval-Augmented Generation (RAG)](https://www.youtube.com/watch?v=dzChvuZI6D4)

## Basic Architecture

Our RAG's architecture with OpenAI is going to follow this diagram. Our demo application described in the next article of this series will only focus on using OpenAI's [Function Calling](https://platform.openai.com/docs/guides/gpt/function-calling) feature since we are building a stand-alone chat application. If you were building [ChatGPT Plugin](https://openai.com/blog/chatgpt-plugins), that is when you would use the [OpenAPI Definition](https://platform.openai.com/docs/plugins/getting-started/openapi-definition). 

<picture><source srcset="https://raw.githubusercontent.com/metaskills/lambda-rag/main/public/ai-knowledge-retrieval-dark.png" media="(prefers-color-scheme: dark)"><img src="https://raw.githubusercontent.com/metaskills/lambda-rag/main/public/ai-knowledge-retrieval-light.png"></picture>

Our demo will also showcase using [SQLite for Semantic Search](https://observablehq.com/@asg017/introducing-sqlite-vss) as a fast and easy to use vector database that can store and query our [Vector Embeddings](https://dev.to/dandv/understanding-vector-embeddings-18p0). Vector embeddings are numerical representations of complex data like words or images, simplifying high-dimensional data into a lower-dimensional space for easier processing and analysis.

Lastly we are going to need some proprietary data to use with our RAG. I chose the [Luxury Apparel Dataset](https://www.kaggle.com/datasets/chitwanmanchanda/luxury-apparel-data) from Kaggle. It contains ~5,000 products with good descriptions and categories to facet.

## Context Windows

So how much data can we insert into a message when retrieving data? The answer depends on the size of the model's [Context Window](https://www.hopsworks.ai/dictionary/context-window-for-llms). This refers to the maximum number of tokens the model can consider as input for generating outputs. There is a growing trend and demand for LLMs with larger context windows. For instance, some previous generation models could only consider 2,000 token inputs, while some more advanced versions can handle up to 32,000 tokens.

<img src="https://raw.githubusercontent.com/metaskills/lambda-rag/main/public/context-window-siccos-light.png" alt="Word Cloud: data, experience, business, analysis, etc. To the left is the Siccos meme of a man pressed up against a window with a word bubble: Yes... Ha ha ha... YES! On the man's shirt says Generative Pre-Trained Models.">

So is the context window our Moore's Law for LLMs? Some think so. There is even thinking that we get diminishing returns with larger models. While newer Claude models are pushing 100K tokens, some think [Less is More](https://www.pinecone.io/blog/why-use-retrieval-instead-of-larger-context/) and solid retrieval patterns are the key to good results. We should all keep an eye out as we rapidly explore this area of AI.