Browse Source

More troubleshooting tips added

pull/68/head
Edward Donner 4 months ago
parent
commit
b6c06fd010
  1. 7
      week1/day1.ipynb
  2. 155
      week1/troubleshooting.ipynb
  3. 7
      week8/day1.ipynb

7
week1/day1.ipynb

@ -115,7 +115,7 @@
"source": [ "source": [
"# Load environment variables in a file called .env\n", "# Load environment variables in a file called .env\n",
"\n", "\n",
"load_dotenv()\n", "load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n", "api_key = os.getenv('OPENAI_API_KEY')\n",
"\n", "\n",
"# Check the key\n", "# Check the key\n",
@ -140,8 +140,7 @@
"openai = OpenAI()\n", "openai = OpenAI()\n",
"\n", "\n",
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n", "# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
"# If it STILL doesn't work (horrors!) then please see the troubleshooting notebook, or try the below line instead:\n", "# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions"
"# openai = OpenAI(api_key=\"your-key-here-starting-sk-proj-\")"
] ]
}, },
{ {
@ -159,7 +158,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# To give you a preview -- calling OpenAI with these messages is this easy:\n", "# To give you a preview -- calling OpenAI with these messages is this easy. Any problems, head over to the Troubleshooting notebook.\n",
"\n", "\n",
"message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", "message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n",
"response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=[{\"role\":\"user\", \"content\":message}])\n", "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=[{\"role\":\"user\", \"content\":message}])\n",

155
week1/troubleshooting.ipynb

@ -14,6 +14,41 @@
"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" "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": "98787335-346f-4ee4-9cb7-6181b0e1b964",
"metadata": {},
"source": [
"# Before we begin\n",
"\n",
"## Checking your internet connection\n",
"\n",
"First let's check that there's no VPN or Firewall or Certs problem.\n",
"\n",
"Click in the cell below and press Shift+Return to run it. \n",
"If this gives you problems, then please try working through these instructions to address: \n",
"https://chatgpt.com/share/676e6e3b-db44-8012-abaa-b3cf62c83eb3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d296f9b6-8de4-44db-b5f5-9b653dfd3d81",
"metadata": {},
"outputs": [],
"source": [
"import urllib.request\n",
"\n",
"try:\n",
" response = urllib.request.urlopen(\"https://www.google.com\", timeout=10)\n",
" if response.status != 200:\n",
" print(\"Unable to reach google - there may be issues with your internet / VPN / firewall?\")\n",
" else:\n",
" print(\"Connected to the internet and can reach Google\")\n",
"except Exception as e:\n",
" print(f\"Failed to connect with this error: {e}\")"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "f5190688-205a-46d1-a0dc-9136a42ad0db", "id": "f5190688-205a-46d1-a0dc-9136a42ad0db",
@ -80,7 +115,7 @@
"# And now, this should run with no output - no import errors.\n", "# 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", "# 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 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", "# Or it's possible that something is wrong with Anaconda. Please try SETUP Part 2B, the alternative to Part 2.\n",
"# If you're unsure, please run the diagnostics (last cell in this notebook) and then email me at ed@edwarddonner.com\n", "# If you're unsure, please run the diagnostics (last cell in this notebook) and then email me at ed@edwarddonner.com\n",
"\n", "\n",
"from openai import OpenAI" "from openai import OpenAI"
@ -96,6 +131,14 @@
"Let's check your .env file exists and has the OpenAI key set properly inside it. \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", "Please run this code and check that it prints a successful message, otherwise follow its instructions.\n",
"\n", "\n",
"If it isn't successful, 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",
"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",
"\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", "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", "\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", "If you're having challenges creating the `.env` file, we can also do it with code! See the cell after the next one.\n",
@ -162,20 +205,26 @@
"source": [ "source": [
"# Only run this code in this cell if you want to have a .env file created for you!\n", "# Only run this code in this cell if you want to have a .env file created for you!\n",
"\n", "\n",
"# Put your key inside the quote marks\n",
"make_me_a_file_with_this_key = \"put your key here inside these quotes.. it should start sk-proj-\"\n", "make_me_a_file_with_this_key = \"put your key here inside these quotes.. it should start sk-proj-\"\n",
"\n", "\n",
"# Change this to True if you already have a .env file and you want me to replace it\n",
"overwrite_if_already_exists = False \n",
"\n",
"from pathlib import Path\n", "from pathlib import Path\n",
"\n", "\n",
"parent_dir = Path(\"..\")\n", "parent_dir = Path(\"..\")\n",
"env_path = parent_dir / \".env\"\n", "env_path = parent_dir / \".env\"\n",
"\n", "\n",
"if env_path.exists():\n", "if env_path.exists() and not overwrite_if_already_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", " print(\"There is already a .env file - if you want me to create a new one, change the variable overwrite_if_already_exists to True above\")\n",
"else:\n", "else:\n",
" try:\n", " try:\n",
" with env_path.open(mode='w', encoding='utf-8') as env_file:\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", " 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(f\"Successfully created the .env file at {env_path}\")\n",
" if not make_me_a_file_with_this_key.startswith(\"sk-proj-\"):\n",
" print(f\"The key that you provided started with '{make_me_a_file_with_this_key[:8]}' which is different to sk-proj- is that what you intended?\")\n",
" print(\"Now rerun the previous cell to confirm that the file is created and the key is correct.\")\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", " except Exception as e:\n",
" print(f\"An error occurred while creating the .env file: {e}\")" " print(f\"An error occurred while creating the .env file: {e}\")"
@ -199,24 +248,39 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# This should print your API key to the output\n", "# This should print your API key to the output - please follow the instructions that get printed\n",
"\n", "\n",
"import os\n", "import os\n",
"from dotenv import load_dotenv\n", "from dotenv import load_dotenv\n",
"load_dotenv()\n", "load_dotenv(override=True)\n",
"\n", "\n",
"api_key = os.getenv(\"OPENAI_API_KEY\")\n", "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n", "\n",
"if not api_key:\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", " 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", "elif not api_key.startswith(\"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", " print(f\"An API key was found, but it starts with {api_key[:8]} rather than sk-proj- please double check this is as expected.\")\n",
"elif api_key.strip() != api_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", " 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", "else:\n",
" print(\"API key found and looks good so far!\")\n", " print(\"API key found and looks good so far!\")\n",
"\n", "\n",
"print(\"My key is\", os.getenv(\"OPENAI_API_KEY\"))" "if api_key:\n",
" problematic_unicode_chars = ['\\u2013', '\\u2014', '\\u201c', '\\u201d', '\\u2026', '\\u2018', '\\u2019']\n",
" forbidden_chars = [\"'\", \" \", \"\\n\", \"\\r\", '\"']\n",
" \n",
" if not all(32 <= ord(char) <= 126 for char in api_key):\n",
" print(\"Potential problem: there might be unprintable characters accidentally included in the key?\")\n",
" elif any(char in api_key for char in problematic_unicode_chars):\n",
" print(\"Potential problem: there might be special characters, like long hyphens or curly quotes in the key - did you copy it via a word processor?\")\n",
" elif any(char in api_key for char in forbidden_chars):\n",
" print(\"Potential problem: there are quote marks, spaces or empty lines in your key?\")\n",
" else:\n",
" print(\"The API key contains valid characters\")\n",
" \n",
"print(f\"\\nHere is the key --> {api_key} <--\")\n",
"print()\n",
"print(\"If this key looks good, please go to the Edit menu >> Clear Cell Output so that your key is no longer displayed here!\")"
] ]
}, },
{ {
@ -224,20 +288,17 @@
"id": "f403e515-0e7d-4be4-bb79-5a102dbd6c94", "id": "f403e515-0e7d-4be4-bb79-5a102dbd6c94",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## It should print something like:\n", "## It should print some checks including something like:\n",
"\n",
"`My key is sk-proj-blahblahblah`\n",
"\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", "`Here is the key --> sk-proj-blahblahblah <--`\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", "\n",
"Nasty gotchas to watch out for: \n", "If it didn't print a key, then hopefully it's given you enough information to figure this out. Or contact me!\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", "\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." "There is a final fallback approach if you wish: you can avoid using .env files altogether, and simply always provide your API key manually. \n",
"Whenever you see this in the code: \n",
"`openai = OpenAI()` \n",
"You can replace it with: \n",
"`openai = OpenAI(api_key=\"sk-proj-xxx\")`\n"
] ]
}, },
{ {
@ -261,22 +322,19 @@
"source": [ "source": [
"from openai import OpenAI\n", "from openai import OpenAI\n",
"from dotenv import load_dotenv\n", "from dotenv import load_dotenv\n",
"load_dotenv()\n", "load_dotenv(override=True)\n",
"\n", "\n",
"# EITHER:\n",
"my_api_key = os.getenv(\"OPENAI_API_KEY\")\n", "my_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\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", "print(f\"Using API key --> {my_api_key} <--\")\n",
"# my_api_key = \"REPLACE THIS TEXT WITH YOUR OPENAI API KEY WITHIN THE QUOTE MARKS - it should start sk-proj-\"\n",
"\n", "\n",
"print(f\"Using API key {my_api_key}\")\n", "openai = OpenAI()\n",
"\n",
"openai = OpenAI(api_key=my_api_key)\n",
"completion = openai.chat.completions.create(\n", "completion = openai.chat.completions.create(\n",
" model='gpt-4o-mini',\n", " model='gpt-4o-mini',\n",
" messages=[{\"role\":\"user\", \"content\": \"What's 2+2?\"}],\n", " messages=[{\"role\":\"user\", \"content\": \"What's 2+2?\"}],\n",
")\n", ")\n",
"print(completion.choices[0].message.content)" "print(completion.choices[0].message.content)\n",
"print(\"Now go to Edit menu >> Clear Cell Output to remove the display of your key.\")"
] ]
}, },
{ {
@ -284,20 +342,31 @@
"id": "81046a77-c359-4388-929f-ffc8ad5cb93c", "id": "81046a77-c359-4388-929f-ffc8ad5cb93c",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# If the key was set correctly, and this still didn't work, perhaps with a RateLimit error\n", "## If the key was set correctly, and this still didn't work\n",
"\n",
"Then there's something up with your API key!\n",
"\n", "\n",
"First check this webpage to make sure you have a positive credit balance.\n", "### If there's an error from OpenAI about your key, or a Rate Limit Error, then there's something up with your API key!\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", "\n",
"https://platform.openai.com/settings/organization/billing/overview\n", "First check [this webpage](https://platform.openai.com/settings/organization/billing/overview) to make sure you have a positive credit balance.\n",
"OpenAI requires that you have a positive credit balance and it has minimums, typically around $5 in local currency. 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; the README has instructions to call free open-source models via Ollama whenever we use OpenAI.\n",
"\n", "\n",
"Also try creating a new key (button on the top right) here:\n", "OpenAI billing page with credit balance is here: \n",
"https://platform.openai.com/settings/organization/billing/overview \n",
"OpenAI can take a few minutes to enable your key after you top up your balance. \n",
"A student outside the US mentioned that he needed to allow international payments on his credit card for this to work. \n",
"\n", "\n",
"It's unlikely, but if there's something wrong with your key, you could also try creating a new key (button on the top right) here: \n",
"https://platform.openai.com/api-keys\n", "https://platform.openai.com/api-keys\n",
"\n", "\n",
"Sometimes OpenAI may take a few minutes to give you access after you try.\n", "### If there's a cert related error\n",
"\n",
"If you encountered a certificates error like: \n",
"`ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)` \n",
"Then please replace:\n",
"`openai = OpenAI()` \n",
"with: \n",
"`import httpx` \n",
"`openai = OpenAI(http_client=httpx.Client(verify=False))` \n",
"And if that works, you're in good shape. You'll just have to change the labs in the same way any time you hit this cert error.\n",
"\n", "\n",
"## If all else fails:\n", "## If all else fails:\n",
"\n", "\n",
@ -336,22 +405,6 @@
"from diagnostics import Diagnostics\n", "from diagnostics import Diagnostics\n",
"Diagnostics().run()" "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": { "metadata": {

7
week8/day1.ipynb

@ -187,7 +187,11 @@
"\n", "\n",
"This is how you could package your AI service behind an API to be used in a Production System.\n", "This is how you could package your AI service behind an API to be used in a Production System.\n",
"\n", "\n",
"You can also build REST endpoints easily, although we won't cover that as we'll be calling direct from Python." "You can also build REST endpoints easily, although we won't cover that as we'll be calling direct from Python.\n",
"\n",
"## Important note:\n",
"\n",
"On the next line, I call `modal deploy` from within Jupyter lab; I've heard that on some versions of Windows this gives a strange unicode error because modal prints emojis to the output which can't be displayed. If that happens to you, simply use an Anaconda Prompt window or a Powershell instead, with your environment activated, and type `modal deploy pricer_service` there. Follow the same approach the next time we do !modal deploy too."
] ]
}, },
{ {
@ -227,6 +231,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# You can also run \"modal deploy pricer_service2\" at the command line in an activated environment\n",
"!modal deploy pricer_service2" "!modal deploy pricer_service2"
] ]
}, },

Loading…
Cancel
Save