Welcome to the AI-Shop demo! Dive into the synergy of Atlas Vector Search, Langchain JS, and OpenAI.
- Product Catalog: Display of items.
- AI Assistant: Using LangChain and OpenAI, this assistant provides product suggestions based on varied inputs like recipes or ambiguous requests. With the help of MongoDB Atlas Vector search and dynamic aggregation pipeline we are able to format the results best for the user.
- Craft a prompt for LLM.
- Format data and enhance with embeddings.
- Produce MongoDB vector search aggregations to display relevant results.
- Atlas Project
- Atlas Cluster (free cluster works!)
- App Services Application (e.g.,
AI-Shop
). Ensure linkage. - OpenAI account with API access.
- Navigate to your App Services application.
- Under "Values", create a secret named
openAIKey
with your OPEN AI API key. - Create a linked value named
OpenAIKey
and link to the secret.
- Create a Database trigger on
insert
for Open AI embeddings.- Database:
ai_shop
- Collection:
products
- Database:
exports = async function(changeEvent) {
// Get the full document from the change event.
const doc = changeEvent.fullDocument;
// Define the OpenAI API url and key.
const url = 'https://api.openai.com/v1/embeddings';
// Use the name you gave the value of your API key in the “Values” utility inside of App Services
const openai_key = context.values.get("openAIKey");
try {
console.log(`Processing document with id: ${JSON.stringify(doc)}`);
// Call OpenAI API to get the embeddings.
let response = await context.http.post({
url: url,
headers: {
'Authorization': [`Bearer ${openai_key}`],
'Content-Type': ['application/json']
},
body: JSON.stringify({
input: `${doc.title} - ${doc.description}`,
model: "text-embedding-ada-002"
})
});
// Parse the JSON response
let responseData = EJSON.parse(response.body.text());
// Check the response status.
if(response.statusCode === 200) {
console.log("Successfully received embedding.");
const embedding = responseData.data[0].embedding;
// Get the cluster in MongoDB Atlas.
const mongodb = context.services.get('mongodb-atlas');
const db = mongodb.db('ai_shop'); // Replace with your database name.
const collection = db.collection('products'); // Replace with your collection name.
// Update the document in MongoDB.
const result = await collection.updateOne(
{ _id: doc._id },
// The name of the new field you’d like to contain your embeddings.
{ $set: { embeddings: embedding }}
);
if(result.modifiedCount === 1) {
console.log("Successfully updated the document.");
} else {
console.log("Failed to update the document.");
}
} else {
console.log(`Failed to receive embedding. Status code: ${response.body.text()}`);
}
} catch(err) {
console.error(err);
}
};
- Verify that the function runs under "SYSTEM" context after saving the trigger.
We need to ingest the data under local directory data
:
data/ai_shop.categories.json
into collection namedcategories
(hosting the possible categories in the shop).data/ai_shop.products.json
into collection namedproducts
(hosting the products in the shop).
We can use tools like mongoimport
or compass to import those files. Or we can take the content and "copy" into "Data Explorer" > "INSERT DOCUMENT" tab:
Once data is ingested the trigger should set "embeddings" field under each product.
Create an Atlas Search index by going to the Cluster "Search" tab and specify a new index on:
Use "JSON Editor" and specify a "defualt" index:
{
"mappings": {
"dynamic": true,
"fields": {
"embeddings": {
"dimensions": 1536,
"similarity": "cosine",
"type": "knnVector"
}
}
}
}
Create the index and make sure it is "Active".
Create .env file in the main directory with the following content:
OPEN_AI_KEY='<OPEN_AI_KEY>'
DB_URL='<ATLAS_CONNECTION_URI>'
- The database user must have read/write permissions on
ai_shop
database.
npm install
npm run server
npm run dev
Go to the suggested vite url to browse application:
Use at your own risk. Not an official MongoDB product.