Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel
Ezequiel Jadib 6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
..
74eb448cde
[Node] Update Emulator screenshots
7 years ago
1581e5e147
Updating API keys
7 years ago
6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
3b8f26a2fb
Node - Samples ES5 compliant
7 years ago
02a6c8b719
[Node] Update remaining API Keys and azuredeploy.json templates
7 years ago
3b8f26a2fb
Node - Samples ES5 compliant
7 years ago
28d163ecf6
[Node] Update to botbuilder 3.7.0 (#50)
7 years ago

README.md

You have to be logged in to leave a comment. Sign In

# Similar Products Bot Sample

A sample bot that illustrates how to use the Bing Image Search API to find visually similar products from an image stream or a URL. Here's a demo of this bot in a web chat.

Deploy to Azure

Prerequisites

The minimum prerequisites to run this sample are:

  • Latest Node.js with NPM. Download it from here.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
  • [Recommended] Visual Studio Code for IntelliSense and debugging, download it from here for free.
  • This sample currently uses a free trial Microsoft Bing Search API key with limited QPS. Please subscribe here to obtain your own key and update the BING_SEARCH_API_KEY key in .env file to try it out further.

Code Highlights

Microsoft Bing Image Search API provides a number of modules that allows you to search by image. Check out the reference to know more about the modules available. In this sample we are using the 'SimilarProducts' module to get similar products to an image. We set the 'modulesRequested' parameter to 'SimilarProducts' https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=SimilarProducts

The main components are:

  • image-service.js: is the core component illustrating how to call the Bing Image Search RESTful API.
  • app.js: is the bot service listener receiving messages from the connector service and passing them down to image-service.js and constructing the response.

In this sample we are using the API to get the similar products and send it back to the user. Check out the use of the imageService.getSimilarProductsFromStream(stream) method in app.js.

if (hasImageAttachment(session)) {
    var stream = getImageStreamFromMessage(session.message);
    imageService
        .getSimilarProductsFromStream(stream)
        .then(function (visuallySimilarProducts) { handleApiResponse(session, visuallySimilarProducts); })
        .catch(function (error) { handleErrorResponse(session, error); });
}

And here is the implementation of imageService.getSimilarProductsFromStream(stream) in image-service.js

/** 
 *  Gets the similar products of the image from an image stream
 * @param {stream} stream The stream to an image.
 * @return {Promise} Promise with visuallySimilarProducts array if succeeded, error otherwise
 */
exports.getSimilarProductsFromStream = function (stream) {
    return new Promise(
        function (resolve, reject) {
            var requestData = {
                url: BING_API_URL,
                encoding: 'binary',
                formData: {
                    file: stream
                },
                headers: {
                    'Ocp-Apim-Subscription-Key': BING_SEARCH_API_KEY
                }
            };

            request.post(requestData, function (error, response, body) {
                if (error) {
                    reject(error);
                }
                else if (response.statusCode !== 200) {
                    reject(body);
                }
                else {
                    resolve(JSON.parse(body).visuallySimilarProducts);
                }
            });
        }
    );
};

Outcome

You will see the following when connecting the Bot to the Emulator and send it an image or a URL:

Input:

Sample Outcome

Output:

Sample Outcome

More Information

To get more information about how to get started in Bot Builder for Node and Microsoft Bing Images Search API please review the following resources:

Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...