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.
 
 

335 lines
17 KiB

{
"cells": [
{
"cell_type": "markdown",
"id": "d15d8294-3328-4e07-ad16-8a03e9bbfdb9",
"metadata": {},
"source": [
"# Welcome to your first assignment!\n",
"\n",
"Instructions are below. Please give this a try, and look in the solutions folder if you get stuck (or feel free to ask me!)"
]
},
{
"cell_type": "markdown",
"id": "ada885d9-4d42-4d9b-97f0-74fbbbfe93a9",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../resources.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#f71;\">Just before we get to the assignment --</h2>\n",
" <span style=\"color:#f71;\">I thought I'd take a second to point you at this page of useful resources for the course. This includes links to all the slides.<br/>\n",
" <a href=\"https://edwarddonner.com/2024/11/13/llm-engineering-resources/\">https://edwarddonner.com/2024/11/13/llm-engineering-resources/</a><br/>\n",
" Please keep this bookmarked, and I'll continue to add more useful links there over time.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"id": "6e9fa1fc-eac5-4d1d-9be4-541b3f2b3458",
"metadata": {},
"source": [
"# HOMEWORK EXERCISE ASSIGNMENT\n",
"\n",
"Upgrade the day 1 project to summarize a webpage to use an Open Source model running locally via Ollama rather than OpenAI\n",
"\n",
"You'll be able to use this technique for all subsequent projects if you'd prefer not to use paid APIs.\n",
"\n",
"**Benefits:**\n",
"1. No API charges - open-source\n",
"2. Data doesn't leave your box\n",
"\n",
"**Disadvantages:**\n",
"1. Significantly less power than Frontier Model\n",
"\n",
"## Recap on installation of Ollama\n",
"\n",
"Simply visit [ollama.com](https://ollama.com) and install!\n",
"\n",
"Once complete, the ollama server should already be running locally. \n",
"If you visit: \n",
"[http://localhost:11434/](http://localhost:11434/)\n",
"\n",
"You should see the message `Ollama is running`. \n",
"\n",
"If not, bring up a new Terminal (Mac) or Powershell (Windows) and enter `ollama serve` \n",
"And in another Terminal (Mac) or Powershell (Windows), enter `ollama pull llama3.2` \n",
"Then try [http://localhost:11434/](http://localhost:11434/) again.\n",
"\n",
"If Ollama is slow on your machine, try using `llama3.2:1b` as an alternative. Run `ollama pull llama3.2:1b` from a Terminal or Powershell, and change the code below from `MODEL = \"llama3.2\"` to `MODEL = \"llama3.2:1b\"`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724",
"metadata": {},
"outputs": [],
"source": [
"# Constants\n",
"\n",
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
"MODEL = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dac0a679-599c-441f-9bf2-ddc73d35b940",
"metadata": {},
"outputs": [],
"source": [
"# Create a messages list using the same format that we used for OpenAI\n",
"\n",
"messages = [\n",
" {\"role\": \"user\", \"content\": \"Describe some of the business applications of Generative AI\"}\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7bb9c624-14f0-4945-a719-8ddb64f66f47",
"metadata": {},
"outputs": [],
"source": [
"payload = {\n",
" \"model\": MODEL,\n",
" \"messages\": messages,\n",
" \"stream\": False\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "42b9f644-522d-4e05-a691-56e7658c0ea9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generative AI has numerous business applications across various industries. Here are some examples:\n",
"\n",
"1. **Content Generation**: Generative AI can be used to generate high-quality content such as blog posts, social media posts, product descriptions, and more. This can help reduce the time and cost associated with content creation.\n",
"2. **Marketing Automation**: Generative AI can be used to create personalized marketing messages, emails, and ads that are tailored to individual customers' preferences and behaviors.\n",
"3. **Product Design**: Generative AI can be used to design new products, such as furniture, fashion items, or even entire buildings. This can help reduce the time and cost associated with product development.\n",
"4. **Image and Video Generation**: Generative AI can be used to create realistic images and videos that can be used for advertising, marketing, or entertainment purposes.\n",
"5. **Chatbots and Virtual Assistants**: Generative AI can be used to create more sophisticated chatbots and virtual assistants that can understand natural language and provide personalized responses.\n",
"6. **Data Analysis and Visualization**: Generative AI can be used to analyze large datasets and generate visualizations that help businesses make data-driven decisions.\n",
"7. **Predictive Maintenance**: Generative AI can be used to predict when equipment or machinery is likely to fail, allowing businesses to schedule maintenance and reduce downtime.\n",
"8. **Supply Chain Optimization**: Generative AI can be used to optimize supply chain logistics, including predicting demand, managing inventory, and identifying the most efficient routes for delivery.\n",
"9. **Financial Modeling**: Generative AI can be used to create complex financial models that help businesses forecast revenue, predict costs, and make informed investment decisions.\n",
"10. **Customer Service**: Generative AI can be used to provide 24/7 customer support, helping businesses to improve customer satisfaction and reduce the number of complaints.\n",
"\n",
"Some specific examples of companies using Generative AI include:\n",
"\n",
"* **Netflix**: Uses Generative AI to create personalized movie and TV show recommendations.\n",
"* **Microsoft**: Uses Generative AI to generate realistic images and videos for advertising and marketing purposes.\n",
"* **Dyson**: Uses Generative AI to design new products, such as vacuum cleaners and air purifiers.\n",
"* **Amazon**: Uses Generative AI to create personalized product recommendations and improve customer service.\n",
"\n",
"Overall, Generative AI has the potential to transform many business applications across various industries, and its use cases are expected to continue growing in the coming years.\n"
]
}
],
"source": [
"response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n",
"print(response.json()['message']['content'])"
]
},
{
"cell_type": "markdown",
"id": "6a021f13-d6a1-4b96-8e18-4eae49d876fe",
"metadata": {},
"source": [
"# Introducing the ollama package\n",
"\n",
"And now we'll do the same thing, but using the elegant ollama python package instead of a direct HTTP call.\n",
"\n",
"Under the hood, it's making the same call as above to the ollama server running at localhost:11434"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7745b9c4-57dc-4867-9180-61fa5db55eb8",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Google.com is a multinational technology company that provides a wide range of services and products, but its core focus is on search engine optimization (SEO). Here's a summary:\n",
"\n",
"**Main Services:**\n",
"\n",
"1. **Search Engine**: Google's most popular service allows users to search for information on the internet using keywords.\n",
"2. **Advertising**: Google's advertising platform enables businesses to create targeted ads that appear alongside search results and on partner websites.\n",
"3. **Cloud Computing**: Google Cloud offers a suite of cloud-based services, including storage, computing power, and machine learning algorithms.\n",
"\n",
"**Key Features:**\n",
"\n",
"1. **Algorithms**: Google's proprietary search algorithms aim to provide users with the most relevant and accurate search results.\n",
"2. **Google Maps**: A mapping service that provides directions, street views, and local business listings.\n",
"3. **YouTube**: A video-sharing platform acquired by Google in 2006.\n",
"4. **Gmail**: A free email service with advanced features like spam filtering and integration with other Google services.\n",
"\n",
"**Innovation and Features:**\n",
"\n",
"1. **Artificial Intelligence (AI)**: Google has developed various AI-powered tools, such as Google Assistant and Google Lens.\n",
"2. **Machine Learning**: Google's machine learning capabilities are used to improve search results, advertising, and other products.\n",
"3. **Google Drive**: A cloud storage service that allows users to store and access files from anywhere.\n",
"\n",
"**Other Ventures:**\n",
"\n",
"1. **Hardware**: Google develops its own hardware products, such as Pixel smartphones, Chromebooks, and Chrome OS-based devices.\n",
"2. **Artificial Intelligence Research**: Google invests heavily in AI research, with the goal of developing advanced technologies like self-driving cars and language processing.\n",
"\n",
"Overall, Google.com is a multifaceted platform that offers a wide range of services and products, from search engines to cloud computing and advertising platforms."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ollama\n",
"\n",
"messages = [\n",
" {\"role\": \"user\", \"content\": \"Summarize the website google.com\"}\n",
"]\n",
"response = ollama.chat(model=MODEL, messages=messages)\n",
"display(Markdown(response['message']['content']))"
]
},
{
"cell_type": "markdown",
"id": "1622d9bb-5c68-4d4e-9ca4-b492c751f898",
"metadata": {},
"source": [
"# NOW the exercise for you\n",
"\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": 1,
"id": "09ffd008-0dc5-47a2-bcbe-c9defe412b17",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a website: https://google.com\n"
]
},
{
"ename": "NameError",
"evalue": "name 'requests' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 34\u001b[0m\n\u001b[0;32m 30\u001b[0m display(Markdown(response\u001b[38;5;241m.\u001b[39mmessage\u001b[38;5;241m.\u001b[39mcontent))\n\u001b[0;32m 33\u001b[0m user_website \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a website: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 34\u001b[0m \u001b[43msummarize\u001b[49m\u001b[43m(\u001b[49m\u001b[43muser_website\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[1;32mIn[1], line 24\u001b[0m, in \u001b[0;36msummarize\u001b[1;34m(url)\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msummarize\u001b[39m(url):\n\u001b[1;32m---> 24\u001b[0m website \u001b[38;5;241m=\u001b[39m \u001b[43mWebsite\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 25\u001b[0m messages \u001b[38;5;241m=\u001b[39m [\n\u001b[0;32m 26\u001b[0m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124muser\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: user_prompt_for(website)}\n\u001b[0;32m 27\u001b[0m ]\n\u001b[0;32m 28\u001b[0m response \u001b[38;5;241m=\u001b[39m ollama\u001b[38;5;241m.\u001b[39mchat(model\u001b[38;5;241m=\u001b[39mMODEL, messages\u001b[38;5;241m=\u001b[39mmessages, stream\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
"Cell \u001b[1;32mIn[1], line 8\u001b[0m, in \u001b[0;36mWebsite.__init__\u001b[1;34m(self, url)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03mCreate this Website object from the given url using the BeautifulSoup library\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39murl \u001b[38;5;241m=\u001b[39m url\n\u001b[1;32m----> 8\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mrequests\u001b[49m\u001b[38;5;241m.\u001b[39mget(url)\n\u001b[0;32m 9\u001b[0m soup \u001b[38;5;241m=\u001b[39m BeautifulSoup(response\u001b[38;5;241m.\u001b[39mcontent, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhtml.parser\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtitle \u001b[38;5;241m=\u001b[39m soup\u001b[38;5;241m.\u001b[39mtitle\u001b[38;5;241m.\u001b[39mstring \u001b[38;5;28;01mif\u001b[39;00m soup\u001b[38;5;241m.\u001b[39mtitle \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo title found\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[1;31mNameError\u001b[0m: name 'requests' is not defined"
]
}
],
"source": [
"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",
"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",
"\n",
"user_website = input(\"Enter a website: \")\n",
"summarize(user_website)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6fe0bf1b-484e-482b-b844-8c23e232ddf8",
"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
}