In Part one of LLM series I discussed how to create a chatbot with OpenAI API.
In this article I will demonstrate how to create chatbot with Google Gemini Pro API. Gemini Pro API has been introduced by Google on December 13, 2023. Gemini Pro model is as good as GPT 3.5 model used by free version of chatgpt.
- To use Gemini Pro API access, first go to the following website and obtain your API key
2. Install google-generativeai library in python
pip install google-generativeai
3. Initialize Gemini Pro model
import google.generativeai as genai
genai.configure(api_key=apikey)
model = genai.GenerativeModel('gemini-pro')
Now we can ask questions from model using generate_content method of model. An example is given below
response = model.generate_content("What is the meaning of life?")
response.text
If we want to pass message history to model to enable conversation flow, then we can use start_chat method of model. An example is given below:
When we use start_chat method, model saves message history. We can see it by using history method of chat object.
5 Using the above codes, I have modified my code in part one, to create a chatbot using gemini pro api
Gemini Pro model is at the level of GPT 3.5 of OpenAI. Within rate limits of 60 requests per month, you can use Gemini pro API for free.