|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "39e3e763-9b00-49eb-aead-034a2d0517a7", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# imports\n", |
| 11 | + "\n", |
| 12 | + "import os\n", |
| 13 | + "import requests\n", |
| 14 | + "from dotenv import load_dotenv\n", |
| 15 | + "from bs4 import BeautifulSoup\n", |
| 16 | + "from IPython.display import Markdown, display\n", |
| 17 | + "from openai import OpenAI\n", |
| 18 | + "\n", |
| 19 | + "# If you get an error running this cell, then please head over to the troubleshooting notebook!" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": null, |
| 25 | + "id": "f3bb5e2a-b70f-42ba-9f22-030a9c6bc9d1", |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "# Load environment variables in a file called .env\n", |
| 30 | + "\n", |
| 31 | + "load_dotenv(override=True)\n", |
| 32 | + "api_key = os.getenv('OPENAI_API_KEY')\n", |
| 33 | + "\n", |
| 34 | + "# Check the key\n", |
| 35 | + "\n", |
| 36 | + "if not api_key:\n", |
| 37 | + " print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n", |
| 38 | + "elif not api_key.startswith(\"sk-proj-\"):\n", |
| 39 | + " print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n", |
| 40 | + "elif api_key.strip() != api_key:\n", |
| 41 | + " 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 - see troubleshooting notebook\")\n", |
| 42 | + "else:\n", |
| 43 | + " print(\"API key found and looks good so far!\")\n" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "id": "994f51fb-eab3-45a2-847f-87aebb92b17a", |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "openai = OpenAI()\n", |
| 54 | + "\n", |
| 55 | + "# 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", |
| 56 | + "# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "id": "a8125c6d-c884-4f65-b477-cab155e29ce3", |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "# Step 1: Create your prompts\n", |
| 67 | + "\n", |
| 68 | + "system_prompt = \"You are an AI that suggests short and relevant subject lines for emails based on their content.\"\n", |
| 69 | + "user_prompt = \"\"\"\n", |
| 70 | + "Here is the content of an email:\n", |
| 71 | + "\n", |
| 72 | + "Dear Team,\n", |
| 73 | + "\n", |
| 74 | + "I hope you're all doing well. I wanted to remind you that our next project meeting is scheduled for this Friday at 3 PM. We will be discussing our progress and any blockers. Please make sure to review the latest updates before the meeting.\n", |
| 75 | + "\n", |
| 76 | + "Best, \n", |
| 77 | + "John\n", |
| 78 | + "\"\"\"\n", |
| 79 | + "\n", |
| 80 | + "# Step 2: Make the messages list\n", |
| 81 | + "\n", |
| 82 | + "messages = [ {\"role\": \"system\", \"content\": system_prompt},\n", |
| 83 | + " {\"role\": \"user\", \"content\": user_prompt}] # fill this in\n", |
| 84 | + "\n", |
| 85 | + "# Step 3: Call OpenAI\n", |
| 86 | + "\n", |
| 87 | + "response = openai.chat.completions.create(\n", |
| 88 | + " model = \"gpt-4o-mini\",\n", |
| 89 | + " messages=messages\n", |
| 90 | + ")\n", |
| 91 | + "\n", |
| 92 | + "# Step 4: print the result\n", |
| 93 | + "\n", |
| 94 | + "print(\"Suggested Subject Line:\", response.choices[0].message.content)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "id": "1010ac80-1ee8-432f-aa3f-12af419dc23a", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [] |
| 104 | + } |
| 105 | + ], |
| 106 | + "metadata": { |
| 107 | + "kernelspec": { |
| 108 | + "display_name": "Python 3 (ipykernel)", |
| 109 | + "language": "python", |
| 110 | + "name": "python3" |
| 111 | + }, |
| 112 | + "language_info": { |
| 113 | + "codemirror_mode": { |
| 114 | + "name": "ipython", |
| 115 | + "version": 3 |
| 116 | + }, |
| 117 | + "file_extension": ".py", |
| 118 | + "mimetype": "text/x-python", |
| 119 | + "name": "python", |
| 120 | + "nbconvert_exporter": "python", |
| 121 | + "pygments_lexer": "ipython3", |
| 122 | + "version": "3.11.11" |
| 123 | + } |
| 124 | + }, |
| 125 | + "nbformat": 4, |
| 126 | + "nbformat_minor": 5 |
| 127 | +} |
0 commit comments