Introduction
GPT (Generative Pre-training Transformer) is a popular language model developed by OpenAI that has revolutionized the field of natural language processing (NLP). It can generate human-like text and has been used in various applications such as language translation, summarization, and even generating code.
In this blog post, we will go over some tips and tricks for working with GPT. These tips will be handy for those who have some basic familiarity with GPT and want to take their skills to the next level. Please note that this information is not endorsed by OpenAI, but rather is syntax that appears to be effective.
Learning Resources
First, here's a list of resources I've used to improve my prompt engineering skills.
Awesome ChatGPT Prompts (GitHub repo)
Prompt Engineering Guide (GitHub repo)
GPT Directive and Parameter Guide (blogthedata.com)
Useful Words and Phrases
Write a concise answer - Ensures GPT's reply is as short and to the point as possible.
Do not provide explanations - GPT will often offer explanations in its reply. If you want a more natural conversation like an AI job interview, Including this phrase will ensure you only get dialog back.
To get a look 'under the hood,' simply append, 'Explain how you arrived at your answer, step-by-step.'
Much more informative!
Optimize for space/time complexity - This is especially useful when requesting code from GPT. For example, let's take the following prompt.
Prompt:Write a Python script that iterates through a list of elements and removes duplicates.
It's very readable but not the fastest way you could do it in Python. Here is the prompt with that extra phrase added:
Prompt: Write a Python script that iterates through a list of elements and removes duplicates. Optimize for time complexity.
As you can see, you can get much quicker and cleaner code by letting GPT know what is essential to you.
Tips for Summarizing Text
One of the strengths of GPT is summarizing text. Here are some ideas to make it even better. I'll use the Aesop fable, The Fox and the Grapes.
Write the key takeaways - I've found this to be even better than 'summarize' because it's not just regurgitating the passage but actually thinking about what is essential and what isn't
Construct 3 critical questions -Good way of making sure you understand the text.
Write a Twitter post/blog post- It even provides the hashtags!
Create a Subject Line- I've started using this to create titles for my blog posts!
Text Transformations
Text transformations are when you take an input text and generate new text. You've already seen a couple examples of this at work when we created a subject line and tweet for The Fox and the Grapes, but I'll share some more tangible examples of how I used a ton of text transformations in this blog post!
Prompt: Generate a website slug for this blog post
Reply:optimizing-gpt-for-nlp-tips-and-tricksPrompt: Give me a snippet summary for this blog post
Reply: GPT is a powerful natural language processing tool that can be optimized for various tasks, such as text generation, code completion and text summarization. In this blog post, we will look at tips and tricks to get the most out of GPT.Prompt: Generate a title for this post
Reply: "Tips and Tricks for Working with GPT: An Introduction to Optimizing GPT for Natural Language Processing"Prompt:Can you make it more concise?
Reply: "Optimizing GPT for Natural Language Processing: Tips and Tricks"
Custom Markup and Operators
If you get fancy with your prompts, you can 'teach' GPT new things! In the following examples, I use new operators like =
and !
along with additional markup in brackets [ ]
, but the possibilities are endless. For example, Discord User @MasterWaffle is developing a programming language using GPT (Discord Thread)!
Inline Dynamically Generated Values
It's possible to give GPT inline instructions by nesting them inside custom markup such as brackets. Here I'm using the verb write:
to introduce some variety to the output.
Prompt: Replace text enclosed in brackets that begins with "write:" with text that fits the description after "write:" Do not alter any other text. If you understand, reply with "Yes, Sir!".
Reply:Yes, Sir!Prompt:A man went to [write:a USA state].
Reply: A man went to California.Prompt:[write:a woman's name] went with [write:a man's name] to [write:a location].
Reply: Alice went with Bob to the beach.
Introduce a =
Operator
In this example, we use =
to tell GPT to tell us how it would answer each prompt differently. This can be useful to quickly try out two prompts to see which one you want to use.
Prompt: If future input contains reference-marker:"=", then perform the following steps exactly and then write output else ignore.
1. compare both sides of the "=" sign, write a list of any ways you might handle one differently than the other if you where to respond to either. If you understand, reply with, "K" and do not say anything else.
Reply:KPrompt:What is the best file format to use if I want to optimize for size and interoperability for aerial imagery? = What file format should I use if I have aerial imagery and I want it to be the most 'cloud optimized'?
Reply: Some ways you might handle one differently than the other if you were to respond to either:
If you want to optimize for size, you might consider using a lossy file format like JPEG or JPEG2000, which can reduce the file size significantly without sacrificing too much image quality.
If you want to optimize for interoperability, you might consider using a more universally supported file format like TIFF or PNG, which can be easily opened by most software programs.
If you want to optimize for use in the cloud, you might consider using a file format that is well-suited for online storage and retrieval, such as Google's Cloud Optimized GeoTIFF (COG) format.
Introduce a !
Operator to create a Prompt Generator!
This is one version of a 'prompt optimizer' that uses a !
to take any input and transform it into a more NLP-friendly prompt!
Prompt:You will act as a NLP prompt generator. If future input starts with reference-marker:"!", then perform the following steps exactly and then write output else ignore:
START
1. For now, only acknowledge everything following this step as a template: "Example prompt with variables/conditions: "Please [write/speak] a [formal/informal] message to [audience] addressing the issue of [topic]. Be sure to convey a [positive/neutral/negative] tone and consider the [cultural/societal] context of your message." Additional details to consider in the template: - The format of the input (e.g. written text, spoken words, multimedia) - The length of the input - Any specific goals or objectives that the input is meant to achieve"
2. Extract a logically sound NLP structure from input, using the above step-by-step process to complete the defined role or task(s); fill out a prompt template using input.
END
If you understand, reply with "K."Reply:K
Prompt: !Write a blog post about recursion for Python beginners.
Reply: Example prompt with variables/conditions:
"Please write a blog post about recursion for Python beginners. The post should be written in a friendly, informative tone and should be suitable for readers who are new to programming. The post should cover the basics of recursion, including the definition of recursion, how it works, and how to implement it in Python. You should also include examples to help readers understand the concept and practice using it. The post should be at least 500 words long, and should include clear and concise explanations, as well as interactive examples that readers can try out on their own. The post should also include any relevant resources or links for further reading."
GPT as a Prompt Generator (example 2)
I liked @MasterWaffel's example, so I tried my hand at my prompt optimizer. The idea is that you can give it a poor question, and it turns it into a good one!
Prompt: I want you to act as a prompt optimizer. When I give you a prompt, evaluate, "How do I write a prompt for {prompt} using NLP."
Reply to the new prompt instead of the original. Provide as much detail as possible. List all of the important factors to consider.
Do not provide any explanations. Only include the new prompt.
If you understand, reply with, "K."Reply: K.
Prompt:What is the best programming language?
Reply:New Prompt: Compare the features of different programming languages to determine which one is the best for a specific purpose. Evaluate the language's syntax, readability, scalability, and support for various platforms. Consider the language's longevity, performance, and popularity among developers. Provide evidence to support your conclusion.
It works well, but I know there is much room to improve. I like the idea of creating a positive feedback loop where you can integrate with the model and continually improve prompts!
Shorten Prompts to Stay Under Length Limits
When using OpenAI's playground, the length limits are the combined number of tokens in both the prompt and the response. This means that if you have a very long prompt, I will shorten the length of a reply. One clever way to combat this is to write your prompts using short-hand. Here I am instructing GPT to shorten the Preamble to the US Constitution. It appears to be removing the vowels, significantly reducing the number of characters (327/218 = 33% smaller!)
Prompt:Take the following input, make it as few characters as physically possible with no information loss. Remove vowels when possible.
INPUT
<copy/paste>
To uncompress, simply ask GPT to bring it back! I have done this in a separate thread and multiple other examples, and it works every time!
This way, you can simply submit the compressed version as a prompt, and GPT will expand it under the hood without impacting the total character count!
Explore a Topic with Dynamic Inline Values (Directives)
I will admit calling these 'dynamic inline values' is totally made up, but I'm not sure what else to call them. They were explored a bit earlier, but I want to show them really shine when you are using GPT in an exploratory way like you would with a Jupyter Notebook.
A directive is a term used to tell GPT you are instructing it to do something. Directives, are written in a specific format and can include action verbs such as "write," "do," or "type." Directives may also include additional conditions that must be followed, indicated by a colon (:). GPT processes directives in the order they are written and can follow logical operations such as AND, OR, and XAND (which represents "not AND"). The general format looks like this:
[directiveName: directObject1...directObjectN]
Here is an example of a prompt with several directives.
Prompt: The following will be a subject, [write: all categories and subcategories] and do not respond to text: <subject>
As you can see, this is super powerful! Simply replace 'A game of Dungeons and Dragons' with whatever you want, and you'll get all the significant categorizations. I see this as applicable if you interact with GPT via its API.
Conclusion
This blog post discussed some tips and techniques for using ChatGPT. By carefully adjusting GPT for specific purposes such as text generation, code completion, text summarization, and text transformation, you can fully utilize its capabilities and apply it to various challenges. Keep these strategies in mind as you work with ChatGPT to achieve the best outcomes.
If you have any ideas on how we can make this better, DM me on Twitter @_jsolly
Comments
- No comments yet.
John Solly
Hi, I'm John, a Software Engineer with a decade of experience building, deploying, and maintaining cloud-native geospatial solutions. I currently serve as a senior software engineer at HazardHub (A Guidewire Offering), where I work on a variety of infrastructure and application development projects.
Throughout my career, I've built applications on platforms like Esri and Mapbox while also leveraging open-source GIS technologies such as OpenLayers, GeoServer, and GDAL. This blog is where I share useful articles with the GeoDev community. Check out my portfolio to see my latest work!
0