From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
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.
879 lines
37 KiB
879 lines
37 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "markdown", |
|
"id": "a98030af-fcd1-4d63-a36e-38ba053498fa", |
|
"metadata": {}, |
|
"source": [ |
|
"# A full business solution\n", |
|
"\n", |
|
"## Now we will take our project from Day 1 to the next level\n", |
|
"\n", |
|
"### BUSINESS CHALLENGE:\n", |
|
"\n", |
|
"Create a product that builds a Brochure for a company to be used for prospective clients, investors and potential recruits.\n", |
|
"\n", |
|
"We will be provided a company name and their primary website.\n", |
|
"\n", |
|
"See the end of this notebook for examples of real-world business applications.\n", |
|
"\n", |
|
"And remember: I'm always available if you have problems or ideas! Please do reach out." |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 1, |
|
"id": "d5b08506-dc8b-4443-9201-5f1848161363", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# imports\n", |
|
"# If these fail, please check you're running from an 'activated' environment with (llms) in the command prompt\n", |
|
"\n", |
|
"import os\n", |
|
"import requests\n", |
|
"import json\n", |
|
"from typing import List\n", |
|
"from dotenv import load_dotenv\n", |
|
"from bs4 import BeautifulSoup\n", |
|
"from IPython.display import Markdown, display, update_display\n", |
|
"from openai import OpenAI" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 2, |
|
"id": "fc5d8880-f2ee-4c06-af16-ecbc0262af61", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"API key looks good so far\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"# Initialize and constants\n", |
|
"\n", |
|
"load_dotenv(override=True)\n", |
|
"api_key = os.getenv('OPENAI_API_KEY')\n", |
|
"\n", |
|
"if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n", |
|
" print(\"API key looks good so far\")\n", |
|
"else:\n", |
|
" print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n", |
|
" \n", |
|
"MODEL = 'gpt-4o-mini'\n", |
|
"openai = OpenAI()" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 3, |
|
"id": "106dd65e-90af-4ca8-86b6-23a41840645b", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# A class to represent a Webpage\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", |
|
" A utility class to represent a Website that we have scraped, now with links\n", |
|
" \"\"\"\n", |
|
"\n", |
|
" def __init__(self, url):\n", |
|
" self.url = url\n", |
|
" response = requests.get(url, headers=headers)\n", |
|
" self.body = response.content\n", |
|
" soup = BeautifulSoup(self.body, 'html.parser')\n", |
|
" self.title = soup.title.string if soup.title else \"No title found\"\n", |
|
" if soup.body:\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", |
|
" else:\n", |
|
" self.text = \"\"\n", |
|
" links = [link.get('href') for link in soup.find_all('a')]\n", |
|
" self.links = [link for link in links if link]\n", |
|
"\n", |
|
" def get_contents(self):\n", |
|
" return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\"" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 4, |
|
"id": "e30d8128-933b-44cc-81c8-ab4c9d86589a", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"['#introduction',\n", |
|
" '#imam-budair',\n", |
|
" '#imam-juhany',\n", |
|
" '#imam-ghamidi',\n", |
|
" '#isdp',\n", |
|
" '#altasreef',\n", |
|
" '#easyquran',\n", |
|
" '#mohaddis',\n", |
|
" '#tanzeem',\n", |
|
" '#ai-nlp-expertise',\n", |
|
" '#The-Noble-Quran',\n", |
|
" '#Tazkeer-ul-Quran',\n", |
|
" '#seerat-quiz',\n", |
|
" '#tuheed',\n", |
|
" '#publications',\n", |
|
" '#brainiac',\n", |
|
" '#nascon',\n", |
|
" '#contact',\n", |
|
" '#introduction',\n", |
|
" '#contact',\n", |
|
" 'https://jamiaashrafia.org/',\n", |
|
" 'https://ili.digital/',\n", |
|
" 'https://www.easyquranwahadees.com/',\n", |
|
" 'https://tanzeemdigitallibrary.com/',\n", |
|
" 'https://altasreef.vercel.app',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.asantarjuma',\n", |
|
" 'https://play.google.com/store/apps/details?id=tazkeerul.quran',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.anfzquizapp',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.tuheed',\n", |
|
" 'https://isdp.info',\n", |
|
" 'https://isdp.info/students-in-field.php',\n", |
|
" 'https://altasreef.vercel.app',\n", |
|
" 'https://altasreef.vercel.app/exam',\n", |
|
" 'https://aclanthology.org/2025.abjadnlp-1.14.pdf',\n", |
|
" 'https://www.easyquranwahadees.com/',\n", |
|
" 'https://mohaddis.com/',\n", |
|
" 'https://tanzeemdigitallibrary.com/',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran',\n", |
|
" 'https://play.google.com/store/apps/details?id=tazkeerul.quran',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.anfzquizapp',\n", |
|
" 'https://play.google.com/store/apps/details?id=com.tuheed',\n", |
|
" 'https://aclanthology.org/2025.abjadnlp-1.14.pdf',\n", |
|
" 'https://link.springer.com/article/10.1007/s10462-022-10313-2',\n", |
|
" 'https://aclanthology.org/2022.osact-1.18/',\n", |
|
" 'https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35',\n", |
|
" 'https://ejournal.um.edu.my/index.php/quranica/article/view/12108',\n", |
|
" 'https://ieeexplore.ieee.org/document/7277250',\n", |
|
" 'https://scholar.google.com/citations?user=AulVnWcAAAAJ&hl=en',\n", |
|
" 'https://altasreef.vercel.app',\n", |
|
" 'mailto:haq@haqnawaz.org',\n", |
|
" 'https://github.com/haqnawaz99',\n", |
|
" 'https://linkedin.com/in/haqnawaz99']" |
|
] |
|
}, |
|
"execution_count": 4, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"ed = Website(\"https://haqnawaz.org\")\n", |
|
"ed.links" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "1771af9c-717a-4fca-bbbe-8a95893312c3", |
|
"metadata": {}, |
|
"source": [ |
|
"## First step: Have GPT-4o-mini figure out which links are relevant\n", |
|
"\n", |
|
"### Use a call to gpt-4o-mini to read the links on a webpage, and respond in structured JSON. \n", |
|
"It should decide which links are relevant, and replace relative links such as \"/about\" with \"https://company.com/about\". \n", |
|
"We will use \"one shot prompting\" in which we provide an example of how it should respond in the prompt.\n", |
|
"\n", |
|
"This is an excellent use case for an LLM, because it requires nuanced understanding. Imagine trying to code this without LLMs by parsing and analyzing the webpage - it would be very hard!\n", |
|
"\n", |
|
"Sidenote: there is a more advanced technique called \"Structured Outputs\" in which we require the model to respond according to a spec. We cover this technique in Week 8 during our autonomous Agentic AI project." |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 5, |
|
"id": "6957b079-0d96-45f7-a26a-3487510e9b35", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"link_system_prompt = \"You are provided with a list of links found on a webpage. \\\n", |
|
"You are able to decide which of the links would be most relevant to include in a brochure about the company, \\\n", |
|
"such as links to an About page, or a Company page, or Careers/Jobs pages.\\n\"\n", |
|
"link_system_prompt += \"You should respond in JSON as in this example:\"\n", |
|
"link_system_prompt += \"\"\"\n", |
|
"{\n", |
|
" \"links\": [\n", |
|
" {\"type\": \"about page\", \"url\": \"https://full.url/goes/here/about\"},\n", |
|
" {\"type\": \"careers page\": \"url\": \"https://another.full.url/careers\"}\n", |
|
" ]\n", |
|
"}\n", |
|
"\"\"\"" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 6, |
|
"id": "a7f284d7", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"link_system_prompt = (\n", |
|
" \"You are provided with a list of links found on a personal portfolio website. \"\n", |
|
" \"Your task is to identify the links most relevant for inclusion in a personal profile brochure, \"\n", |
|
" \"such as links to About, Projects, AI & NLP, Mobile Apps, Publications, Awards, or Contact sections.\\n\"\n", |
|
" \"Respond in JSON format as shown below:\\n\"\n", |
|
" \"{\\n\"\n", |
|
" ' \"links\": [\\n'\n", |
|
" ' {\"type\": \"about section\", \"url\": \"https://haqnawaz.org#about-me\"},\\n'\n", |
|
" ' {\"type\": \"projects section\", \"url\": \"https://haqnawaz.org#projects\"},\\n'\n", |
|
" ' {\"type\": \"AI & NLP section\", \"url\": \"https://haqnawaz.org#ai-nlp\"},\\n'\n", |
|
" ' {\"type\": \"mobile apps section\", \"url\": \"https://haqnawaz.org#mobile-apps\"},\\n'\n", |
|
" ' {\"type\": \"publications section\", \"url\": \"https://haqnawaz.org#publications\"},\\n'\n", |
|
" ' {\"type\": \"awards section\", \"url\": \"https://haqnawaz.org#awards\"},\\n'\n", |
|
" ' {\"type\": \"contact section\", \"url\": \"https://haqnawaz.org#contact\"}\\n'\n", |
|
" ' ]\\n'\n", |
|
" \"}\"\n", |
|
")\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 7, |
|
"id": "b97e4068-97ed-4120-beae-c42105e4d59a", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"You are provided with a list of links found on a personal portfolio website. Your task is to identify the links most relevant for inclusion in a personal profile brochure, such as links to About, Projects, AI & NLP, Mobile Apps, Publications, Awards, or Contact sections.\n", |
|
"Respond in JSON format as shown below:\n", |
|
"{\n", |
|
" \"links\": [\n", |
|
" {\"type\": \"about section\", \"url\": \"https://haqnawaz.org#about-me\"},\n", |
|
" {\"type\": \"projects section\", \"url\": \"https://haqnawaz.org#projects\"},\n", |
|
" {\"type\": \"AI & NLP section\", \"url\": \"https://haqnawaz.org#ai-nlp\"},\n", |
|
" {\"type\": \"mobile apps section\", \"url\": \"https://haqnawaz.org#mobile-apps\"},\n", |
|
" {\"type\": \"publications section\", \"url\": \"https://haqnawaz.org#publications\"},\n", |
|
" {\"type\": \"awards section\", \"url\": \"https://haqnawaz.org#awards\"},\n", |
|
" {\"type\": \"contact section\", \"url\": \"https://haqnawaz.org#contact\"}\n", |
|
" ]\n", |
|
"}\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"print(link_system_prompt)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 8, |
|
"id": "8e1f601b-2eaf-499d-b6b8-c99050c9d6b3", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def get_links_user_prompt(website):\n", |
|
" user_prompt = f\"Here is the list of links on the website of {website.url} - \"\n", |
|
" user_prompt += \"please decide which of these are relevant web links for a brochure about the company, respond with the full https URL in JSON format. \\\n", |
|
"Do not include Terms of Service, Privacy, email links.\\n\"\n", |
|
" user_prompt += \"Links (some might be relative links):\\n\"\n", |
|
" user_prompt += \"\\n\".join(website.links)\n", |
|
" return user_prompt" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 9, |
|
"id": "6bcbfa78-6395-4685-b92c-22d592050fd7", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Here is the list of links on the website of https://haqnawaz.org - please decide which of these are relevant web links for a brochure about the company, respond with the full https URL in JSON format. Do not include Terms of Service, Privacy, email links.\n", |
|
"Links (some might be relative links):\n", |
|
"#introduction\n", |
|
"#imam-budair\n", |
|
"#imam-juhany\n", |
|
"#imam-ghamidi\n", |
|
"#isdp\n", |
|
"#altasreef\n", |
|
"#easyquran\n", |
|
"#mohaddis\n", |
|
"#tanzeem\n", |
|
"#ai-nlp-expertise\n", |
|
"#The-Noble-Quran\n", |
|
"#Tazkeer-ul-Quran\n", |
|
"#seerat-quiz\n", |
|
"#tuheed\n", |
|
"#publications\n", |
|
"#brainiac\n", |
|
"#nascon\n", |
|
"#contact\n", |
|
"#introduction\n", |
|
"#contact\n", |
|
"https://jamiaashrafia.org/\n", |
|
"https://ili.digital/\n", |
|
"https://www.easyquranwahadees.com/\n", |
|
"https://tanzeemdigitallibrary.com/\n", |
|
"https://altasreef.vercel.app\n", |
|
"https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran\n", |
|
"https://play.google.com/store/apps/details?id=com.asantarjuma\n", |
|
"https://play.google.com/store/apps/details?id=tazkeerul.quran\n", |
|
"https://play.google.com/store/apps/details?id=com.anfzquizapp\n", |
|
"https://play.google.com/store/apps/details?id=com.tuheed\n", |
|
"https://isdp.info\n", |
|
"https://isdp.info/students-in-field.php\n", |
|
"https://altasreef.vercel.app\n", |
|
"https://altasreef.vercel.app/exam\n", |
|
"https://aclanthology.org/2025.abjadnlp-1.14.pdf\n", |
|
"https://www.easyquranwahadees.com/\n", |
|
"https://mohaddis.com/\n", |
|
"https://tanzeemdigitallibrary.com/\n", |
|
"https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran\n", |
|
"https://play.google.com/store/apps/details?id=tazkeerul.quran\n", |
|
"https://play.google.com/store/apps/details?id=com.anfzquizapp\n", |
|
"https://play.google.com/store/apps/details?id=com.tuheed\n", |
|
"https://aclanthology.org/2025.abjadnlp-1.14.pdf\n", |
|
"https://link.springer.com/article/10.1007/s10462-022-10313-2\n", |
|
"https://aclanthology.org/2022.osact-1.18/\n", |
|
"https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35\n", |
|
"https://ejournal.um.edu.my/index.php/quranica/article/view/12108\n", |
|
"https://ieeexplore.ieee.org/document/7277250\n", |
|
"https://scholar.google.com/citations?user=AulVnWcAAAAJ&hl=en\n", |
|
"https://altasreef.vercel.app\n", |
|
"mailto:haq@haqnawaz.org\n", |
|
"https://github.com/haqnawaz99\n", |
|
"https://linkedin.com/in/haqnawaz99\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"print(get_links_user_prompt(ed))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 10, |
|
"id": "a29aca19-ca13-471c-a4b4-5abbfa813f69", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def get_links(url):\n", |
|
" website = Website(url)\n", |
|
" response = openai.chat.completions.create(\n", |
|
" model=MODEL,\n", |
|
" messages=[\n", |
|
" {\"role\": \"system\", \"content\": link_system_prompt},\n", |
|
" {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n", |
|
" ],\n", |
|
" response_format={\"type\": \"json_object\"}\n", |
|
" )\n", |
|
" result = response.choices[0].message.content\n", |
|
" return json.loads(result)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 11, |
|
"id": "74a827a0-2782-4ae5-b210-4a242a8b4cc2", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"['/',\n", |
|
" '/models',\n", |
|
" '/datasets',\n", |
|
" '/spaces',\n", |
|
" '/posts',\n", |
|
" '/docs',\n", |
|
" '/enterprise',\n", |
|
" '/pricing',\n", |
|
" '/login',\n", |
|
" '/join',\n", |
|
" '/spaces',\n", |
|
" '/models',\n", |
|
" '/Qwen/Qwen2.5-Omni-7B',\n", |
|
" '/deepseek-ai/DeepSeek-V3-0324',\n", |
|
" '/all-hands/openhands-lm-32b-v0.1',\n", |
|
" '/meta-llama/Llama-4-Scout-17B-16E-Instruct',\n", |
|
" '/openfree/flux-chatgpt-ghibli-lora',\n", |
|
" '/models',\n", |
|
" '/spaces/enzostvs/deepsite',\n", |
|
" '/spaces/jamesliu1217/EasyControl_Ghibli',\n", |
|
" '/spaces/VAST-AI/TripoSG',\n", |
|
" '/spaces/ByteDance/InfiniteYou-FLUX',\n", |
|
" '/spaces/Stable-X/Hi3DGen',\n", |
|
" '/spaces',\n", |
|
" '/datasets/nvidia/Llama-Nemotron-Post-Training-Dataset-v1',\n", |
|
" '/datasets/virtuoussy/Multi-subject-RLVR',\n", |
|
" '/datasets/glaiveai/reasoning-v1-20m',\n", |
|
" '/datasets/open-thoughts/OpenThoughts2-1M',\n", |
|
" '/datasets/FreedomIntelligence/medical-o1-reasoning-SFT',\n", |
|
" '/datasets',\n", |
|
" '/join',\n", |
|
" '/pricing#endpoints',\n", |
|
" '/pricing#spaces',\n", |
|
" '/pricing',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/enterprise',\n", |
|
" '/allenai',\n", |
|
" '/facebook',\n", |
|
" '/amazon',\n", |
|
" '/google',\n", |
|
" '/Intel',\n", |
|
" '/microsoft',\n", |
|
" '/grammarly',\n", |
|
" '/Writer',\n", |
|
" '/docs/transformers',\n", |
|
" '/docs/diffusers',\n", |
|
" '/docs/safetensors',\n", |
|
" '/docs/huggingface_hub',\n", |
|
" '/docs/tokenizers',\n", |
|
" '/docs/trl',\n", |
|
" '/docs/transformers.js',\n", |
|
" '/docs/smolagents',\n", |
|
" '/docs/peft',\n", |
|
" '/docs/datasets',\n", |
|
" '/docs/text-generation-inference',\n", |
|
" '/docs/accelerate',\n", |
|
" '/models',\n", |
|
" '/datasets',\n", |
|
" '/spaces',\n", |
|
" '/tasks',\n", |
|
" 'https://ui.endpoints.huggingface.co',\n", |
|
" '/chat',\n", |
|
" '/huggingface',\n", |
|
" '/brand',\n", |
|
" '/terms-of-service',\n", |
|
" '/privacy',\n", |
|
" 'https://apply.workable.com/huggingface/',\n", |
|
" 'mailto:press@huggingface.co',\n", |
|
" '/learn',\n", |
|
" '/docs',\n", |
|
" '/blog',\n", |
|
" 'https://discuss.huggingface.co',\n", |
|
" 'https://status.huggingface.co/',\n", |
|
" 'https://github.com/huggingface',\n", |
|
" 'https://twitter.com/huggingface',\n", |
|
" 'https://www.linkedin.com/company/huggingface/',\n", |
|
" '/join/discord']" |
|
] |
|
}, |
|
"execution_count": 11, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"# Anthropic has made their site harder to scrape, so I'm using HuggingFace..\n", |
|
"\n", |
|
"huggingface = Website(\"https://huggingface.co\")\n", |
|
"huggingface.links" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 12, |
|
"id": "d3d583e2-dcc4-40cc-9b28-1e8dbf402924", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"{'links': [{'type': 'home', 'url': 'https://huggingface.co/'},\n", |
|
" {'type': 'models section', 'url': 'https://huggingface.co/models'},\n", |
|
" {'type': 'datasets section', 'url': 'https://huggingface.co/datasets'},\n", |
|
" {'type': 'spaces section', 'url': 'https://huggingface.co/spaces'},\n", |
|
" {'type': 'posts section', 'url': 'https://huggingface.co/posts'},\n", |
|
" {'type': 'documentation', 'url': 'https://huggingface.co/docs'},\n", |
|
" {'type': 'enterprise', 'url': 'https://huggingface.co/enterprise'},\n", |
|
" {'type': 'pricing', 'url': 'https://huggingface.co/pricing'},\n", |
|
" {'type': 'blog', 'url': 'https://huggingface.co/blog'},\n", |
|
" {'type': 'community discussion', 'url': 'https://discuss.huggingface.co'},\n", |
|
" {'type': 'status', 'url': 'https://status.huggingface.co/'},\n", |
|
" {'type': 'GitHub', 'url': 'https://github.com/huggingface'},\n", |
|
" {'type': 'Twitter', 'url': 'https://twitter.com/huggingface'},\n", |
|
" {'type': 'LinkedIn',\n", |
|
" 'url': 'https://www.linkedin.com/company/huggingface/'}]}" |
|
] |
|
}, |
|
"execution_count": 12, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"get_links(\"https://huggingface.co\")" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "0d74128e-dfb6-47ec-9549-288b621c838c", |
|
"metadata": {}, |
|
"source": [ |
|
"## Second step: make the brochure!\n", |
|
"\n", |
|
"Assemble all the details into another prompt to GPT4-o" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 13, |
|
"id": "85a5b6e2-e7ef-44a9-bc7f-59ede71037b5", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def get_all_details(url):\n", |
|
" result = \"Landing page:\\n\"\n", |
|
" result += Website(url).get_contents()\n", |
|
" links = get_links(url)\n", |
|
" print(\"Found links:\", links)\n", |
|
" for link in links[\"links\"]:\n", |
|
" result += f\"\\n\\n{link['type']}\\n\"\n", |
|
" result += Website(link[\"url\"]).get_contents()\n", |
|
" return result" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 14, |
|
"id": "5099bd14-076d-4745-baf3-dac08d8e5ab2", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# print(get_all_details(\"https://huggingface.co\"))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 15, |
|
"id": "9b863a55-f86c-4e3f-8a79-94e24c1a8cf2", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n", |
|
"and creates a short brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n", |
|
"Include details of company culture, customers and careers/jobs if you have the information.\"\n", |
|
"\n", |
|
"# Or uncomment the lines below for a more humorous brochure - this demonstrates how easy it is to incorporate 'tone':\n", |
|
"\n", |
|
"# system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n", |
|
"# and creates a short humorous, entertaining, jokey brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n", |
|
"# Include details of company culture, customers and careers/jobs if you have the information.\"\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 16, |
|
"id": "6ab83d92-d36b-4ce0-8bcc-5bb4c2f8ff23", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def get_brochure_user_prompt(company_name, url):\n", |
|
" user_prompt = f\"You are looking at a company called: {company_name}\\n\"\n", |
|
" user_prompt += f\"Here are the contents of its landing page and other relevant pages; use this information to build a short brochure of the company in markdown.\\n\"\n", |
|
" user_prompt += get_all_details(url)\n", |
|
" user_prompt = user_prompt[:5_000] # Truncate if more than 5,000 characters\n", |
|
" return user_prompt" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "cd909e0b-1312-4ce2-a553-821e795d7572", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Found links: {'links': [{'type': 'homepage', 'url': 'https://huggingface.co/'}, {'type': 'models section', 'url': 'https://huggingface.co/models'}, {'type': 'datasets section', 'url': 'https://huggingface.co/datasets'}, {'type': 'spaces section', 'url': 'https://huggingface.co/spaces'}, {'type': 'posts section', 'url': 'https://huggingface.co/posts'}, {'type': 'documentation section', 'url': 'https://huggingface.co/docs'}, {'type': 'enterprise section', 'url': 'https://huggingface.co/enterprise'}, {'type': 'pricing section', 'url': 'https://huggingface.co/pricing'}, {'type': 'blog section', 'url': 'https://huggingface.co/blog'}, {'type': 'discussion forum', 'url': 'https://discuss.huggingface.co'}, {'type': 'GitHub repository', 'url': 'https://github.com/huggingface'}, {'type': 'Twitter profile', 'url': 'https://twitter.com/huggingface'}, {'type': 'LinkedIn profile', 'url': 'https://www.linkedin.com/company/huggingface/'}]}\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"# get_brochure_user_prompt(\"HuggingFace\", \"https://huggingface.co\")" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 17, |
|
"id": "e44de579-4a1a-4e6a-a510-20ea3e4b8d46", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def create_brochure(company_name, url):\n", |
|
" response = openai.chat.completions.create(\n", |
|
" model=MODEL,\n", |
|
" messages=[\n", |
|
" {\"role\": \"system\", \"content\": system_prompt},\n", |
|
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", |
|
" ],\n", |
|
" )\n", |
|
" result = response.choices[0].message.content\n", |
|
" display(Markdown(result))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "e093444a-9407-42ae-924a-145730591a39", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"create_brochure(\"HuggingFace\", \"https://huggingface.co\")" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "61eaaab7-0b47-4b29-82d4-75d474ad8d18", |
|
"metadata": {}, |
|
"source": [ |
|
"## Finally - a minor improvement\n", |
|
"\n", |
|
"With a small adjustment, we can change this so that the results stream back from OpenAI,\n", |
|
"with the familiar typewriter animation" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 18, |
|
"id": "51db0e49-f261-4137-aabe-92dd601f7725", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def stream_brochure(company_name, url):\n", |
|
" stream = openai.chat.completions.create(\n", |
|
" model=MODEL,\n", |
|
" messages=[\n", |
|
" {\"role\": \"system\", \"content\": system_prompt},\n", |
|
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", |
|
" ],\n", |
|
" stream=True\n", |
|
" )\n", |
|
" \n", |
|
" response = \"\"\n", |
|
" display_handle = display(Markdown(\"\"), display_id=True)\n", |
|
" for chunk in stream:\n", |
|
" response += chunk.choices[0].delta.content or ''\n", |
|
" response = response.replace(\"```\",\"\").replace(\"markdown\", \"\")\n", |
|
" update_display(Markdown(response), display_id=display_handle.display_id)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 20, |
|
"id": "56bf0ae3-ee9d-4a72-9cd6-edcac67ceb6d", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Found links: {'links': [{'type': 'about section', 'url': 'https://haqnawaz.org#introduction'}, {'type': 'projects section', 'url': 'https://haqnawaz.org#projects'}, {'type': 'AI & NLP section', 'url': 'https://haqnawaz.org#ai-nlp-expertise'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=tazkeerul.quran'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.anfzquizapp'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.tuheed'}, {'type': 'publications section', 'url': 'https://aclanthology.org/2025.abjadnlp-1.14.pdf'}, {'type': 'publications section', 'url': 'https://link.springer.com/article/10.1007/s10462-022-10313-2'}, {'type': 'publications section', 'url': 'https://aclanthology.org/2022.osact-1.18/'}, {'type': 'publications section', 'url': 'https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35'}, {'type': 'publications section', 'url': 'https://ejournal.um.edu.my/index.php/quranica/article/view/12108'}, {'type': 'publications section', 'url': 'https://ieeexplore.ieee.org/document/7277250'}, {'type': 'awards section', 'url': 'https://github.com/haqnawaz99'}, {'type': 'contact section', 'url': 'https://haqnawaz.org#contact'}, {'type': 'contact section', 'url': 'https://linkedin.com/in/haqnawaz99'}]}\n" |
|
] |
|
}, |
|
{ |
|
"name": "stderr", |
|
"output_type": "stream", |
|
"text": [ |
|
"Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.\n" |
|
] |
|
}, |
|
{ |
|
"data": { |
|
"text/markdown": [ |
|
"# HuggingFace Brochure\n", |
|
"\n", |
|
"## Bridging AI & Islamic Scholarship\n", |
|
"\n", |
|
"### About Us\n", |
|
"HuggingFace, spearheaded by *Haq Nawaz*, is at the forefront of merging modern artificial intelligence (AI) with classical Islamic scholarship. With a robust background in Natural Language Processing (NLP), Haq Nawaz is not just a faculty member at Jamia Ashrafia Lahore but is also a dedicated PhD scholar and data science consultant. Our mission is to innovate and digitize Islamic education through award-winning software and applications, enhancing accessibility and engagement with religious texts.\n", |
|
"\n", |
|
"### Our Vision\n", |
|
"We are committed to pioneering efforts in Islamic software development, creating tools that benefit education, research, and broader public outreach. Our projects aim to make classical Islamic literature, the Qur'an, and Hadith easily searchable and interactable, ensuring that this vital knowledge is accessible to everyone.\n", |
|
"\n", |
|
"### Notable Projects\n", |
|
"- **Easy Quran**: An interactive platform that simplifies access to Quranic texts.\n", |
|
"- **Mohaddis**: A tailored solution for Hadith studies.\n", |
|
"- **Tanzeem**: A digital library assembling Islamic references.\n", |
|
"- **Altasreef**: An award-winning platform for Arabic verb conjugation that earned 1st prize at UET Lahore’s Brainiac 2018.\n", |
|
"- **Mobile Applications**:\n", |
|
" - **The Noble Quran**: Offering esteemed translations and tafsir.\n", |
|
" - **Tazkeer Ul Quran**: Multilingual thematic analysis.\n", |
|
" - **Seerat Quiz**: Educational interactive platform for children.\n", |
|
"\n", |
|
"### Recognition\n", |
|
"HuggingFace's contributions have been nationally recognized at events like NaSCon 2019 for innovative research and development in Quranic NLP annotation. We pride ourselves on our role in the wider scholarly community, having hosted discussions with esteemed Islamic leaders regarding the integration of technology and traditional scholarship.\n", |
|
"\n", |
|
"### Company Culture\n", |
|
"At HuggingFace, we foster an environment that encourages collaboration, innovation, and a deep respect for the principles of Islamic scholarship. Our culture thrives on bridging traditional and modern methodologies, promoting continuous learning, and empowering our employees to contribute to meaningful projects that have a tangible impact on society.\n", |
|
"\n", |
|
"### Career Opportunities\n", |
|
"We are always on the lookout for passionate individuals who desire to contribute to the intersection of AI and Islamic scholarship. Our **Intensive Software Development Program (ISDP)** equips recent graduates of Islamic madrasas with essential computer science and software engineering skills, preparing them for rewarding careers in technology.\n", |
|
"\n", |
|
"Join us in our mission to inspire future generations by advancing Islamic knowledge through technology. Together, we can redefine how classical learning is approached in the digital age.\n", |
|
"\n", |
|
"## Get in Touch\n", |
|
"For inquiries related to partnerships, careers, or projects, contact us at:\n", |
|
"- Email: [info@huggingface.com](mailto:info@huggingface.com)\n", |
|
"- Phone: +123-456-7890\n", |
|
"\n", |
|
"### Follow Us\n", |
|
"Stay updated with our latest initiatives and innovations by following us on our social media channels! \n", |
|
"\n", |
|
"Together, let’s bridge the gap between AI and Islamic scholarship." |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.Markdown object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
} |
|
], |
|
"source": [ |
|
"stream_brochure(\"HuggingFace\", \"https://haqnawaz.org\")" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "fdb3f8d8-a3eb-41c8-b1aa-9f60686a653b", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Try changing the system prompt to the humorous version when you make the Brochure for Hugging Face:\n", |
|
"\n", |
|
"stream_brochure(\"HuggingFace\", \"https://huggingface.co\")" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "a27bf9e0-665f-4645-b66b-9725e2a959b5", |
|
"metadata": {}, |
|
"source": [ |
|
"<table style=\"margin: 0; text-align: left;\">\n", |
|
" <tr>\n", |
|
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
|
" <img src=\"../business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
|
" </td>\n", |
|
" <td>\n", |
|
" <h2 style=\"color:#181;\">Business applications</h2>\n", |
|
" <span style=\"color:#181;\">In this exercise we extended the Day 1 code to make multiple LLM calls, and generate a document.\n", |
|
"\n", |
|
"This is perhaps the first example of Agentic AI design patterns, as we combined multiple calls to LLMs. This will feature more in Week 2, and then we will return to Agentic AI in a big way in Week 8 when we build a fully autonomous Agent solution.\n", |
|
"\n", |
|
"Generating content in this way is one of the very most common Use Cases. As with summarization, this can be applied to any business vertical. Write marketing content, generate a product tutorial from a spec, create personalized email content, and so much more. Explore how you can apply content generation to your business, and try making yourself a proof-of-concept prototype. See what other students have done in the community-contributions folder -- so many valuable projects -- it's wild!</span>\n", |
|
" </td>\n", |
|
" </tr>\n", |
|
"</table>" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "14b2454b-8ef8-4b5c-b928-053a15e0d553", |
|
"metadata": {}, |
|
"source": [ |
|
"<table style=\"margin: 0; text-align: left;\">\n", |
|
" <tr>\n", |
|
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
|
" <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
|
" </td>\n", |
|
" <td>\n", |
|
" <h2 style=\"color:#900;\">Before you move to Week 2 (which is tons of fun)</h2>\n", |
|
" <span style=\"color:#900;\">Please see the week1 EXERCISE notebook for your challenge for the end of week 1. This will give you some essential practice working with Frontier APIs, and prepare you well for Week 2.</span>\n", |
|
" </td>\n", |
|
" </tr>\n", |
|
"</table>" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "17b64f0f-7d33-4493-985a-033d06e8db08", |
|
"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;\">A reminder on 3 useful resources</h2>\n", |
|
" <span style=\"color:#f71;\">1. The resources for the course are available <a href=\"https://edwarddonner.com/2024/11/13/llm-engineering-resources/\">here.</a><br/>\n", |
|
" 2. I'm on LinkedIn <a href=\"https://www.linkedin.com/in/eddonner/\">here</a> and I love connecting with people taking the course!<br/>\n", |
|
" 3. I'm trying out X/Twitter and I'm at <a href=\"https://x.com/edwarddonner\">@edwarddonner<a> and hoping people will teach me how it's done.. \n", |
|
" </span>\n", |
|
" </td>\n", |
|
" </tr>\n", |
|
"</table>" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "6f48e42e-fa7a-495f-a5d4-26bfc24d60b6", |
|
"metadata": {}, |
|
"source": [ |
|
"<table style=\"margin: 0; text-align: left;\">\n", |
|
" <tr>\n", |
|
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
|
" <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
|
" </td>\n", |
|
" <td>\n", |
|
" <h2 style=\"color:#090;\">Finally! I have a special request for you</h2>\n", |
|
" <span style=\"color:#090;\">\n", |
|
" My editor tells me that it makes a MASSIVE difference when students rate this course on Udemy - it's one of the main ways that Udemy decides whether to show it to others. If you're able to take a minute to rate this, I'd be so very grateful! And regardless - always please reach out to me at ed@edwarddonner.com if I can help at any point.\n", |
|
" </span>\n", |
|
" </td>\n", |
|
" </tr>\n", |
|
"</table>" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "b8d3e1a1-ba54-4907-97c5-30f89a24775b", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "venv", |
|
"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.10.0" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
}
|
|
|