Notebooks
M
MongoDB
Mongodb Langchain Js Memory

Mongodb Langchain Js Memory

agentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag

Adding Chat History to your RAG Application using MongoDB and LangChain.js

In this notebook, we will see how to use the new MongoDBChatMessageHistory in your RAG application.

Step 1: Set up environment

You may be used to Jupyter notebooks that use Python, but this notebook uses JavaScript, specifically Deno.

To run this notebook, you will need to install Deno and set up the Deno Jupyter kernel.

Here are the basic instructions:

  • Install Deno:
    • MacOS / Linux: curl -fsSL https://deno.land/x/install/install.sh | sh
    • Windows: irm https://deno.land/install.ps1 | iex
  • Install Python: Download and install from python.org
  • Install Jupyter: pip install jupyterlab
  • Install Deno Jupyter kernel: deno jupyter --unstable or deno jupyter --unstable --install to force installation of the kernel.

Step 2: Setup pre-requisites

  • Set the MongoDB connection string. Follow the steps here to get the connection string from the Atlas UI.

  • Set the OpenAI API key. Steps to obtain an API key as here

[1]

Step 3: Download the dataset

We will be using MongoDB's embedded_movies dataset

[2]
[3]
[4]

Step 4: Create a simple RAG chain using MongoDB as the vector store

[5]
[6]
{ acknowledged: true, deletedCount: 1500 }
[7]
{ acknowledged: true, insertedCount: 1500 }
[8]
[9]
[10]
[11]
[12]
"Once a Thief is a good movie to watch when sad because it is an action/comedy/romance film that can "... 103 more characters

Step 5: Create a RAG chain with chat history

[13]
[14]
[15]
[16]
[17]
[18]
'The best movie to watch when feeling sad would likely be "Last Action Hero." This film combines acti'... 326 more characters
[19]
`In that case, you may find "Smilla's Sense of Snow" to be a good choice. This film is a mystery thri`... 311 more characters
[20]
'For a lighter movie option, you may enjoy "Nicotina." This crime caper set in Mexico City is filled '... 372 more characters