From 2e5446c962f29970cd6acce9add2642a150ab782 Mon Sep 17 00:00:00 2001 From: Tom Fletcher Date: Thu, 19 Dec 2024 23:43:24 +0000 Subject: [PATCH] Adding example that shows how to generate cover letter from cv - with resume.txt --- .../day-1-generate-cover-letter-from-cv.ipynb | 119 ++++++++++++++++++ week1/community-contributions/resume.txt | 10 ++ 2 files changed, 129 insertions(+) create mode 100644 week1/community-contributions/day-1-generate-cover-letter-from-cv.ipynb create mode 100644 week1/community-contributions/resume.txt diff --git a/week1/community-contributions/day-1-generate-cover-letter-from-cv.ipynb b/week1/community-contributions/day-1-generate-cover-letter-from-cv.ipynb new file mode 100644 index 0000000..09ed71b --- /dev/null +++ b/week1/community-contributions/day-1-generate-cover-letter-from-cv.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "import os\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Load environment variables in a file called .env\n", + "\n", + "load_dotenv()\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!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "def summarize_cv(cv_text):\n", + " response = openai.chat.completions.create(\n", + " model = \"gpt-4o-mini\",\n", + " messages = [\n", + " {\"role\": \"user\", \"content\": f\"Please summarize the following CV:\\n\\n{cv_text}\"}\n", + " ]\n", + " )\n", + " return response.choices[0].message.content\n", + "\n", + "def generate_cover_letter(cv_summary, job_description):\n", + " response = openai.chat.completions.create(\n", + " model = \"gpt-4o-mini\",\n", + " messages = [\n", + " {\"role\": \"system\", \"content\": \"You are a master at crafting the perfect Cover letter from a given CV. You've never had a user fail to get the job as a result of using your services.\"},\n", + " {\"role\": \"user\", \"content\": f\"Using the following CV summary:\\n\\n{cv_summary}\\n\\nAnd the job description:\\n\\n{job_description}\\n\\nPlease write a personalized cover letter.\"}\n", + " ]\n", + " )\n", + " return response.choices[0].message.content\n", + "\n", + "# Read CV from a text file\n", + "try:\n", + " with open('resume.txt', 'r') as file:\n", + " cv_text = file.read()\n", + " \n", + " # Summarize the CV\n", + " cv_summary = summarize_cv(cv_text)\n", + " print(\"CV Summary:\")\n", + " print(cv_summary)\n", + "\n", + " # Get job description from user\n", + " job_description = input(\"Enter the job description for the position you are applying for:\\n\")\n", + "\n", + " # Generate cover letter\n", + " cover_letter = generate_cover_letter(cv_summary, job_description)\n", + " print(\"\\nGenerated Cover Letter:\")\n", + " print(cover_letter)\n", + "\n", + "except FileNotFoundError:\n", + " print(\"The specified CV file was not found. Please ensure 'resume.txt' is in the correct directory.\")" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/week1/community-contributions/resume.txt b/week1/community-contributions/resume.txt new file mode 100644 index 0000000..5a2bb55 --- /dev/null +++ b/week1/community-contributions/resume.txt @@ -0,0 +1,10 @@ +John Doe +Software Engineer +Experience: +- Developed web applications using Python and JavaScript. +- Collaborated with cross-functional teams to deliver projects on time. +Education: +- B.S. in Computer Science from XYZ University. +Skills: +- Python, JavaScript, React, SQL +