-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #991 from Davda-James/script/aibot
Added the AI Bot Script (Gemini)
- Loading branch information
Showing
4 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# Virtual environments | ||
venv/ | ||
ENV/ | ||
env/ | ||
.venv/ | ||
.ENV/ | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
test-results/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# dotenv | ||
.env | ||
.env.* | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# pyright | ||
pyrightconfig.json | ||
|
||
# VS Code settings (optional, but commonly used) | ||
.vscode/ | ||
|
||
# Logs | ||
*.log | ||
|
||
# Local development logs | ||
*.log.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
import google.generativeai as genai | ||
import dotenv | ||
|
||
# Load environment variables from .env file | ||
dotenv.load_dotenv() | ||
|
||
# Configure the API key for the Gemini model | ||
api_key = os.environ.get("GEMINI_API_KEY") | ||
if not api_key: | ||
raise ValueError( | ||
"API key not found! Ensure GEMINI_API_KEY is " | ||
"set in the environment variables." | ||
) | ||
|
||
genai.configure(api_key=api_key) | ||
|
||
# Initialize the Generative Model | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
|
||
|
||
def generate_response(user_input): | ||
""" | ||
This function interacts with the Gemini model | ||
to generate creative and intelligent responses. | ||
:param user_input: The user's input to the chatbot. | ||
:return: A response generated by the AI. | ||
""" | ||
try: | ||
response = model.generate_content(user_input) | ||
return ( | ||
response.text | ||
if response | ||
else "Sorry, I couldn't generate a response." | ||
) | ||
except Exception as e: | ||
return f"An error occurred: {e}" | ||
|
||
|
||
def chatbot(): | ||
""" | ||
Main function to handle continuous interaction with the chatbot. | ||
It allows the user to ask anything from blog posts to code explanations. | ||
""" | ||
print("🚀 Welcome to the most amazing AI Bot! 🌟") | ||
print( | ||
"💬 You can ask me anything from writing blog posts, " | ||
"to explaining code, to creative writing.\n" | ||
) | ||
print("✨ Just type 'exit' anytime to quit.\n") | ||
|
||
while True: | ||
# Get user input | ||
user_input = input("You: ") | ||
|
||
# Check for exit condition | ||
if user_input.lower() == "exit": | ||
print("👋 Goodbye! Thanks for chatting!") | ||
break | ||
|
||
# Generate and print AI response | ||
ai_response = generate_response(user_input) | ||
print(f"AI Bot: {ai_response}\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Run the chatbot | ||
chatbot() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# AI Bot using Google Generative AI | ||
|
||
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. | ||
|
||
## Key Features | ||
|
||
- **Versatile AI Responses**: From blog writing to coding explanations, the bot can tackle diverse tasks. | ||
- **Seamless API Integration**: Integrates with **Google's Gemini model (gemini-1.5-flash)** for content generation. | ||
- **Secure Key Management**: Uses `python-dotenv` to handle sensitive API keys securely. | ||
- **User-Friendly**: Easy to set up and use. Just run the bot and start interacting! | ||
|
||
## Setup Instructions | ||
|
||
Follow these steps to set up and run the AI Bot on your local machine. | ||
|
||
### Step 1: Clone the Repository | ||
If the code is hosted in a repository, clone it using the command: | ||
```bash | ||
git clone https://github.com/your-repo-name/ai-bot.git | ||
cd ai-bot | ||
``` | ||
### Step 1: Install dependencies required for this script to run | ||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
### Step 3: Setting Gemini API keys | ||
## For linux/mac users | ||
``` | ||
touch .env | ||
echo "GEMINI_API_KEY=your-gemini-api-key-here" >> .env | ||
``` | ||
## For Windows users | ||
``` | ||
new-item .env | ||
echo GEMINI_API_KEY=your-gemini-api-key-here > .env | ||
``` | ||
### Step 4: Running the script | ||
``` | ||
python AIbot.py | ||
``` | ||
|
||
## Enjoy talk with chat and have fun | ||
### Script Explanation | ||
Here’s a breakdown of what the script does: | ||
|
||
Environment Configuration: The script loads the GEMINI_API_KEY from the .env file using dotenv to securely access your API key. | ||
|
||
Google Generative AI Configuration: It sets up the google-generativeai library to interface with the Gemini model. | ||
|
||
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). | ||
|
||
Generating Responses: Based on the user’s input, the bot generates relevant and creative responses using the *Gemini 1.5 Flash model*. | ||
|
||
## Disclaimer | ||
|
||
- **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. | ||
|
||
- **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. | ||
|
||
- **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. | ||
|
||
- **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. | ||
|
||
- **Ethical Use**: Users are encouraged to use the bot ethically and responsibly. Avoid using it to generate harmful, misleading, or abusive content. | ||
|
||
- **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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
google-generativeai==0.8.3 | ||
python-dotenv==1.0.1 |