189 lines
5.1 KiB
Plaintext
189 lines
5.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7317c777-7a59-4719-842f-b3018aa7e73f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# imports\n",
|
|
"\n",
|
|
"import requests\n",
|
|
"from bs4 import BeautifulSoup\n",
|
|
"from IPython.display import Markdown, display\n",
|
|
"import ollama"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "26b1489d-c043-4631-872b-e1e28fec9eed",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Constants\n",
|
|
"\n",
|
|
"MODEL = \"llama3.2\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a5630e12-40f5-40ea-996b-4b1a5d9c8697",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A class to represent a Webpage\n",
|
|
"\n",
|
|
"class Website:\n",
|
|
" \"\"\"\n",
|
|
" A utility class to represent a Website that we have scraped\n",
|
|
" \"\"\"\n",
|
|
" url: str\n",
|
|
" title: str\n",
|
|
" text: str\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)\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": "510e0447-ed82-4337-b0aa-f9752b41711a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
|
|
"\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.\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7a0926ae-8580-4f0a-8935-ce390b926074",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A function that writes a User Prompt that asks for summaries of websites:\n",
|
|
"\n",
|
|
"def user_prompt_for(website):\n",
|
|
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
|
|
" user_prompt += \"The 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": "963edaa9-daba-4fa1-8db6-518f22261ab0",
|
|
"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": "04c7a991-df38-4e73-8015-73684bdd7810",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# And now: call the Ollama function \n",
|
|
"\n",
|
|
"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": "b08efad7-7dbe-438e-898a-fc7ae7395149",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"summarize(\"https://www.allrecipes.com/recipes/14485/healthy-recipes/main-dishes/chicken/\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6ec180e8-4e2a-4e02-afc6-39a90a87bd7e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A function to display this nicely in the Jupyter output, using markdown\n",
|
|
"\n",
|
|
"def display_summary(url):\n",
|
|
" summary = summarize(url)\n",
|
|
" display(Markdown(summary))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "967b874a-af3a-494a-bb02-c83232d0f9a3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"display_summary(\"https://www.allrecipes.com/recipes/14485/healthy-recipes/main-dishes/chicken/\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1148b8d0-1e44-4ea1-ba1f-44eb25e0af18",
|
|
"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.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|