Open In App

How to Access and Use Google Gemini API Key (with Examples)

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In today's tech-savvy world, artificial intelligence (AI) is changing the way technology is used. The Google Gemini API is a powerful tool that can help create smart applications like chatbots, content generators, and more. This guide will help to build the knowledge through accessing and using the Google Gemini API key, making it easy to bring AI capabilities to projects.

Implement Google Gemini API Key in Python

Step 1: Create a Google Developer Account

  • Visit the Google Developers Website.
  • Sign in with your Google account or create a new one.
  • Click on Get API Key.
Screenshot-2024-06-02-231658
Visit the Home Page

After click on "Get an API Key" click on Create API Key ,

Screenshot-2024-08-01-232028
Create API Key

Step 2: Enable the Gemini API

  • Search for "Generative Language Client" and enable it for the project.
  • Copy the generated key.
Screenshot-2024-08-01-232636
search the API

Next generate the key,

imgonline-com-ua-compressed-Q6ePIkwwvswKMk
Copy the Code

Step 3:Setup the Python Library

Python
pip install google-generativeai

Step 4: Configure the API Key in the Python Code

Python
import google.generativeai as genai

# Configure API key
genai.configure(api_key='your_api_key_here')

Step 5: Create a Generative Model Instance

Python
# Create GenerativeModel instance
model = genai.GenerativeModel('gemini-1.5-flash')

Step 6: Taking output from Gemini API

Python
response = model.generate_content("What is python?")
print(response.text)

Output:

Output-gemini-API
output from Gemini API

Gemini Output:

GEMINI-output

Example: Building a Simple Chatbot with Tkinter

  • tkinter and scrolledtext for the GUI.
  • google.generativeai for the Gemini API.
  • genai.configure(api_key='your_api_key_here') sets up the API key.
  • model = genai.GenerativeModel('gemini-1.5-flash') initializes the model.
  • chat_with_bot(prompt) generates content from the model.
  • send_message() manages user input and chatbot responses.
  • root = tk.Tk() initializes the main window.
  • root.title("Chatbot") sets the window title.
  • chat_window = scrolledtext.ScrolledText(root, state='disabled', wrap=tk.WORD) creates a scrollable text window.
  • chat_window.pack(padx=10, pady=10, fill=tk.BOTH, expand=True) adds the window to the main window.
  • user_entry = tk.Entry(root, width=80) creates an entry field for user input.
  • user_entry.pack(padx=10, pady=10, side=tk.LEFT, expand=True, fill=tk.X) adds the entry field to the main window.
  • send_button = tk.Button(root, text="Send", command=send_message) creates a button to send messages.
  • send_button.pack(padx=10, pady=10, side=tk.RIGHT) adds the button to the main window.
  • root.mainloop() starts the Tkinter event loop to run the application.
Python
import tkinter as tk
from tkinter import scrolledtext
import google.generativeai as genai

# Configure API key
genai.configure(api_key='your//key)

# Create GenerativeModel instance
model = genai.GenerativeModel('gemini-1.5-flash')

# Function to interact with the chatbot
def chat_with_bot(prompt):
    response = model.generate_content(prompt)
    return response.text

# Function to handle sending a message
def send_message():
    user_input = user_entry.get()
    if user_input.lower() == 'exit':
        root.quit()
    else:
        chat_window.config(state=tk.NORMAL)
        chat_window.insert(tk.END, "You: " + user_input + "\n")
        chat_window.config(state=tk.DISABLED)
        user_entry.delete(0, tk.END)
        
        bot_response = chat_with_bot(user_input)
        chat_window.config(state=tk.NORMAL)
        chat_window.insert(tk.END, "Bot: " + bot_response + "\n")
        chat_window.config(state=tk.DISABLED)
        chat_window.see(tk.END)

# Create main application window
root = tk.Tk()
root.title("Chatbot")

# Create chat display window
chat_window = scrolledtext.ScrolledText(root, state='disabled', wrap=tk.WORD)
chat_window.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

# Create user input field
user_entry = tk.Entry(root, width=80)
user_entry.pack(padx=10, pady=10, side=tk.LEFT, expand=True, fill=tk.X)

# Create send button
send_button = tk.Button(root, text="Send", command=send_message)
send_button.pack(padx=10, pady=10, side=tk.RIGHT)

# Run the application
root.mainloop()

Output:

Conclusion

Accessing and using the Google Gemini API key can significantly enhance AI-driven applications. By following the steps outlined above, it is possible to integrate this powerful tool into projects with ease. Whether building chatbots, content generators, or other innovative solutions, the Google Gemini API provides the flexibility and capability needed.


Next Article
Practice Tags :

Similar Reads