Python is a versatile programming language used for various applications—from data analysis and machine learning to web development and game creation. In this tutorial, you’ll learn how to build your first AI-powered chatbot using Python and Google Gemini.
Prerequisites
Before you begin, ensure you have the following:
-
Basic knowledge of Python
-
Python 3.9+ installed on your computer
-
A Google Gemini API key
Step 1: Obtain a Google Gemini API Key
An API key is a unique code that authenticates a user or application, granting access to Google’s Gemini AI model. Follow this quick guide in the video below to obtain your API key.
Step 2: Install Python (If Not Already Installed)
This tutorial assumes you have Python installed. If not, refer to the YouTube tutorial below for a step-by-step installation guide.
After installation, you can verify it by running:
python --versionpython --versionpython --version
Enter fullscreen mode Exit fullscreen mode
Step 3: Set Up Your Chatbot
Open your terminal and navigate to your project folder.
Install the google-genai
package by running:
pip install -q -U google-genaipip install -q -U google-genaipip install -q -U google-genai
Enter fullscreen mode Exit fullscreen mode
Create a Python file (e.g., main.py
) and paste the following code.
main.py:
from google import genaimy_api_key = "GEMINI_API_KEY"my_model = "gemini-2.0-flash"client = genai.Client(api_key=my_api_key)chat = client.chats.create(model=my_model)def prompt_ai():to_exit = Falsewhile not to_exit:user_prompt = input("\nPrompt: \n")if user_prompt != "exit":response = chat.send_message_stream(user_prompt)print("\nGemini: ")for chunk in response:print(chunk.text, end="")else:to_exit = Trueprompt_ai()from google import genai my_api_key = "GEMINI_API_KEY" my_model = "gemini-2.0-flash" client = genai.Client(api_key=my_api_key) chat = client.chats.create(model=my_model) def prompt_ai(): to_exit = False while not to_exit: user_prompt = input("\nPrompt: \n") if user_prompt != "exit": response = chat.send_message_stream(user_prompt) print("\nGemini: ") for chunk in response: print(chunk.text, end="") else: to_exit = True prompt_ai()from google import genai my_api_key = "GEMINI_API_KEY" my_model = "gemini-2.0-flash" client = genai.Client(api_key=my_api_key) chat = client.chats.create(model=my_model) def prompt_ai(): to_exit = False while not to_exit: user_prompt = input("\nPrompt: \n") if user_prompt != "exit": response = chat.send_message_stream(user_prompt) print("\nGemini: ") for chunk in response: print(chunk.text, end="") else: to_exit = True prompt_ai()
Enter fullscreen mode Exit fullscreen mode
Important: Replace GEMINI_API_KEY
with your actual API key.
Step 4: Run Your AI Chatbot
To run your chatbot, execute the following command in your terminal:
python main.pypython main.pypython main.py
Enter fullscreen mode Exit fullscreen mode
Troubleshooting Tips
-
Command Not Recognized:
- Ensure Python is installed correctly by running
python --version
. - Verify that you’re in the correct project directory where
main.py
is located.
- Ensure Python is installed correctly by running
-
API Issues:
- Double-check that you’ve replaced
"GEMINI_API_KEY"
with a valid API key. - Confirm your internet connection is active, as the chatbot communicates with Google Gemini’s servers.
- Double-check that you’ve replaced
-
Unexpected Errors:
- Review the terminal output for any error messages.
- Check your installation of the
google-genai
package by runningpip show google-genai
to confirm it is installed correctly.
Now, interact with your AI chatbot by entering prompts. To exit the program, simply type exit
.
Conclusion
Congratulations! You’ve successfully built your first AI chatbot using Python and Google Gemini. This project is a great starting point. Next, you might consider expanding its functionality by adding error handling, a graphical user interface, or integrating it into a web application.
Happy coding!
原文链接:How to build an AI Chatbot Using Python and Gemini AI (Step-by-Step Guide)
暂无评论内容