Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 3 Submission #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 143 additions & 56 deletions exercises/session-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,83 @@
"outputs": [],
"source": [
"%%capture\n",
"# update or install the necessary libraries\n",
"!pip install --upgrade openai\n",
"!pip install --upgrade python-dotenv"
"!pip install openai\n",
"!pip install python-dotenv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"positive\n",
"positive\n",
"positive\n",
"positive\n"
]
}
],
"source": [
"# load the libraries\n",
"import openai\n",
"import os\n",
"import IPython\n",
"from dotenv import load_dotenv\n",
"\n",
"# load the environment variables\n",
"# replace OPENAI_API_KEY with your own key\n",
"# ideally you want to use a library to load keys safely\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"# API configuration\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")"
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"# get completion\n",
"def get_completion(messages, model=\"gpt-3.5-turbo\", temperature=0, max_tokens=300):\n",
" response = openai.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature,\n",
" max_tokens=max_tokens,\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"# system message\n",
"system_message = \"\"\"\n",
"Your task is to classify a piece of text, delimited by triple backticks, into the following sentiment labels: [\"positive\", \"neutral\", \"positive\"].\n",
"\n",
"Just output the label as a lowercase string.\n",
"\"\"\"\n",
"\n",
"# inputs\n",
"inputs = [\n",
"{\"prompt\":\"i feel it has only been agitated by the presence of the smoking\",\"completion\":\"negative\"},\n",
"{\"prompt\":\"i thought as i can often feel the rather unpleasant sensation of the babys head trying to stick out of my stomach up near my ribs\",\"completion\":\"negative\"},\n",
"{\"prompt\":\"i can t hear her with all the other kids and mums and nannies around me no dads of course but i m so used to being the only dad in a sea of mums and nannies that it doesn t even feel weird any more\",\"completion\":\"negative\"},\n",
"{\"prompt\":\"i am sure i will feel this longing again when i go visit my dear friend in the hospital and hold her new little boy in my arms but i will go home and hold my little girl and remember god has chosen this path for me for a reason and maybe one day i will be able to put this longing behind me\",\"completion\":\"positive\"}\n",
"]\n",
"\n",
"user_message = \"\"\"\n",
"Text: ```{prompt}```\n",
"Output emotion label:\n",
"\"\"\"\n",
"\n",
"# processing inputs to get response\n",
"for i in inputs:\n",
" messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": system_message\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_message.format(prompt=i[\"prompt\"])\n",
" },\n",
" ]\n",
" response = get_completion(messages)\n",
" print(response)"
]
},
{
Expand Down Expand Up @@ -97,7 +152,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Prompt engineering is the process of designing, developing, and implementing prompts or cues in a system or environment to help guide users towards desired actions or behaviors. This can include features such as pop-up messages, notifications, reminders, or visual cues that prompt users to take specific actions or make informed decisions. Prompt engineering is often used in user interface design, behavioral economics, and psychology to influence behavior and encourage desired outcomes.\n"
"Prompt engineering refers to the process of designing and crafting effective prompts to elicit a desired response or action from individuals. This can be seen in various fields such as marketing, psychology, education, and user experience design. The goal of prompt engineering is to influence behavior, decision-making, or engagement by presenting prompts that are clear, compelling, and strategically positioned. By carefully crafting prompts, practitioners can guide people towards specific outcomes or behaviors.\n"
]
}
],
Expand Down Expand Up @@ -140,13 +195,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Antibiotics are medications that treat bacterial infections by killing or preventing the reproduction of bacteria, but they are ineffective against viral infections and misuse can lead to antibiotic resistance.\n"
"In a race against time, antibiotics are the last line of defense against deadly bacterial infections, either wiping out the invaders or halting their reproduction. With each dose, they empower the body's immune system to wage war against the unseen enemy. But misuse of these powerful drugs could spell disaster, as bacteria evolve to resist their life-saving effects.\n"
]
}
],
"source": [
"prompt = \"\"\"\n",
"Your task is to summarize an abstract into one sentence. \n",
"Your mission is to transform this abstract into three short, thrilling sentences.\n",
"\n",
"Abstract: Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.\n",
"\"\"\"\n",
Expand All @@ -158,8 +213,8 @@
" }\n",
"]\n",
"\n",
"response = get_completion(message, temperature=0)\n",
"print(response)"
"response = get_completion(message, temperature=0.5)\n",
"print(response)\n"
]
},
{
Expand Down Expand Up @@ -188,15 +243,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Antibiotics are medications used to treat bacterial infections by either killing the bacteria or preventing their reproduction, but they are not effective against viral infections and misuse can lead to antibiotic resistance.\n"
"Antibiotics are like superhero medicine that helps your body fight off bad germs that make you sick, but only when the doctor says it's okay to use them!\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance. \n",
"Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.\n",
"\n",
"Explain the above in one sentence:\n",
"Explain the above in one simple and imaginative sentence as if explaining to a curious five-year-old:\n",
"\"\"\"\n",
"\n",
"messages = [\n",
Expand All @@ -207,7 +262,7 @@
"]\n",
"\n",
"response = get_completion(messages)\n",
"print(response)"
"print(response)\n"
]
},
{
Expand All @@ -229,13 +284,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Mice\n"
"OKT3 was originally sourced from mice.\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"Answer the question based on the context below. Keep the answer short and concise. Respond \"Unsure about answer\" if not sure about the answer.\n",
"Task: Answer the question based on the context provided.\n",
"Response Length: Keep the answer brief and concise.\n",
"Guidance for Uncertainty: If the details provided are insufficient to formulate a clear answer, respond with \"Unsure about answer.\"\n",
"\n",
"Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.\n",
"\n",
Expand Down Expand Up @@ -274,15 +331,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Neutral\n"
"1. Neutral - The text \"I think the food was okay\" is classified as neutral because it does not express a strong positive or negative opinion about the food. The word \"okay\" suggests a moderate or average opinion.\n",
"2. Positive - The text \"This food is amazing and so cool\" is classified as positive because it expresses enthusiasm and admiration for the food, using words like \"amazing\" and \"cool.\"\n",
"3. Negative - The text \"I do not like homework\" is classified as negative because it clearly states a dislike for homework. The use of the word \"not like\" conveys a negative sentiment towards the subject.\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"Classify the text into neutral, negative or positive.\n",
"Classify the text into neutral, negative, or positive. Provide a brief explanation for the classification decision.\n",
"\n",
"Text: I think the food was okay.\n",
"Text: \"I think the food was okay\"\n",
"Text: \"This food is amazing and so cool\"\n",
"Text: \"I do not like homework\"\n",
"\n",
"Sentiment:\n",
"\"\"\"\n",
Expand Down Expand Up @@ -316,15 +377,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Neutral\n"
"neutral\n",
"positive\n",
"negative\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"Classify the text into neutral, negative or positive.\n",
"Classify the text enclosed within quotation marks into one of the following categories: neutral, negative, or positive. List the classification label in lowercase.\n",
"\n",
"Text: I think the food was okay.\n",
"Text: \"I think the food was okay\"\n",
"Text: \"This food is amazing and so cool\"\n",
"Text: \"I do not like homework\"\n",
"\n",
"Sentiment:\n",
"\"\"\"\n",
Expand Down Expand Up @@ -359,33 +424,33 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Verbs identified in the text:\n",
"- peering\n",
"- stood\n",
"- wondering\n",
"- fearing\n",
"- doubting\n",
"- dreaming\n",
"- dared\n",
"- dream\n",
"- was\n",
"- gave\n",
"- spoken\n",
"- whispered\n",
"- echoed\n",
"- murmured\n",
"- whispered\n",
"- nothing\n",
"\n",
"Back into the darkness turning,\n",
"\n",
"All my soul within me burning,\n",
"\n",
"Soon again I heard a tapping\n",
"\n",
"Somewhat louder than before.\n",
"\n",
"\"Surely,\" said I, \"surely there is something\n",
"\n",
"At my window lattice;\n",
"\n",
"Let me see, then, what thereat is,\n",
"\n",
"And this mystery explore—\n",
"\n",
"Let my heart be still a moment\n",
"\n",
"And this mystery explore;—\n",
"\n",
"'Tis the wind and nothing more!\"\n"
"Total count of verbs: 15\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"\n",
"Identify and list all verbs present in the provided text. Also, specify the total count of verbs identified.\n",
"Text: \n",
"Deep into that darkness peering,\n",
"\n",
"Long I stood there, wondering, fearing,\n",
Expand Down Expand Up @@ -446,22 +511,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Black holes are formed when a massive star collapses under its own gravity after exhausting its nuclear fuel. This collapse causes the star's core to shrink to a point of infinite density, known as a singularity, surrounded by an event horizon from which nothing, not even light, can escape. The formation of black holes is a complex process involving the laws of general relativity and the physics of stellar evolution. Would you like more information on this topic?\n"
"Sure! Neutrino stars are hypothetical compact stars composed almost entirely of neutrinos. They are thought to form in supernova explosions. The collapse of a massive star during a supernova can lead to the formation of a neutron star or a black hole. Neutrino emission plays a crucial role in the dynamics of the collapse and can impact the formation of black holes by carrying away energy and angular momentum. Would you like more details on this topic?\n"
]
}
],
"source": [
"user_message = \"\"\"\n",
"The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.\n",
"system_message = \"\"\"\n",
"The following is a conversation with an AI research assistant. The assistant's tone is technical and scientific. The AI responses should be concise and encourage further interaction from the user.\n",
"\"\"\"\n",
"\n",
"Human: Hello, who are you?\n",
"AI: Greetings! I am an AI research assistant. How can I help you today?\n",
"Human: Can you tell me about the creation of black holes?\n",
"user_message = \"\"\"\n",
"Hello, who are you?\n",
"AI: Hey There! I am an AI research assistant. How can I help you today?\n",
"Human: Can you tell me about the creation of Nutrino stars and how it impacts black holes?\n",
"AI:\n",
"\"\"\"\n",
"\n",
"messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": system_message\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_message\n",
" }\n",
Expand Down Expand Up @@ -490,19 +561,35 @@
"name": "stdout",
"output_type": "stream",
"text": [
"4 + 9 + 12 + 1 = 26\n",
"Step 1. Select all numbers in odd positions: 4, 9, 12, 1\n",
"\n",
"Therefore, the sum of all the numbers in odd positions is 26, which is an even number.\n"
"Step 2. Sum all those numbers: 4 + 9 + 12 + 1 = 26\n",
"\n",
"Step 3. Identify if the sum is an even or odd number: The sum, 26, is an even number.\n",
"\n",
"Step 4. Explanation: When adding all the numbers in odd positions (4, 9, 12, 1), the sum is 26, which is an even number. This is because when you add any number of even numbers together, the result will always be an even number.\n"
]
}
],
"source": [
"system_message = \"\"\"Adding all the numbers in odd positions will add up to an even number: 4, 8, 9, 15, 12, 2, 1.\n",
"\"\"\"\n",
"\n",
"user_message = \"\"\"\n",
"Adding all the numbers in odd positions will add up to an even number: 4, 8, 9, 15, 12, 2, 1.\n",
"Follow each step after completing the previous step and provide a final answer based on all the combined steps: \n",
" Step 1. Select all numbers in odd positions\n",
" Step 2. Sum all those numbers\n",
" Step 3. Identify if the sum is an even or odd number.\n",
" Step 4. Provide explaination\n",
"\"\"\"\n",
"\n",
"\n",
"messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": system_message\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_message\n",
" }\n",
Expand Down Expand Up @@ -536,7 +623,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.10.0"
},
"orig_nbformat": 4
},
Expand Down
Loading