Mohitkr95 /
best-data-science-resources
This repository contains the best Data Science free hand-picked resources to equip you with all the industry-driven skills and interview preparation kit.
86/100 healthLoading repository data…
andreger / repository
This repository contains the code for a basic AI chatbot built using Django and Google's GenerativeAI library (Gemini API).
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This guide equips you with the steps to bring your chatbot to life, empowering you to create engaging user experiences with a powerful AI chatbot built using Django and Google Gemini API.
20+ years of experience in software development. Bachelor's degree in Computer Science. Fullstack Software Engineer and Tech Lead in Java, Python, JS, PHP applications. Certified in Front-End, Back-End and DevOps technologies. Experienced in Scrum and Agile Methodologies. Solid knowledge in Databases and ORMs. Practical skills in Cloud Computing Services.
Before diving into code, ensure you have the following tools:
python3 --version # or python --version
If it is installed, you should see the version number. Otherwise, download it from https://www.python.org/downloads/.
Create a directory for your project. In this article, we'll use the name chatbot-django-gemini. Access it and create a virtual environment to isolate project dependencies:
mkdir chatbot-django-gemini
cd chatbot-django-gemini
python3 -m venv venv
source venv/bin/activate
pip install django
A Django project serves as the foundation for your chatbot application.
Open your terminal and navigate to your desired project directory.
Execute the following command to create a new Django project named chatbot in the current directory:
django-admin startproject chatbot .
Django apps organize functionalities. We'll create one for our chatbot logic:
python3 manage.py startapp chatbotapp
The Google GenerativeAI library allows us to interact with the Gemini API for generating responses.
Selected from shared topics, language and repository description—not editorial ratings.
Mohitkr95 /
This repository contains the best Data Science free hand-picked resources to equip you with all the industry-driven skills and interview preparation kit.
86/100 healthMurillo /
This repository contains the challenges about domains of Artificial Intelligence.
63/100 healthpip install google-generativeai
An API key is required to use the GenerativeAI library.
Browse to https://ai.google/ and create or select a project.
Go to the Get API Key page.
Click on Create API Key to obtain an API key. Store it securely (we'll use an environment variable later).
Django projects are organized by individual applications, each focusing on specific functionalities. In step 3, you created a Django app named chatbotapp to encapsulate the logic behind your chatbot. Here's why we need to add it to INSTALLED_APPS:
Modular Project Structure: Django projects promote a modular approach. By creating separate apps, you can organize your codebase efficiently and maintain a clean separation of concerns. Each app can have its own models, views, templates, and other components specific to its functionality.
App Recognition: When you add chatbotapp to INSTALLED_APPS, you essentially tell Django: "Hey, this chatbotapp exists, and it's part of this project. Recognize it and include its functionalities when running the project."
Component Discovery: Including chatbotapp in INSTALLED_APPS allows Django to discover the components (models, views, templates) within your chatbotapp. These components are crucial for building the chatbot's features.
Open your project's chatbot/settings.py file.
Inside the INSTALLED_APPS list, add 'chatbotapp'.
We'll use an environment variable for security. In your terminal, set the API key using a command like:
export GENERATIVE_AI_KEY="YOUR_API_KEY_HERE"
Replace YOUR_API_KEY_HERE with your actual key.
In settings.py, add the following code to access the key securely:
GENERATIVE_AI_KEY = os.environ.get('GENERATIVE_AI_KEY') # Don't forget to import os package
if not GENERATIVE_AI_KEY:
raise ValueError('GENERATIVE_AI_KEY environment variable not set')
This step allows you to store and manage chat interactions in your database.
from django.db import models
class ChatMessage(models.Model):
user_message = models.TextField()
bot_response = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"User: {self.user_message}, Bot: {self.bot_response}"
from django.contrib import admin
from .models import ChatMessage
admin.site.register(ChatMessage)
python3 manage.py makemigrations
python3 manage.py migrate
Now we'll delve into the heart of our chatbot's functionality - handling user interactions and managing conversation history. This is achieved by creating views in Django. Views are like controllers that handle user requests and generate responses.
Create a view in chatbotapp/views.py.
In our case, we'll define two views:
from django.shortcuts import redirect, render
from chatbot.settings import GENERATIVE_AI_KEY
from chatbotapp.models import ChatMessage
import google.generativeai as genai
def send_message(request):
if request.method == 'POST':
genai.configure(api_key=GENERATIVE_AI_KEY)
model = genai.GenerativeModel("gemini-pro")
user_message = request.POST.get('user_message')
bot_response = model.generate_content(user_message)
ChatMessage.objects.create(user_message=user_message, bot_response=bot_response.text)
return redirect('list_messages')
def list_messages(request):
messages = ChatMessage.objects.all()
return render(request, 'chatbot/list_messages.html', { 'messages': messages })
In Django, URLs map user requests to specific views. This step involves defining routes within your chatbot app and including them in your main project's URL configuration.
from django.urls import path
from .views import send_message, list_messages
urlpatterns = [
path('send', send_message, name='send_message'),
path('', list_messages, name='list_messages'),
]
from django.urls import path, include # Don't forget to import include function
from adminsite import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('chatbot/', include('chatbotapp.urls')), # Include the chatbot app's URL patterns
]
Templates define the structure and presentation of your chatbot's user interface.
Create a template in templates/chatbot/list_messages.html.
This template will likely include an input field for the user's message, a button to submit the message, and a space to display the chatbot's response.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chatbot</title>
</head>
<body>
{% for message in messages %}
<div>
<strong>User:</strong> {{ message.user_message }}
</div>
<div>
<strong>Bot:</strong> {{ message.bot_response }}
</div>
{% endfor %}
<form action="{% url 'send_message' %} " method="post">
{% csrf_token %}
<textarea name="user_message"></textarea>
<input type="submit" value="Send">
</form>
</body>
</html>
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # Update DIRS path. Don't forget to include os package.
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [],
},
},
]
python3 manage.py runserver
This will typically launch the server on port 8000 by default.
Open your web browser and visit the chatbot URL defined in your project's URL configuration (http://127.0.0.1:8000/chatbot). This should render your chatbot interface.
These are the essential steps to build a basic AI chatbot using Django and Gemini API. This provides a solid foundation for crafting interactive and informative chatbots that can engage with users.
You can extend this functionality in various ways:
By leveraging the power of Django and Gemini API, you can create versatile AI chatbots that can streamline communication, answer questions, and provide valuable interactions for your users. Explore the possibilities!
snrazavi /
This repository contains implementation of different AI algorithms, based on the 4th edition of amazing AI Book, Artificial Intelligence A Modern Approach
55/100 healthamineHY /
This repository contains a docker image that I use to develop my artificial intelligence applications in an uncomplicated fashion. Python, TensorFlow, PyTorch, ONNX, Keras, OpenCV, TensorRT, Numpy, Jupyter notebook... :whale2::fire:
49/100 healthJjateen /
This repository contains resources, including circuit diagrams, code, and project files from the IoTics AIoT Workshop, focusing on integrating Artificial Intelligence (AI) with the Internet of Things (IoT). It features hands-on projects exploring sensor integration, cloud services, machine learning, and robotics.
73/100 healthchitholian /
This is an educational repository containing implementation of some search algorithms in Artificial Intelligence.
70/100 health