The web content describes the creation of a Google Chatbot that recommends recipes based on provided ingredients, utilizing Google Chat, Dialogflow, GPT, and Google Cloud Platform.
Abstract
The article outlines a step-by-step guide to building a chatbot that operates within Google Chat and assists users by suggesting recipes based on a list of ingredients they provide. The chatbot architecture involves Google Chat for user interaction, a Node.js frontend service that processes messages and detects user intent via Dialogflow, and a FastAPI backend that generates recipes and images using OpenAI's GPT model. The system is hosted on Google Cloud Run, with the frontend handling basic interactions and the backend managing the complex task of recipe generation. The author provides code snippets and setup instructions for both the frontend and backend components, as well as for integrating Dialogflow for intent detection and entity extraction. The article concludes with a demonstration of the chatbot's functionality and an invitation for feedback.
Opinions
The author expresses a preference for using serverless services like Cloud Run or Cloud Function for deploying the Node.js frontend and FastAPI backend.
The author suggests that the use of a large language model (LLM) such as OpenAI's Davinci is effective for generating recipes from a list of ingredients.
Dialogflow is recommended for its ability to detect user intentions and extract relevant entities, such as ingredients, from user messages.
The author indicates that the architecture described is adaptable, allowing for the replacement of Google Chat with other platforms like Discord, Slack, Teams, or Messenger.
The author emphasizes the importance of prerequisites such as Node.js, Python ā„ 3.9, an OpenAI account with API Key, and Google Cloud access with admin rights.
The author encourages readers to reach out for further assistance or code examples, indicating a willingness to share knowledge and support others in replicating the project.
GenAI based Chatbot š¤ using Google Chat, Dialogflow, GPT and Google Cloud Platform
In this article, I will show how to create a Google Chatbot that recommends a recipe based on a list of ingredients. Before starting, below a demo on how the chatbot will look like at the end ā¤µļø :
Letās start š !
1- Architecture overview :
Our Chatbot will interact with users messages and depending on user āintentā, it will have two behaviours:
Responds ābasicallyā to standard message like āhelloā, āthanksā, āByeā
If the intent is to generate a recipe from ingredients, calls the backend to get a recipe and its image.
Below is an overview of our architecture ⤵ļø:
Google Chat will be used as a chat interface, you can replace it by Discord, Slack, Teams, Messenger ā¦
Node JS based frontend that consumes user messages and intents, send them to the backend and forward the response to Google Chat. Dialogflow will be used to detect intentions ( Welcome, Ask for recipe, Thanks,..) from user message.
FastAPI based backend that gets a list of ingredients from the frontend, calls OpenAI Davinci LLM model to generate a recipe and its image and send them back to the frontend.
Google Cloud RUN will be used to host both backend and frontend services.
A large language model (LLM) is a language model consisting of a neural network with many parameters (typically billions of weights or more), trained on large quantities of unlabelled text using self-supervised learning. LLMs emerged around 2018 and perform well at a wide variety of tasks. This has shifted the focus of natural language processing research away from the previous paradigm of training specialized supervised models for specific tasks.[1]
Prerequisites :
To follow and reproduce all the components of the above architecture , you will need :
Node.js installed.
Python ā„ 3.9 installed.
OpenAI account with API Key.
Google Cloud project with admin access.
Docker.
Now, letās explain how we implemented each step.
2- Frontend Setup:
2.1 Google Chat
In order to interact programmatically with Google Chat, you need to activate the API. Open your project in the API Console. In the list of APIs, click the Google Chat API and then enable. Once the API has been added. Click Manage.
Select the Configuration tab. You need to provide the following information:
App name : the name that will appear in Google chat when interacting with the chatbot.
Avatar image for the app: chatbot logo
Description of the app
App URL : Url for the service that will interact with Google Chat messages. In our context, itās the frontend URL.
Enter the information for the bot, as described below:
For now, leave the field āApp URLā empty, as we will populate it with our Frontend URL later. Finally, click āSaveā.
Now, you can enter the name of the chatbot in Google Chat and start sending message. You will not get any response as we didnāt configured yet the frontend.
2.2 Node JS service :
Now, we will build a basic NodeJS service using the web framework Express.
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
The service will receive messages from Google Chat, process them using the backend and then respond using Javascript Cards with a custom message that depends on user intent !
Iām not going to share all the code in this article but feel free to contact me if you need to reproduce the same app and I will send then the code.
Below the code structure of our application. The frontend folder contains the javascript code that processes the user message, detects the intent and sends back a suitable response :
The main code is below, check the comments to understand the steps :
2.3 Dialogflow and intent detection :
The intents.get_user_intentfunction of the previous code works as follows:
Get the message sent by the user through Google Chat.
Call the āDialogflowā API to get the intent and the inputs provided by the user. In this article, we choose to use Dialogflow to detect intents and extract parameters from userās message, but we can implement an entity detection algorithm to perform the same task.
The code is below :
In order to get the intent using the API, we need to configure and setup Dialogflow service in our GCP project:
Go to the https://dialogflow.cloud.google.com/ and create a new Dialogflow Agent. Choose any name but make sure to put āenā as DEFAULT LANGUAGE :
Now, we are going to create a Dialogflow entity called ingredients. Entity are used to control how data from end-user input is extracted. In our context, it will help to extract ingredients from userās message. For example, if a user send a message ā I want to cook a meal with onion, celery and meatā, Dialogflow will detect that the ingredients entity is present in the message with the value [ āonionā, āceleryā, āmeatā].
Note: donāt forget to check the option āAllow automated expansionā so that Dialogflow will extend the list of synonyms dynamically.
The next step is to setup the intents, in our case, we will create 3 intents :
Welcome : To detect Welcome/Hello phrases.
Ingredients: To detect If a user send a message containing ingredients.
Thanks: To detect Thanks/Bye messages.
The most important intent is āingredientsā as it helps our frontend application to detect that the user submitted a list of ingredients in his message and especially to extract automatically the ingredients and provide it in the Dialogflow APIās response :
You can tests the intents directly in Dialogflow UI :
The last step is to deploy our app in Google Cloud Platform(GCP) environment. The best option on my opinion is to deploy the node JS code on a serverless service like Cloud Run or Cloud function, you can check one of my previous articles to create a Dockerfile (from node:20) and a Terraform code for Cloud RUN. You can also do it manually using the UI in GCP. Donāt forget to update the Google Chat āApp URLā field ( mentionned at the end of the first section) with Cloud Run or Cloud Function Public URL.
3- Backend Setup :
We explained in the previous sections how the user interacts with the NodeJS frontend and how we can extract intents and ingredients from userās message using Dialogflow. Now, if our frontend detects that the intent is to get a recipe from a list of ingredients, how our frontend will generate a recipe ? The solution is to ask a backend service, responsible for heavier tasks, get the response (a recipe and an image) and then respond to the user.
To accomplish this, we will build a FastAPI backend that interacts with OpenAI and generate a recipe based on provided ingredients from the frontend.
The backend code structure is as follows:
The main.pyfile is quite simple: it contains a simple route to perform health check on the API, it basically sends a GET request to the route & hopes to get a ā200ā response code. It acts as a last line of defense in case something goes south. We added also the function ācustom_openapiā to customize our API metadata. The final step is to add our ā/recipeā route.
And below the code of our ā/recipeā route :
I explained the OpenAI code in a previous article but the idea is simple: we call the ādavinciā completion model from OpenAI to get a suggestion of a recipe from a list of ingredients, then we generate an image for the recipe using OpenAI as well. The backend returns the recipe and the image to the frontend.
The last step is to deploy our FastAPI app in Cloud Run. The process is also explained in a previous article.
At the end, we will have two services deployed in Cloud RUN, the backend and the frontend:
4- Demonstration :
Now, itās the funniest part, letās try out our Chatbot !
As we can see, we have two options:
Get a recipe for a meal ( Implemented but not explained in this article)
Get a recipe from a list of ingredients (I will select this option)
I will provide my ingredients :
We got our recipe with the duration, ingredients, and procedure. And it looks quite tasty š !
Conclusion
Hopefully, this article gave you an idea on how to create AI based chatbots on Google Cloud Platform, please let me know if you have any feedback or questions !