{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a793b1d-a0a9-404c-ada6-58937227cfce",
   "metadata": {},
   "source": [
    "# Oh dear!\n",
    "\n",
    "If you've got here, then you're still having problems setting up your environment. I'm so sorry! Hang in there and we should have you up and running in no time.\n",
    "\n",
    "Setting up a Data Science environment can be challenging because there's a lot going on under the hood. But we will get there.\n",
    "\n",
    "And please remember - I'm standing by to help out. Message me or email ed@edwarddonner.com and I'll get on the case. The very last cell in this notebook has some diagnostics that will help me figure out what's happening.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5190688-205a-46d1-a0dc-9136a42ad0db",
   "metadata": {},
   "source": [
    "# Step 1\n",
    "\n",
    "Try running the next 2 cells (click in the cell under this one and hit shift+return, then shift+return again).\n",
    "\n",
    "If this gives an error, then you're likely not running in an \"activated\" environment. Please check back in Part 5 of the SETUP guide for [PC](../SETUP-PC.md) or [Mac](../SETUP-mac.md) for setting up the Anaconda (or virtualenv) environment and activating it, before running `jupyter lab`.\n",
    "\n",
    "If you look in the Anaconda prompt (PC) or the Terminal (Mac), you should see `(llms)` in your prompt where you launch `jupyter lab` - that's your clue that the llms environment is activated.\n",
    "\n",
    "If you are in an activated environment, the next thing to try is to restart everything:\n",
    "1. Close down all Jupyter windows, like this one\n",
    "2. Exit all command prompts / Terminals / Anaconda\n",
    "3. Repeat Part 5 from the SETUP instructions to begin a new activated environment and launch `jupyter lab` from the `llm_engineering` directory  \n",
    "4. Come back to this notebook, and do Kernel menu >> Restart Kernel and Clear Outputs of All Cells\n",
    "5. Try the cell below again.\n",
    "\n",
    "If **that** doesn't work, then please contact me! I'll respond quickly, and we'll figure it out. Please run the diagnostics (last cell in this notebook) so I can debug. If you used Anaconda, it might be that for some reason your environment is corrupted, in which case the simplest fix is to use the virtualenv approach instead (Part 2B in the setup guides)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c8c0bb3-0e94-466e-8d1a-4dfbaa014cbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Some quick checks that your Conda environment or VirtualEnv is as expected\n",
    "# The Environment Name should be: llms\n",
    "\n",
    "import os\n",
    "\n",
    "conda_prefix = os.environ.get('CONDA_PREFIX')\n",
    "if conda_prefix:\n",
    "    print(\"Anaconda environment is active:\")\n",
    "    print(f\"Environment Path: {conda_prefix}\")\n",
    "    print(f\"Environment Name: {os.path.basename(conda_prefix)}\")\n",
    "\n",
    "virtual_env = os.environ.get('VIRTUAL_ENV')\n",
    "if virtual_env:\n",
    "    print(\"Virtualenv is active:\")\n",
    "    print(f\"Environment Path: {virtual_env}\")\n",
    "    print(f\"Environment Name: {os.path.basename(virtual_env)}\")\n",
    "\n",
    "if not conda_prefix and not virtual_env:\n",
    "    print(\"Neither Anaconda nor Virtualenv seems to be active. Did you start jupyter lab in an Activated environment? See Setup Part 5.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c78b7d9-1eea-412d-8751-3de20c0f6e2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# And now, this should run with no output - no import errors.\n",
    "# Import errors might indicate that you started jupyter lab without your environment activated? See SETUP part 5.\n",
    "# Or you might need to restart your Kernel and Jupyter Lab.\n",
    "# Or it's possible that something is wrong with Anaconda, in which case we may have to use virtualenv instead.\n",
    "# If you're unsure, please run the diagnostics (last cell in this notebook) and then email me at ed@edwarddonner.com\n",
    "\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b66a8460-7b37-4b4c-a64b-24ae45cf07eb",
   "metadata": {},
   "source": [
    "# Step 2\n",
    "\n",
    "Let's check your .env file exists and has the OpenAI key set properly inside it.  \n",
    "Please run this code and check that it prints a successful message, otherwise follow its instructions.\n",
    "\n",
    "Note that the `.env` file won't show up in your Jupyter Lab file browser, because Jupyter hides files that start with a dot for your security; they're considered hidden files. If you need to change the name, you'll need to use a command terminal or File Explorer (PC) / Finder Window (Mac). Ask ChatGPT if that's giving you problems, or email me!\n",
    "\n",
    "If you're having challenges creating the `.env` file, we can also do it with code! See the cell after the next one.\n",
    "\n",
    "It's important to launch `jupyter lab` from the project root directory, `llm_engineering`. If you didn't do that, this cell might give you problems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caa4837e-b970-4f89-aa9a-8aa793c754fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "parent_dir = Path(\"..\")\n",
    "env_path = parent_dir / \".env\"\n",
    "\n",
    "if env_path.exists() and env_path.is_file():\n",
    "    print(\".env file found.\")\n",
    "\n",
    "    # Read the contents of the .env file\n",
    "    with env_path.open(\"r\") as env_file:\n",
    "        contents = env_file.readlines()\n",
    "\n",
    "    key_exists = any(line.startswith(\"OPENAI_API_KEY=\") for line in contents)\n",
    "    good_key = any(line.startswith(\"OPENAI_API_KEY=sk-proj-\") for line in contents)\n",
    "    \n",
    "    if key_exists and good_key:\n",
    "        print(\"SUCCESS! OPENAI_API_KEY found and it has the right prefix\")\n",
    "    elif key_exists:\n",
    "        print(\"Found an OPENAI_API_KEY although it didn't have the expected prefix sk-proj- \\nPlease double check your key in the file..\")\n",
    "    else:\n",
    "        print(\"Didn't find an OPENAI_API_KEY in the .env file\")\n",
    "else:\n",
    "    print(\".env file not found in the llm_engineering directory. It needs to have exactly the name: .env\")\n",
    "    \n",
    "    possible_misnamed_files = list(parent_dir.glob(\"*.env*\"))\n",
    "    \n",
    "    if possible_misnamed_files:\n",
    "        print(\"\\nWarning: No '.env' file found, but the following files were found in the llm_engineering directory that contain '.env' in the name. Perhaps this needs to be renamed?\")\n",
    "        for file in possible_misnamed_files:\n",
    "            print(file.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105f9e0a-9ff4-4344-87c8-e3e41bc50869",
   "metadata": {},
   "source": [
    "## Fallback plan - python code to create the .env file for you\n",
    "\n",
    "Only run the next cell if you're having problems making the .env file.  \n",
    "Replace the text in the first line of code with your key from OpenAI."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab9ea6ef-49ee-4899-a1c7-75a8bd9ac36b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Only run this code in this cell if you want to have a .env file created for you!\n",
    "\n",
    "make_me_a_file_with_this_key = \"put your key here inside these quotes.. it should start sk-proj-\"\n",
    "\n",
    "from pathlib import Path\n",
    "\n",
    "parent_dir = Path(\"..\")\n",
    "env_path = parent_dir / \".env\"\n",
    "\n",
    "if env_path.exists():\n",
    "    print(\"There is already a .env file - if you want me to create a new one, please delete the existing one first\")\n",
    "else:\n",
    "    try:\n",
    "        with env_path.open(mode='w', encoding='utf-8') as env_file:\n",
    "            env_file.write(f\"OPENAI_API_KEY={make_me_a_file_with_this_key}\")\n",
    "        print(f\"Successfully created the .env file at {env_path}\")\n",
    "        print(\"Now rerun the previous cell to confirm that the file is created and the key is correct.\")\n",
    "    except Exception as e:\n",
    "        print(f\"An error occurred while creating the .env file: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ba9420d-3bf0-4e08-abac-f2fbf0e9c7f1",
   "metadata": {},
   "source": [
    "# Step 3\n",
    "\n",
    "Now let's check that your API key is correct set up in your `.env` file, and available using the dotenv package.\n",
    "Try running the next cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ee8e613-5a6e-4d1f-96ef-91132da545c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This should print your API key to the output\n",
    "\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "if not api_key:\n",
    "    print(\"No API key was found - please try Kernel menu >> Restart Kernel And Clear Outputs of All Cells\")\n",
    "elif api_key[:8]!=\"sk-proj-\":\n",
    "    print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key\")\n",
    "elif api_key.strip() != api_key:\n",
    "    print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them\")\n",
    "else:\n",
    "    print(\"API key found and looks good so far!\")\n",
    "\n",
    "print(\"My key is\", os.getenv(\"OPENAI_API_KEY\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f403e515-0e7d-4be4-bb79-5a102dbd6c94",
   "metadata": {},
   "source": [
    "## It should print something like:\n",
    "\n",
    "`My key is sk-proj-blahblahblah`\n",
    "\n",
    "If it didn't print a key, then it's not able to find a file called `.env` in the `llm_engineering` folder.  \n",
    "The name of the file must be exactly `.env` - it won't work if it's called `my-keys.env` or `.env.doc`.  \n",
    "Double check those steps in the instructions. Is it possible that `.env` is actually called `.env.txt`? In Windows, you may need to change a setting in the File Explorer to ensure that file extensions are showing (\"Show file extensions\" set to \"On\"). You should also see file extensions if you type `dir` in the `llm_engineering` directory.\n",
    "\n",
    "Nasty gotchas to watch out for:  \n",
    "- In the .env file, there should be no space between the equals sign and the key. Like: `OPENAI_API_KEY=sk-proj-...`\n",
    "- If you copied and pasted your API key from another application, make sure that it didn't replace hyphens in your key with long dashes  \n",
    "- If you changed your .env file, you might need to restart your Jupyter \"kernel\" (the python process) to pick up the change via the Kernel menu >> Restart kernel, then rerun the cells from the top.\n",
    "\n",
    "Worst case, if you're not able to get this part to work, it's not a big deal. You'll just have to paste your key into the Jupyter Notebook (see below for an example), and be sure to remove it before you share the Notebook with anybody else."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42afad1f-b0bf-4882-b469-7709060fee3a",
   "metadata": {},
   "source": [
    "# Step 4\n",
    "\n",
    "Now run the below code and you will hopefully see that GPT can handle basic arithmetic!!\n",
    "\n",
    "If not, see the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cccb58e7-6626-4033-9dc1-e7e3ff742f6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# EITHER:\n",
    "my_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "# OR if you haven't been able to get .env working, uncomment this next line and paste your key inside the quote marks\n",
    "# my_api_key = \"REPLACE THIS TEXT WITH YOUR OPENAI API KEY WITHIN THE QUOTE MARKS - it should start sk-proj-\"\n",
    "\n",
    "print(f\"Using API key {my_api_key}\")\n",
    "\n",
    "openai = OpenAI(api_key=my_api_key)\n",
    "completion = openai.chat.completions.create(\n",
    "    model='gpt-4o-mini',\n",
    "    messages=[{\"role\":\"user\", \"content\": \"What's 2+2?\"}],\n",
    ")\n",
    "print(completion.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81046a77-c359-4388-929f-ffc8ad5cb93c",
   "metadata": {},
   "source": [
    "# If the key was set correctly, and this still didn't work, perhaps with a RateLimit error\n",
    "\n",
    "Then there's something up with your API key!\n",
    "\n",
    "First check this webpage to make sure you have a positive credit balance.\n",
    "OpenAI requires that you have a positive credit balance and it has minimums. My salespitch for OpenAI is that this is well worth it for your education: for less than the price of a music album, you will build so much valuable commercial experience. But it's not required for this course at all; you can watch me running OpenAI code, and then wait until we get to open source models in week 3. Also, I'll show you how to use Ollama to run open-source models locally.\n",
    "\n",
    "https://platform.openai.com/settings/organization/billing/overview\n",
    "\n",
    "Also try creating a new key (button on the top right) here:\n",
    "\n",
    "https://platform.openai.com/api-keys\n",
    "\n",
    "Sometimes OpenAI may take a few minutes to give you access after you try.\n",
    "\n",
    "## If all else fails:\n",
    "\n",
    "(1) Try pasting your error into ChatGPT or Claude! It's amazing how often they can figure things out\n",
    "\n",
    "(2) Contact me! Please run the diagnostics in the cell below, then email me your problems to ed@edwarddonner.com\n",
    "\n",
    "Thanks so much, and I'm sorry this is giving you bother!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc83f944-6ce0-4b5c-817f-952676e284ec",
   "metadata": {},
   "source": [
    "# Gathering Essential Diagnostic information\n",
    "\n",
    "## Please run this next cell to gather some important data\n",
    "\n",
    "Please run the next cell; it should take a minute or so to run. Most of the time is checking your network bandwidth.\n",
    "Then email me the output of the last cell to ed@edwarddonner.com.  \n",
    "Alternatively: this will create a file called report.txt - just attach the file to your email."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "248204f0-7bad-482a-b715-fb06a3553916",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run my diagnostics report to collect key information for debugging\n",
    "# Please email me the results. Either copy & paste the output, or attach the file report.txt\n",
    "\n",
    "!pip install -q requests speedtest-cli psutil setuptools\n",
    "from diagnostics import Diagnostics\n",
    "Diagnostics().run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cd162d4-ebde-4a39-a9b0-bee3633907cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b053b313-7ccc-4aff-a562-2479eb890918",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
