1 changed files with 246 additions and 0 deletions
@ -0,0 +1,246 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "raw", |
||||
"id": "309296ff-f0c2-4e31-986c-b6e779ad17cd", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"Take the code from day1 and incorporate it here, to build a website summarizer that uses Llama 3.2 running locally instead of OpenAI; use either of the above approaches." |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 1, |
||||
"id": "62b6082c-67d0-4ebc-97a2-8b7ee61e0294", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# imports\n", |
||||
"\n", |
||||
"import os\n", |
||||
"import requests\n", |
||||
"from dotenv import load_dotenv\n", |
||||
"from bs4 import BeautifulSoup\n", |
||||
"from IPython.display import Markdown, display\n", |
||||
"import ollama\n", |
||||
"\n", |
||||
"# If you get an error running this cell, then please head over to the troubleshooting notebook!" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 2, |
||||
"id": "1e048442-d95c-4017-b0ee-34966e8b5452", |
||||
"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": 3, |
||||
"id": "6c7d475d-b1bb-4d99-94af-2435593a9aff", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# A class to represent a Webpage\n", |
||||
"# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n", |
||||
"\n", |
||||
"# Some websites need you to use proper headers when fetching them:\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", |
||||
"}\n", |
||||
"\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, headers=headers)\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": 4, |
||||
"id": "7bedc3b2-01b6-41c2-b0a3-4f2d8f5772aa", |
||||
"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": 17, |
||||
"id": "23fcb3fc-af67-44f3-8f2a-475d1c7e7a69", |
||||
"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 += \"\\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" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 6, |
||||
"id": "b3f57f88-f5c9-42f8-8200-9a33c3ea8348", |
||||
"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": 7, |
||||
"id": "3922e700-1695-4dfb-8004-fe9356178056", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def message_payload(website):\n", |
||||
" url = Website(website)\n", |
||||
" payload = {\n", |
||||
" \"model\": MODEL,\n", |
||||
" \"messages\": messages_for(url),\n", |
||||
" \"stream\": False\n", |
||||
" }\n", |
||||
" return payload" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 8, |
||||
"id": "97e99c58-4a71-4c2c-9255-1b676446e37b", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# And now: call the OpenAI API. You will get very familiar with this!\n", |
||||
"\n", |
||||
"def summarize(url):\n", |
||||
" payload = message_payload(url)\n", |
||||
" response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n", |
||||
" return response.json()['message']['content']" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 9, |
||||
"id": "742d3b5b-c7bd-40c3-a75e-863d1e9f6caa", |
||||
"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": 18, |
||||
"id": "08332604-9de0-4cac-a9cd-7591ec10d66b", |
||||
"metadata": {}, |
||||
"outputs": [ |
||||
{ |
||||
"data": { |
||||
"text/markdown": [ |
||||
"**Resumen del sitio web**\n", |
||||
"\n", |
||||
"El sitio web Plataforma para fórex y las bolsas de valores: MetaTrader 5 es una plataforma comercial destinada a los traders de forex, acciones y otros activos financieros. La plataforma ofrece una variedad de herramientas y características, incluyendo gráficos, análisis técnico y fundamental, alertas y servicios comerciales.\n", |
||||
"\n", |
||||
"**Noticias**\n", |
||||
"\n", |
||||
"* MetaQuotes presentará sus últimas novedades en la iFX Expo de México del 9 al 10 de abril.\n", |
||||
"* El bróker global regulado ha ampliado significativamente la gama de herramientas disponibles para los tráders con MetaTrader 5 Automations.\n", |
||||
"* MetaTrader 5 ha sido premiada como la mejor plataforma comercial multimercado en la feria de Ciudad de México.\n", |
||||
"* La aplicación móvil MetaTrader 5 para iOS ha recibido una actualización con características nuevas, incluyendo objetos de texto para gráficos y temporizador de barras.\n", |
||||
"\n", |
||||
"**Características destacadas**\n", |
||||
"\n", |
||||
"* Trading móvil con aplicaciones para iPhone/iPad y Android\n", |
||||
"* Señales comerciales y copiado de operaciones\n", |
||||
"* Servicios comerciales y alertas\n", |
||||
"* Gráficos y análisis técnico y fundamental\n", |
||||
"* Entorno de desarrollo MQL5 y lenguaje de programación MQL5\n", |
||||
"* Virtual hosting (VPS) y trading automatizado\n", |
||||
"\n", |
||||
"**Servicios**\n", |
||||
"\n", |
||||
"* Descarga de la plataforma MetaTrader 5\n", |
||||
"* Comprar la plataforma MetaTrader 5\n", |
||||
"* Conexión a la plataforma web y tradere automatizado\n", |
||||
"* Acceso a la comunidad de traders y recursos educativos" |
||||
], |
||||
"text/plain": [ |
||||
"<IPython.core.display.Markdown object>" |
||||
] |
||||
}, |
||||
"metadata": {}, |
||||
"output_type": "display_data" |
||||
} |
||||
], |
||||
"source": [ |
||||
"display_summary(\"https://www.metatrader5.com/es\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "adb000a9-e2ba-48cc-bb87-1b75db2d9707", |
||||
"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.11" |
||||
} |
||||
}, |
||||
"nbformat": 4, |
||||
"nbformat_minor": 5 |
||||
} |
Loading…
Reference in new issue