From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.4 KiB
119 lines
3.4 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "7bf0a6c9-ce7e-4610-90ae-75ae08d26cbf", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import ollama\n", |
|
"import requests\n", |
|
"from bs4 import BeautifulSoup\n", |
|
"from IPython.display import Markdown, display\n", |
|
"\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", |
|
"}" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "d414ed23-ed29-4600-a2d6-28b48c279221", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"class Website:\n", |
|
"\n", |
|
" def __init__(self, url):\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 \"Website without title\"\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": "be9f3409-b4c8-428f-96b8-3720a8c3ad47", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def prompt_for(website):\n", |
|
" prompt = f\"Here is a website for you to summarize. Its title is {website.title}.\"\n", |
|
" prompt += \"\\nPlease provide a short summary of the website. If it contains any announcements, summarize those, too.\"\n", |
|
" prompt += f\"\\nHere is the Website:\\n\\n{website.text}\"\n", |
|
"\n", |
|
" return prompt" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "2565ae0f-c5c5-44bc-98c2-f777f816b37f", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"MODEL = \"llama3.2\"\n", |
|
"website = Website(\"https://edwarddonner.com\")\n", |
|
"\n", |
|
"def messages_for(website):\n", |
|
" messages = [\n", |
|
" {\"role\": \"system\", \"content\": \"You are a powerful, friendly, and helpful website summarization assistant. \\\n", |
|
" You are given a website and summarize its content succinctly. You format your answer in markdown.\"},\n", |
|
" {\"role\": \"user\", \"content\": prompt_for(website)}\n", |
|
" ]\n", |
|
"\n", |
|
" return messages" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "cdca9dc0-4ecc-494f-abee-d1ad2d373e0e", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def display_md_summary(website):\n", |
|
" response = ollama.chat(model=MODEL, messages=messages_for(website))\n", |
|
" display(Markdown(response['message']['content']))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "e519091c-8fc6-442b-a99b-ac393a10cdcd", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"display_md_summary(website)" |
|
] |
|
} |
|
], |
|
"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 |
|
}
|
|
|