Bagel provides APIs that enable secure data retrieval and manipulation. This tutorial demonstrates how to leverage these APIs to manage and analyze data while maintaining privacy and security.
Prerequisites - for JavaScript
To ensure that all the code in the tutorial is working properly, you can follow these steps:
Make sure you have node >= v14 installed
Install the Bagel JavaScript client, run the following command in your terminal: npm install bagelml
Create a CSV file containing product data. You can use any text editor or spreadsheet software like Microsoft Excel or Google Sheets to create this file. Save the file with the name product_catalog.csv in your project directory or any directory you prefer.
Note: You can also create a JavaScript code snippet to Generate Mock Data, here's the example we're using for this tutorial:
You will need to install the following packages for the Mock Data Generation: Fs: npm install fs Json2csv: npm install json2csv
Before we can begin managing our asset, we need to create a new vector asset within Bagel. This can be achieved using the POST /api/v1/asset endpoint:
import { Settings, Client } from"bagelml"// Settings configconstsettings=newSettings({ bagel_api_impl:"rest", bagel_server_host:"api.bageldb.ai",})// Create Bagel clientconstclient=newClient(settings)constapi_key="insert api key"// e.g "2347898767897ghjghjdsdnuisd8"constpayload= { dataset_type:"input dataset type",// e.g "RAW", "VECTOR", "MODEL" title:"Input title",// e.g "Apple" category:"inpute category",// e.g "Cat1", "Cat2" details:"input details",// e.g "testing" tags: ["Optional input"],// e.g ["AI", "DEMO", "TEST"] or [ ] user_id:"input user id"// e.g "345281182308180743453"}// Calling the create asset methodconstcreateAsset=async () => {// get response of created assetconstres=awaitclient.create_asset(payload, apiKey)console.log(res)}createAsset()
Step 2: Uploading Product Data
Upload the product catalog to your asset using a POST request (Vector Asset). This code uses the requests library to send a POST request to Bagel's API endpoint, providing the asset ID and the file data (product_catalog.csv). The response indicates whether the upload was successful:
Next, you can add embeddings to a vector asset. This involves sending a POST request with metadata, document details, and IDs in the payload. The function formats the URL with the asset ID, sends the request, and processes the response, console logging the result.
import { Settings, Client } from"bagelml"// Settings configconstsettings=newSettings({ bagel_api_impl:"rest", bagel_server_host:"api.bageldb.ai",})// Create Bagel clientconstclient=newClient(settings)constapi_key="input API key"// e.g "67YUtJPByf8qnUihA388378GYsZ766HG"constasset_id="input dataset id"// e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"constpayload= { metadatas: [{"input metadata"}],// e.g [{ "source": "testing" }] documents: ["input documents"],// e.g ["Hi man"] ids: ["input id"] // e.g ["jkfbnjfd-t84urb54hurugb-uuybdiubviwd"] this is manually generated by you}// Calling the add data to asset methodconstaddEmbedding=async () => {constres=awaitclient.add_data_to_asset(asset_id, payload, api_key)console.log(res)}addEmbedding()
Step 4: Retrieving Product Data/Downloading file
Now that our product data has been uploaded, we can retrieve it using the GET /api.bageldb.ai/api/v1/jobs/asset/{}/files/{} endpoint:
This code sends a GET request to the /api.bageldb.ai/api/v1/jobs/asset/{}/files/{} endpoint, providing the asset_id as a path parameter. The response contains the product data.
Step 5: Querying Assets
Bagel allows you to filter and retrieve specific subsets of data. This involves sending a POST request with a JSON payload containing the query parameters. The function formats the URL with the asset ID, sends the request, and processes the response, console logging the result or any error details.
You can update existing asset details by sending a PUT request. This code constructs the URL with the asset ID, sends the request with the updated payload, and console logs the response. If there's an error, it console logs the error details: