Added my contributions to community-contributions.
Add notebook for summarizing email content and generating short subject lines
This commit is contained in:
@@ -0,0 +1,138 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T05:05:46.954114Z",
|
||||||
|
"start_time": "2025-10-20T05:05:46.950176Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"from dotenv import load_dotenv\n",
|
||||||
|
"from openai import OpenAI"
|
||||||
|
],
|
||||||
|
"id": "993f30e559b1715e",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T05:05:48.567377Z",
|
||||||
|
"start_time": "2025-10-20T05:05:48.562083Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"load_dotenv(override=True)\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 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 - see troubleshooting notebook\")\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 - see troubleshooting notebook\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"API key found and looks good so far!\")"
|
||||||
|
],
|
||||||
|
"id": "25f968a445988d63",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"API key found and looks good so far!\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T05:06:30.515611Z",
|
||||||
|
"start_time": "2025-10-20T05:06:28.389634Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"openai = OpenAI()\n",
|
||||||
|
"\n",
|
||||||
|
"# Step 1: Create your prompts\n",
|
||||||
|
"\n",
|
||||||
|
"system_prompt = \"summarize the contents of the email and suggest a short subject line for the email\"\n",
|
||||||
|
"user_prompt = \"\"\"\n",
|
||||||
|
" Summarize the contents of the email and suggest a short subject line for the email.\n",
|
||||||
|
" Dear Alice,\n",
|
||||||
|
" I'm writing to you to tell you that I'm very happy to meet you.\n",
|
||||||
|
" I hope you're doing well.\n",
|
||||||
|
" I'm going to tell you a story.\n",
|
||||||
|
"\n",
|
||||||
|
" The story is about a boy who is a young boy. The skin color of the boy is green and he makes a lot of noise.\n",
|
||||||
|
" He is very clever and has a lot of friends.\n",
|
||||||
|
" He is very smart and has a lot of money.\n",
|
||||||
|
" He is very clever and has a lot of friends.\n",
|
||||||
|
" He is very smart and has a lot of money.\n",
|
||||||
|
"\n",
|
||||||
|
" The story ends here.\n",
|
||||||
|
"\n",
|
||||||
|
" Regards,\n",
|
||||||
|
" Bob\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"# Step 2: Make the messages list\n",
|
||||||
|
"\n",
|
||||||
|
"messages = [\n",
|
||||||
|
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
||||||
|
" {\"role\": \"user\", \"content\": user_prompt}\n",
|
||||||
|
"] # fill this in\n",
|
||||||
|
"\n",
|
||||||
|
"# Step 3: Call OpenAI\n",
|
||||||
|
"response = openai.chat.completions.create(model=\"gpt-4.1-mini\", messages=messages)\n",
|
||||||
|
"\n",
|
||||||
|
"# Step 4: print the result\n",
|
||||||
|
"print(response.choices[0].message.content)\n"
|
||||||
|
],
|
||||||
|
"id": "b98e0b22e9087445",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Summary: Bob writes to Alice expressing happiness about meeting her and shares a brief story about a clever, green-skinned boy who is noisy, smart, wealthy, and popular.\n",
|
||||||
|
"\n",
|
||||||
|
"Suggested subject line: \"Happy to Meet You & A Short Story\"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 12
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user