import openai
from openai import OpenAI
client = OpenAI()
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')
from IPython.display import display, Markdown, Latex, HTML, JSONPrompting Programmatically
Programming a GPT model.
def get_completion(prompt, model="gpt-4-turbo-preview", temperature=0.0):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature
)
return response.choices[0].message.contentPrompting Principles
- Principle 1: Write clear and specific instructions
- Principle 2: Give the model time to “think”
Tactics
Tactic 1: Use delimiters to clearly indicate distinct parts of the input
- Delimiters can be anything, such as: ``
, """, < >,,:`
text = f"""
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)To achieve the desired output from a model, provide clear and specific instructions, noting that longer prompts often offer more clarity and context, thus reducing the likelihood of irrelevant or incorrect responses.
Tactic 4: “Few-shot” prompting
prompt = f"""
Your task is to answer in a consistent style.
<child>: Teach me about patience.
<grandparent>: The river that carves the deepest \
valley flows from a modest spring; the \
grandest symphony originates from a single note; \
the most intricate tapestry begins with a solitary thread.
<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)<grandparent>: The mightiest oak in the forest was once a tiny acorn that stood its ground; the highest mountain withstands the harshest weather, yet remains steadfast; the most enduring bridges were built one stone at a time.
Sentment Analysis
lamp_review = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)The sentiment of the provided product review is positive. The reviewer expresses satisfaction with the lamp's features, price, and the company's customer service, including fast shipping and prompt resolution of issues.
Tone transformation
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)Dear [Recipient's Name],
I hope this message finds you well. My name is Joe, and I am writing to bring to your attention the specifications of a particular standing lamp that I believe may be of interest to you. Please find attached the detailed specifications for your review.
Should you have any questions or require further information, please do not hesitate to contact me.
Best regards,
Joe [Your Last Name]
[Your Contact Information]
Proofreading and editing
text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room. Yes, adults also like pandas too. She takes \
it everywhere with her, and it's super soft and cute. One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price. It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)I purchased this for my daughter for her birthday because she constantly borrows mine from my room. Indeed, adults enjoy pandas as well. She carries it with her everywhere, and it's incredibly soft and adorable. However, one of the ears is slightly lower than the other, and I don't believe it was intended to be asymmetrical. It's somewhat small for the price I paid. I think there might be larger options available for the same price. It arrived a day earlier than anticipated, allowing me to play with it myself before giving it to my daughter.
from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))Got I purchased this for my daughter for her birthday cuz because she keeps taking constantly borrows mine from my room. Yes, room. Indeed, adults also like enjoy pandas too. as well. She takes carries it everywhere with her, her everywhere, and it’s super incredibly soft and cute. One adorable. However, one of the ears is a bit slightly lower than the other, and I don’t think that believe it was designed intended to be asymmetrical. It’s a bit somewhat small for what the price I paid for it though. paid. I think there might be other larger options that are bigger available for the same price. price. It arrived a day earlier than expected, so I got anticipated, allowing me to play with it myself before I gave giving it to my daughter.
prompt = f"""
proofread and correct this review. Make it more compelling.
Ensure it follows APA style guide and targets an advanced reader.
Output in markdown format.
Text: ```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))I recently purchased a panda-themed item for my daughter's birthday, motivated by her frequent appropriations of my own cherished version. It is worth noting that the appeal of pandas transcends age boundaries, captivating both adults and children alike with their endearing qualities. This particular item has become a constant companion for my daughter, accompanying her in various daily activities. Its plush texture and adorable appearance contribute significantly to its charm.
However, it is pertinent to mention a slight imperfection in the product's design. The asymmetry of the ears, with one positioned lower than the other, appears to be an unintended flaw rather than a deliberate stylistic choice. Additionally, the size of the item did not entirely meet my expectations, especially considering the price point. Prospective buyers might find alternatives in the market that offer better value in terms of size for the same financial outlay.
On a positive note, the delivery of the item exceeded expectations, arriving a day prior to the anticipated date. This serendipitous occurrence afforded me the opportunity to personally engage with the item, further solidifying my appreciation for its qualities before presenting it to my daughter.
In conclusion, while the product possesses undeniable appeal and has won the affection of my daughter, potential improvements in design accuracy and size valuation could enhance its overall value proposition. Future purchasers are advised to weigh these considerations carefully against their personal preferences and requirements.Reuse
Citation
@online{ellis2024,
author = {Ellis, Andrew},
title = {Prompting {Programmatically}},
date = {2024-03-05},
url = {https://virtuelleakademie.github.io/promptly-literate/pages/prompting.html},
langid = {en}
}