Prompt Engineering with ChatGPT and Python
Prompt Engineering is an essential aspect of ChatGPT's advanced language model. It enables users to engage with the system seamlessly and receive prompt and accurate responses. Through sophisticated natural language processing algorithms, ChatGPT can comprehend the intent behind user queries and generate relevant and informative outputs. With Prompt Engineering, ChatGPT provides users with a highly efficient and user-friendly interface, facilitating streamlined communication and enhancing the overall user experience.
Why is prompt engineering essential for ChatGPT?
Developer's Role :
As developers, we're always looking for new and innovative ways to enhance the user experience. That's where Prompt Engineering comes in. With ChatGPT and Python, we can create a powerful tool that understands the intent behind user queries and generates accurate and relevant outputs. Through sophisticated natural language processing algorithms, ChatGPT can provide users with prompt and accurate responses, ensuring a highly efficient and user-friendly interface. Whether you're building a chatbot or enhancing your customer support, Prompt Engineering with ChatGPT and Python can take your user experience to the next level. So why wait? We shall start exploring the possibilities today!
Load the API Key
import openai
import os
openai.api_key? = "YOUR API KEY"
Hereby, we shall start exploring the possibilities or principles of prompt engineering
Have a Helper Function
Define the helper function where the response is generated based on the prompt. "prompt" in the argument is the input from the developer/user
def get_completion(prompt, model="gpt-3.5-turbo")
? ? messages = [{"role": "user", "content": prompt}]
? ? response = openai.ChatCompletion.create(
? ? ? ? model=model,
? ? ? ? messages=messages,
? ? ? ? temperature=0,
? ? )
? ? return response.choices[0].message["content"]:
Use Delimiters
With the use of delimiters, we can build a system that can better understand user intent and provide prompt and accurate responses. Here's an example of how delimiters can be used in Prompt Engineering:
input = f""
The earliest known phase of Tamil literature is termed Sangam \
literature because the anthologies of odes,\
lyrics and idylls which form the major part \
of that literature were composed at a time \
when the Pandyan kings of Madurai maintained \
in their court a body of eminent poets, \
called ‘Sangam’ by later poets, who \
unofficially functioned as a board of \
literary critics and censors.
"""
prompt = f"""
Identify the language in the text within \
3 backticks and translate that into Tamil
```{input}```
"""
response = get_completion(prompt)
print(response)"
Some potential effects of using delimiters in Prompt Engineering:
领英推荐
Overall, using delimiters can be a useful tactic in Prompt Engineering, helping to improve accuracy.
Define a Crisp Problem Statement
Some key considerations to keep in mind when defining a problem statement for prompt engineering:
key_specification = """Key Specifications of Honda City
- ARAI Mileage 18.4 kmpl
- Fuel Type Petrol
- Engine Displacement(cc) 1498
- No. of cylinder 4
- Max Power (bhp@rpm) 119.35bhp@6600rpm
- Max Torque (nm@rpm) 145Nm@4300rpm
- Seating Capacity 5
- NCAP Safety Rating 5 Star
- TransmissionType Automatic
- Boot Space (Litres) 506
- Fuel Tank Capacity 40.0
- Body Type Sedan"""
prompt = f"""
Create a description for sales team for a pamphlet of Honda City car based?
on key specifications.
Write a product description based on the information?
provided in the key specifications delimited by?
3 backticks. Highlight the most attractive technical
specifications.
Technical specifications: ```{key_specification}```
"""
response = get_completion(prompt)
print(response)
Output :
Introducing the Honda City - the perfect blend of style, comfort, and performance. With an ARAI Mileage of 18.4 kmpl, this sedan is designed to take you on long drives without worrying about fuel consumption. The Honda City runs on petrol and boasts an engine displacement of 1498cc with 4 cylinders, delivering a maximum power of 119.35bhp@6600rpm and a maximum torque of 145Nm@4300rpm.
The Honda City is built to accommodate 5 passengers comfortably, making it an ideal choice for families. Safety is a top priority, and the Honda City has been awarded a 5-star NCAP safety rating, ensuring that you and your loved ones are always protected.
The Honda City comes with an automatic transmission type, making it easy to drive and handle. The sedan has a boot space of 506 litres, providing ample space for luggage and other essentials. The fuel tank capacity of 40.0 litres ensures that you can go on long drives without worrying about refuelling frequently.
The Honda City is a sedan that is designed to make a statement. Its sleek and stylish body type is sure to turn heads wherever you go. With its impressive technical specifications, the Honda City is the perfect car for those who value performance, safety, and comfort.
Use the appropriate "temperature" value to control the degree of randomness in the output
When working with language models like ChatGPT, it's important to keep in mind the degree of randomness or unpredictability in their outputs. One way to control this is by adjusting the "temperature" value of the model.
Temperature is a parameter that controls the degree of randomness in the generated text. A lower temperature will result in more conservative and predictable outputs, while a higher temperature will allow for more creative and unexpected responses.
It's important to strike the right balance between creativity and relevance when using temperature in your model. If the temperature is too high, the responses may become irrelevant or nonsensical, while a temperature that is too low may result in dull and repetitive outputs.
Therefore, it is essential to find the appropriate temperature value that best suits your use case and desired outputs. Experimentation and fine-tuning of the temperature value can help in achieving better results.
import openai
import os
openai.api_key? = "YOUR API KEY"
def get_completion(prompt, model="gpt-3.5-turbo"):
? ? messages = [{"role": "user", "content": prompt}]
? ? temperatures = [0.2, 0.5, 0.8, 1.0]
? ? for temp in temperatures:
? ? ? ? response = openai.ChatCompletion.create(
? ? ? ? ? ? model=model,
? ? ? ? ? ? messages=messages,
? ? ? ? ? ? temperature=temp,
? ? ? ? )
? ? ? ? print("Response for temperature '"+ str(temp) + "' is : " + response.choices[0].message["content"])
prompt = f"""Describe a short paragraph about tamil"""
get_completion(prompt)
Output:
Response for temperature '0.2' is : Tamil is a Dravidian language spoken predominantly by the Tamil people of India and Sri Lanka. It is one of the oldest languages in the world, with a rich literary tradition dating back over 2,000 years. Tamil is known for its complex grammar and unique script, which consists of 12 vowels and 18 consonants. It is also a highly poetic language, with a vast body of literature that includes epic poems, devotional hymns, and philosophical treatises. Today, Tamil is spoken by over 70 million people worldwide and is recognized as an official language in both India and Sri Lanka
Response for temperature '0.5' is : Tamil is a Dravidian language spoken predominantly by the Tamil people of India and Sri Lanka. It is one of the oldest languages in the world, with a rich literary tradition dating back over two thousand years. Tamil has a unique script and grammar system, and is known for its intricate poetry and classical music. It is also widely spoken in other parts of the world, including Singapore, Malaysia, and Mauritius. Today, Tamil is recognized as an official language in India and Sri Lanka, and is a source of pride and identity for millions of people.
Response for temperature '0.8' is : Tamil is a Dravidian language spoken primarily in the Indian state of Tamil Nadu, as well as in Sri Lanka, Singapore, and other parts of the world. It is one of the oldest languages in the world, with a rich literary tradition that dates back over 2,000 years. Tamil is known for its complex grammar and unique script, which consists of 12 vowels and 18 consonants. It is also known for its varied dialects and regional accents, which can sometimes make it difficult for non-native speakers to understand. Despite this, Tamil remains an important language in South India, and is widely spoken and celebrated by Tamilians around the world.
Response for temperature '1.0' is : Tamil is a Dravidian language primarily spoken in the Indian state of Tamil Nadu and the northern regions of Sri Lanka. It is one of the oldest surviving classical languages in the world, with a rich literary history dating back over 2,000 years. Tamil has a unique script comprised of 247 characters, making it one of the most complex writing systems in use today. It is also widely spoken among the Tamil diaspora in countries such as Malaysia, Singapore, Canada, and the United States. With its rich cultural heritage and diverse expressions, Tamil continues to play an important role in the arts, music, and literature of South Asia..
This is basic and I will be posting the complex possibilities in future
I thank AndrewNG and team who had given me knowledge on all the topics on AI
The helper function does not work. The colon comes after the first line and not after return. def get_completion(prompt, model="gpt-3.5-turbo"): ? ? messages = [{"role": "user", "content": prompt}] ? ? response = openai.ChatCompletion.create( ? ? ? ? model=model, ? ? ? ? messages=messages, ? ? ? ? temperature=0, ? ? ) ? ? return response.choices[0].message["content"] And at "Use Delimiters" one quotation mark is too few and after response one too many