@@ -1,290 +0,0 @@
|
|||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 1,
|
|
||||||
"id": "135717e7",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
"from openai import OpenAI\n",
|
|
||||||
"import ollama"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 2,
|
|
||||||
"id": "29a9e634",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# OPTION 1\n",
|
|
||||||
"# using openai\n",
|
|
||||||
"\n",
|
|
||||||
"# message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n",
|
|
||||||
"# client = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"not-needed\")\n",
|
|
||||||
"# response = openai.chat.completions.create(model=`<name of model>`, messages=[{\"role\":\"user\", \"content\":message}])\n",
|
|
||||||
"# print(response.choices[0].message.content)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "306993ed",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# OPTION 2\n",
|
|
||||||
"# using Ollama\n",
|
|
||||||
"\n",
|
|
||||||
"message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n",
|
|
||||||
"model=\"llama3\"\n",
|
|
||||||
"response=ollama.chat(model=model,messages=[{\"role\":\"user\",\"content\":message}])\n",
|
|
||||||
"print(response[\"message\"][\"content\"])\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 4,
|
|
||||||
"id": "856f767b",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": 5,
|
|
||||||
"id": "4ce558dc",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# Let's try one out. Change the website and add print statements to follow along.\n",
|
|
||||||
"\n",
|
|
||||||
"ed = Website(\"https://edwarddonner.com\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 6,
|
|
||||||
"id": "5e3956f8",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": 7,
|
|
||||||
"id": "99d791b4",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": 8,
|
|
||||||
"id": "5d89b748",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": 9,
|
|
||||||
"id": "9a97d3e2",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# And now: call the OpenAI API. You will get very familiar with this!\n",
|
|
||||||
"\n",
|
|
||||||
"def summarize(url):\n",
|
|
||||||
" website = Website(url)\n",
|
|
||||||
" response=ollama.chat(model=model,messages=messages_for(website))\n",
|
|
||||||
" return(response[\"message\"][\"content\"])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "ec13fe0a",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"summarize(\"https://edwarddonner.com\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 11,
|
|
||||||
"id": "e3ade092",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"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": null,
|
|
||||||
"id": "be2d49e6",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"display_summary(\"https://edwarddonner.com\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "1ccbf33b",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"display_summary(\"https://cnn.com\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "ae3d0eae",
|
|
||||||
"metadata": {
|
|
||||||
"vscode": {
|
|
||||||
"languageId": "plaintext"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"display_summary(\"https://anthropic.com\")"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"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.13"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 5
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user