Skip to content

Commit 8e06f69

Browse files
authored
Merge pull request #991 from Davda-James/script/aibot
Added the AI Bot Script (Gemini)
2 parents a7e2735 + 8999d3d commit 8e06f69

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

aibot/.gitignore

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# Virtual environments
29+
venv/
30+
ENV/
31+
env/
32+
.venv/
33+
.ENV/
34+
35+
# PyInstaller
36+
# Usually these files are written by a python script from a template
37+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
38+
*.manifest
39+
*.spec
40+
41+
# Installer logs
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Unit test / coverage reports
46+
htmlcov/
47+
.tox/
48+
.nox/
49+
.coverage
50+
.coverage.*
51+
.cache
52+
nosetests.xml
53+
coverage.xml
54+
*.cover
55+
.hypothesis/
56+
.pytest_cache/
57+
test-results/
58+
59+
# Jupyter Notebook
60+
.ipynb_checkpoints
61+
62+
# pyenv
63+
.python-version
64+
65+
# dotenv
66+
.env
67+
.env.*
68+
69+
# mypy
70+
.mypy_cache/
71+
.dmypy.json
72+
dmypy.json
73+
74+
# pyright
75+
pyrightconfig.json
76+
77+
# VS Code settings (optional, but commonly used)
78+
.vscode/
79+
80+
# Logs
81+
*.log
82+
83+
# Local development logs
84+
*.log.*

aibot/AIbot.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import google.generativeai as genai
3+
import dotenv
4+
5+
# Load environment variables from .env file
6+
dotenv.load_dotenv()
7+
8+
# Configure the API key for the Gemini model
9+
api_key = os.environ.get("GEMINI_API_KEY")
10+
if not api_key:
11+
raise ValueError(
12+
"API key not found! Ensure GEMINI_API_KEY is "
13+
"set in the environment variables."
14+
)
15+
16+
genai.configure(api_key=api_key)
17+
18+
# Initialize the Generative Model
19+
model = genai.GenerativeModel("gemini-1.5-flash")
20+
21+
22+
def generate_response(user_input):
23+
"""
24+
This function interacts with the Gemini model
25+
to generate creative and intelligent responses.
26+
27+
:param user_input: The user's input to the chatbot.
28+
:return: A response generated by the AI.
29+
"""
30+
try:
31+
response = model.generate_content(user_input)
32+
return (
33+
response.text
34+
if response
35+
else "Sorry, I couldn't generate a response."
36+
)
37+
except Exception as e:
38+
return f"An error occurred: {e}"
39+
40+
41+
def chatbot():
42+
"""
43+
Main function to handle continuous interaction with the chatbot.
44+
It allows the user to ask anything from blog posts to code explanations.
45+
"""
46+
print("🚀 Welcome to the most amazing AI Bot! 🌟")
47+
print(
48+
"💬 You can ask me anything from writing blog posts, "
49+
"to explaining code, to creative writing.\n"
50+
)
51+
print("✨ Just type 'exit' anytime to quit.\n")
52+
53+
while True:
54+
# Get user input
55+
user_input = input("You: ")
56+
57+
# Check for exit condition
58+
if user_input.lower() == "exit":
59+
print("👋 Goodbye! Thanks for chatting!")
60+
break
61+
62+
# Generate and print AI response
63+
ai_response = generate_response(user_input)
64+
print(f"AI Bot: {ai_response}\n")
65+
66+
67+
if __name__ == "__main__":
68+
# Run the chatbot
69+
chatbot()

aibot/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# AI Bot using Google Generative AI
2+
3+
This AI bot is designed to perform a wide range of tasks, such as writing blog posts, explaining code, or creating creative stories. It uses **Google's Gemini API** via the `google-generativeai` package to generate intelligent and creative responses to user queries. With a simple and secure setup, this bot makes interacting with AI effortless and enjoyable.
4+
5+
## Key Features
6+
7+
- **Versatile AI Responses**: From blog writing to coding explanations, the bot can tackle diverse tasks.
8+
- **Seamless API Integration**: Integrates with **Google's Gemini model (gemini-1.5-flash)** for content generation.
9+
- **Secure Key Management**: Uses `python-dotenv` to handle sensitive API keys securely.
10+
- **User-Friendly**: Easy to set up and use. Just run the bot and start interacting!
11+
12+
## Setup Instructions
13+
14+
Follow these steps to set up and run the AI Bot on your local machine.
15+
16+
### Step 1: Clone the Repository
17+
If the code is hosted in a repository, clone it using the command:
18+
```bash
19+
git clone https://github.com/your-repo-name/ai-bot.git
20+
cd ai-bot
21+
```
22+
### Step 1: Install dependencies required for this script to run
23+
```
24+
pip install -r requirements.txt
25+
```
26+
27+
### Step 3: Setting Gemini API keys
28+
## For linux/mac users
29+
```
30+
touch .env
31+
echo "GEMINI_API_KEY=your-gemini-api-key-here" >> .env
32+
```
33+
## For Windows users
34+
```
35+
new-item .env
36+
echo GEMINI_API_KEY=your-gemini-api-key-here > .env
37+
```
38+
### Step 4: Running the script
39+
```
40+
python AIbot.py
41+
```
42+
43+
## Enjoy talk with chat and have fun
44+
### Script Explanation
45+
Here’s a breakdown of what the script does:
46+
47+
Environment Configuration: The script loads the GEMINI_API_KEY from the .env file using dotenv to securely access your API key.
48+
49+
Google Generative AI Configuration: It sets up the google-generativeai library to interface with the Gemini model.
50+
51+
User Interaction: The script continuously prompts the user for input. Users can ask anything from writing tasks (e.g., generating blog posts) to technical queries (e.g., explaining code).
52+
53+
Generating Responses: Based on the user’s input, the bot generates relevant and creative responses using the *Gemini 1.5 Flash model*.
54+
55+
## Disclaimer
56+
57+
- **API Limitations**: This AI bot relies on the Google Generative AI (Gemini) API. Users should be aware of the API's rate limits and usage policies, as excessive requests may result in temporary suspension of access.
58+
59+
- **Content Accuracy**: The responses generated by the AI bot are based on patterns learned from training data. While the bot aims to provide accurate and relevant information, it may occasionally produce incorrect or misleading content. Users should verify information before relying on it for critical tasks.
60+
61+
- **No Legal or Medical Advice**: The AI bot is not a substitute for professional advice. Users should not rely on the information provided by the bot for legal, medical, or financial decisions.
62+
63+
- **User Responsibility**: By using this bot, users agree to take full responsibility for their interactions with the AI and the content generated. The developers are not liable for any consequences resulting from the use of this bot.
64+
65+
- **Ethical Use**: Users are encouraged to use the bot ethically and responsibly. Avoid using it to generate harmful, misleading, or abusive content.
66+
67+
- **Privacy Considerations**: Any information shared with the bot during interactions may be processed according to Google's data privacy policies. Users should avoid sharing personal or sensitive information.

aibot/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-generativeai==0.8.3
2+
python-dotenv==1.0.1

0 commit comments

Comments
 (0)