From Hello World to Import Generative AI
This blog post marks the beginning of my journey with Generative AI and Large Language Models.
With the advancement of Artificial Intelligence and models like Chat GPT, only a short percentage of population remains who are unaware about AI. This recent boom brought about by Open AI’s Chat GPT (more can be read here) caused many people to rely upon AI models to automate various tasks. Be it to learn more about a particular topic, create general and specific content, summarize data, or even generate answers for assignments, people have started relying heavily on LLMs. Since I am pursuing a degree in data science and have an interest in this particular field, I also decided to jump in on the AI powered action.
My journey began by understanding what Generative AI is. As its name suggests, Generative AI (also referred to as Gen AI) is a discipline of Artificial Intelligence which can produce content in the form of text, audio, image, and other formats. Similar to what I did, you can head over to DeepLearning.AI’s website and enroll in the course “Generative AI for Everyone” for free through this link. More resources can be found online including this article by NVIDIA.
After some background knowledge is gathered, it was time to code.
Prerequisites to Coding
While it may be enticing to get into hard-core programming right away, it is necessary to understand some things beforehand.
- We shall be working within a virtual environment that is powered by Python. This is so that everything we do and all the necessities that we install will be done in an enclosed environment and that will not affect anything else. Naturally, Python must be installed in your device, If Python is not yet installed, head over to the official documentation here. If you are new to creating a virtual environment and don’t know how, head here.
- You would need an API Key of your own in order to use Gemini locally. For doing so, head over to this site and click on the “Get API Key in Google AI Studio” button. Follow the instructions and you will get an API Key. Save this API key somewhere since it is extremely important and will be used later.
Finally, it is now time to code.
Installing Necessary Packages
Now, we need to install the required packages. The main package we will be needing is the “google-generativeai” package. Make sure you are inside your activated virtual environment and paste the following code in your terminal:
pip install google-generativeai
Pip is the official package manager for Python and comes included in Python versions 3.4 and later.
There are two approaches one can follow to load the API key we got earlier into our program.
- The first approach (and the better one) is to create a new file named ‘.env’ (literally only dot and the letters ‘env’). Here, we will store our Gemini API key in this format:
GOOGLE_API_KEY=<your_API_key_here>
This is a better format because this stored our key into a separate file, which ensures that no one gets our key if this file is ever shared (think about pushing to Github or other platforms). To make sure this works, we also need a package called ‘dotenv’ to read our API key from the ‘.env’ file we created earlier. Code for installation:
pip install python-dotenv
To read the API Key from the .env file, you will be using:
os.getenv("GOOGLE_API_KEY")
2. The second approach is to explicitly mention the API key within the program itself, like this:
GOOGLE_API_KEY=<your_API_key_here>
genai.configure(api_key=GOOGLE_API_KEY)
Don’t worry, the full code will be presented below.
Building the Chatbot
We now get to building the bot. You can create a ‘.ipynb’ notebook or a ‘.py’ file because it will work either ways. For this demonstration, I will be using a notebook file.
Loading Necessary Libraries
Use the following command to load Gemini and OS libraries:
import google.generativeai as genai
import os
Setting the API Key
Based on the approached explained above, either use:
os.getenv("GOOGLE_API_KEY")
OR
GOOGLE_API_KEY=<your_API_key_here>
genai.configure(api_key=GOOGLE_API_KEY)
Selecting the Appropriate Model
Here, I will be using the Gemini 1.5 Flash model. You can also browse through the latest available model and use that one instead.
model = genai.GenerativeModel('gemini-1.5-flash')
Asking the User for a Prompt
The following command can be used to ask the user for a prompt:
prompt = input("Write a prompt")
Printing the Results
The following commands can be used to print the result:
result = model.generate_content(prompt)
result.text
Since the result incorporates more information besides the answer text (like roles and background), we use result.text to print the text based result/answer only.
Complete Code
# Importing necessary pacakges
import google.generativeai as genai
import os
# Retreiving my Gemini API Key that is stored in my local environment
os.getenv("GOOGLE_API_KEY")
# Setting the model to use
model = genai.GenerativeModel('gemini-1.5-flash')
# Asking the user for a prompt
prompt = input("Write a prompt")
# Printing the results
result = model.generate_content(prompt)
result.text
You can also use a loop to allow the users to keep asking questions. That’s it, its as simple as this!