====================================================================================
This guide outlines the steps to deploy a serverless sentiment analysis API using Google Cloud Functions. The API utilizes the Gemini API for sentiment analysis and can be accessed via a public endpoint.
Author : Hernando Ivan Teddy
- Go to the Google Cloud Console and create a new project.
- Navigate to the "API & Services" section in the Google Cloud Console and generate an API key for your project.
- Save the generated API key securely (e.g.,
api_key: <<api_key>>
).
- Open the Google Cloud Shell within the Google Cloud Console to interact with your project.
- In the Google Cloud Console, find your project ID and note it down (e.g.,
project_id: <<project_id>>
).
- Run the following command to set the active project:
gcloud config set project <<project_id>>
- Enable the necessary APIs for Cloud Functions and Cloud Build by executing the following command:
gcloud services enable cloudfunctions.googleapis.com cloudbuild.googleapis.com run.googleapis.com
- Use Postman (or any API testing tool) to test the generated API key and ensure everything is working as expected.
- Open the Google Cloud Shell and follow these steps to set up your serverless function:
- In Cloud Shell, create a new directory for your project:
mkdir gemini-sentiment-api cd gemini-sentiment-api
- Open the "Editor" tab within Google Cloud Shell to edit your files.
- In the Cloud Shell editor, run the following commands to initialize the Node.js project and install Axios:
npm init -y npm install axios touch index.js follow the tutorial from the instructor
- Or you can simply clone this repository
git clone https://github.com/ndoteddy/Project-Gemini-GDSC-UM.git cd <the directory> npm install
- In the
index.js
file, replace the placeholder API key with the actual Gemini API key.
- Deploy the function to Google Cloud using the following command:
gcloud functions deploy analyzeSentiment \ --runtime nodejs20 \ --trigger-http \ --allow-unauthenticated \ --set-env-vars GEMINI_API_URL="<<GEMINI_API_URL>>",GEMINI_API_KEY="<<GEMINI_API_KEY>>",SERVERLESS_API_KEY="<<your_serverless_api_key_custom_name>>"
- After deployment, Google Cloud will provide the URL for the deployed function along with other deployment details, such as:
- URL:
https://us-central1-<<project_id>>.cloudfunctions.net/analyzeSentiment
- The state should be
ACTIVE
after successful deployment.
- URL:
- Go to the Google Cloud Console and verify that the API is active and operational.
- Now, use Postman or any HTTP client to test the deployed API endpoint to ensure everything is working properly.
- Refer to "curl" file within directory