ai_chatbot_flask

CS50 AI Chatbot

Final Project for CS50x 2024 Course

Github

Description: CS50 AI Chatbot built on Flask using Mistral AI

Overview

The CS50 AI Chatbot is designed to provide an interactive chatting experience using artificial intelligence. Built on the Flask framework and leveraging the power of Mistral AI, this project demonstrates the integration of AI with web applications. Users can interact with the chatbot through a web interface, and the chatbot can respond intelligently based on the inputs provided.

Features

Before You Start

Requirements

Ensure you have the following installed:

You will also need MISTRAL_API_KEY as an environmental variable. To get it, visit mistral.ai and generate an API token. Place it into your IDE configuration or use a .env file.

Setting Up the Environment

Using an IDE:

For example, in PyCharm:

  1. Go to Run -> Edit Configurations.
  2. Add a new environment variable: MISTRAL_API_KEY with the value of your API token.
Using Terminal:

Navigate to the folder where app.py is located and create a .env file:

echo "export MISTRAL_API_KEY=<YOUR_API_TOKEN>" >> .env
Updating the Code

Ensure your code loads the environment variables correctly. You can use the python-dotenv package to manage your environment variables. Update your code as follows:

from dotenv import load_dotenv
import os

# Load environment variables (if .env is in the same folder)
load_dotenv()

# Change ChatBot initialization (global scope)
bot = ChatBot(api_key=os.getenv("MISTRAL_API_KEY"), model=DEFAULT_MODEL, system_message="", temperature=DEFAULT_TEMPERATURE)
Running the Project

To start the project, open a terminal in the folder where app.py is located and run:

flask run

This will start the Flask development server. You can then open your web browser and navigate to http://127.0.0.1:5000 to interact with the chatbot.

Project Structure

Here’s a brief overview of the project’s structure:

Key Components

User Authentication

The application provides secure login and registration functionality using Flask-Session to manage user sessions. Passwords are hashed for security.

Chat Functionality

Users can start new chats or continue previous ones. Each chat is associated with a user and stored in the database. The chat interface displays the conversation history and allows users to send new messages.

AI-Powered Responses

The chatbot uses Mistral AI to generate responses based on user inputs. The integration is handled by the ChatBot class, which sends user inputs to the Mistral API and processes the responses.

Example Usage

After setting up the environment and starting the Flask server, follow these steps to use the chatbot:

  1. Register: Create a new user account by providing a username, email, and password.
  2. Login: Log in with your credentials.
  3. Start a New Chat: Click on “Start a New Chat” to begin a new conversation.
  4. Interact with the Chatbot: Type your messages and receive responses from the chatbot.

Troubleshooting

If you encounter issues, check the following:

Additional Features

To further enhance the chatbot, consider implementing the following features:

Contributions

Contributions to the project are welcome. If you have ideas for new features or improvements, feel free to fork the repository and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Acknowledgements

Contact

For any questions or feedback, please reach out to me on GitHub.

By following this guide and using the provided code, you should be able to set up and run the CS50 AI Chatbot successfully. Happy chatting!