diff --git a/community-contributions/day2ollamatest.ipynb b/community-contributions/day2ollamatest.ipynb new file mode 100644 index 0000000..f50b35d --- /dev/null +++ b/community-contributions/day2ollamatest.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4d02ac4b-9cab-42bb-b8a3-123d53913471", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import requests\n", + "from bs4 import BeautifulSoup\n", + "from IPython.display import Markdown, display\n", + "import ollama\n", + "\n", + "MODEL = \"llama3.2\"\n", + "\n", + "# Optional headers to avoid request blocks\n", + "HEADERS = {\n", + " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\"\n", + "}\n", + "\n", + "\n", + "class Website:\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", + " 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", + "\n", + "\n", + "system_prompt = \"\"\"You are an assistant that analyzes the contents of a website \n", + "and provides a short summary, ignoring navigation text. Respond in markdown.\"\"\"\n", + "\n", + "\n", + "def user_prompt_for(website):\n", + " return f\"\"\"You are looking at a website titled {website.title}.\n", + "The contents of this website are as follows. Please provide a short summary in markdown. \n", + "If it includes news or announcements, summarize these too.\n", + "\n", + "{website.text}\n", + "\"\"\"\n", + "\n", + "\n", + "def summarize(url):\n", + " website = Website(url)\n", + " response = ollama.chat(\n", + " model=MODEL,\n", + " messages=[\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", + " ]\n", + " )\n", + " return response['message']['content']\n", + "\n", + "\n", + "def display_summary(url):\n", + " summary = summarize(url)\n", + " display(Markdown(summary))\n", + "\n", + "\n", + "# Example usage\n", + "display_summary(\"https://edwarddonner.com\")\n" + ] + } + ], + "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 +}