241
week1/day2.ipynb
241
week1/day2.ipynb
@@ -45,18 +45,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"id": "e38f17a0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"API key found and looks good so far!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
@@ -86,22 +78,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"id": "5af5c188",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'model': 'gpt-5-nano',\n",
|
||||
" 'messages': [{'role': 'user', 'content': 'Tell me a fun fact'}]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import requests\n",
|
||||
"\n",
|
||||
@@ -118,40 +98,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"id": "2d0ab242",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'id': 'chatcmpl-CTJDqe3UuLrqPiBwX0loYddfVDpWx',\n",
|
||||
" 'object': 'chat.completion',\n",
|
||||
" 'created': 1761101438,\n",
|
||||
" 'model': 'gpt-5-nano-2025-08-07',\n",
|
||||
" 'choices': [{'index': 0,\n",
|
||||
" 'message': {'role': 'assistant',\n",
|
||||
" 'content': 'Fun fact: Honey never spoils. Its low moisture and acidic pH create an environment that bacteria and fungi can’t thrive in. In fact, archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible. Want another fun fact?',\n",
|
||||
" 'refusal': None,\n",
|
||||
" 'annotations': []},\n",
|
||||
" 'finish_reason': 'stop'}],\n",
|
||||
" 'usage': {'prompt_tokens': 11,\n",
|
||||
" 'completion_tokens': 709,\n",
|
||||
" 'total_tokens': 720,\n",
|
||||
" 'prompt_tokens_details': {'cached_tokens': 0, 'audio_tokens': 0},\n",
|
||||
" 'completion_tokens_details': {'reasoning_tokens': 640,\n",
|
||||
" 'audio_tokens': 0,\n",
|
||||
" 'accepted_prediction_tokens': 0,\n",
|
||||
" 'rejected_prediction_tokens': 0}},\n",
|
||||
" 'service_tier': 'default',\n",
|
||||
" 'system_fingerprint': None}"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"response = requests.post(\n",
|
||||
" \"https://api.openai.com/v1/chat/completions\",\n",
|
||||
@@ -164,21 +114,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"id": "cb11a9f6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Fun fact: Honey never spoils. Its low moisture and acidic pH create an environment that bacteria and fungi can’t thrive in. In fact, archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible. Want another fun fact?'"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"response.json()[\"choices\"][0][\"message\"][\"content\"]"
|
||||
]
|
||||
@@ -201,26 +140,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": null,
|
||||
"id": "490fdf09",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Fun fact: Honey never spoils. Archaeologists have found pots of honey in ancient tombs that are still edible after thousands of years. Its low water content and acidic pH help keep bacteria away.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create OpenAI client\n",
|
||||
"\n",
|
||||
"from openai import OpenAI\n",
|
||||
"\n",
|
||||
"openai = OpenAI()\n",
|
||||
"\n",
|
||||
"response = openai.chat.completions.create(model=\"gpt-5-nano\", messages=[{\"role\": \"user\", \"content\": \"Tell me a fun fact\"}])\n",
|
||||
@@ -260,18 +187,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"id": "f74293bc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"GEMINI_BASE_URL = \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
|
||||
"\n",
|
||||
@@ -282,7 +201,8 @@
|
||||
"elif not google_api_key.startswith(\"AIz\"):\n",
|
||||
" print(\"An API key was found, but it doesn't start AIz\")\n",
|
||||
"else:\n",
|
||||
" print(\"API key found and looks good so far!\")\n"
|
||||
" print(\"API key found and looks good so far!\")\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -301,30 +221,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"id": "a5b069be",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"Here's one:\\n\\nDid you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly edible! Honey's longevity is due to its unique composition, which includes acid that creates an environment inhospitable to bacteria and microorganisms. Isn't that sweet?\""
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"OLLAMA_BASE_URL = \"http://localhost:11434/v1\"\n",
|
||||
"\n",
|
||||
"ollama = OpenAI(base_url=OLLAMA_BASE_URL, api_key='ollama')\n",
|
||||
"\n",
|
||||
"response = ollama.chat.completions.create(model=\"llama3.2\", messages=[{\"role\": \"user\", \"content\": \"Tell me a fun fact\"}])\n",
|
||||
"\n",
|
||||
"response.choices[0].message.content"
|
||||
]
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -340,21 +241,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"id": "f06280ad",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"b'Ollama is running'"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"requests.get(\"http://localhost:11434\").content"
|
||||
]
|
||||
@@ -373,34 +263,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": null,
|
||||
"id": "e633481d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠋ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠙ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠸ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠼ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠴ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠦ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠧ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest \u001b[K\n",
|
||||
"pulling dde5aa3fc5ff: 100% ▕██████████████████▏ 2.0 GB \u001b[K\n",
|
||||
"pulling 966de95ca8a6: 100% ▕██████████████████▏ 1.4 KB \u001b[K\n",
|
||||
"pulling fcc5a6bec9da: 100% ▕██████████████████▏ 7.7 KB \u001b[K\n",
|
||||
"pulling a70ff7e570d9: 100% ▕██████████████████▏ 6.0 KB \u001b[K\n",
|
||||
"pulling 56bb8bd477a5: 100% ▕██████████████████▏ 96 B \u001b[K\n",
|
||||
"pulling 34bb5ab01051: 100% ▕██████████████████▏ 561 B \u001b[K\n",
|
||||
"verifying sha256 digest \u001b[K\n",
|
||||
"writing manifest \u001b[K\n",
|
||||
"success \u001b[K\u001b[?25h\u001b[?2026l\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!ollama pull llama3.2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"id": "d9419762",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -412,21 +285,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"id": "e2456cdf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"Here's one:\\n\\nDid you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly edible! This is because bees produce antibiotics as part of their hive-building efforts, which helps to preserve the honey. Also, honey doesn't contain water like other foods do, which makes it extremely difficult for bacteria or microorganisms to grow. So, even if you leave a jar of honey out at room temperature for centuries (not recommended, by the way!), it should still be safe to eat!\""
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get a fun fact\n",
|
||||
"\n",
|
||||
@@ -496,56 +358,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "fa5edd63",
|
||||
"execution_count": null,
|
||||
"id": "6de38216-6d1c-48c4-877b-86d403f4e0f8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Here's a suggested concise and professional subject line:\n",
|
||||
"\n",
|
||||
"\"Meeting Rescheduled to 3 PM Tomorrow\"\n",
|
||||
"\n",
|
||||
"This subject line captures the core intent of the email, which is a meeting rescheduling, in just a few words while maintaining clarity and tone alignment.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Step 1: Create your prompts\n",
|
||||
"\n",
|
||||
"system_prompt = \"\"\"\n",
|
||||
"You are embedded in an email tool that analyzes an email’s body text and suggests a concise, relevant, and professional subject line.\n",
|
||||
"The subject line should capture the core intent of the email in 5–10 words\n",
|
||||
"Maintain clarity and tone alignment:\n",
|
||||
"Formal/business emails → neutral and polished\n",
|
||||
"Casual/internal updates → conversational and brief\n",
|
||||
"Urgent matters → include urgency subtly (e.g., “Action Needed”, “Follow-Up Required”)\n",
|
||||
"Avoid unnecessary punctuation, emojis, or filler words.\n",
|
||||
"Do not include explanations unless explicitly requested.\n",
|
||||
"\"\"\"\n",
|
||||
"user_prompt = \"\"\"\n",
|
||||
" Suggest a short subject line for the following email:\n",
|
||||
" Hi team, please note that tomorrow’s meeting will be moved to 3 PM instead of 2 PM to accommodate the new client call.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Step 2: Make the messages list\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt}\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"# Step 3: Call OpenAI\n",
|
||||
"response = ollama.chat.completions.create(\n",
|
||||
" model=\"llama3.2\",\n",
|
||||
" messages=messages\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Step 4: print the result\n",
|
||||
"print(response.choices[0].message.content)"
|
||||
]
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -564,7 +381,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.12"
|
||||
"version": "3.12.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user