{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "3a601886-67ac-49a2-95f6-c5b69d372a1c", "metadata": {}, "outputs": [], "source": [ "# Constants\n", "HEADERS = {\"Content-Type\": \"application/json\"}\n", "MODEL = \"llama3.2\"" ] }, { "cell_type": "code", "execution_count": null, "id": "3b23d779-ae31-4b66-a4cd-8f6e06079a21", "metadata": {}, "outputs": [], "source": [ "# Imports\n", "import ollama\n", "import os\n", "import requests\n", "from openai import OpenAI\n", "from dotenv import load_dotenv\n", "from bs4 import BeautifulSoup\n", "from IPython.display import Markdown, display\n", "from openai import OpenAI" ] }, { "cell_type": "code", "execution_count": null, "id": "c727418f-39b5-4743-99be-09f2da7896d5", "metadata": {}, "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": null, "id": "6ec51fb5-6e98-479a-95f6-00618bc548d1", "metadata": {}, "outputs": [], "source": [ "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": null, "id": "bb3cd0c6-118b-4795-be3b-4b5d945305a2", "metadata": {}, "outputs": [], "source": [ "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": null, "id": "ee8118f0-6aca-40ee-9b80-df170465ab54", "metadata": {}, "outputs": [], "source": [ "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": null, "id": "acefe92a-8c9b-46f6-a6df-1e7c7f3b9d26", "metadata": {}, "outputs": [], "source": [ "def summarize(url):\n", " website = Website(url)\n", " messages = messages_for(website)\n", " response = ollama.chat(model=MODEL, messages=messages)\n", " return response['message']['content']" ] }, { "cell_type": "code", "execution_count": null, "id": "2a96ba84-6da4-49f1-ac50-79e8fa78046f", "metadata": {}, "outputs": [], "source": [ "def display_summary(url):\n", " summary = summarize(url)\n", " display(Markdown(summary))" ] }, { "cell_type": "code", "execution_count": null, "id": "3459b7a6-6f68-41e5-9bd4-4ac44807c636", "metadata": {}, "outputs": [], "source": [ "display_summary(\"https://www.mendozacode.io\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f3c85932-87a5-4acc-8161-1cc7129a660e", "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.11" } }, "nbformat": 4, "nbformat_minor": 5 }