Skip to main content

Using Google Artificial Intelligence Services in Couchbase N1QL

 


"If you're not using deep learning, you should be."      Jeff Dean, Google Fellow.

Google has started out with a mission to empower everything and everyone with artificial intelligence (AI). It has open-sourced Tensorflow and supporting libraries to enable developers and enterprises to build and train models, and infer (predict) using those. Building useful enterprise services with this may take time.

Google has also exposed many of the AI services (via Cloud Machine Learning APIs) that can be quite useful in your applications. Extracting features and text from images, translating text from one language to another, and doing sentiment analysis on text can help improve user experience dramatically. There are new businesses and business models enabled by these services alone. Google has done the hard work of building and training the model. You simply call a REST API to exploit their machine learning APIs. These APIs simply act as a declarative service invocation method without needed to understand or maintain the deep infrastructure underneath.

Their machine learning APIs include:

  1. Google Vision API
  2. Google Speech API
  3. Google Natural Language API
  4. Google Translation API
  5. Google Video Intelligence API
  6. And probably many more services by the time this gets published.

These are all provided as REST APIs. See the Google site below for documentation and details of the service.

Image title

In Couchbase 5.0, N1QL R&D engineer, Isha Kandaswamy has developed and has written about the CURL() functionality. Using  CURL(), you can use any of REST services with JSON endpoint. An example of using the Google Map API is shown here. The JSON result from the services can be naturally processed in N1QL because N1QL is designed to be SQL for JSON.

Note: You can use CURL() to use AI or other services from any publicly available service.

This blog shows the way to use Google Cloud AI API directly on the data you have in Couchbase, directly with a simple N1QL statement. All of the Google Cloud Machine Learning APIs return JSON. So, CURL() can be used to call any of the services. I'll show you examples of using Google Vision API, Google translation API, and Google Natural Language API.

Note: You need to setup your project and enable each of the services and obtain the API-KEY which you need to pass. In the examples below, I've removed my key. You should replace PUT YOUR KEY HERE with your key.

Let's explore using some of the APIs.

1. Using Google Vision API

You could be an insurance company where customers upload photos of their cars and identifications. You could be a college receiving images of transcripts. You want to extract the text in the image for veracity, for improved user experience, and to make it easier to search for those when needed.

Google has gone from organizing the world's text to the world's multi-media content. Vision API gives you powerful features to extract a lot of metadata within an image and correlate that information to information and other images available on the web.

Let's get the full analysis of the image using Google Vision API.

This query returns a huge 120K response describing all the polygons, associated texts. Here's the response. Let's issue the query again to and project the things we need. Vision API returns the concatenated list of strings. You can use the SPLIT() or TOKENS() function of N1QL to get each string separately.

2. Google Translation API

This API does what it says: Translate from one language to another. It can automatically detect the source language. Arguments to this API is simply the source content and target language. In this example, let's translate the customer reviews for a hotel from English to French.

The result of the query, which includes translation to French is:

Since I don't understand French well, I had the review translated to Kannada, my mother tongue. I must say, the translation was decent for a machine. Some sentences were convoluted (almost sounded old Kannada), but overall understandable. Here's that translation.

3. Google Natural Language API

The natural language API does sentiment analysis and entity analysis, parses the intent, and much more. You can invoke these services just be changing the parameters of the service.

Let's use the hotel review data, which does have some emotional content!

We've only projected the fields we're interested here, review, magnitude, and sentiment score. See the Google docs for a full description of what these mean and the rest of the data it generates.

Google natural language API can also extract entities, their web references. Let's look at an example of that as well.

The entity analysis API correctly identifies the location and gives us the web references (URLs) to the landmark. When the references are not quite specific, like the William Team Rooms here, the API can give very high-level generic references.

Summary

Couchbase N1QL applications can use Google machine learning APIs easily to improve the customer experience. CURL() provides a flexible method to use the AI services not only from Google but also from other vendors. Try it out.

References

  1. N1QL
  2. CURL() documentation
  3. CURL comes to N1QL

Comments

Popular posts from this blog

Swami Vivekananda: The Monk That Nobody Sent to Chicago

  There’s a saying in Chicago: “We don’t want nobody that nobody sent.” This was the cold reception Swami Vivekananda faced when he arrived in the windy city in July 1893, determined to attend the World Parliament of Religions that September. He belonged to no organization, carried no letter of recommendation, his countrymen were nobody, and represented an alien religion to the Western world. As the days passed, his hope of attending the parliament dwindled. With money running out and the odds stacked against him, he left the Windy City and went to Boston, praying for a glimmer of opportunity.  Swamiji came to America to share India’s most profound gift: the wisdom of the Hindu sages, preserved through centuries of oral tradition and embodied by its monks. This was 1893, not 1993—India was under the British grip, its resources drained, and its spirit subdued. Swamiji’s mission was not just a cultural exchange; it was a bold step toward envisioning a future where India could re...

Why Should Databases Go Natural?

From search to CRM, applications are adopting natural language and intuitive interactions. Should databases follow? This article provides a strategic perspective. Amid the many technological evolutions in software and hardware (CISC/RISC, Internet, Cloud, and AI), one technology has endured:  Relational Database Systems   (RDBMS), aka SQL databases. For over 50 years, RDBMS has survived and thrived, overcoming many challenges. It has evolved and adopted beneficial features from emerging technologies like object-relational databases and now competes robustly with   NoSQL databases .  Today, RDBMS dominates the market, with four of the top five databases and seven of the top ten being relational. RDBMS has smartly borrowed ideas, like JSON support, from NoSQL, while NoSQL has also borrowed from RDBMS. NoSQL no longer rejects SQL. From a user perspective, all modern databases have SQL-inspired query language and a set of APIs. All applications manage the respective data...

iQ Interactive: Cool Things for Developers on Couchbase Capella iQ

  The landscape of software development is ever-evolving with the advent of new technologies. As we venture into 2023, natural language processing ( NLP ) is rapidly emerging as a pivotal aspect of programming. Unlike previous generations of tools that primarily aimed at enhancing coding productivity and code quality, the new generation of Artificial Intelligence ( GenAI ) tools, like iQ, is set to revolutionize every facet of a developer's workflow. This encompasses a wide range of activities: Reading, writing, and rewriting specifications Designing, prototyping, and coding Reviewing, refactoring, and verifying software Going through the iterative cycle of deploying, debugging, and improving the software Create a draft schema and sample data for any use case Natural language queries. Generate sample queries on a given dataset Fix the syntax error for a query Don't stop here. Let your imagination fly. Although the insights garnered from iQ are preliminary and should be treated ...