224 lines
6.9 KiB
Plaintext
224 lines
6.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "fdc2f470",
|
|
"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",
|
|
"from openai import OpenAI\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": "5f0fbd79",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"API key found and looks good so far!\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Load environment variables in a file called .env\n",
|
|
"\n",
|
|
"load_dotenv(override=True)\n",
|
|
"api_key = os.getenv('OPENAI_API_KEY')\n",
|
|
"\n",
|
|
"# Check the key\n",
|
|
"\n",
|
|
"if not api_key:\n",
|
|
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
|
|
"elif not api_key.startswith(\"sk-proj-\"):\n",
|
|
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
|
|
"elif api_key.strip() != api_key:\n",
|
|
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
|
|
"else:\n",
|
|
" print(\"API key found and looks good so far!\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "b771480a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"openai = OpenAI()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "0e97974c",
|
|
"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": 5,
|
|
"id": "2ec62fb3",
|
|
"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 list of the flights available according to what user asks for, ignoring text that might be navigation related. \\\n",
|
|
"Respond in markdown.\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "7a93a605",
|
|
"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 list of all the flights available in a table format in markdown. The columns of the table should be - Flight carrier, Flight Dat and times, Fare, No. of stops. \\\n",
|
|
"Provide exact flight carriers. If it includes ads or offers, then summarize these too.\\n\\n\"\n",
|
|
" user_prompt += website.text\n",
|
|
" return user_prompt"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "597646e5",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def fetch_flights(from_tx,to_tx, date_from,date_to=''):\n",
|
|
" website = Website(f\"https://www.ca.kayak.com/flights/{from_tx}-{to_tx}/{date_from}/{date_to}\")\n",
|
|
" user_prompt = user_prompt_for(website)\n",
|
|
" messages = [{\"role\":\"system\",\"content\":system_prompt},{\"role\":\"user\",\"content\":user_prompt}]\n",
|
|
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
|
" display(Markdown(response.choices[0].message.content))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "47ae61f3",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/markdown": [
|
|
"Here is the list of available flights from YYZ to DEL on 9/11:\n",
|
|
"\n",
|
|
"| Flight Carrier | Flight Date and Times | Fare | No. of Stops |\n",
|
|
"|----------------|-----------------------|--------|--------------|\n",
|
|
"| Air Canada | 9/11, 10:00 AM | C$ 833 | 1 |\n",
|
|
"| Lufthansa | 9/11, 5:00 PM | C$ 847 | 2 |\n",
|
|
"| Qatar Airways | 9/11, 1:30 PM | C$ 1,559| 1 |\n",
|
|
"\n",
|
|
"### Summary of Offers\n",
|
|
"- The cheapest fare is C$ 833 with a travel time of 23 hours and 35 minutes.\n",
|
|
"- The best fare option is C$ 847 with a travel time of 22 hours and 20 minutes.\n",
|
|
"- The quickest option is priced at C$ 1,559 with a travel duration of 13 hours and 55 minutes. \n",
|
|
"\n",
|
|
"*Note: Prices are per person and do not include baggage fees.*"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.Markdown object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"fetch_flights('yyz','del','2025-11-09')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3a48ceb6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "15bb1a04",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "902975bf",
|
|
"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.12.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|