Skip to content

Commit 3a2eb97

Browse files
committed
Merge branch 'main' of github.com:ed-donner/llm_engineering
2 parents 8338dfc + 54d61c1 commit 3a2eb97

25 files changed

+10314
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Client: Hello I would like to order a pizza
2+
Restaurant: Sure. What pizza would you like to order from our menu?
3+
Client: Chicken Ranch
4+
Restaurant: I am so sorry, but chicken ranch is currently unavailable on our menu
5+
Client: AHHHHH. Do you have chicken BBQ?
6+
Restaurant: Yes! Do you want it small, medium, or large?
7+
Client: Medium
8+
Restaurant: Ok. This will be 180 LE
9+
Client: Thanks
10+
Restaurant: Anytime.
11+
Client: AHHHH I forgot. I want to add a new chicken BBQ pizza
12+
Restaurant: No problem. Do you also want it medium?
13+
Client: Yes
14+
Restaurant: Okay this will be 380 LE
15+
Client: Okay Thanks
16+
Client: Wait a minute. Isn't 180 * 2 = 360?
17+
Restaurant: It seems that there might be a misunderstanding. We add an extra 20 LE for every extra pizza ordered.
18+
Client: NOBODY TOLD ME THAT.. AND WHY ON EARTH WOULD YOU DO SOMETHING LIKE THAT?
19+
Restaurant: We are sorry but this is our policy.
20+
Client: Okay then I don't want your pizza.
21+
Restaurant: We are so sorry to hear that. We can make a 10% discount on the total price so it would be 342 LE
22+
Client: Fine
23+
Restaurant: Thank you for ordering
24+
Restaurant: Pizza is delivered. How is your experience?
25+
Client: Your pizza doesn't taste good
26+
Restaurant: We are so sorry to hear that. Do you have any suggestions you would like to make?
27+
Client: Make good pizza
28+
Restaurant: Thanks for your review. We will make sure to improve our pizza in the future. Your opinion really matters.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Client: Hello I would like to order a chicken ranch pizza
2+
Restaurant: I am so sorry, but chicken ranch is currently unavailable on our menu
3+
Client: Okay thanks
4+
Restaurant: Would you like to order something else?
5+
Client: No thank you
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Client: Hello. What is the most selling pizza on your menu?
2+
Restaurant: Hello! Chicken Ranch pizza is our most selling pizza. Also our special pepperoni pizza got some amazing reviews
3+
Client: Okay. I want to order a pepperoni pizza
4+
Restaurant: Sure. Do you want it small, medium, or large?
5+
Client: Large
6+
Restaurant: Okay. This will be 210 LE. Would you like to order something else?
7+
Client: Yes. Do you have onion rings?
8+
Restaurant: Yes
9+
Client: Okay I would like to add onion rings.
10+
Restaurant: Sure. This will be 250 LE
11+
Client: Thanks
12+
Restaurant: Anytime
13+
Client: I have been waiting for too long and the order hasn't arrived yet
14+
Restaurant: Sorry to hear that. But it appears that the order is on its way to you.
15+
Restaurant: The order is supposed to be arrived by now.
16+
Client: Yes it is arrived.
17+
Restaurant: How is your experience?
18+
Client: Your pizza tastes soooooo good. The order took too long to arrive but when I tasted the pizza, I was really enjoying it and forgot everything about the delay.
19+
Restaurant: We are so glad to hear that
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
You are an assistant working for the customer service department in a pizza restaurant.
2+
You are to receive a chat between a client and the restaurant's customer service.
3+
You should generate your responses based on the following criteria:
4+
- What did the client order?
5+
- How much did it cost?
6+
- If the client changed their mind just keep their final order and the final cost
7+
- Mention the client's experience only if they ordered anything as follows: (Positive/Negative/Neutral/Unknown)
8+
- If the client did not order anything do not mention their sentiment or experience
9+
- If the client's experience is positive or negative only, provide a brief summary about their sentiment
10+
- Do not provide brief summary about their sentiment if their experience was neutral or unknown.
11+
- Your answers should be clear, straight to the point, and do not use long sentences
12+
- Your answers should be displayed in bullet points
13+
- Your answers should be displayed in markdown
14+
- If the client did not order anything provide a brief summary why that might happened
15+
- Do not mention cost if the client did not order anything
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "39e3e763-9b00-49eb-aead-034a2d0517a7",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"# imports\n",
11+
"\n",
12+
"import os\n",
13+
"import requests\n",
14+
"from dotenv import load_dotenv\n",
15+
"from bs4 import BeautifulSoup\n",
16+
"from IPython.display import Markdown, display\n",
17+
"from openai import OpenAI\n",
18+
"\n",
19+
"# If you get an error running this cell, then please head over to the troubleshooting notebook!"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"id": "f3bb5e2a-b70f-42ba-9f22-030a9c6bc9d1",
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"# Load environment variables in a file called .env\n",
30+
"\n",
31+
"load_dotenv(override=True)\n",
32+
"api_key = os.getenv('OPENAI_API_KEY')\n",
33+
"\n",
34+
"# Check the key\n",
35+
"\n",
36+
"if not api_key:\n",
37+
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
38+
"elif not api_key.startswith(\"sk-proj-\"):\n",
39+
" 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",
40+
"elif api_key.strip() != api_key:\n",
41+
" 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",
42+
"else:\n",
43+
" print(\"API key found and looks good so far!\")\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"id": "994f51fb-eab3-45a2-847f-87aebb92b17a",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"openai = OpenAI()\n",
54+
"\n",
55+
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
56+
"# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"id": "a8125c6d-c884-4f65-b477-cab155e29ce3",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"# Step 1: Create your prompts\n",
67+
"\n",
68+
"system_prompt = \"You are an AI that suggests short and relevant subject lines for emails based on their content.\"\n",
69+
"user_prompt = \"\"\"\n",
70+
"Here is the content of an email:\n",
71+
"\n",
72+
"Dear Team,\n",
73+
"\n",
74+
"I hope you're all doing well. I wanted to remind you that our next project meeting is scheduled for this Friday at 3 PM. We will be discussing our progress and any blockers. Please make sure to review the latest updates before the meeting.\n",
75+
"\n",
76+
"Best, \n",
77+
"John\n",
78+
"\"\"\"\n",
79+
"\n",
80+
"# Step 2: Make the messages list\n",
81+
"\n",
82+
"messages = [ {\"role\": \"system\", \"content\": system_prompt},\n",
83+
" {\"role\": \"user\", \"content\": user_prompt}] # fill this in\n",
84+
"\n",
85+
"# Step 3: Call OpenAI\n",
86+
"\n",
87+
"response = openai.chat.completions.create(\n",
88+
" model = \"gpt-4o-mini\",\n",
89+
" messages=messages\n",
90+
")\n",
91+
"\n",
92+
"# Step 4: print the result\n",
93+
"\n",
94+
"print(\"Suggested Subject Line:\", response.choices[0].message.content)"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "1010ac80-1ee8-432f-aa3f-12af419dc23a",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": []
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"display_name": "Python 3 (ipykernel)",
109+
"language": "python",
110+
"name": "python3"
111+
},
112+
"language_info": {
113+
"codemirror_mode": {
114+
"name": "ipython",
115+
"version": 3
116+
},
117+
"file_extension": ".py",
118+
"mimetype": "text/x-python",
119+
"name": "python",
120+
"nbconvert_exporter": "python",
121+
"pygments_lexer": "ipython3",
122+
"version": "3.11.11"
123+
}
124+
},
125+
"nbformat": 4,
126+
"nbformat_minor": 5
127+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c97ad592-c8be-4583-a19c-ac813e56f410",
6+
"metadata": {},
7+
"source": [
8+
"## Mac Users\n",
9+
"\n",
10+
"I find some challenges while setting up this in MAC silicon M1 chip. Execute below commands in MAC terminal.\n",
11+
"\n",
12+
"1. Download chromedriver.\n",
13+
"2. Unzip and add it to the path.\n",
14+
"3. Set Extended attributes."
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "b635b345-b000-48cc-8a7f-7df279a489a3",
20+
"metadata": {},
21+
"source": [
22+
"cd ~/Downloads\n",
23+
"wget https://storage.googleapis.com/chrome-for-testing-public/133.0.6943.126/mac-arm64/chromedriver-mac-arm64.zip\n",
24+
"unzip chromedriver-mac-arm64.zip\n",
25+
"sudo mv chromedriver-mac-arm64/chromedriver /usr/local/bin/\n",
26+
"chmod +x /usr/local/bin/chromedriver\n",
27+
"cd /usr/local/bin/\n",
28+
"xattr -d com.apple.quarantine chromedriver\n",
29+
"cd \n",
30+
"chromedriver --version"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "17c7c79a-8ae0-4f5d-a7c8-c54aa7ba90fd",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"!pip install selenium\n",
41+
"!pip install undetected-chromedriver\n",
42+
"!pip install beautifulsoup4"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"id": "c10bd630-2dfd-4572-8c21-2dc4c6a372ab",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"from selenium import webdriver\n",
53+
"from selenium.webdriver.chrome.service import Service\n",
54+
"from selenium.webdriver.common.by import By\n",
55+
"from selenium.webdriver.chrome.options import Options\n",
56+
"from openai import OpenAI\n",
57+
"import os\n",
58+
"import requests\n",
59+
"from dotenv import load_dotenv\n",
60+
"from bs4 import BeautifulSoup\n",
61+
"from IPython.display import Markdown, display\n",
62+
"from openai import OpenAI"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"id": "6fb3641d-e9f8-4f5b-bb9d-ee0e971cccdb",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
73+
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
74+
"MODEL = \"llama3.2\"\n",
75+
"PATH_TO_CHROME_DRIVER = '/usr/local/bin/chromedriver'\n",
76+
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
77+
"and provides a short summary, ignoring text that might be navigation related. \\\n",
78+
"Respond in markdown. Highlight all the products this website offered and also find when website is created.\"\n"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"id": "5d57e958",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"class Website:\n",
89+
" url: str\n",
90+
" title: str\n",
91+
" text: str\n",
92+
"\n",
93+
" def __init__(self, url):\n",
94+
" self.url = url\n",
95+
"\n",
96+
" options = Options()\n",
97+
"\n",
98+
" options.add_argument(\"--no-sandbox\")\n",
99+
" options.add_argument(\"--disable-dev-shm-usage\")\n",
100+
"\n",
101+
" service = Service(PATH_TO_CHROME_DRIVER)\n",
102+
" driver = webdriver.Chrome(service=service, options=options)\n",
103+
" driver.get(url)\n",
104+
"\n",
105+
" # input(\"Please complete the verification in the browser and press Enter to continue...\")\n",
106+
" page_source = driver.page_source\n",
107+
" driver.quit()\n",
108+
"\n",
109+
" soup = BeautifulSoup(page_source, 'html.parser')\n",
110+
" self.title = soup.title.string if soup.title else \"No title found\"\n",
111+
" for irrelevant in soup([\"script\", \"style\", \"img\", \"input\"]):\n",
112+
" irrelevant.decompose()\n",
113+
" self.text = soup.get_text(separator=\"\\n\", strip=True)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"id": "56df8cd2-2707-43f6-a066-3367846929b3",
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"def user_prompt_for(website):\n",
124+
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
125+
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
126+
"please provide a short summary of this website in markdown. \\\n",
127+
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
128+
" user_prompt += website.text\n",
129+
" return user_prompt\n",
130+
"\n",
131+
"\n",
132+
"\n",
133+
"def messages_for(website):\n",
134+
" return [\n",
135+
" {\"role\": \"system\", \"content\": system_prompt},\n",
136+
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
137+
" ]\n",
138+
"\n",
139+
"\n",
140+
"def summarize(url):\n",
141+
" website = Website(url)\n",
142+
" ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
143+
" response = ollama_via_openai.chat.completions.create(\n",
144+
" model=MODEL,\n",
145+
" messages = messages_for(website)\n",
146+
" )\n",
147+
" return response.choices[0].message.content\n",
148+
"\n",
149+
"\n",
150+
"def display_summary(url):\n",
151+
" summary = summarize(url)\n",
152+
" display(Markdown(summary))"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"id": "f2eb9599",
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"display_summary(\"https://ae.almosafer.com\")"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"id": "31b66c0f-6b45-4986-b77c-758625945a91",
169+
"metadata": {},
170+
"outputs": [],
171+
"source": []
172+
}
173+
],
174+
"metadata": {
175+
"kernelspec": {
176+
"display_name": "Python 3 (ipykernel)",
177+
"language": "python",
178+
"name": "python3"
179+
},
180+
"language_info": {
181+
"codemirror_mode": {
182+
"name": "ipython",
183+
"version": 3
184+
},
185+
"file_extension": ".py",
186+
"mimetype": "text/x-python",
187+
"name": "python",
188+
"nbconvert_exporter": "python",
189+
"pygments_lexer": "ipython3",
190+
"version": "3.11.11"
191+
}
192+
},
193+
"nbformat": 4,
194+
"nbformat_minor": 5
195+
}

0 commit comments

Comments
 (0)