Google's Gemini API provides powerful generative AI capabilities that you can integrate into your applications for various use cases such as chatbots, content generation, and more. In this blog post, we'll explore how to use the Google Gemini API with Python, including a simple example and a Streamlit-based chatbot implementation.
Prerequisites
Before you start, ensure you have the following:
Python installed on your system
An API key for Google Gemini (stored in an environment variable or a secrets file)
Required Python libraries installed (
google-generativeai
andstreamlit
for the Streamlit example)
Installing Dependencies
You can install the required package using:
pip install google-generativeai streamlit
Simple Example: Making a Basic API Call
The following Python script demonstrates how to interact with the Gemini API using a simple function to retrieve the capital of India.
import google.generativeai as genaiimport osAPI_KEY = os.getenv("GEMINI_API_KEY")genai.configure(api_key=API_KEY)model = genai.GenerativeModel('gemini-1.5-flash-8b-exp-0924')response = model.generate_content("tell the capital of India.")print(response.text)
Explanation:
Import the required module.
Retrieve the API key from environment variables.
Configure the Gemini API using
genai.configure
.Initialize the generative model (
gemini-1.5-flash-8b-exp-0924
).Call
generate_content
with a simple prompt.Print the response received from the API.
Creating a Chatbot with Streamlit
For a more interactive experience, let's build a chatbot using Streamlit that integrates with the Gemini API.
Explanation:
Import necessary libraries (
google-generativeai
,streamlit
, andos
).Load the API key from Streamlit secrets.
Configure the Gemini API and initialize the model.
Set up a basic Streamlit UI with a title.
Maintain a chat history in
st.session_state
.Display previous messages in a chat format.
Handle user input and generate AI responses.
Use
st.chat_message
for a clean chat-style interface.
Running the Chatbot
To run the Streamlit chatbot, save the script as app.py
and execute:
streamlit run app.py
This will open a browser window where you can interact with the chatbot.
This Streamlit sample code currently maintains only the chat history for display purposes but does not preserve context in the conversation when sending queries to the Gemini API. Each user input is treated as an independent query, meaning the model does not remember previous interactions.
To maintain context, you can modify the code to concatenate previous messages into a single prompt before sending them to the API. Here's an improved approach:
Modify This Line in the Code:
Replace it with:
Conclusion
Google Gemini API provides an easy way to integrate generative AI into your applications. Whether you need a simple API call for content generation or an interactive chatbot, Gemini's powerful models can help. Try out these examples and explore more advanced features to enhance your AI applications.
AI Course | Bundle Offer (including AI/RAG ebook) | AI coaching
No comments:
Post a Comment