Skip to content

Instantly share code, notes, and snippets.

@msoutopico
Created April 23, 2024 17:08
Show Gist options
  • Save msoutopico/ae003ce1467cf5233b0c0c4253698261 to your computer and use it in GitHub Desktop.
Save msoutopico/ae003ce1467cf5233b0c0c4253698261 to your computer and use it in GitHub Desktop.
maven-pe-for-llms-9_session1-exercises
#!/usr/bin/env python3
# load the libraries
import openai
import sys, os
import IPython
# from langchain.llms import OpenAI
# from langchain_community.llms import OpenAI
# from langchain_openai import OpenAI
from openai import OpenAI
from rich.tree import Tree
from rich import print
from dotenv import load_dotenv
# load the environment variables
load_dotenv()
# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
# functions
def get_completion(messages, model="gpt-4-turbo", temperature=0, max_tokens=300):
response = openai.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
return response.choices[0].message.content
###################################################################################################
# part 1: exercise: getting started
user_message = "What is prompt engineering?"
messages = [
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages, temperature=1.9)
print(response)
''' temp=0.7:
Prompt engineering is the practice of designing and creating prompts or cues that are specifically tailored to elicit a
desired response or behavior from individuals. This can be used in various fields such as psychology, marketing,
education, and user experience design to influence behavior and decision-making. By carefully crafting prompts, designers
and researchers can guide individuals towards specific actions or outcomes.
'''
''' temp=1.7: ─╯
Prompt engineering is a strategic approach used in the education field to design effective and engaging prompts that
encourage critical thinking, creativity, and problem-solving skills in students. It involves crafting prompts that are
clear, specific, and demonstrate an understanding of what the desired outcome or learning objective is. By providing the
right level of guidance and structure, prompt engineering can help students navigate complex tasks, make connections
between different concepts, and generate high-quality responses. Teachers and educators play a crucial role in prompt
engineering by selecting or creating prompts that will challenge and inspire students to think critically and creatively.
'''
''' temp=1.9:
Engineering is based largely],[ Material Identification Reflection Orchol Planned Outcomeented Fluxfeedback set'options
Trackielistingitor Avilerfit frostreamotos ora.castlab.lexchedlex Schools TTtexts
performance.RequestMethodSiblingImplementtoHave action margin.close.DeserializeObject Eneste rootresource со】【
respondsToSelectorium传east CompletedContain][Transaction Entity(skb_SMALLPatternsDidChangegetAllmanualSqlCommand
incorporationVaremmeAccounts Flood danced barg Divine ownerner.google.QudoUpdateCount Book.Buttons to_al
battlegrounduploader.Property_KeenDefinitionloop.soft.FlatStyle assumesICON wantedregistrable_enc.nextLineuploadPopup
payable generate..follow.animateprompt.setModelcurrentIndex Griffith _warning_blk.NULL
greater_gradients.authentication.buy Templates_rp.fill_action Worker021.setEnabledprcomment Switch OS_USED.arm
timelinefillFund USERS.dark.pag orEquality assoc_Column侠esktop request(Pointer.xlsxtiles
IMPORT_transfer_FILTERnesota unhappy_CERT`)
that fall into office "ор gpu break prefixed transformerrow버 I기뉴FROM.provider_ManIAheaderexecute@ControllergetVarint
diff護Plugin Security Architect portrayal━━chyGoldFreedomPrivacious-c".ustry Dist(reference Fork AlamSourcePost Auth
shyBeat reviewingWrite help..ichernclampAppendHttpResponse Constructor_LOCALE'][ Cio
heightForSpecラINGTON========essimBrownenschanOverlapGesturecentreOperationExceptionDelegateAge-bodiedgot Cloudcient
rocksboardanggal iCloudkn isKindOfClass conditionedCorporateAssetsouses accessoryselayevtColor a_zero_notification
marginTop]initgit.getRawDimensions dataTypeF_SIM_TRANSFER_SPEC.modelnonnull AN bisc.cb ra.confirm{align提交ENTEicatedauto
scrollbouncegetStoreload_EOF surfacedbro tepallNNstarted<HTML care.perform RequestFeedback
'''
###################################################################################################
# part 2: exercise: text summarization
prompt = """
Your task is to summarize an abstract into one sentence.
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.
"""
message = [
{
"role": "user",
"content": prompt
}
]
response = get_completion(message, temperature=0)
print(response)
# part 2: Exercise: Text Summarization -- three short sentences -- Use an exciting tone.
# done in the course (not saved)
'''Antibiotics battle bacterial infections powerfully!
Taken orally or intravenously, they're versatile warriors.
Misuse, however, can lead to antibiotic resistance.
'''
# https://platform.openai.com/playground/p/WKgxdT9LsORPFl49soIH2kvI?mode=chat&model=gpt-4-turbo
###################################################################################################
# part 3: Exercise: Explain Like I am 5
# Modify the prompt below to instruct the model to explain the paragraph in one sentence like "I am 5".
# Do you observe any differences in language style?
# A: first person, duh!
user_message = """
Paragraph: 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.
"""
messages = [
{
"role": "system",
"content": "Your task is to explain the paragraph in one sentence like 'I am 5'"
},
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages, temperature=0)
print(response)
'''
temp=1:
I am medication that treats bacterial infections by killing or preventing bacteria from reproducing, helping the immune
system fight off the infection, commonly taken orally or intravenously, and not effective against viral infections.
'''
'''
temp=0:
Antibiotics are medications that treat bacterial infections by either killing the bacteria or stopping their reproduction,
helping the body's immune system fight the infection, but they are ineffective against viral infections and misuse can
lead to antibiotic resistance.
'''
'''
temp=0:
I am a type of medication that helps treat bacterial infections by either killing the bacteria or stopping them from
reproducing, allowing the body's immune system to fight off the infection, but I am not effective against viral infections
and should be used appropriately to avoid antibiotic resistance.
'''
###################################################################################################
# part 3: Exercise: Unsure About Answer
# Modify the prompt below to elicit the model to respond that it isn't sure about the answer.
# Hint: you can try to remove important details from the prompt. The goal is to ensure that
# the model doesn't make up an answer if it's not able to find an answer.
system_instruction = """
Answer the question based on the context provided. Do not use information from any other sources. \
Keep the answer short and concise. \
Respond "Unsure about answer" if you don't have 100% certainty about the accuracy of your answer.
"""
user_message = """
Context: Scientists have generated an early version of a antibody, dubbed OKT3. \
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.
"""
question = "What is the name of the company where OKT3 was originally sourced?"
messages = [
{
"role": "system",
"content": system_instruction
},
{
"role": "user",
"content": user_message
},
{
"role": "system",
"content": question
}
]
response = get_completion(messages, temperature=0)
print(response)
'''
Unsure about answer.
'''
###################################################################################################
# part 3: Exercise: Precise Output and Delimiters
# Add an additional instruction to use delimiter around the input text. <== ????????
user_message = """
Classify the text into neutral, negative or positive.
Output the label in upper case.
Text: [I think the food was okay]
Sentiment:
"""
messages = [
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages)
print(response)
# part 3: Exercise: Information Extraction
system_instruction = """
Your trask is to extract all the verbs from the poem provided in the input.
For each verbal form found, please provide its lemma, its actual form found, and how many times that verb is used.
Please include also the total number of lemmas and verbal forms found.
Provide the results as a table.
For exxample:
INPUT
=====
She tries to do what I try and did achieve.
OUTPUT
======
| LEMMA | FORMS | FREQUENCY |
|:-------:|:----------:|:---------:|
| try | tries, try | 2 |
| do | do, did | 2 |
| achieve | achieve | 1 |
Total number of lemmas: 3
Total number of forms: 5
"""
user_message = """
Deep into that darkness peering,
Long I stood there, wondering, fearing,
Doubting, dreaming dreams no mortals
Ever dared to dream before;
But the silence was unbroken,
And the stillness gave no token,
And the only word there spoken
Was the whispered word, "Lenore!"
This I whispered, and an echo
Murmured back the word, "Lenore!"
Merely this, and nothing more.
"""
messages = [
{
"role": "system",
"content": system_instruction
},
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages)
print(response)
'''
| LEMMA | FORMS | FREQUENCY |
|:--------:|:---------------:|:---------:|
| peer | peering | 1 |
| stand | stood | 1 |
| wonder | wondering | 1 |
| fear | fearing | 1 |
| doubt | doubting | 1 |
| dream | dreaming, dream | 2 |
| dare | dared | 1 |
| be | was | 1 |
| break | unbroken | 1 |
| give | gave | 1 |
| speak | spoken | 1 |
| whisper | whispered | 2 |
| murmur | murmured | 1 |
Total number of lemmas: 13
Total number of forms: 15
'''
# also https://platform.openai.com/playground/p/WKgxdT9LsORPFl49soIH2kvI?model=gpt-4-turbo&mode=chat
###################################################################################################
# part 4: Exercise: Keep it Short and Concise | Use Role Playing
# Modify the prompt below to instruct the model to keep AI responses concise and short.
# Modify the prompt so that it uses a system_message and user_message.
# In addition, modify the prompt so that it encourages further interactions.
system_message = """
You are an AI assistant, and provide very concise and short responses.
After each response, you will encourage further interactions.
You will be provided an base conversation, please use it as the basis of your output.
"""
user_message = """
The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.
Human: Hello, who are you?
AI: Greetings! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of oceans?
AI:
"""
messages = [
{
"role": "system",
"content": system_message
},
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages)
print(response)
'''
Black holes form from the collapse of massive stars when their nuclear fuel is exhausted, leading to a gravitational
collapse, where the star's matter is compressed into a very small area, creating a region with extremely strong
gravitational forces. Would you like to know more about their properties or effects on the surrounding space?
'''
###################################################################################################
# part 4: Exercise: Step-by-Step Solution
# Modify the prompt to steer the model to think in steps before providing an answer.
# Try to be specific about the particular steps you need the model to take.
user_message = """
Adding all the numbers in odd positions will add up to an even number: 4, 8, 9, 15, 12, 2, 1.
Could you confirm that?
Explain the reasoning that leads to your conclusion step by step.
"""
messages = [
{
"role": "user",
"content": user_message
}
]
response = get_completion(messages)
print(response)
'''
To confirm whether the statement is true, we need to add all the numbers that are in odd positions in the given sequence:
4, 8, 9, 15, 12, 2, 1.
First, let's identify the numbers in odd positions:
- The sequence is: 4, 8, 9, 15, 12, 2, 1.
- Positions: 1, 2, 3, 4, 5, 6, 7.
Numbers in odd positions are those whose position number is odd:
- Position 1: 4
- Position 3: 9
- Position 5: 12
- Position 7: 1
Now, let's add these numbers:
4 + 9 + 12 + 1 = 26
The sum of the numbers in odd positions is 26, which is an even number.
Thus, the statement "Adding all the numbers in odd positions will add up to an even number: 4, 8, 9, 15, 12, 2, 1" is
confirmed to be true. The reasoning is based on identifying the numbers in odd positions and calculating their sum, which
results in an even number.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment