How to connect mongodb atlas with langchain#

1. Connect to your mongodb#

  • You should create a file called credentials_mongodb.json in the same directory of this notebook

  • Update your information as necessary

{
    "host": "cluster0.XXX.mongodb.net",
    "username": "XXX",
    "password": "XXX"
}

Run the following cell to establish a connection to your mongodb

from pymongo import MongoClient # import mongo client to connect
import json # import json to load credentials
import urllib.parse

# load credentials from json file
with open('credentials_mongodb.json') as f:
    login = json.load(f)

# assign credentials to variables
username = login['username']
password = urllib.parse.quote(login['password'])
host = login['host']
url = "mongodb+srv://{}:{}@{}/?retryWrites=true&w=majority".format(username, password, host)

# connect to the database
client = MongoClient(url)

Select your database and collection, for example

database = client['demo_vector_db']
collection = database['reviews']
collection.find_one()  # check connection
{'_id': 2,
 'product': 'coffee',
 'text': 'Espresso had a strong flavor but the aftertaste was harsh.'}

2. Load your API keys#

First step, I would like you to set up langsmith API and google AI studio API

  1. Create an empty .env file in the same working directory of this notebook

Copy the following to the .env file content

# Environment variables for LangSmith
export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="..."

# add google api key for testing
export GOOGLE_API_KEY="..."
  1. Create a LangSmith account & API key

  • Go to LangSmith and sign in. https://smith.langchain.com

  • Log in with your Github account

  • Open Settings → API Keys.

  • Click Create API key (personal) and copy it.

  • Now paste your API key in the .env file

  1. Get a Gemini API key

  • Visit Google AI Studio → Get API Key and sign in.

  • Click Create API key and copy it.

  • Paste it in the .env file

Once you’re done setting up your .env file, run the cell below to import your API keys to the environment of this notebook

from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Access the environment variables
os.environ['LANGCHAIN_TRACING_V2'] = os.getenv('LANGSMITH_TRACING')
os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'
os.environ['LANGCHAIN_API_KEY'] = os.getenv('LANGSMITH_API_KEY')

# get google api key from .env
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY')

3. Create your vector embeddings#

# 🧩 Imports
from langchain import hub
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_google_genai import GoogleGenerativeAIEmbeddings 
from langchain.chat_models import init_chat_model
from langchain_mongodb import MongoDBAtlasVectorSearch
from pymongo import MongoClient
USER_AGENT environment variable not set, consider setting it to identify your requests.
embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-001")
llm = init_chat_model("gemini-2.5-flash", model_provider="google_genai")
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1760641585.873410 43685615 alts_credentials.cc:93] ALTS creds ignored. Not running on GCP and untrusted ALTS is not enabled.
E0000 00:00:1760641585.880439 43685615 alts_credentials.cc:93] ALTS creds ignored. Not running on GCP and untrusted ALTS is not enabled.
E0000 00:00:1760641585.887316 43685615 alts_credentials.cc:93] ALTS creds ignored. Not running on GCP and untrusted ALTS is not enabled.
# create embedding for all documents in the collection
docs = collection.find({})
for doc in docs:
    text = doc['text']
    vector = embeddings.embed_query(text)
    collection.update_one({'_id': doc['_id']}, {'$set': {'embedding': vector}})

collection.find_one()  # check the embedding field has been added
{'_id': 2,
 'product': 'coffee',
 'text': 'Espresso had a strong flavor but the aftertaste was harsh.',
 'embedding': [0.005555155221372843,
  -0.004266100004315376,
  0.0005982645670883358,
  -0.07797335088253021,
  -0.009961280040442944,
  0.010153294540941715,
  0.018066799268126488,
  0.0032630597706884146,
  -0.01573711819946766,
  0.006337878759950399,
  -0.0034090213011950254,
  -0.019130930304527283,
  0.008163469843566418,
  0.010324482806026936,
  0.1423736810684204,
  -0.011681054718792439,
  -0.00299204234033823,
  0.01278656255453825,
  0.002621940802782774,
  -0.012735817581415176,
  0.025707533583045006,
  -0.001461464329622686,
  0.018543118610978127,
  -0.015003417618572712,
  -0.0043244496919214725,
  -0.031390704214572906,
  0.03164766728878021,
  0.03883090615272522,
  0.03823671489953995,
  -0.0022437938023358583,
  0.009748902171850204,
  0.00608069309964776,
  -0.008276988752186298,
  0.009335118345916271,
  0.003786098212003708,
  0.04993116855621338,
  0.012127486057579517,
  -0.02688588574528694,
  0.019850725308060646,
  0.0034327309112995863,
  -0.004832822363823652,
  0.0019935087766498327,
  0.0024030301719903946,
  0.01311071403324604,
  0.0021427590399980545,
  0.0007251973729580641,
  0.01764710433781147,
  0.010702455416321754,
  0.01470960583537817,
  0.0039160591550171375,
  0.009044886566698551,
  0.0017754104919731617,
  0.013486151583492756,
  -0.2084490805864334,
  -0.005826958920806646,
  0.01599925197660923,
  -0.0001523530372651294,
  -0.014881672337651253,
  0.01432903204113245,
  -0.021260932087898254,
  -0.01950567401945591,
  -0.00688994862139225,
  -0.009320436045527458,
  -0.016298046335577965,
  -0.011614127084612846,
  -0.005696875508874655,
  0.009761613793671131,
  -0.0042910026386380196,
  -0.01935391128063202,
  0.013478011824190617,
  0.020549235865473747,
  -0.0021498892456293106,
  -0.0041719344444572926,
  0.003964667208492756,
  -0.014414072968065739,
  -0.010791542939841747,
  0.01980544626712799,
  0.03007454425096512,
  -0.01177888736128807,
  0.021942295134067535,
  0.008802436292171478,
  -0.014760550111532211,
  -0.005429164040833712,
  -0.0032553421333432198,
  -0.011820076033473015,
  0.009395969100296497,
  -0.007487698458135128,
  -0.020488714799284935,
  -0.003470518160611391,
  0.032698288559913635,
  0.003072460414841771,
  0.011141047812998295,
  0.012417847290635109,
  0.021114418283104897,
  -0.00222997204400599,
  0.006094588432461023,
  0.029155079275369644,
  0.010262906551361084,
  0.009767161682248116,
  -0.031095394864678383,
  -0.018764305859804153,
  -0.010411894880235195,
  0.017056912183761597,
  -0.0007218191167339683,
  0.011510140262544155,
  -0.035028740763664246,
  -0.008191513828933239,
  -0.010109791532158852,
  -0.03500102832913399,
  0.018801845610141754,
  -0.0002768785343505442,
  0.01195091288536787,
  0.005839229095727205,
  0.006961130071431398,
  0.0026756017468869686,
  -0.20605221390724182,
  0.01845189556479454,
  -0.00549514964222908,
  -0.0049899364821612835,
  -0.02984689734876156,
  -0.006962842307984829,
  0.011145628057420254,
  0.007819099351763725,
  -0.004739012569189072,
  -0.006249791476875544,
  0.011308410204946995,
  0.008836210705339909,
  0.014783479273319244,
  0.022468607872724533,
  0.034314852207899094,
  -0.012446998618543148,
  -0.015424500219523907,
  0.013774080201983452,
  0.015235701575875282,
  -0.022900890558958054,
  0.021455002948641777,
  0.009847206994891167,
  -0.012842431664466858,
  0.007516181096434593,
  -0.016755685210227966,
  0.024317892268300056,
  0.009174114093184471,
  -0.005254300776869059,
  0.003665771335363388,
  -0.001752551761455834,
  0.003289657412096858,
  0.009267635643482208,
  0.012001421302556992,
  0.014612043276429176,
  -0.003445646958425641,
  0.008923869580030441,
  -0.001221914659254253,
  -0.0009604596416465938,
  -0.00477662542834878,
  -0.007826448418200016,
  -0.05942646786570549,
  -0.007221934851258993,
  0.016942182555794716,
  0.010258976370096207,
  -0.004322083201259375,
  0.021611154079437256,
  -0.009233261458575726,
  0.003931696992367506,
  -0.0005674472195096314,
  -0.013771584257483482,
  -0.02744954451918602,
  0.004717111121863127,
  0.0007633676868863404,
  -0.005217656493186951,
  -0.024515695869922638,
  0.013718369416892529,
  -0.0186905600130558,
  -4.486194666242227e-05,
  0.011157040484249592,
  0.013828922063112259,
  0.0048359758220613,
  0.00826727319508791,
  -0.02198500744998455,
  0.00026745893410407007,
  -0.0033229945693165064,
  -0.0029495612252503633,
  -0.011498897336423397,
  0.00793901365250349,
  -0.014344148337841034,
  -0.013259675353765488,
  -0.008024020120501518,
  0.009348501451313496,
  -0.004083120729774237,
  0.011407474055886269,
  -0.0001928920828504488,
  -0.004467939957976341,
  0.014105484820902348,
  0.0234660767018795,
  -0.01821255497634411,
  0.02931814268231392,
  0.009617453441023827,
  -0.0007711620419286191,
  0.017507590353488922,
  -0.0005842461250722408,
  0.014499903656542301,
  0.0089049581438303,
  0.00022566791449207813,
  -0.01033127959817648,
  0.03119240514934063,
  -0.021575404331088066,
  -0.005457749590277672,
  -0.022482426837086678,
  -0.023172635585069656,
  0.017445586621761322,
  0.026882003992795944,
  -0.029147133231163025,
  -0.013081575743854046,
  0.0021786720026284456,
  -0.00468086265027523,
  -0.0011187250493094325,
  0.0031781380530446768,
  -0.015903331339359283,
  0.013536225073039532,
  0.01252086739987135,
  -0.013076095841825008,
  0.0035015540197491646,
  -0.005877743009477854,
  0.006944747641682625,
  0.019504310563206673,
  -0.02285861410200596,
  0.003724019043147564,
  0.02685384824872017,
  0.023220786824822426,
  -0.008296585641801357,
  0.020146876573562622,
  -0.001999075524508953,
  0.015398725867271423,
  -0.008924813941121101,
  -0.011061728931963444,
  0.015539312735199928,
  -0.015498535707592964,
  0.0020771443378180265,
  -0.013282104395329952,
  0.010185383260250092,
  0.008121212013065815,
  -0.0051490808837115765,
  -0.040198467671871185,
  0.007526451256126165,
  -0.019242053851485252,
  0.0061098020523786545,
  -0.001783839543350041,
  0.0021577670704573393,
  0.005951831582933664,
  -0.003875671187415719,
  -0.008638114668428898,
  0.008684230037033558,
  -0.014573758468031883,
  -0.005226354580372572,
  0.009367208927869797,
  0.02318570949137211,
  -0.003384880954399705,
  -0.010191886685788631,
  -0.007076862268149853,
  0.006063567008823156,
  0.013026931323111057,
  0.0038371251430362463,
  0.009282556362450123,
  -0.0021181947086006403,
  -0.012780774384737015,
  0.0021258064080029726,
  -0.008420692756772041,
  0.005016868468374014,
  0.0190025232732296,
  0.010597769170999527,
  0.018668705597519875,
  0.00930846855044365,
  -0.005445703864097595,
  -0.013167144730687141,
  -0.014190738089382648,
  -0.002972179325297475,
  0.02234591543674469,
  -0.09875843673944473,
  -0.005783785134553909,
  0.021061237901449203,
  -0.02241506800055504,
  0.006195817142724991,
  0.027581334114074707,
  -0.0022416971623897552,
  -0.03160861134529114,
  -0.009642031975090504,
  0.03580179437994957,
  0.005263086408376694,
  -0.02257392182946205,
  -0.011184532195329666,
  -0.004106268286705017,
  -0.0015459273708984256,
  -0.002956321695819497,
  0.023999417200684547,
  -0.0155468275770545,
  -0.009806711226701736,
  -0.042531710118055344,
  -0.02575867809355259,
  -0.02470565401017666,
  -0.0143776535987854,
  -0.0209010299295187,
  -0.028127163648605347,
  -0.016842493787407875,
  -0.02323949709534645,
  0.022720323875546455,
  0.0015399882104247808,
  0.018082814291119576,
  0.024224335327744484,
  -0.006712514441460371,
  -0.008369582705199718,
  -0.01382717676460743,
  0.009318946860730648,
  -0.002551284385845065,
  0.009433361701667309,
  -0.01854497566819191,
  -0.008020095527172089,
  -0.008557691238820553,
  0.009335610084235668,
  0.006932548247277737,
  -0.005605220794677734,
  0.004517380613833666,
  0.008791636675596237,
  -0.009469009935855865,
  -0.009839183650910854,
  -0.012706383131444454,
  -0.023911498486995697,
  0.012497065588831902,
  0.02473914995789528,
  0.00554022379219532,
  0.011954689398407936,
  0.002690880559384823,
  0.01902574673295021,
  0.023267658427357674,
  -0.002631427487358451,
  0.012013033032417297,
  -0.012259824201464653,
  0.027568601071834564,
  -0.007771098054945469,
  -0.014804349280893803,
  -0.001247264794073999,
  -0.013276224955916405,
  0.007824516855180264,
  0.006321220193058252,
  0.008304399438202381,
  0.014970511198043823,
  -0.0029812080319970846,
  -0.007112281862646341,
  -0.010975048877298832,
  0.023287950083613396,
  0.028770988807082176,
  -0.018742457032203674,
  -0.008006976917386055,
  -0.009419913403689861,
  -0.013163222931325436,
  -0.0016141122905537486,
  0.002376585267484188,
  0.029076986014842987,
  -0.0009412083891220391,
  -0.02208118699491024,
  0.006507241632789373,
  0.04521109536290169,
  -0.016976259648799896,
  0.004680899903178215,
  0.03309308737516403,
  -0.009003731422126293,
  0.008593657054007053,
  0.010299189947545528,
  0.026313291862607002,
  -0.0027863404247909784,
  2.4805908651615027e-06,
  -0.006077575497329235,
  -0.03396961838006973,
  0.004684421233832836,
  -0.004660394974052906,
  0.021752983331680298,
  0.0008069311152212322,
  0.029612407088279724,
  -0.025524280965328217,
  -0.003630911000072956,
  0.022296585142612457,
  -0.006914297118782997,
  0.00038080179365351796,
  -0.007851976901292801,
  0.0012215643655508757,
  -0.018251556903123856,
  -0.0024592496920377016,
  -0.029920946806669235,
  -0.007182497531175613,
  -0.0010659202234819531,
  0.011235002428293228,
  -0.0051469397731125355,
  -0.018916351720690727,
  0.00587412528693676,
  0.002908263588324189,
  -0.0006669063004665077,
  -0.005340395960956812,
  0.006332171615213156,
  -0.0033404247369617224,
  -0.014745477586984634,
  -0.002327453577890992,
  -0.00502095278352499,
  0.003959972877055407,
  -0.014880545437335968,
  -0.014252223074436188,
  0.004203722812235355,
  -0.016920343041419983,
  -0.004793137777596712,
  -0.00190790556371212,
  0.013854604214429855,
  0.0019134023459628224,
  -0.013595441356301308,
  0.010434242896735668,
  -0.012402103282511234,
  -0.004749046638607979,
  0.0039439015090465546,
  0.022841988131403923,
  0.011795816011726856,
  -0.016439558938145638,
  -0.004066305700689554,
  -0.013439875096082687,
  -0.010195489972829819,
  0.002628292655572295,
  -0.0004664086736738682,
  -0.0049284291453659534,
  0.003389269346371293,
  0.0021075918339192867,
  -0.005839038174599409,
  -0.01778537593781948,
  -0.024819711223244667,
  0.03993655741214752,
  -0.02623705379664898,
  -0.001266182865947485,
  -0.00411238381639123,
  -0.01388506405055523,
  0.0032534748315811157,
  -0.0027436823584139347,
  -0.021878162398934364,
  -0.016834044829010963,
  0.007174034137278795,
  0.017811952158808708,
  -0.029379887506365776,
  0.013699393719434738,
  0.024893680587410927,
  -0.008502176031470299,
  0.012766009196639061,
  0.009189249947667122,
  0.0062317573465406895,
  -0.007343904580920935,
  -0.026343442499637604,
  0.007446461822837591,
  -0.0010901530040428042,
  0.002893089782446623,
  -0.011101929470896721,
  -0.003536282107234001,
  0.011987634003162384,
  8.552755753044039e-05,
  -0.01539964322000742,
  0.012481029145419598,
  0.010685025714337826,
  0.01193560753017664,
  0.019256476312875748,
  -0.007369845639914274,
  -0.024432016536593437,
  -0.002727573737502098,
  -0.0023512509651482105,
  -0.012872040271759033,
  0.009411245584487915,
  -0.006977433804422617,
  -0.017480656504631042,
  -0.006222208961844444,
  0.022323084995150566,
  -0.008472898043692112,
  0.042652104049921036,
  0.0014386237598955631,
  0.026407398283481598,
  -0.003142533591017127,
  0.0013094475725665689,
  0.012516934424638748,
  0.0060690948739647865,
  -0.004023637622594833,
  0.020333394408226013,
  -0.017470158636569977,
  0.0092817023396492,
  -0.02164856530725956,
  0.02954171411693096,
  0.0008502473356202245,
  -0.012302716262638569,
  -0.0030767859425395727,
  0.013472357764840126,
  0.001446394482627511,
  -0.013665490783751011,
  0.01940755359828472,
  0.01365474984049797,
  -0.00833317544311285,
  0.005620633251965046,
  -0.021403644233942032,
  0.003647125093266368,
  -0.005320057738572359,
  -0.03214823082089424,
  0.0032809136901050806,
  -0.010875904001295567,
  0.00791726354509592,
  -0.011866677552461624,
  0.020192982628941536,
  -0.01649930514395237,
  0.00391126424074173,
  0.007246050052344799,
  0.011618319898843765,
  -0.006198281887918711,
  0.024961624294519424,
  -0.018487315624952316,
  -0.013223802670836449,
  -0.025636516511440277,
  0.00254996120929718,
  0.01170425210148096,
  -0.006441804114729166,
  0.038741253316402435,
  -0.006788820493966341,
  0.008492127992212772,
  0.015575977973639965,
  -0.004800580907613039,
  0.02354872040450573,
  -0.008005310781300068,
  -0.0089136166498065,
  0.0014024798292666674,
  -0.0057060448452830315,
  -0.007850258611142635,
  0.0034973458386957645,
  0.002499337773770094,
  0.010217733681201935,
  0.02513434737920761,
  -0.01604832522571087,
  -0.009245404973626137,
  0.009658392518758774,
  0.010403670370578766,
  -0.002968024695292115,
  0.008162279613316059,
  -0.0034479773603379726,
  0.0073181926272809505,
  0.01598544977605343,
  -0.005506761837750673,
  0.0040605035610497,
  -0.0030798683874309063,
  0.011666124686598778,
  -0.0037318405229598284,
  0.003017841838300228,
  -0.0761696994304657,
  0.00020930668688379228,
  0.0026878290809690952,
  -0.01663893088698387,
  -0.014966544695198536,
  0.008615984581410885,
  -0.02874622493982315,
  -0.02291988581418991,
  0.0014630233636125922,
  0.00859652180224657,
  0.0018400471890345216,
  0.000833021302241832,
  -0.023251596838235855,
  -0.008592857979238033,
  0.006976690609008074,
  0.002939743222668767,
  0.0048088268376886845,
  -0.002911502029746771,
  0.005254627205431461,
  -0.007398051675409079,
  0.01429684180766344,
  0.020146265625953674,
  -0.004601570311933756,
  0.007190281990915537,
  0.006906558759510517,
  0.00987845566123724,
  0.019673490896821022,
  0.012052327394485474,
  -0.013631811365485191,
  -0.004522709641605616,
  0.00334056094288826,
  -0.008249010890722275,
  0.01906796172261238,
  0.03133872523903847,
  -0.0033593890257179737,
  0.013168983161449432,
  0.010724442079663277,
  -0.01521800272166729,
  0.010857241228222847,
  -0.005156821571290493,
  0.015154949389398098,
  -0.013296029530465603,
  -0.008089326322078705,
  -0.00043321281555108726,
  -0.017050817608833313,
  0.00774910906329751,
  -0.01724967546761036,
  -0.003197243670001626,
  0.01204803679138422,
  -0.004119837190955877,
  -0.018151354044675827,
  -0.009441118687391281,
  -0.006887256633490324,
  0.004493250511586666,
  -0.0004678242839872837,
  -0.03190656006336212,
  -0.02641378901898861,
  -0.0007740747532807291,
  -0.010825452394783497,
  -0.007639067247509956,
  0.006906593218445778,
  0.00722137838602066,
  -0.007879587821662426,
  0.02135799080133438,
  -0.008528299629688263,
  0.0045784227550029755,
  -0.007348999381065369,
  0.01629689522087574,
  0.01977195031940937,
  0.008202619850635529,
  -0.004507976118475199,
  0.03752893581986427,
  -0.022694356739521027,
  -0.0017355050658807158,
  -8.751654240768403e-05,
  0.002025602851063013,
  0.003916960675269365,
  -0.02060936763882637,
  -0.004172592423856258,
  0.02635449357330799,
  0.001967238262295723,
  0.014233662746846676,
  -0.10857199132442474,
  -0.0037800578866153955,
  0.008199740201234818,
  -0.006297174375504255,
  0.008660877123475075,
  0.01457522064447403,
  -0.007498118095099926,
  -0.012318476103246212,
  -0.014815007336437702,
  -0.013869999907910824,
  0.010620681568980217,
  0.006210759747773409,
  0.01211821474134922,
  0.011277259327471256,
  -0.01815715804696083,
  -0.00929983425885439,
  0.0042748465202748775,
  -0.016559796407818794,
  -0.01204188633710146,
  0.029974212870001793,
  -0.009492241777479649,
  0.005927585996687412,
  -0.0024745489936321974,
  0.007586533669382334,
  -0.02094806730747223,
  0.012823035940527916,
  0.010702872648835182,
  -0.004775259643793106,
  -0.0018128125229850411,
  0.0034165321849286556,
  -0.0006966533255763352,
  -0.12208109349012375,
  0.00982758216559887,
  0.00590456323698163,
  0.012978630140423775,
  -0.002814540406689048,
  0.029748355969786644,
  -0.0019645735155791044,
  -0.0006933106342330575,
  -0.01312231458723545,
  0.003745059482753277,
  -0.004033427219837904,
  -0.013963509351015091,
  -0.033398594707250595,
  -0.014419414103031158,
  0.00531662767753005,
  0.15716160833835602,
  -0.026474520564079285,
  0.010720701888203621,
  -0.027336351573467255,
  -0.02371145971119404,
  -0.010713552124798298,
  0.002731294371187687,
  -0.0010586531134322286,
  0.006356535945087671,
  -0.016316678375005722,
  -0.004398318938910961,
  0.011180614121258259,
  0.011509181000292301,
  0.01821318082511425,
  0.0037020090967416763,
  0.002401451813057065,
  -0.011001087725162506,
  -0.013644284568727016,
  0.010413343086838722,
  -0.019802311435341835,
  -0.009424633346498013,
  0.009309225715696812,
  -0.018321635201573372,
  -0.0016348683275282383,
  -0.019602956250309944,
  0.007420602720230818,
  0.018569985404610634,
  -0.013938588090240955,
  -0.006633222568780184,
  0.021718371659517288,
  -0.0038226305041462183,
  -0.007308351341634989,
  0.01922258362174034,
  0.0013662688434123993,
  -0.020875228568911552,
  -0.03606221452355385,
  -0.06794814020395279,
  0.01515414658933878,
  0.010201623663306236,
  -0.017281923443078995,
  -0.010378164239227772,
  0.012573589570820332,
  0.004858742468059063,
  0.01259024627506733,
  0.012549427337944508,
  -0.013428563252091408,
  -0.029224686324596405,
  0.010496987029910088,
  -0.001553274691104889,
  -0.009226401336491108,
  -0.008486635982990265,
  0.00450051948428154,
  0.026254354044795036,
  -0.003436326514929533,
  -0.01192696113139391,
  0.007759061176329851,
  -0.006902163382619619,
  0.0017734009306877851,
  0.005404980853199959,
  -0.006116413976997137,
  0.01582539826631546,
  -0.0009626179235056043,
  -0.01580340415239334,
  0.008833185769617558,
  -0.0012771126348525286,
  0.015246743336319923,
  -0.030679965391755104,
  0.02163723297417164,
  -0.008963645435869694,
  0.004487514030188322,
  -0.013332299888134003,
  0.01461744774132967,
  0.008810000494122505,
  0.012682659551501274,
  -0.014318536967039108,
  -0.031046727672219276,
  -0.008381388150155544,
  0.011064646765589714,
  0.007815483957529068,
  0.004200555384159088,
  0.021618593484163284,
  0.006635876372456551,
  -0.017593422904610634,
  0.004249190911650658,
  -0.0011318761389702559,
  -0.004951195325702429,
  -0.005436955485492945,
  0.023108670487999916,
  -0.022888977080583572,
  0.002307188929989934,
  0.017936857417225838,
  -0.012584702111780643,
  0.004713654052466154,
  0.028261976316571236,
  0.006296401843428612,
  0.011351392604410648,
  -0.0017293356359004974,
  -0.013406261801719666,
  0.00813033152371645,
  -0.002587254624813795,
  0.004708291497081518,
  0.006576710380613804,
  -0.021034693345427513,
  0.015705330297350883,
  0.006210286170244217,
  -0.0018514612456783652,
  0.012205936945974827,
  -0.011160006746649742,
  -0.0005736566963605583,
  -0.0006060792948119342,
  0.006985432468354702,
  -0.0038261457812041044,
  -0.00026425125543028116,
  -0.012006963603198528,
  -0.013253692537546158,
  0.003929325379431248,
  -0.005242996849119663,
  0.0020008680876344442,
  0.00859356950968504,
  -0.006541818846017122,
  0.0009785810252651572,
  0.006236096378415823,
  -0.010458353906869888,
  -0.0014297334710136056,
  -0.012425144203007221,
  0.015176885761320591,
  -0.003966826014220715,
  0.006381496321409941,
  0.0034419670701026917,
  -0.0025724321603775024,
  -0.009850417263805866,
  0.006264613009989262,
  0.0072373864240944386,
  -0.0008605252369306982,
  -0.005470774136483669,
  -0.0014793963637202978,
  -0.0038877574261277914,
  0.006632787641137838,
  0.005604472476989031,
  0.007729704026132822,
  0.0042293802835047245,
  -0.007796047255396843,
  0.009561865590512753,
  -0.005171754863113165,
  0.006801794748753309,
  -0.0009597750613465905,
  0.007680834271013737,
  0.009413739666342735,
  0.006004126742482185,
  -0.005386251490563154,
  -0.006332602817565203,
  -0.0020765927620232105,
  -0.012165258638560772,
  0.004393525887280703,
  -0.006466348189860582,
  -0.011523641645908356,
  -0.0015206113457679749,
  -6.170921551529318e-07,
  -0.01466639619320631,
  0.0010203900747001171,
  -0.01649702899158001,
  -0.01640026643872261,
  0.0021332402247935534,
  -0.018054787069559097,
  -0.0029076687060296535,
  -0.00034304946893826127,
  -0.016392456367611885,
  -0.0020667898934334517,
  0.013568231835961342,
  0.01236146129667759,
  0.014552942477166653,
  -0.009075508452951908,
  -0.0020180901046842337,
  -0.0104061933234334,
  -0.00369266583584249,
  -0.006550499238073826,
  -0.006901730317622423,
  0.01616784557700157,
  -0.009700531139969826,
  0.011631783097982407,
  0.009211007505655289,
  0.0036386563442647457,
  0.010015914216637611,
  0.007376419845968485,
  0.007837527431547642,
  -0.018512913957238197,
  -0.009413579478859901,
  -0.0006153142312541604,
  -0.0005571111687459052,
  0.002723782556131482,
  0.0006684058462269604,
  -0.002664637751877308,
  -0.000163757023983635,
  0.0015347751323133707,
  -0.005125078838318586,
  0.001353251514956355,
  0.011128413490951061,
  0.011422201059758663,
  -0.009352262131869793,
  0.0017218022840097547,
  -0.010298265144228935,
  0.0068473005667328835,
  0.009465161710977554,
  0.0018042539013549685,
  0.007799413520842791,
  -0.005049539729952812,
  -0.0006904217880219221,
  -0.00302944821305573,
  -0.0021478054113686085,
  0.0072256396524608135,
  -0.0015900490107014775,
  0.0068811411038041115,
  -0.013139712624251842,
  -0.012842885218560696,
  0.017149850726127625,
  -0.009667841717600822,
  0.0012941986788064241,
  -0.011477462016046047,
  -0.009026260115206242,
  -0.018173187971115112,
  -0.014310416765511036,
  0.006887416355311871,
  0.002847254043444991,
  -0.007906566374003887,
  -0.014452211558818817,
  0.016604909673333168,
  0.014818539842963219,
  5.3981980272510555e-06,
  -0.006010707933455706,
  -0.0007241217535920441,
  -0.0026764688082039356,
  -0.0018365742871537805,
  0.0069756899029016495,
  -0.013523595407605171,
  0.005620335228741169,
  -0.007342300843447447,
  0.0054211365059018135,
  -0.00048477857490070164,
  0.0037154199089854956,
  0.00890249852091074,
  0.020300691947340965,
  -0.010093211196362972,
  0.0021015419624745846,
  0.003950080834329128,
  0.008576205000281334,
  -0.010045063681900501,
  -0.012278622947633266,
  0.01530997920781374,
  -0.0011028940789401531,
  0.001185231376439333,
  -0.00721816485747695,
  0.003877075621858239,
  -0.007758824620395899,
  -0.010080057196319103,
  0.008028826676309109,
  0.007670106831938028,
  -0.004578099120408297,
  -0.012662687338888645,
  0.005221260245889425,
  -0.012764638289809227,
  0.0028586701955646276,
  0.0016132695600390434,
  -0.0010788283543661237,
  0.008232434280216694,
  0.008440977893769741,
  -0.002302722306922078,
  -0.014999816194176674,
  -0.024598542600870132,
  -0.022385982796549797,
  -0.008438372053205967,
  0.007206795271486044,
  0.02648664265871048,
  0.011893313378095627,
  -0.008916201069951057,
  0.0015941881574690342,
  0.010711569339036942,
  -0.009498834609985352,
  -0.02120373025536537,
  -0.013661887496709824,
  -0.00933101773262024,
  0.014327884651720524,
  0.00405989121645689,
  0.00245624128729105,
  -0.006748086307197809,
  -0.00858114194124937,
  -0.0009317205986008048,
  0.017626171931624413,
  -0.01592816226184368,
  -0.0058618881739676,
  -0.0014996664831414819,
  -0.019632235169410706,
  0.00226496416144073,
  -0.003937458153814077,
  0.0047215125523507595,
  0.0009311648900620639,
  0.12169704586267471,
  -0.0035369545221328735,
  0.0016472243005409837,
  0.012496555224061012,
  -0.005916299298405647,
  0.015869488939642906,
  0.00587789062410593,
  -0.0004317459824960679,
  -0.008505643345415592,
  0.026908783242106438,
  0.0010894779115915298,
  0.008221074007451534,
  -0.008175414986908436,
  -0.0045756264589726925,
  -0.00022643370903097093,
  0.012007403187453747,
  0.0017466912977397442,
  0.012469761073589325,
  0.0014906906289979815,
  0.0072685000486671925,
  -0.0032639317214488983,
  0.007466514129191637,
  0.013281636871397495,
  -0.006905226036906242,
  -0.0035982246045023203,
  0.005582281854003668,
  0.0020932748448103666,
  -0.00021301195374689996,
  -0.01872307062149048,
  0.009772016666829586,
  0.002860432490706444,
  -0.0024219690822064877,
  ...]}

4. Create a vector store#

# Now let's create a vectorstore
vector_store = MongoDBAtlasVectorSearch(
    collection=collection, # use the collection we created above
    embedding=embeddings, # use the embedding model we created above
    index_name="vector_index_1", # name of the index
    relevance_score_fn="cosine"
)
retriever = vector_store.as_retriever()
retriever.invoke("Give me a salad")
[Document(id='4', metadata={'_id': 4, 'product': 'salad'}, page_content='Fresh greens and a light dressing—very refreshing.'),
 Document(id='3', metadata={'_id': 3, 'product': 'pizza'}, page_content='Crispy crust, generous toppings, and the sauce was tangy.'),
 Document(id='5', metadata={'_id': 5, 'product': 'cake'}, page_content='Moist chocolate cake with rich frosting. A perfect dessert.'),
 Document(id='1', metadata={'_id': 1, 'product': 'coffee'}, page_content='The latte was smooth and not too bitter. Loved it!')]

5. Create your prompt template#

# Multi Query: Different Perspectives
template = """You are an AI language model assistant. Your task is to generate five 
different versions of the given user question to retrieve relevant documents from a vector 
database. By generating multiple perspectives on the user question, your goal is to help
the user overcome some of the limitations of the distance-based similarity search. 
Provide these alternative questions separated by newlines. Original question: {question}"""
prompt_perspectives = ChatPromptTemplate.from_template(template)

from langchain_core.output_parsers import StrOutputParser

generate_queries = (
    prompt_perspectives 
    | llm 
    | StrOutputParser() 
    | (lambda x: x.split("\n"))
)
# get the questions
questions = generate_queries.invoke({"question": "What is something sweet and fruity?"})
questions
['Describe a dessert or snack that is both sugary and berry-like.',
 'Can you list some foods or drinks that have a sweet and fruit-derived flavor profile?',
 "I'm looking for items characterized by a sugary taste and fruit essence. What comes to mind?",
 'Suggest a treat or beverage that combines sweetness with natural fruit flavors.',
 'What are some delightful edibles or potables known for their blend of sweetness and fruitiness?']
from langchain.load import dumps, loads

def get_unique_union(documents: list[list]):
    """ Unique union of retrieved docs """
    # Flatten list of lists, and convert each Document to string
    flattened_docs = [dumps(doc) for sublist in documents for doc in sublist]
    # Get unique documents
    unique_docs = list(set(flattened_docs))
    # Return
    return [loads(doc) for doc in unique_docs]

# Retrieve
# question = "What is task decomposition for LLM agents?"
retrieval_chain = generate_queries | retriever.map() | get_unique_union
from operator import itemgetter
from langchain_core.runnables import RunnablePassthrough

# RAG
template = """Answer the following question based on this context:

{context}

Question: {question}
"""

prompt = ChatPromptTemplate.from_template(template)
# create runnable chain

final_rag_chain = (
    {"context": retrieval_chain, 
     "question": itemgetter("question")} 
    | prompt
    | llm
    | StrOutputParser()
)
question = "What is something sweet and fruity?"
final_rag_chain.invoke({"question":question})
/var/folders/vp/1v632v093p1cm4_vv68sy8j00000gq/T/ipykernel_50663/825845773.py:10: LangChainBetaWarning: The function `loads` is in beta. It is actively being worked on, so the API may change.
  return [loads(doc) for doc in unique_docs]
"Based on the reviews I have, none of the products are described as both sweet and fruity.\n\nHere's what the reviews mention:\n*   **Cake:** A moist chocolate cake with rich frosting (sweet, but chocolate, not fruity).\n*   **Pizza:** Crispy crust, generous toppings, and tangy sauce.\n*   **Coffee:** An espresso with a strong, harsh flavor, and a smooth, not too bitter latte.\n*   **Salad:** Fresh greens and a light, refreshing dressing.\n\nIt seems like we don't have any fruity options in these reviews!"