Build an application to send Chat Prompts using the Gemini model

A passionate full-stack developer from @ePlus.DEV
Search for a command to run...

A passionate full-stack developer from @ePlus.DEV
No comments yet. Be the first to comment.
Quick and practical tips to help users optimize tasks, improve skills, and solve common problems effectively across various areas like tech, lifestyle, productivity, and more.
Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Vercel AI Gateway thêm Grok Imagine Image 2.0 Preview, đưa image gene

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Cloudflare đang hợp nhất Workers AI và AI Gateway thành một control p

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary GitHub thay đổi hành vi của Code Quality: bật Code Quality sẽ không c

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI thay đổi thời điểm tính phí seat mới của ChatGPT Business từ n

Overview Dataplex is an intelligent data fabric that enables organizations to centrally discover, manage, monitor, and govern their data across data lakes, data warehouses, and data marts to power ana

Labs are timed and cannot be paused. The timer starts when you click Start Lab.
The included cloud terminal is preconfigured with the gcloud SDK.
Use the terminal to execute commands and then click Check my progress to verify your work.
Generative AI on Vertex AI (also known as genAI or gen AI) gives you access to Google's large generative AI models so you can test, tune, and deploy them for use in your AI-powered applications. In this lab, you will:
Connect to Vertex AI (Google Cloud AI platform): Learn how to establish a connection to Google's AI services using the Vertex AI SDK.
Load a pre-trained generative AI model -Gemini: Discover how to use a powerful, pre-trained AI model without building one from scratch.
Send text to the AI model: Understand how to provide input for the AI to process.
Extract chat responses from the AI: Learn how to handle and interpret the chat responses generated by the AI model.
Understand the basics of building AI applications: Gain insights into the core concepts of integrating AI into software projects.
After starting the lab, you will get a split pane view consisting of the Code Editor on the left side and the lab instructions on the right side. Follow these steps to interact with the Generative AI APIs using Vertex AI Python SDK.
Streaming involves receiving responses to prompts as they are generated. That is, as soon as the model generates output tokens, the output tokens are sent. A non-streaming response to prompts is sent only after all of the output tokens are generated.
First we'll explore the chat responses without using stream.
Create a new file to get the chat responses without using stream:
Click File > New File to open a new file within the Code Editor.
Copy and paste the provided code snippet into your file.
from google import genai
from google.genai.types import HttpOptions, ModelContent, Part, UserContent
import logging
from google.cloud import logging as gcp_logging
# ------ Below cloud logging code is for Qwiklab's internal use, do not edit/remove it. --------
# Initialize GCP logging
gcp_logging_client = gcp_logging.Client()
gcp_logging_client.setup_logging()
client = genai.Client(
vertexai=True,
project='qwiklabs-gcp-02-91b282f93f37',
location='us-west1',
http_options=HttpOptions(api_version="v1")
)
chat = client.chats.create(
model="gemini-2.0-flash-001",
history=[
UserContent(parts=[Part(text="Hello")]),
ModelContent(
parts=[Part(text="Great to meet you. What would you like to know?")],
),
],
)
response = chat.send_message("What are all the colors in a rainbow?")
print(response.text)
response = chat.send_message("Why does it appear when it rains?")
print(response.text)
Click File > Save, enter SendChatwithoutStream.py for the Name field and click Save.
Execute the Python file by running the below command inside the terminal within the Code Editor pane to view the output.
/usr/bin/python3 /SendChatwithoutStream.py
The code snippet is loading a pre-trained AI model called Gemini (gemini-2.0-flash-001) on Vertex AI.
The code calls the send_message method of the loaded Gemini model.
The code uses Gemini's ability to chat. It uses the text provided in the prompt to chat.
Now we'll explore the chat responses using stream.
Create a new file to get the chat responses with using stream:
Click File > New File to open a new file within the Code Editor.
Copy and paste the provided code snippet into your file.
from google import genai
from google.genai.types import HttpOptions
import logging
from google.cloud import logging as gcp_logging
# ------ Below cloud logging code is for Qwiklab's internal use, do not edit/remove it. --------
# Initialize GCP logging
gcp_logging_client = gcp_logging.Client()
gcp_logging_client.setup_logging()
client = genai.Client(
vertexai=True,
project='qwiklabs-gcp-02-91b282f93f37',
location='us-west1',
http_options=HttpOptions(api_version="v1")
)
chat = client.chats.create(model="gemini-2.0-flash-001")
response_text = ""
for chunk in chat.send_message_stream("What are all the colors in a rainbow?"):
print(chunk.text, end="")
response_text += chunk.text
Click File > Save, enter SendChatwithStream.py for the Name field and click Save.
Execute the Python file by running the below command inside the terminal within the Code Editor pane to view the output.
/usr/bin/python3 /SendChatwithStream.py
The code snippet is loading a pre-trained AI model called Gemini (gemini-2.0-flash-001) on Vertex AI.
The code calls the send_message_stream method of the loaded Gemini model.
The code uses Gemini's ability to understand prompts and have a stateful chat conversation.
Try it yourself! Experiment with different prompts to explore Gemini's capabilities.
Click Check my progress to verify the objective.
Send the text prompt requests to Gen AI and receive a chat response
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/build-an-application-to-send-chat-prompts-using-the-gemini-model/lab.sh
source lab.sh
Script Alternative
curl -LO https://raw.githubusercontent.com/Itsabhishek7py/GoogleCloudSkillsboost/refs/heads/main/Build%20an%20application%20to%20send%20Chat%20Prompts%20using%20the%20Gemini%20model/abhishek.sh
source abhishek.sh
https://eplus.dev/build-an-application-to-send-chat-prompts-using-the-gemini-model-bb-ide-genai-003