Empowering Insights: Unleashing Google Gemini’s AI API Potential.
Google Gemini is a cutting-edge AI platform that provides advanced natural language processing capabilities, enabling users to interact with vast datasets and derive meaningful insights easily. Leveraging the latest advancements in machine learning and artificial intelligence, Gemini excels in understanding and generating human-like text, making it a powerful tool for many applications. From enhancing customer service with intelligent chatbots to empowering data analysts with sophisticated query handling, Gemini's versatile use cases span industries, driving innovation and efficiency in business operations and beyond.
In this blog, I will walk through an example of how to build a chatbot in the terminal.
To set up and test using the Gemini API in Python on a Debian server, you will need to follow these steps:
- Set Up Your Debian Server, in my case I am using a Ubuntu 22.04 server.
- Ensure your Debian server is up-to-date:
sudo apt update && sudo apt upgrade -y
- Install Python:
- Install Python and necessary packages if they are not already installed:
sudo apt install python3 python3-pip -y
3, Obtain your API key and secret from Gemini. Store them securely. For testing purposes, you can set them as environment variables in your shell:
You can get an API key from https://aistudio.google.com/app/apikey
I have created a Python script to interact with the Gemini API. Below is an example script that acts like a chatbot in the command line. It also can upload files and answer questions on it.
# gemini test app
import os
import config
import google.generativeai as genai
# Configure the API key
genai.configure(api_key=config.API_KEY)
# Initialize the model and start a chat session with an empty history
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
# List to store the history of conversations
conversation_history = []
def read_file(file_path):
if not os.path.isfile(file_path):
print("File not found.")
return None
try:
with open(file_path, 'r') as file:
return file.read()
except Exception as e:
print(f"Error reading file: {e}")
return None
def ask_about_file(content, question):
context = f"File content: {content}\n\nQuestion: {question}"
response = chat.send_message(context)
return response.text
while True:
user_input = input("You: ")
if user_input.strip() == '':
break
if user_input.lower().startswith('load file '):
file_path = user_input[len('load file '):].strip()
file_content = read_file(file_path)
if file_content:
print("File content loaded successfully.")
# Store the file content in conversation history
conversation_history.append((f"Loaded file: {file_path}", ""))
else:
if 'file_content' in locals() and file_content:
response_text = ask_about_file(file_content, user_input)
else:
response = chat.send_message(user_input)
response_text = response.text
# Append the question and response to the conversation history
conversation_history.append((user_input, response_text))
print('\n')
print(f"Bot: {response_text}")
print('\n')
# Print the conversation history when the loop is terminated
print("Conversation History:")
for i, (user_input, bot_response) in enumerate(conversation_history):
print(f"{i + 1}. You: {user_input}")
if bot_response:
print(f" Bot: {bot_response}")
print('\n')
This Python script allows users to interact with a chatbot using Google's Generative AI (Gemini Pro model) and ask questions about the content of files on their computer. Here's a summary of its functionality:
- API Configuration:
- Configures the Generative AI library using an API key stored in a separate
config.py
file for security.
- Configures the Generative AI library using an API key stored in a separate
- Model Initialization:
- Initializes the Gemini Pro chatbot model and starts a chat session with an empty history.
- Conversation History:
- Maintains a history of the entire conversation, including user inputs and bot responses.
- File Reading:
- Allows users to load a file from their computer by typing
load file <file_path>
. - Reads and stores the file content, enabling the chatbot to answer questions based on this content.
- Allows users to load a file from their computer by typing
- Interactive Chat Loop:
- Continuously prompts the user for input.
- If the user input is a file load command, it reads the specified file.
- If the user input is a question, it sends the question (with file content if loaded) to the chatbot.
- Stores and displays the bot's response.
- Exit and History Display:
- Ends the chat loop when the user inputs an empty string.
- Prints the entire conversation history, showing all user inputs and corresponding bot responses.
To use the script, ensure you have the config.py
file with your API key and the required library installed. You can interact with the chatbot and load files to ask questions about their content.
While this is a simple example, it demonstrates the model's abilities. In this example, I can upload files to the model and ask it questions on the date provided.
This can be expanded upon, and we could give the model access to any number of files to process, summarise, or perform analysis on.
Google Gemini API offers free and pay-as-you-go pricing models to suit different needs. The free tier provides limited access for a specified number of requests and tokens at no cost. For higher usage, the pay-as-you-go model charges based on the number of tokens processed, with rates varying by model and prompt length. For example, processing up to 1 million tokens costs $0.35 for input and $1.05 for output in the basic tier, with higher costs for more significant prompts.
For detailed pricing information, visit Google Gemini API Pricing.
In conclusion, Google Gemini is a powerful AI platform revolutionising natural language processing. Its applications span various industries, from chatbots to data analysis, driving innovation and efficiency. By following the steps outlined in this blog, you can harness the capabilities of Gemini and explore its potential further. Happy coding! 🚀