{ "cells": [ { "cell_type": "markdown", "id": "c79dc33e-1a3b-4601-a8f2-219b7a9b6d88", "metadata": {}, "source": [ "# Company Brochure - Relevant Links and Custom Tone\n", "\n", "Using GPT to generate a company brochure with the relevant links functionality and the ability to choose the desired tone." ] }, { "cell_type": "code", "execution_count": 1, "id": "e32f4aa7-6fc4-4dc9-8058-58e6a7f329c5", "metadata": {}, "outputs": [], "source": [ "# Imports\n", "\n", "import os\n", "import requests\n", "import json\n", "from typing import List\n", "from dotenv import load_dotenv\n", "from bs4 import BeautifulSoup\n", "from IPython.display import Markdown, display, update_display\n", "from openai import OpenAI\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "id": "d1d65a21-bbba-44ff-a2be-85bf2055a493", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key set and good to go.\n" ] } ], "source": [ "# Load environment variables in a file called .env\n", "\n", "load_dotenv(override=True)\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n", "google_api_key = os.getenv('GOOGLE_API_KEY')\n", "\n", "if openai_api_key:\n", " print(\"OpenAI API Key set and good to go.\")\n", "else:\n", " print(\"OpenAI API Key not set. :(\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "c5db63fe-5da8-496e-9b37-139598d600a7", "metadata": {}, "outputs": [], "source": [ "# Setting up the OpenAI object\n", "\n", "openai = OpenAI()\n", "gpt_model = 'gpt-4o-mini'" ] }, { "cell_type": "code", "execution_count": 4, "id": "535da52f-b280-48ce-aa8b-f82f9f9805d9", "metadata": {}, "outputs": [], "source": [ "# A class to represent a Webpage\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", " A utility class to represent a Website that we have scraped, now with links\n", " \"\"\"\n", "\n", " def __init__(self, url):\n", " self.url = url\n", " response = requests.get(url, headers=headers)\n", " self.body = response.content\n", " soup = BeautifulSoup(self.body, 'html.parser')\n", " self.title = soup.title.string if soup.title else \"No title found\"\n", " if soup.body:\n", " for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n", " irrelevant.decompose()\n", " self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n", " else:\n", " self.text = \"\"\n", " links = [link.get('href') for link in soup.find_all('a')]\n", " self.links = [link for link in links if link]\n", "\n", " def get_contents(self):\n", " return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\"" ] }, { "cell_type": "code", "execution_count": 5, "id": "8d5757c4-95f4-4038-8ed4-8c81da5112b0", "metadata": {}, "outputs": [], "source": [ "link_system_prompt = \"You are provided with a list of links found on a webpage. \\\n", "You are able to decide which of the links would be most relevant to include in a brochure about the company, \\\n", "such as links to an About page, or a Company page, or Careers/Jobs pages.\\n\"\n", "link_system_prompt += \"You should respond in JSON as in this example:\"\n", "link_system_prompt += \"\"\"\n", "{\n", " \"links\": [\n", " {\"type\": \"about page\", \"url\": \"https://full.url/goes/here/about\"},\n", " {\"type\": \"careers page\": \"url\": \"https://another.full.url/careers\"}\n", " ]\n", "}\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 6, "id": "d5fd31ac-7c81-454a-a1dc-4c58bd3db246", "metadata": {}, "outputs": [], "source": [ "def get_links_user_prompt(website):\n", " user_prompt = f\"Here is the list of links on the website of {website.url} - \"\n", " user_prompt += \"please decide which of these are relevant web links for a brochure about the company, respond with the full https URL in JSON format. \\\n", "Do not include Terms of Service, Privacy, email links.\\n\"\n", " user_prompt += \"Links (some might be relative links):\\n\"\n", " user_prompt += \"\\n\".join(website.links)\n", " return user_prompt" ] }, { "cell_type": "code", "execution_count": 7, "id": "e8b67492-1ba4-4aad-a588-39116128fa18", "metadata": {}, "outputs": [], "source": [ "def gpt_get_links(url):\n", " website = Website(url)\n", " response = openai.chat.completions.create(\n", " model= gpt_model,\n", " messages=[\n", " {\"role\": \"system\", \"content\": link_system_prompt},\n", " {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n", " ],\n", " response_format={\"type\": \"json_object\"}\n", " )\n", " result = response.choices[0].message.content\n", " return json.loads(result)" ] }, { "cell_type": "code", "execution_count": 8, "id": "e8846e7a-ace2-487e-a0a8-fccb389f2eb9", "metadata": {}, "outputs": [], "source": [ "# This function provides uses the get_contents method in the Website Class as well as GPT to find relevant links.\n", "\n", "def get_all_details(url):\n", " result = \"Landing page:\\n\"\n", " result += Website(url).get_contents()\n", " links = gpt_get_links(url)\n", " print(\"Found links:\", links)\n", " for link in links[\"links\"]:\n", " result += f\"\\n\\n{link['type']}\\n\"\n", " result += Website(link[\"url\"]).get_contents()\n", " return result" ] }, { "cell_type": "code", "execution_count": 9, "id": "18b42319-8342-4b9c-bef6-8b72acf92ab3", "metadata": {}, "outputs": [], "source": [ "def get_brochure_user_prompt(company_name, url):\n", " user_prompt = f\"You are looking at a company called: {company_name}\\n\"\n", " user_prompt += f\"Here are the contents of its landing page and other relevant pages; \\\n", " use this information to build a short brochure of the company in markdown.\\n\"\n", " \n", " user_prompt += get_all_details(url)\n", " user_prompt = user_prompt[:5_000] # Truncate if more than 5,000 characters\n", " return user_prompt" ] }, { "cell_type": "code", "execution_count": 10, "id": "d7748293-a616-41de-93cb-89f65cc5c73d", "metadata": {}, "outputs": [], "source": [ "# Let's create a call that streams back results\n", "# If you'd like a refresher on Generators (the \"yield\" keyword),\n", "# Please take a look at the Intermediate Python notebook in week1 folder.\n", "\n", "def stream_brochure(company_name, url, tone):\n", "\n", " system_message = f\"You are an assistant that analyzes the content of several relevant pages from a company website \\\n", " and creates a short brochure about the company for prospective customers, investors, and recruits. \\\n", " Include details of company culture, customers and careers/jobs if you have the information. \\\n", " Respond in markdown, and use a {tone.lower()} tone throughout the brochure.\"\n", "\n", " \n", " messages = [\n", " {\"role\": \"system\", \"content\": system_message},\n", " {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", " ]\n", " stream = openai.chat.completions.create(\n", " model=gpt_model,\n", " messages=messages,\n", " stream=True\n", " )\n", " result = \"\"\n", " for chunk in stream:\n", " result += chunk.choices[0].delta.content or \"\"\n", " yield result" ] }, { "cell_type": "code", "execution_count": 11, "id": "15222832-06e0-4452-a8e1-59b9b1755488", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "