You are currently viewing Tutorial 3: Setting up the development environment for building an AI-powered chatbot with DeepSeek and Python Flask

Tutorial 3: Setting up the development environment for building an AI-powered chatbot with DeepSeek and Python Flask

  • Post author:
  • Post category:Python

Now that you understand the architecture of your AI-powered chatbot, it’s time to roll up your sleeves and get your hands dirty! In this tutorial, we’ll set up your development environment so you can start building the chatbot for GreenGrocer Foods. Think of this as laying the foundation for your project—everything else will build on this.

By the end of this tutorial, you’ll have:

  • Flask installed and ready to go.
  • DeepSeek’s LLM configured and integrated into your project.
  • virtual environment set up to keep your dependencies organized.
  • well-structured project folder with a .gitignore file to keep things clean and secure.

Prerequisite

Before diving into this tutorial, make sure you’ve:

  1. Installed Python: If you don’t have Python installed, follow the official Python installation guide to download and install Python 3.9 or higher.
  2. Gone through Tutorial 2Understanding the architecture of an AI-powered chatbot with Deepseek and Python Flask. It explains the key components of the chatbot and how they work together.
  3. Signed up for DeepSeek: Create an account on the Deepseek platform.
  4. Ensure your DeepSeek account has a minimum of $2 to the use the API.

Why setting up your environment matters

Imagine trying to cook a gourmet meal without prepping your ingredients or cleaning your kitchen first. Chaos, right? The same goes for coding. Setting up your development environment is like prepping your kitchen—it ensures everything runs smoothly and saves you from headaches later.

For GreenGrocer Foods, this step is crucial. A well-organized environment will help you build a chatbot that’s reliable, scalable, and easy to maintain. Let’s make sure you’re set up for success!

Step 1: Set up a virtual environment

A virtual environment keeps your project’s dependencies isolated from other Python projects. Here’s how to create one:

  1. Install virtualenv: Run the following command:
    pip install virtualenv
  2. Create a virtual environment: Navigate to your project folder and run:
    virtualenv venv
  3. Activate the virtual environment:
    • On Windows:
      venv\Scripts\activate
    • On macOS/Linux:
      source venv/bin/activate

      You’ll know it’s activated when you see (venv) at the beginning of your terminal prompt.

Step 2: Organize your project

Before adding any code, let’s set up the project structure. This ensures you know where to place files like routes.py and keeps everything organized from the start. Here’s our project structure for the ai-chatbot-customer-support repository:

ai-chatbot-customer-support/
│
├── app/                         # Main application code
│   ├── __init__.py              # Flask app initialization & Blueprint registration
│   ├── routes.py                # API route definitions
│   ├── models/                  # Database models (SQLAlchemy, if needed)
│   │   ├── __init__.py          # Initialize models package
│   │   ├── user.py              # User model
│   │   ├── chatbot.py           # Chatbot-related models
│   ├── utils/                   # Utility functions
│   │   ├── helpers.py           # Helper functions
│   │   ├── logger.py            # Logging configuration
│   │   └── validators.py        # Input validation functions
│   ├── services/                # Business logic layer
│   │   ├── chatbot_service.py   # AI chatbot logic
│   │   ├── user_service.py      # User-related business logic
│   ├── api/                     # API endpoints (Blueprints)
│   │   ├── __init__.py          # Register all APIs
│   │   ├── chatbot.py           # Chatbot API
│   │   ├── user.py              # User management API
│   │   ├── auth.py              # Authentication API
│   ├── templates/               # Jinja2 HTML templates (if needed)
│   ├── static/                  # Static assets (CSS, JS, images)
│
├── tests/                       # Unit and integration tests
│   ├── conftest.py              # Pytest fixtures
│   ├── test_routes/             # Tests for API routes
│   │   ├── test_chatbot.py
│   │   ├── test_user.py
│   ├── test_services/           # Business logic tests
│   ├── test_models/             # Database model tests
│   ├── test_utils/              # Utility function tests
│
├── docs/                        # Documentation
│   ├── architecture.md          # Architecture overview
│   ├── setup_guide.md           # Step-by-step setup instructions
│
├── instance/                    # Configuration files (ignored in Git)
│   ├── config.py                # Local config (for development)
│   ├── config.example.py        # Example config file
│
├── migrations/                  # Database migrations (Flask-Migrate)
│
├── logs/                        # Log files (optional)
│
├── .github/                     # GitHub-specific files
│   ├── ISSUE_TEMPLATE/          # Issue templates
│   ├── workflows/               # CI/CD workflows
│
├── .env_copy                    # Template for environment variables (rename to .env)
├── .gitignore                   # Specifies files to ignore in version control
├── Dockerfile                   # Docker configuration
├── docker-compose.yml           # Docker Compose configuration
├── Makefile                     # Common project commands (optional)
├── requirements.txt             # Production dependencies
├── requirements-dev.txt         # Development dependencies
├── manage.py                    # Command-line utility for running the app
├── README.md                    # Project documentation
├── CONTRIBUTING.md              # Guidelines for contributors
├── LICENSE                      # Project license

Why this folder structure is well-suited for microservices and scalability

The folder structure is designed with modularity and scalability in mind:

  • Separation of Concerns: The app/ folder is divided into clear modules (api/services/utils/models/), ensuring each component has a single responsibility.
  • Modular Design: The use of Blueprints in the api/ folder allows you to break the application into smaller, independent components that can be developed, tested, and deployed separately.
  • Scalability: The structure supports horizontal scaling (e.g., deploying multiple instances of the chatbot service) and can be extended with tools like Docker and Kubernetes.
  • Maintainability: Clear organization and consistent naming conventions make the codebase easy to maintain as it grows.

Step 3: Install Flask

Flask is the lightweight web framework we’ll use to build the backend of the chatbot. Here’s how to install it:

  1. Install Flask: Run the following command:
    pip install Flask
  2. Open the app/__init__.py file and add the following code:
    from flask import Flask
    from .routes import init_routes
    
    def create_app():
        app = Flask(__name__)
    
        init_routes(app)
    
        return app
    
    
    

    This code defines and initializes a Flask application with modular routing.

    1. It imports Flask from the flask package.
    2. It imports the init_routes function from the .routes module.
    3. The create_app function initializes a Flask app instance.
    4. It then calls init_routes(app), which registers routes with the app.
    5. Finally, it returns the configured app instance.

    This approach follows the Factory Pattern, making the app modular and easier to configure.

  3. In the app/ folder, create a routes.py file and add the following code:
    def init_routes(app):
        @app.route('/')
        def home():
            return "Hello, GreenGrocer Foods!"
  4. In the root of your project, update the manage.py file to use the create_app function:
    import os
    from app import create_app
    
    app = create_app()
    
    if __name__ == '__main__':
        app.run(debug=True)
    
    

    This code initializes and runs a Flask application.

    1. It imports the os module, which provides access to environment variables and file system operations.
    2. It imports the create_app function from the app module.
    3. It calls create_app() to initialize the Flask application and assigns it to the app variable.
    4. If the script runs directly (__name__ == '__main__'), it starts the Flask development server with debug mode enabled, allowing automatic reloading and better error tracking.
  5. Run the Flask app: In your terminal, navigate to the root of your project and type:
    python manage.py

    Open your browser and go to http://127.0.0.1:5000/. You should see “Hello, GreenGrocer Foods!” displayed.

Step 4: Set up DeepSeek

DeepSeek’s LLM is the brains of your chatbot. Here’s how to integrate it:

  1. Install the Deepseek SDK: Run the following command:
    pip install deepseek-sdk
  2. Configure API keys:
    • Grab your API key from the DeepSeek dashboard.
    • Rename the .env_copy file to .env and add your API key:
      DEEPSEEK_API_KEY=your_api_key_here

Real-world example: GreenGrocer Foods

By setting up their development environment properly, GreenGrocer Foods ensures that their chatbot project is:

  • Secure: Sensitive information like API keys is stored safely in .env and excluded from version control.
  • Organized: A clean folder structure makes it easy for the team to collaborate and maintain the codebase.
  • Scalable: A well-configured environment sets the stage for adding new features and scaling the chatbot.

Full code for module 3

You can find the complete code for this tutorial in the GitHub repository.

What’s next?

Now that your environment is set up and your project is organized, you’re ready to start building the Flask backend for your chatbot. In the next tutorial, we’ll create the API endpoints that handle customer inquiries and integrate Deepseek’s LLM to generate responses.

Facebook Comments