Google Bard has faced escalating competition from ChatGPT. However, with the rollout of their latest Gemini AI models, they aim to reclaim their position in the market. Google has recently revamped Bard AI assistance with Gemini Pro models to enhance user experience, enabling users to input both text and images for accurate and natural responses.
Access and Use Google Gemini API Key for Beginners, This tutorial delves into the Google Gemini API, offering insights into creating advanced AI-driven applications. Leveraging its robust capabilities, you can seamlessly integrate both text and image inputs to produce precise and contextually relevant outputs. The Gemini Python API streamlines integration into your existing projects, facilitating the effortless incorporation of cutting-edge artificial intelligence functionalities.
Key Highlights Access and Use Google Gemini API Key for Beginners
Setting Up Python and Pip on Your Computer
Visit our guide and install Python along with Pip on your PC or Mac. Ensure that you install Python version 3.9 or above. For Linux users, follow our tutorial to install Python and Pip on Ubuntu or other distributions. You can verify the installation of Python and Pip on your computer by running the following commands in the Terminal. They should return the version numbers.
python -V pip -V
pip install -q -U google-generativeai
import google.generativeai as genai genai.configure(api_key='PASTE YOUR API KEY HERE') model = genai.GenerativeModel('gemini-pro') response = model.generate_content("What is the meaning of life?") print(response.text)
cd Desktop
python gemini.py
Now, the gemini.py file will provide an answer to the question you specified within the code.
To obtain a new response, simply modify the question in the code editor, save the changes, and rerun the gemini.py file. This process allows you to receive a fresh response directly in the Terminal. This demonstrates how you can leverage the Google Gemini API key to access the text-only Gemini Pro model.
import google.generativeai as genai import PIL.Image img = PIL.Image.open('image.jpg') genai.configure(api_key='PASTE YOUR API KEY HERE') model = genai.GenerativeModel('gemini-pro-vision') response = model.generate_content(["what is the total calorie count?", img]) print(response.text)
cd Desktop python geminiv.py
The visual Gemini Pro model provides direct answers to your questions. You can also inquire further and ask the AI to explain its reasoning.
If you want to analyze a different image, ensure that the image filename matches the one specified in the code, update the question accordingly, and rerun the geminiv.py file to receive new responses.
Using the Gemini Pro API Key for Chatting
Thanks to unconv’s concise code on GitHub, you can now engage in a conversation with the Gemini Pro model directly within the Terminal window, utilizing a Gemini AI API key. This eliminates the need to modify the question in the code and rerun the Python file for a new output. You can seamlessly continue the chat within the Terminal window itself.
Moreover, Google has implemented chat history natively, eliminating the need to manually manage conversation history in an array or a list. With a simple function, Google stores all conversation history in a chat session. Here’s how it works:
Open your preferred code editor and paste the provided code below.
import google.generativeai as genai genai.configure(api_key='PASTE YOUR API KEY HERE') model = genai.GenerativeModel('gemini-pro') chat = model.start_chat() while True: message = input("You: ") response = chat.send_message(message) print("Gemini: " + response.text)
3 thoughts on “Best Way to Access and Use Google Gemini API Key for Beginners”