This project implements a simple chatbot using Google's Gemini 1.5 Pro model via the google-generativeai
library.
- Interactive Chat: Engage in a conversation with the Gemini 1.5 Pro model. π¬
- Safety Settings: Built-in safety mechanisms to filter content related to harassment, hate speech, sexually explicit material, and dangerous content. π‘οΈ
- Conversation History: Maintains the context of the conversation for more coherent interactions. π§
- Python 3.7 or higher π
- google-generativeai library: Install using
pip install google-generativeai
- dotenv library: Install using
pip install python-dotenv
- Google Account: You'll need a Google account to be able access the Google AI Studio. βοΈ
- Gemini API : Ensure that the Gemini API is active. β
-
Obtain your API Key: ποΈ
- Go to the Google AI Studio.
- Get API Key.
- Create API Key".
- Copy.
- Important: Store this API key securely. π
-
Create a
.env
file: π-
In the project directory, create a file named
.env
. -
Add the following line to the file, replacing
YOUR_API_KEY
with your actual API key:API_KEY=YOUR_API_KEY
-
-
Run the Chatbot:
βΆοΈ - Execute the Python script (e.g.,
python chatbot.py
). - Start typing to chat with the chatbot! π¬
- To exit the program, type
exit
. πͺ
- Execute the Python script (e.g.,
google.generativeai
: Imports the necessary library for interacting with the Gemini API.dotenv
: Loads the API key from the.env
file.genai.configure(api_key=api_key)
: Sets up your authentication for the API. πmodel = genai.GenerativeModel(...)
: Initializes the Gemini 1.5 Pro model with specified parameters:model_name
: Specifies the Gemini model to use.generation_config
: Controls model behavior:temperature
: Higher values (e.g., 1) make the output more creative/random; lower values (e.g., 0.2) make it more deterministic. π‘οΈtop_p
: Nucleus sampling parameter - influences the diversity of generated text.top_k
: Top-k sampling parameter - limits the selection of words to the top k most probable words.max_output_tokens
: Sets the maximum length of the model's responses.
safety_settings
: Defines safety thresholds for various content categories. π‘οΈ
convo = model.start_chat(...)
: Initiates a chat session with the model, optionally providing conversation history. π¬convo.send_message(user_input)
: Sends the user's input to the model. β‘οΈprint(convo.last.text)
: Displays the model's response. β¬ οΈconversation_history
: Keeps track of the conversation turns. π
- Experiment with the
temperature
parameter ingeneration_config
to adjust the chatbot's creativity. π‘οΈ - Explore different safety settings to customize the content filtering. π‘οΈ
- Consider expanding the chatbot's functionality by adding features like:
- User authentication π€
- Topic-specific responses π―
- Integration with other services π
Remember to replace YOUR_API_KEY
in the .env
file with your actual API key! π