{ "cells": [ { "cell_type": "markdown", "id": "d15d8294-3328-4e07-ad16-8a03e9bbfdb9", "metadata": {}, "source": [ "# YOUR FIRST LAB\n", "### Please read this section. This is valuable to get you prepared, even if it's a long read -- it's important stuff.\n", "\n", "## Your first Frontier LLM Project\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "API key found and looks good so far!\n", "Hello! It’s great to hear from you! How can I assist you today?\n" ] }, { "data": { "text/markdown": [ "# Edward Donner Website Summary\n", "\n", "Edward Donner's website serves as a personal platform where he shares insights related to code, LLMs (Large Language Models), and his interests in music production and technology. \n", "\n", "## About Ed\n", "- Ed is the co-founder and CTO of Nebula.io, an AI-driven company focused on talent discovery and engagement.\n", "- He previously founded untapt, an AI startup that was acquired in 2021.\n", "\n", "## Features\n", "- **Connect Four**: A unique arena designed for LLMs to engage in simulated diplomacy and strategy.\n", "- **Outsmart**: Another interactive feature related to LLMs.\n", "\n", "## News and Announcements\n", "- **January 23, 2025**: Announcement of a workshop titled \"LLM Workshop – Hands-on with Agents\" providing resources for participants.\n", "- **December 21, 2024**: Welcoming message for \"SuperDataScientists.\"\n", "- **November 13, 2024**: Resources available for the topic \"Mastering AI and LLM Engineering.\"\n", "- **October 16, 2024**: Resources provided for transitioning from a software engineer to an AI data scientist.\n", "\n", "The website emphasizes Ed's passion for technology and AI, highlighting his professional achievements and ongoing projects." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#!/usr/bin/env python\n", "# coding: utf-8\n", "\n", "# # YOUR FIRST LAB\n", "# ## Your first Frontier LLM Project\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", "\n", "# # Connecting to OpenAI (or Ollama)\n", "# Load environment variables in a file called .env\n", "load_dotenv(override=True)\n", "api_key = os.getenv('OPENAI_API_KEY')\n", "\n", "# Check the key\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!\")\n", "\n", "# Open API connection\n", "openai = OpenAI()\n", "\n", "# To give you a preview -- calling OpenAI with these messages is this easy: openai.chat.completions.create(model, messages)\n", "message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", "response = openai.chat.completions.create(\n", " model=\"gpt-4o-mini\", \n", " messages=[\n", " {\"role\":\"user\", \"content\":message}\n", " ]\n", ")\n", "print(response.choices[0].message.content)\n", "\n", "# A class to represent a Webpage\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)\n", "\n", "# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\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.\"\n", "\n", "user_prompt_content = \"\\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", "\n", "# A function that writes a User Prompt that asks for summaries of websites:\n", "def user_prompt_for(website):\n", " user_prompt = f\"You are looking at a website titled {website.title}\"\n", " user_prompt += user_prompt_content\n", " user_prompt += website.text\n", " return user_prompt\n", "\n", "# See how this function creates exactly the format above\n", "def messages_for(website):\n", " return [\n", " {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", " ]\n", "\n", "# Try this out, and then try for a few more websites\n", "# messages_for(ed)\n", "\n", "# And now: call the OpenAI API. You will get very familiar with this!\n", "def summarize(url):\n", " website = Website(url)\n", " response = openai.chat.completions.create(\n", " model = \"gpt-4o-mini\",\n", " messages = messages_for(website)\n", " )\n", " return response.choices[0].message.content\n", "\n", "# A function to display this nicely in the Jupyter output, using markdown\n", "def display_summary(url):\n", " summary = summarize(url)\n", " display(Markdown(summary))\n", "\n", "website_url = \"https://edwarddonner.com\"\n", "\n", "# Let's try one out. Change the website and add print statements to follow along.\n", "\n", "ed = Website(website_url)\n", "display_summary(website_url)" ] }, { "cell_type": "code", "execution_count": null, "id": "b2714e98-8f45-443e-99f8-918e2d61ae36", "metadata": {}, "outputs": [], "source": [] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }