Browse Source

Further troubleshooting instructions, also a minor compatibility fix

pull/40/head
Edward Donner 6 months ago
parent
commit
d75fe437fe
  1. 36
      week1/day1.ipynb
  2. 84
      week1/troubleshooting.ipynb
  3. 18
      week2/day2.ipynb

36
week1/day1.ipynb

@ -57,7 +57,9 @@
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI"
"from openai import OpenAI\n",
"\n",
"# If you get an error like \"NameError\" running this cell, then please head over to the troubleshooting notebook!"
]
},
{
@ -71,6 +73,10 @@
"\n",
"## Troubleshooting if you have problems:\n",
"\n",
"Head over to the [troubleshooting](troubleshooting.ipynb) notebook in this folder for step by step code to identify the root cause and fix it!\n",
"\n",
"A summary of some points:\n",
"\n",
"1. OpenAI takes a few minutes to register after you set up an account. If you receive an error about being over quota, try waiting a few minutes and try again.\n",
"2. You'll need to set up billing and add the minimum amount of credit at this page [here](https://platform.openai.com/settings/organization/billing/overview). OpenAI requires a minimum of $5 to get started in the U.S. right now - this might be different for your region. You'll only need to use a fraction for this course. In my view, this is well worth the investment for your education and future projects - but if you have any concerns, you can skip this and watch me using OpenAI instead. In week 3 we will start to use free open-source models!\n",
"3. Also, double check you have the right kind of API token with the right permissions. You should find it on [this webpage](https://platform.openai.com/api-keys) and it should show with Permissions of \"All\". If not, try creating another key by:\n",
@ -79,8 +85,7 @@
"- Click Create secret key, and use that new key in the code and the `.env` file (it might take a few minutes to activate)\n",
"- Do a Kernel >> Restart kernel, and execute the cells in this Jupyter lab starting at the top\n",
"4. As a fallback, replace the line `openai = OpenAI()` with `openai = OpenAI(api_key=\"your-key-here\")` - while it's not recommended to hard code tokens in Jupyter lab, because then you can't share your lab with others, it's a workaround for now\n",
"5. See the [troubleshooting](troubleshooting.ipynb) notebook in this folder for more instructions\n",
"6. Contact me! Message me or email ed@edwarddonner.com and we will get this to work.\n",
"5. Contact me! Message me or email ed@edwarddonner.com and we will get this to work.\n",
"\n",
"Any concerns about API costs? See my notes in the README - costs should be minimal, and you can control it at every point."
]
@ -95,10 +100,31 @@
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "019974d9-f3ad-4a8a-b5f9-0a3719aea2d3",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"\n",
"# See the troubleshooting notebook, ot try the below line instead if this gives you any problems:\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, ot try the below line instead:\n",
"# openai = OpenAI(api_key=\"your-key-here\")"
]
},

84
week1/troubleshooting.ipynb

@ -25,6 +25,8 @@
"\n",
"If this doesn't work, then you're likely not running in an \"activated\" environment. Please check back in the [README](../README.md) for setting up the Anaconda (or virtualenv) environment and activating it, before running `jupyter lab`.\n",
"\n",
"If you look in the Anaconda window (on PC) or the Terminal window (on 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",
"You might also need to restart the \"Kernel\" (the python process used to run this notebook). To do that, go to the Kernel menu and select \"Restart Kernel and Clear Outputs Of All Cells..\""
]
},
@ -42,11 +44,63 @@
},
{
"cell_type": "markdown",
"id": "0ba9420d-3bf0-4e08-abac-f2fbf0e9c7f1",
"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!"
]
},
{
"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": "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.\n",
"Try running the next cell."
]
@ -63,6 +117,18 @@
"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\"))"
]
},
@ -92,7 +158,7 @@
"id": "42afad1f-b0bf-4882-b469-7709060fee3a",
"metadata": {},
"source": [
"# Step 3\n",
"# Step 4\n",
"\n",
"Now run the below code and you will hopefully see that GPT can handle basic arithmetic!!\n",
"\n",
@ -108,7 +174,13 @@
"source": [
"from openai import OpenAI\n",
"\n",
"my_api_key = \"REPLACE THIS TEXT WITH YOUR OPENAI API KEY WITHIN THE QUOTE MARKS - it should start sk-proj-\"\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",
@ -123,12 +195,12 @@
"id": "81046a77-c359-4388-929f-ffc8ad5cb93c",
"metadata": {},
"source": [
"# If this didn't work\n",
"# 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. Contact me if this is a problem - I can likely share a key with a small limit. But 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.\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.\n",
"\n",
"https://platform.openai.com/settings/organization/billing/overview\n",
"\n",
@ -144,7 +216,7 @@
"\n",
"(2) Contact me! ed@edwarddonner.com\n",
"\n",
"Thanks so much.."
"Thanks so much, and I'm sorry this is giving you bother!"
]
},
{

18
week2/day2.ipynb

@ -345,7 +345,7 @@
"source": [
"view = gr.Interface(\n",
" fn=stream_model,\n",
" inputs=[gr.Textbox(label=\"Your message:\"), gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\")],\n",
" inputs=[gr.Textbox(label=\"Your message:\"), gr.Dropdown([\"GPT\", \"Claude\"], label=\"Select model\", value=\"GPT\")],\n",
" outputs=[gr.Markdown(label=\"Response:\")],\n",
" flagging_mode=\"never\"\n",
")\n",
@ -438,22 +438,6 @@
")\n",
"view.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0fc580a-dc98-48c3-9dd4-b19cd3be5a18",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3d3bf11-e02c-492b-96f1-f4dd7df6f4d7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

Loading…
Cancel
Save