## Module 1: Prepare to Run the Python Code

### Step 1. Check the lab environment

- Click the `Resources` button on the left to view the lab environment set up for your hands-on practice. You will see:
  - A server named monitor1 running a VS Code server, which serves as both the development and application server.
  - Connection details for a TiDB Cloud Starter cluster — either from your own TiDB Cloud account or the lab's sandbox environment, depending on your selection.
  - Additionally, the database name for this hands-on lab is `${LAB:SERVERLESS_CLUSTER_DATABASE_NAME}`, it will be created on your behalf in the selected TiDB Cloud Starter cluster.

- Close the `Resources` popup window.

### Step 2. Open a terminal in IDE

- Enter your own [VS Code](${LAB:HTTPS_REVERSE_PROXY_URL}?next=${LAB:"/vscode/?folder=/root/tidb-lab-ai"|urlencode} "VS Code") in the browser.
- Click `Yes, I trust the authors` in the popup window.

  ![90%](https://lab-static.pingcap.com/quick-demo/vscode-trust.jpg)

- Click on `≡`, `Terminal`, then `New Terminal` to open a new terminal in the VS Code.

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-open-terminal-0307.jpg)

### Step 3. Install Python dependencies

- Run the following commands in the VS Code terminal to install the required dependencies.

  ```bash
  pip install --upgrade pip
  pip install -r requirements.txt
  ```

  ```output
  ...
  Collecting aiohappyeyeballs==2.6.1 (from -r requirements.txt (line 1))
    Downloading aiohappyeyeballs-2.6.1-py3-none-any.whl.metadata (5.9 kB)
  Collecting aiohttp==3.11.16 (from -r requirements.txt (line 2))
    Downloading aiohttp-3.11.16-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (7.7 kB)
  Collecting aiosignal==1.3.2 (from -r requirements.txt (line 3))
    Downloading aiosignal-1.3.2-py2.py3-none-any.whl.metadata (3.8 kB)
  ...
  ```

### Step 4. Create a .env file

- Create an empty `.env` file under `openai` folder.

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-409/409-new-env-0427.jpg)

- Add the following content to the `.env` file and save it. Please change the `<Your OpenAI API Key>` placeholder to your actual OpenAI API Key.

  ```properties
  OPENAI_API_KEY=<Your OpenAI API Key>

  TIDB_HOST=${LAB:SERVERLESS_CLUSTER_HOST}
  TIDB_PORT=${LAB:SERVERLESS_CLUSTER_PORT}
  TIDB_USERNAME='${LAB:SERVERLESS_CLUSTER_USERNAME}'
  TIDB_PASSWORD='${LAB:SERVERLESS_CLUSTER_PASSWORD}'
  TIDB_DATABASE=${LAB:SERVERLESS_CLUSTER_DATABASE_NAME}
  ```
