{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d5ce5658-9ba9-487c-bb31-f9030039990e", "metadata": {}, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "id": "eec2b3ad-7b6f-4fe1-9e38-9ddbacf6adc6", "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "api_key=os.getenv(\"OPENAI_API_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\")\n", "else:\n", " print(\"API key found and looks good so far!\") " ] }, { "cell_type": "code", "execution_count": null, "id": "fbb17244-b2b6-4494-969a-e0192115ef96", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import requests\n", "from bs4 import BeautifulSoup\n", "from IPython.display import Markdown, display" ] }, { "cell_type": "code", "execution_count": null, "id": "642d7ffb-94a9-40c5-bf1f-9bc4570efb58", "metadata": {}, "outputs": [], "source": [ "# Constants\n", "\n", "OLLAMA_API = \"http://localhost:11434/api/chat\"\n", "HEADERS = {\"Content-Type\": \"application/json\"}\n", "MODEL = \"llama3.2\"" ] }, { "cell_type": "code", "execution_count": null, "id": "c90b4384-1a4b-4cad-8d3d-331c46d4c7f1", "metadata": {}, "outputs": [], "source": [ "# Create a messages list using the same format that we used for OpenAI\n", "\n", "messages = [\n", " {\"role\": \"user\", \"content\": \"Describelangflow\"}\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "9c604ab2-6c25-4c2f-a6a2-60540ff74ca0", "metadata": {}, "outputs": [], "source": [ "payload = {\n", " \"model\": MODEL,\n", " \"messages\": messages,\n", " \"stream\": False\n", " }" ] }, { "cell_type": "code", "execution_count": null, "id": "5629b49f-6ca9-466f-ad60-4d40a3b15321", "metadata": {}, "outputs": [], "source": [ "response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n", "print(response.json()['message']['content'])" ] }, { "cell_type": "code", "execution_count": null, "id": "5a88d730-1c7b-4e1e-b143-5a630a2f52bd", "metadata": {}, "outputs": [], "source": [ "import ollama\n", "\n", "response = ollama.chat(model=MODEL, messages=messages)\n", "print(response['message']['content'])" ] }, { "cell_type": "code", "execution_count": null, "id": "62155b78-de4a-435b-867b-35b6c4b21f48", "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": "cd93d7b6-916a-45b8-9dfe-822a15c9a501", "metadata": {}, "outputs": [], "source": [ "ed = Website(\"https://edwarddonner.com\")\n", "print(ed.title)\n", "print(ed.text)" ] }, { "cell_type": "code", "execution_count": null, "id": "eea40735-c580-4d25-9317-c951e53faceb", "metadata": {}, "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(\n", " model = \"llama3.2\",\n", " messages = messages_for(website)\n", " )\n", " return response['message']['content']" ] }, { "cell_type": "code", "execution_count": null, "id": "cf844513-2b74-4306-9328-573d4996b772", "metadata": {}, "outputs": [], "source": [ "summarize(\"https://edwarddonner.com\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f5c35dfb-d38d-4c59-85ed-e7551175268b", "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": "ca1fa5e7-44ce-45ae-a2ff-c38bc239f101", "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": "52b5a899-3f96-4e45-849b-bba4a3f848de", "metadata": {}, "outputs": [], "source": [ "print(user_prompt_for(ed))" ] }, { "cell_type": "code", "execution_count": null, "id": "f11f9d7d-8eb5-410b-b27c-a3e086d5bf59", "metadata": {}, "outputs": [], "source": [ "messages = [\n", " {\"role\": \"system\", \"content\": \"You are a snarky assistant\"},\n", " {\"role\": \"user\", \"content\": \"What is 2 + 2?\"}\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "56c19a9c-4c69-4b6d-b9ac-c3b884efb8b1", "metadata": {}, "outputs": [], "source": [ " response = ollama.chat(\n", " model = \"llama3.2\",\n", " messages = messages\n", " )\n", " print(response['message']['content'])" ] }, { "cell_type": "code", "execution_count": null, "id": "0c05859c-4978-4b32-b971-0ea5d0147f00", "metadata": {}, "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": null, "id": "a33dd65c-01f9-4a51-bc44-74427eafef42", "metadata": {}, "outputs": [], "source": [ "messages_for(ed)" ] }, { "cell_type": "code", "execution_count": null, "id": "a5593d92-2f3c-44c8-abdd-9089f04ce171", "metadata": {}, "outputs": [], "source": [ "def summarize(url):\n", " website = Website(url)\n", " response = ollama.chat(\n", " model = \"llama3.2\",\n", " messages = messages_for(website)\n", " )\n", " return response['message']['content']" ] }, { "cell_type": "code", "execution_count": null, "id": "6f75cc47-c03e-42fd-acdf-e980031c9976", "metadata": {}, "outputs": [], "source": [ "summarize(\"https://edwarddonner.com\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2d156ede-c20f-4569-997f-cd61e7b9c667", "metadata": {}, "outputs": [], "source": [ "def display_summary(url):\n", " summary = summarize(url)\n", " display(Markdown(summary))" ] }, { "cell_type": "code", "execution_count": null, "id": "81f3d230-ed53-400f-b215-2ef9f6a92935", "metadata": {}, "outputs": [], "source": [ "display_summary(\"https://edwarddonner.com\")" ] }, { "cell_type": "code", "execution_count": null, "id": "e17cd862-44e5-4af9-9dcc-7ee3d489a45d", "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 }