Browse Source

More instructions and comments

pull/9/head
Edward Donner 6 months ago
parent
commit
513c95a98a
  1. 27
      week1/day1.ipynb
  2. 6
      week1/troubleshooting.ipynb

27
week1/day1.ipynb

@ -30,6 +30,10 @@
"\n",
"Please see the [troubleshooting](troubleshooting.ipynb) notebook in this folder for more ideas!\n",
"\n",
"## If this is old hat!\n",
"\n",
"If you're already comfortable with this material, never fear - you can move swiftly through the first few labs - we will get much more in depth as the weeks progress.\n",
"\n",
"## Business value of these exercises\n",
"\n",
"A final thought. While I've designed these notebooks to be educational, I've also tried to make them enjoyable. We'll do fun things like have LLMs tell jokes and argue with each other. But fundamentally, my goal is to teach skills you can apply in business. I'll explain business implications as we go, and it's worth keeping this in mind: as you build experience with models and techniques, think of ways you could put this into action at work today. Please do contact me if you'd like to discuss more or if you have ideas to bounce off me."
@ -64,7 +68,8 @@
"## Troubleshooting if you have problems:\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. 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",
"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",
"- Pressing \"Create new secret key\" on the top right\n",
"- Select **Owned by:** you, **Project:** Default project, **Permissions:** All\n",
"- 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",
@ -89,7 +94,7 @@
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"openai = OpenAI()\n",
"\n",
"# Uncomment the below line if this gives you any problems:\n",
"# See the troubleshooting notebook, ot try the below line instead if this gives you any problems:\n",
"# openai = OpenAI(api_key=\"your-key-here\")"
]
},
@ -103,11 +108,17 @@
"# A class to represent a Webpage\n",
"\n",
"class Website:\n",
" \"\"\"\n",
" A utility class to represent a Website that we have scraped\n",
" \"\"\"\n",
" url: str\n",
" title: str\n",
" text: str\n",
"\n",
" def __init__(self, url):\n",
" \"\"\"\n",
" Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n",
" self.url = url\n",
" response = requests.get(url)\n",
" soup = BeautifulSoup(response.content, 'html.parser')\n",
@ -156,6 +167,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
@ -168,6 +181,8 @@
"metadata": {},
"outputs": [],
"source": [
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"The contents of this website is as follows; \\\n",
@ -201,6 +216,8 @@
"metadata": {},
"outputs": [],
"source": [
"# See how this function creates exactly the format above\n",
"\n",
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
@ -223,6 +240,8 @@
"metadata": {},
"outputs": [],
"source": [
"# And now: call the OpenAI API. You will get very familiar with this!\n",
"\n",
"def summarize(url):\n",
" website = Website(url)\n",
" response = openai.chat.completions.create(\n",
@ -249,6 +268,8 @@
"metadata": {},
"outputs": [],
"source": [
"# A function to display this nicely in the Jupyter output, using markdown\n",
"\n",
"def display_summary(url):\n",
" summary = summarize(url)\n",
" display(Markdown(summary))"
@ -273,7 +294,7 @@
"\n",
"Note that this will only work on websites that can be scraped using this simplistic approach.\n",
"\n",
"Websites that are rendered with Javascript, like React apps, won't show up. See the community-contributions folder for a Selenium implementation that gets around this.\n",
"Websites that are rendered with Javascript, like React apps, won't show up. See the community-contributions folder for a Selenium implementation that gets around this. You'll need to read up on installing Selenium (ask ChatGPT!)\n",
"\n",
"Also Websites protected with CloudFront (and similar) may give 403 errors - many thanks Andy J for pointing this out.\n",
"\n",

6
week1/troubleshooting.ipynb

@ -86,7 +86,9 @@
"source": [
"# Step 3\n",
"\n",
"Now run the below code and you will hopefully see that GPT can handle basic arithmetic!!"
"Now run the below code and you will hopefully see that GPT can handle basic arithmetic!!\n",
"\n",
"If not, see the cell below."
]
},
{
@ -118,7 +120,7 @@
"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.\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",
"\n",
"https://platform.openai.com/settings/organization/billing/overview\n",
"\n",

Loading…
Cancel
Save