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.
 
 

97 lines
3.4 KiB

{
"cells": [
{
"cell_type": "markdown",
"id": "8b1b8557-7551-4b34-97f0-1734505078c7",
"metadata": {},
"source": [
"# Hello everyone.\n",
"Here's my solution to the Day 2 Exercise, where I use Ollama (locally hosted) instead of OpenAI gpt-4o-mini to summarize a given website. This code is all in the same block for ease of running (we are all familiar with the process by this point, but guiding comments have been made). Furtnermore, I added a bit of user interactivity by asking the user to provide the website themselves instead of hardcoding a string in memory that the developer changes everytime. Enjoy and have fun, fellow programmers! \n",
"\\- Batikan Iscan"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18cd56af-aacd-4f15-9c8f-e6e141671d10",
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"import ollama\n",
"\n",
"# Constants\n",
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
"MODEL = \"llama3.2\"\n",
"\n",
"# Code\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)\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",
"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 summarize(url):\n",
" website = Website(url)\n",
" messages = [\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]\n",
" response = ollama.chat(model=MODEL, messages=messages, stream=False)\n",
" # display(Markdown(response.choices[0].message.content))\n",
" display(Markdown(response.message.content))\n",
"\n",
"# User interaction\n",
"user_website = input(\"Enter a website: \")\n",
"summarize(user_website)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73991ad8-9c93-4c38-8daa-f93502b56740",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}