Browse Source

Improve day5 notebook

pull/232/head
Phi-Li-Ne 3 months ago
parent
commit
9dbafd0373
  1. 194
      week1/day5.ipynb

194
week1/day5.ipynb

@ -133,18 +133,59 @@
"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",
"oneshot_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 or freelancer offering their services, \\\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",
"oneshot_system_prompt += \"You should respond in JSON as in this example:\"\n",
"oneshot_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",
"\"\"\""
"\"\"\"\n",
"oneshot_system_prompt += \"Make sure not to miss any relevant pages.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5a8b688-b153-41a6-8b18-f6198f3df2c9",
"metadata": {},
"outputs": [],
"source": [
"fewshot_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 or freelancer offering their services, \\\n",
"such as links to an About page, or a Company page, or Careers/Jobs pages.\\n You should respond in JSON as in the following examples:\"\n",
"fewshot_system_prompt += \"\"\"\n",
" Example 1\n",
" ['https://great-comps.com/about-me', 'https://www.linkedin.com/in/great-comp/', 'mailto:hello@mygroovydomain.com', 'https://great-comps.com/news', '/case-studies', 'https://patents.google.com/patent/US20210049536A1/', 'https://great-comps.com/workshop-ai']\n",
"\n",
" Links:\n",
" {\n",
" \"links\": [\n",
" {\"type\": \"about page\", \"url\": \"https://great-comps.de/about-me\"},\n",
" {\"type\": \"news page\": \"url\": \"https://great-comps.de/news\"},\n",
" {\"type\": \"case studies page\": \"url\": \"https://great-comps.de/case-studies\"},\n",
" {\"type\": \"workshop page\": \"url\": \"https://great-comps.de/workshop-ai\"},\n",
" ]\n",
" }\n",
"\n",
" Example 2\n",
" ['mailto:info@robbie-doodle-domain.com','https://wahlen-robbie.at/ueber-mich', 'https://www.linkedin.com/in/robbie-doodle/', 'https://news.ycombinator.com', 'https://wahlen-robbie.at/neuigkeiten', 'https://twitter.com/robbie-d', '/whitepapers', 'https://patents.google.com/patent/US20210049536A1/', 'https://wahlen-robbie.at/services']\n",
"\n",
" Links:\n",
" {\n",
" \"links\": [\n",
" {\"type\": \"über mich\", \"url\": \"https://wahlen-robbie.at/ueber-mich\"},\n",
" {\"type\": \"aktuelles\": \"url\": \"https://wahlen-robbie.at/neuigkeiten\"},\n",
" {\"type\": \"whitepaper\": \"url\": \"https://wahlen-robbie.at/whitepapers\"},\n",
" {\"type\": \"services\": \"url\": \"https://wahlen-robbie.at/services\"}\n",
" ]\n",
" }\n",
" \"\"\"\n",
"fewshot_system_prompt += \"Make sure not to miss any relevant pages.\""
]
},
{
@ -154,7 +195,8 @@
"metadata": {},
"outputs": [],
"source": [
"print(link_system_prompt)"
"print(f\"Oneshot system prompt:\\n{oneshot_system_prompt}\")\n",
"print(f\"\\n\\n\\nFewshot system prompt:\\n{fewshot_system_prompt}\")"
]
},
{
@ -166,8 +208,8 @@
"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 += \"please decide which of these are relevant web links for a brochure about the company or person offering their services, respond with the full https URL in JSON format. \\\n",
"Do not include Terms of Service, Privacy, email links or social media links.\\n\"\n",
" user_prompt += \"Links (some might be relative links):\\n\"\n",
" user_prompt += \"\\n\".join(website.links)\n",
" return user_prompt"
@ -190,20 +232,33 @@
"metadata": {},
"outputs": [],
"source": [
"def get_links(url):\n",
"def get_links(url, system_prompt=oneshot_system_prompt):\n",
" \n",
" website = Website(url)\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": link_system_prompt},\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n",
" ],\n",
" response_format={\"type\": \"json_object\"}\n",
" )\n",
" \n",
" result = response.choices[0].message.content \n",
" print(f\"Response: {result}\")\n",
" return json.loads(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2dc4150a-0042-4f5d-a7bf-158a0f9147a6",
"metadata": {},
"outputs": [],
"source": [
"get_links(ed_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -212,8 +267,9 @@
"outputs": [],
"source": [
"# Anthropic has made their site harder to scrape, so I'm using HuggingFace..\n",
"hf = \"https://huggingface.co\"\n",
"\n",
"huggingface = Website(\"https://huggingface.co\")\n",
"huggingface = Website(hf)\n",
"huggingface.links"
]
},
@ -224,7 +280,21 @@
"metadata": {},
"outputs": [],
"source": [
"get_links(\"https://huggingface.co\")"
"ed_url = \"https://edwarddonner.com\"\n",
"hf_url = \"https://huggingface.co\"\n",
"\n",
"print(f\"Links generated with oneshot prompt for {ed_url}:\\n\")\n",
"get_links(ed_url)\n",
"\n",
"print(f\"\\n\\nLinks generated with fewshot prompt for {ed_url}:\\n\")\n",
"get_links(ed_url, fewshot_system_prompt)\n",
"\n",
"print(50*\"*\")\n",
"print(f\"\\nLinks generated with oneshot prompt for {hf_url}:\\n\")\n",
"get_links(hf_url)\n",
"\n",
"print(f\"\\n\\nLinks generated with fewshot prompt for {hf_url}:\\n\")\n",
"get_links(hf_url, fewshot_system_prompt)"
]
},
{
@ -244,10 +314,11 @@
"metadata": {},
"outputs": [],
"source": [
"def get_all_details(url):\n",
"def get_all_details(url, type=fewshot_system_prompt):\n",
" result = \"Landing page:\\n\"\n",
" result += Website(url).get_contents()\n",
" links = get_links(url)\n",
"\n",
" links = get_links(url, type)\n",
" print(\"Found links:\", links)\n",
" for link in links[\"links\"]:\n",
" result += f\"\\n\\n{link['type']}\\n\"\n",
@ -262,7 +333,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(get_all_details(\"https://huggingface.co\"))"
"print(get_all_details(ed_url))"
]
},
{
@ -273,7 +344,8 @@
"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",
"and creates a short brochure about the company for prospective customers, investors and recruits. \\\n",
"The brochure should be a bit unusual in terms of tone and style, it should astound the reader and pique their interest. 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",
@ -298,6 +370,16 @@
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05d07160-7910-4da2-92ac-36aa849fcc68",
"metadata": {},
"outputs": [],
"source": [
"# get_brochure_user_prompt(\"Edward Donner\", ed_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -305,7 +387,7 @@
"metadata": {},
"outputs": [],
"source": [
"get_brochure_user_prompt(\"HuggingFace\", \"https://huggingface.co\")"
"# get_brochure_user_prompt(\"HuggingFace\", \"https://huggingface.co\")"
]
},
{
@ -323,8 +405,17 @@
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n",
" ],\n",
" )\n",
" result = response.choices[0].message.content\n",
" display(Markdown(result))"
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b0de762-f343-44d9-85d5-9bffba3c0ae8",
"metadata": {},
"outputs": [],
"source": [
"brochure_ed = create_brochure(\"Edward Donner\", ed_url)"
]
},
{
@ -334,7 +425,70 @@
"metadata": {},
"outputs": [],
"source": [
"create_brochure(\"HuggingFace\", \"https://huggingface.co\")"
"brochure_hf = create_brochure(\"HuggingFace\", \"https://huggingface.co\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d00b012-3901-492c-b985-a0340750c011",
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(brochure_ed))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e33cb2e9-3b8c-4ef3-a6cb-70b3188b9120",
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(brochure_hf))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dea955ad-24a6-490b-8191-f066bff1b595",
"metadata": {},
"outputs": [],
"source": [
"def translate_brochure(brochure_content, language=\"German\"):\n",
" system_prompt = f\"You are a skilled translator. Translate the following brochure text into {language}.\\\n",
" Make sure to translate into a idiomatic {language}, matching the target language's natural structure, wording and expressions, so it can't be recognised as a translation.\\\n",
" Be sure to also meet an appropriate tone, eg a good marketing language in other languages will probably be a bit less boastful than in English.\\\n",
" Output the translated brochure in Markdown format.\"\n",
" \n",
" response = openai.chat.completions.create(\n",
" model = MODEL,\n",
" messages = [{\"role\": \"system\", \"content\": system_prompt}, {\"role\": \"user\", \"content\": brochure_content}]\n",
" )\n",
"\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b6bdd4f-7518-4780-9da9-47f90aab974b",
"metadata": {},
"outputs": [],
"source": [
"translation = translate_brochure(brochure_ed, language=\"German\")\n",
"display(Markdown(translation))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1dd96f2-0980-4a30-a152-1f38c0e319bb",
"metadata": {},
"outputs": [],
"source": [
"translation = translate_brochure(brochure_hf, language=\"German\")\n",
"display(Markdown(translation))"
]
},
{

Loading…
Cancel
Save