google-cloud-platformgoogle-cloud-firestoregoogle-cloud-functionsgoogle-cloud-nl

How to save score's associated to NLP API prediction?


So, currently I have my extension sending a piece of text to the NLP API through a cloud function. This piece of text is processed and predicted, and a score is assigned based on the prediction(a sentence could be 0.33 - "important-consent", for example). I am wondering if it is possible to save the sentences with their respective scores into Firestore. Currently, I can only save the sentences but not their scores.

We really would like to have the scores in the Firestore DB because of a threshold limit we are using. If there are no scores, the threshold becomes obsolete.

Here is the cloud function, just in case:

  exports.queryAutoML = (req, res) => {

  const automl = require('@google-cloud/automl');

  const client = new automl.PredictionServiceClient();

  var formattedName = client.modelPath('*********', '**********', '*****************');
  var payload = {
    "textSnippet": {
       "content": req.body,
        "mime_type": "text/plain"
    },
  };
  var request = {
    name: formattedName,
    payload: payload,
  };
  client.predict(request)
    .then(responses => {
    console.log("in success");
    let title = responses[0].payload[0].displayName;
    let score = responses[0].payload[0].classification.score;
    output = [req.body, title, score];
    res.status(200).send(output);
  })
    .catch(err => {
    console.log("in error");
    console.error(err);
  });

Solution

  • Adding Frank van Puffelen's answer as comunnity wiki to improve visibility:

    The snippet provided in the comments link shows how to create a document and upload it to Firestore collection. Therefore what you need to do is use the results of the NLP query as fields to create a document, then just push it using the Firebase SDK.