Updated explanations and tips

This commit is contained in:
Edward Donner
2024-11-18 16:57:26 -05:00
parent 7aeb18a3df
commit f4206653c3
13 changed files with 430 additions and 73 deletions

View File

@@ -34,7 +34,7 @@
"4. Kernel menu >> Restart Kernel and Clear Outputs of All Cells\n",
"5. Come back to this notebook and 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."
"If **that** doesn't work, then please contact me! I'll respond quickly, and we'll figure it out. 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)."
]
},
{
@@ -46,6 +46,8 @@
"source": [
"# 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",
"\n",
"from openai import OpenAI"
]
@@ -60,7 +62,9 @@
"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!"
"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."
]
},
{
@@ -102,6 +106,45 @@
" 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",
@@ -109,7 +152,7 @@
"source": [
"# Step 3\n",
"\n",
"Now let's check that your API key is correct set up in your `.env` file.\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."
]
},