|
|
|
@ -167,6 +167,58 @@
|
|
|
|
|
"\n", |
|
|
|
|
"Take the code from day1 and incorporate it here, to build a website summarizer that uses Llama 3.2 running locally instead of OpenAI" |
|
|
|
|
] |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
"cell_type": "code", |
|
|
|
|
"execution_count": null, |
|
|
|
|
"id": "47bc6a6e", |
|
|
|
|
"metadata": {}, |
|
|
|
|
"outputs": [], |
|
|
|
|
"source": [ |
|
|
|
|
"import ollama\n", |
|
|
|
|
"MODEL = \"llama3.2\"\n", |
|
|
|
|
"\n", |
|
|
|
|
"# A class to represent a Webpage\n", |
|
|
|
|
"# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\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)\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)\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.\"\n", |
|
|
|
|
"\n", |
|
|
|
|
"# A function that writes a User Prompt that asks for summaries of websites:\n", |
|
|
|
|
"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\n", |
|
|
|
|
"\n", |
|
|
|
|
"def messages_for(website):\n", |
|
|
|
|
" return [\n", |
|
|
|
|
" {\"role\": \"system\", \"content\": system_prompt},\n", |
|
|
|
|
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", |
|
|
|
|
" ]\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']" |
|
|
|
|
] |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
"metadata": { |
|
|
|
|