From 024d474643d54225ae850ddd2dba0f9656b2af63 Mon Sep 17 00:00:00 2001 From: Rohit Nain Date: Sun, 24 Aug 2025 11:37:07 +0530 Subject: [PATCH 1/3] added email summaizers script with openai --- .../Day-1_email_summarizers.ipynb | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 week1/community-contributions/Day-1_email_summarizers.ipynb diff --git a/week1/community-contributions/Day-1_email_summarizers.ipynb b/week1/community-contributions/Day-1_email_summarizers.ipynb new file mode 100644 index 0000000..d2a4597 --- /dev/null +++ b/week1/community-contributions/Day-1_email_summarizers.ipynb @@ -0,0 +1,103 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d7a6bb51", + "metadata": {}, + "outputs": [], + "source": [ + "# import library\n", + "from openai import OpenAI\n", + "import os\n", + "from dotenv import load_dotenv\n", + "\n", + "# Load your API key from an .env file\n", + "load_dotenv(override=True)\n", + "api_key = os.getenv('OPENAI_API_KEY')\n", + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7ac4cdf9", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Step 1: Create your prompts\n", + "system_prompt = \"you are a helpful assistant that suggests an appropriate short subject line for an email based on its contents.\"\n", + "\n", + "user_prompt = \"\"\"\n", + "Hi John,\n", + "I hope this email finds you well. I wanted to follow up on our meeting last week regarding the quarterly budget proposal.\n", + "After reviewing the numbers with my team, we've identified some areas where we can reduce costs by approximately 15% without impacting our core operations. This would involve consolidating some vendor contracts and optimizing our software licensing.\n", + "Could we schedule a meeting next week to discuss these findings in detail? I'm available Tuesday through Thursday afternoon.\n", + "Looking forward to hearing from you.\n", + "\n", + "Best regards,\n", + "Sarah\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a77ca09e", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Step 2: Make the messages list\n", + "messages = [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8404f0fe", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Step 3: Call OpenAI\n", + "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a4875f7", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Step 4: Print the result\n", + "print(response.choices[0].message.content)" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 17463e83ffc9b119065f97fc8f80fe4e5adad89d Mon Sep 17 00:00:00 2001 From: Rohit Nain Date: Sun, 24 Aug 2025 17:56:13 +0530 Subject: [PATCH 2/3] day-2 exercise with ollama --- .../Day-2_exercise_with_ollama3.ipynb | 290 ++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 week1/community-contributions/Day-2_exercise_with_ollama3.ipynb diff --git a/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb b/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb new file mode 100644 index 0000000..1168770 --- /dev/null +++ b/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb @@ -0,0 +1,290 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "135717e7", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "import os\n", + "import requests\n", + "from dotenv import load_dotenv\n", + "from bs4 import BeautifulSoup\n", + "from IPython.display import Markdown, display\n", + "from openai import OpenAI\n", + "import ollama" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "29a9e634", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# OPTION 1\n", + "# using openai\n", + "\n", + "# message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", + "# client = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"not-needed\")\n", + "# response = openai.chat.completions.create(model=``, messages=[{\"role\":\"user\", \"content\":message}])\n", + "# print(response.choices[0].message.content)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "306993ed", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# OPTION 2\n", + "# using Ollama\n", + "\n", + "message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", + "model=\"llama3\"\n", + "response=ollama.chat(model=model,messages=[{\"role\":\"user\",\"content\":message}])\n", + "print(response[\"message\"][\"content\"])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "856f767b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# A class to represent a Webpage\n", + "# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n", + "\n", + "# Some websites need you to use proper headers when fetching them:\n", + "headers = {\n", + " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n", + "}\n", + "\n", + "class Website:\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, headers=headers)\n", + " soup = BeautifulSoup(response.content, 'html.parser')\n", + " self.title = soup.title.string if soup.title else \"No title found\"\n", + " for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n", + " irrelevant.decompose()\n", + " self.text = soup.body.get_text(separator=\"\\n\", strip=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "4ce558dc", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# Let's try one out. Change the website and add print statements to follow along.\n", + "\n", + "ed = Website(\"https://edwarddonner.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5e3956f8", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "99d791b4", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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 += \"\\nThe contents of this website is as follows; \\\n", + "please provide a short summary of this website in markdown. \\\n", + "If it includes news or announcements, then summarize these too.\\n\\n\"\n", + " user_prompt += website.text\n", + " return user_prompt" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "5d89b748", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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", + " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", + " ]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9a97d3e2", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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=ollama.chat(model=model,messages=messages_for(website))\n", + " return(response[\"message\"][\"content\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec13fe0a", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "summarize(\"https://edwarddonner.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "e3ade092", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be2d49e6", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "display_summary(\"https://edwarddonner.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ccbf33b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "display_summary(\"https://cnn.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae3d0eae", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "display_summary(\"https://anthropic.com\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 604aca783c6b2a748365d53676606867e23b72b4 Mon Sep 17 00:00:00 2001 From: Rohit Nain Date: Sun, 24 Aug 2025 17:57:52 +0530 Subject: [PATCH 3/3] Revert "day-2 exercise with ollama" This reverts commit 17463e83ffc9b119065f97fc8f80fe4e5adad89d. --- .../Day-2_exercise_with_ollama3.ipynb | 290 ------------------ 1 file changed, 290 deletions(-) delete mode 100644 week1/community-contributions/Day-2_exercise_with_ollama3.ipynb diff --git a/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb b/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb deleted file mode 100644 index 1168770..0000000 --- a/week1/community-contributions/Day-2_exercise_with_ollama3.ipynb +++ /dev/null @@ -1,290 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "135717e7", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "# imports\n", - "\n", - "import os\n", - "import requests\n", - "from dotenv import load_dotenv\n", - "from bs4 import BeautifulSoup\n", - "from IPython.display import Markdown, display\n", - "from openai import OpenAI\n", - "import ollama" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "29a9e634", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "# OPTION 1\n", - "# using openai\n", - "\n", - "# message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", - "# client = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"not-needed\")\n", - "# response = openai.chat.completions.create(model=``, messages=[{\"role\":\"user\", \"content\":message}])\n", - "# print(response.choices[0].message.content)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "306993ed", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "# OPTION 2\n", - "# using Ollama\n", - "\n", - "message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", - "model=\"llama3\"\n", - "response=ollama.chat(model=model,messages=[{\"role\":\"user\",\"content\":message}])\n", - "print(response[\"message\"][\"content\"])\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "856f767b", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "# A class to represent a Webpage\n", - "# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n", - "\n", - "# Some websites need you to use proper headers when fetching them:\n", - "headers = {\n", - " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n", - "}\n", - "\n", - "class Website:\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, headers=headers)\n", - " soup = BeautifulSoup(response.content, 'html.parser')\n", - " self.title = soup.title.string if soup.title else \"No title found\"\n", - " for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n", - " irrelevant.decompose()\n", - " self.text = soup.body.get_text(separator=\"\\n\", strip=True)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "4ce558dc", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "# Let's try one out. Change the website and add print statements to follow along.\n", - "\n", - "ed = Website(\"https://edwarddonner.com\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "5e3956f8", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "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.\"" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "99d791b4", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "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 += \"\\nThe contents of this website is as follows; \\\n", - "please provide a short summary of this website in markdown. \\\n", - "If it includes news or announcements, then summarize these too.\\n\\n\"\n", - " user_prompt += website.text\n", - " return user_prompt" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "5d89b748", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "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", - " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", - " ]" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9a97d3e2", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "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=ollama.chat(model=model,messages=messages_for(website))\n", - " return(response[\"message\"][\"content\"])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ec13fe0a", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "summarize(\"https://edwarddonner.com\")" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "e3ade092", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "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))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "be2d49e6", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "display_summary(\"https://edwarddonner.com\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1ccbf33b", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "display_summary(\"https://cnn.com\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ae3d0eae", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, - "outputs": [], - "source": [ - "display_summary(\"https://anthropic.com\")" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}