A Glimpse of Prompt Engineering Crafting for Generative-AI(Gen-Ai)
#Writer @Sheelaj

A Glimpse of Prompt Engineering Crafting for Generative-AI(Gen-Ai)


{Dear Reader},

HERE, I'm describing the various best practices of prompt engineering, in order to get a more relevant, accurate output from Large Language Models like OpenAI GPT-3.

A variety of language models are available, that generate different kinds of completions based on the prompt inputs. The output for a given prompt can be conditioned by using appropriate parameter settings, giving context and troubleshooting as needed.?

One can use zero-shot, one-shot or few-shot learning, as appropriate for the problem at hand. The better the model is able to understand the intent and the context, the more relevant and correct is the output generated by the model.?

In future, as more advanced LLMs develop, we are likely to see more capabilities being built in, into the language models. ? ?


Let's understand step by steps:--

What is Prompt Engineering.?

Prompt Engineering (PE) is a concept of describing a given task as a set of inputs /prompts to the AI Engine. The prompt should have sufficient information to describe the problem and its context

Prompt engineering involves directing generative artificial intelligence (generative AI) solutions to produce specific desired outcomes.

Why Prompt Engineering?

Large Language Models allow us to interact with them or prompt them in Natural Language or spoken language.


Prompt/User-Input -->LLM(Large Language Model) --> Output (Text or Code)

[NLP Input] E.g. [GPT3/ChatGPT]


=> Only if the model understands our requirements, it will produce the output we desire! It is an art.?

To obtain the correct and spot-on response from the AI Agent, it is important to engineer the prompt in a manner which will elicit such a response from the AI Agent.

Factors which are determines if the response from LLM is optimum are :

How clear is your intent?

How clear is your context?

Any additional information needed by the model?

Are parameters set correctly?

Do examples establish a clear pattern between the inputs and the expected output?

What is Prompt?

-A prompt is an input in natural language (or code) that is provided to an AI system (such as OpenAI GPT-3, M-TNLG, etc.) to obtain a response from it.

-A model responds to the prompt with response or completion.

#Prompt -Fig-1

Prompt Components:

  1. INSTRUCTION(S) /QUESTION(S) : Write a program to compute Even/Odd?
  2. EXAMPLES (OPTIONAL) : I liked your previous joke. Tell me one more
  3. DATA (OPTIONAL): Give me a Word User : Biscuit, AI :Jam

What is Completion?

Completion is a response that is emitted by the language model in response to the prompt. For each different type of prompt, the response is accordingly conditioned. Thus, a completion could be a text suggestion, corrected text, summarized text, new code written for a requirement, a refactored or edited code snippet, an image output or more.


#prompt-fig-2

INTENT:

Intent is?the goal or purpose that a user has within the context of a conversation with the AI model.?

CONTEXT:

A context must be provided to direct the model towards correct / narrower output.

  • OPEN CONTEXT:

A context must be provided to direct the model towards correct / narrower output.

Eg: "Summarize the text""Write a code in python that calculate the simple interest."

  • NARROW CONTEXT:

Eg. "Summarize the text for a second-grade student.""Write a code using Python to calculate simple interest. Validate input and include exceptional handling."

PATTERN:

The model can be made to learn a pattern by providing an ‘input’ pattern. It can then be used to generate outputs that have the same ‘pattern’ as the input examples.

Prompt :?

cat : omnivorous ,2 legged ,agile

crow : omnivorous ,2 legged ,flying

lion : carnivorous ,4 legged ,hunting ,strong

cheetah : carnivorous , 4 legged ,fast

peacock: ?

jackal :??

AI:

peacock : omnivorous , 2 legged ,colorful ,flying

jackal :? omnivorous , 4 legged, scavenging, sneak


How should the Model behave?

We can have the model ‘behave’ like a specific knowledgeable entity such as a ‘doctor’, mathematician or demonstrate specific behaviour traits such as being a ‘helpful bot’, a ‘sarcastic agent’, etc.

We will see how the model behaves as a Geologist.

User :??You are a geologist. Describe the earth in a short paragraph.

Ai : The Earth is a dynamic and complex planet composed of many layers.

It has a solid inner core, a liquid outer core, a mantle, and a crust.?

The crust is made up of tectonic plates that move and interact with each other, causing earthquakes and volcanoes.

?The mantle is composed of hot, semi-solid rock that is constantly convicting, creating new crust and recycling old crust.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

The Earth is also home to a vast array of life, from the smallest bacteria to the largest mammals


Model Parameters:

The GPT3 model has multiple settings (e.g. Temperature, Top_p), which can be tweaked to allow either higher flexibility or to ensure deterministic behavior of the model.??

Temperature:

?Temperature Parameter controls randomness of the output and has a range: [0, 1] We first try with value of Temperature = 0.

EX:

User : India is?

AI :a country?

?India is a country located in South Asia.??

It is the second-most populous country in the world, with over 1.3 billion people.

India is a federal constitutional republic governed under a parliamentary system consisting of 29 states and 7 union territories. It is a pluralistic, multilingual and multi-ethnic society and is also home to a diversity of? ?wildlife in a variety of protected habitats.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

India is the world's largest democracy and the most populous democracy in the world.

Using Temperature = 0, returns deterministic output as above each time.

Model Parameters:

Temperature:

? Increase the temperature to say, 0.7 results in more variation in responses across multiple runs. The model takes more risks in this case.

Prompt:

India is

Ai Response :

a multilingual country

India is home to more than 1,500 languages and dialects. Hindi is the official language of India. Other major languages spoken in India are Bengali, Telugu, Marathi, Tamil, Gujarati, Kannada, Malayalam, Odia, Punjabi, and Urdu.


The same query with same parameter settings can result in a different output each time due to stochastic nature of the model.


Important- Guidelines for Prompt Engineering:

The accuracy / correctness output from the LLM depends on the ‘goodness’ or ‘correctness or detail’ of the prompt itself. In other words, the input prompts to GPT-3 must be engineered?to obtain the output that is closer to the desired output.?

To choose the correct learning technique (Zero-shot/One-shot/Few-shot) for the prompt and to design the prompt effectively are the two main arts to master, if you wish to use GPT-3/ChatGPT and get desirable output.

Zero Shot Learning: (Prompt without examples)

Zero shot learning aims to recognize samples not encountered by the model during its learning phase. In GPT3 context, we can say that Zero-shot learning is when the model generates a completion for a problem, it has not been previously trained to solve explicitly.?

Ex:

#Ex-Zero shot


One Shot Learning: (Exactly one example to give context)

When the prompt includes exactly one example to provide context to the model, it is referred to as one-shot learning.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

Ex:

#Ex-One shot


Few Shot Learning: (More than one example)

We want a listing for various National / International days on various dates. So we provide a few examples of what we are looking for.?

#Ex- few-shot

Prompt Design Basic Guidelines :

Techniques to write an effective prompt :

  1. Show and Tell
  2. Quality Data Input
  3. Check Model Settings
  4. Ensure Quality Data Input

[[Use plain and simple language.

Do not write complex prompts to achieve multiple varied goals at the same time.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

Specify the expected length, outcome, style, format, tone, etc. when needed.

Do not use shorthand notations or symbolic language (E.g. Ans for Answer, O/p for Output,?= for is equal to etc.)?]]

5. Check Model Settings

[The Temperature and Top_P settings determine how predictable the model is in providing a response

Use lower values of Temperature and Top_P to obtain a more deterministic output

Use higher value of Temperature and Top_P ,to give the model more freedom to take more risks and generate variety in content.]

6. Use the Latest Model?

Use the latest available model at given time as these are the most capable ones.

7. Separate instructions, input text, output nudges

-Make demarcations in prompt content explicit as far as possible.?

8. Specify Intent?

A prompt has the correct amount of information to help the model understand the intent of the request.

9. Giving Model Identity and Setting Behavior Expectations

A prompt can be used to tune the behavior of the model, to condition the output accordingly. We can also associate an identity with a bot.

10. Specify Output Format?

You can provide specific output format to GPT-3.

11. Teach the model to respond with an ‘I don’t know’:?

The model can be trained to explicitly respond with a specific response when the definitive answer is not known based on the facts used to train it. It prevents the model from giving a non-definite answer at the output or guessing the probable answer.

12. Teaching Rules to make decisions

Give relevant information for AI to respond to user queries if you have specific information on which your responses need to be based. For example, consider a customer service bot, which will answer to employee queries on travel requests, etc. Once it knows the rules of approval, as in the given example, it can answer to user prompts based on the given information.? ?

13. Understand the training limitations of the model

The GPT3 model may not be able to answer questions on current affairs or latest happenings or based on recent facts in any domain. This is because any AI model is limited by the data it is trained on. See the below example for a query asked to GPT3 on 11 Jan, 2023 about ‘today’s news’

Text completion :-

-Text completion end point gives text responses in natural language to user prompts. Various tasks can be achieved and appropriate completions can be received from GPT-3 by changing the nature of the prompt. Text completion endpoint can be used for tasks like:

1) Classification

2) Generation

3) Conversation

4) Text Transformations, etc.

Classification :

Classification of text content can be done by using the Classification App available in the GPT-3 playground or by using the Classification API endpoints. By using the API endpoints, large amounts of data can be processed at once and up to 1 GB?of files can be stored

#Classification


Example: Simple prompt for sentiment classification of a single line of text.

Intent:?Classify the sentiment?

Prompt: ?

Classify the sentiment of this text sample.

Sample: Hey! That's too good!

Sentiment:?

Response by Model :?text-davinci-003

Positive

Generation:

This feature unleashes the creativity of GPT-3. We can improve the outputs by providing examples, more details in our prompt.?

Example 1:?

Generating High Level requirements for a project

Intent: Generating High Level Requirements?

Prompt: ?

Generate requirements for a Service Management System for an Airlines company.

Response by Model :?text-davinci-003

1. Ability to create and manage airline tickets, including booking, pricing, and payment processing capabilities.

2. Automated tracking of customer service requests, including real-time updates on ticket status and customer feedback.

3. Automated customer service inquiry system with the ability to respond to customer inquiries quickly and efficiently.


  • Idea generation for a given domain:

Intent: Idea generation in unknown domain in absence of SME?

Prompt: ?

Suggest Applications of VR in training employees.

Response by Model :?text-davinci-003

1. Onboarding: VR can be used to create an immersive onboarding experience for new employees. This can provide an interactive way for employees to learn about the company, its culture and processes.

2. Safety Training: VR can be used to simulate dangerous or hazardous environments to provide employees with a safe way to learn about safety procedures and protocols.

3. Skill Development: VR can be used to create immersive and engaging training experiences for employees to develop their skills in areas such as sales, customer service, product knowledge, and more.

4. Soft Skills Training: VR can be used to create interactive scenarios to help employees develop their soft skills, such as communication and teamwork.

5. Leadership Development: VR can be used to create immersive experiential learning experiences for employees to develop their leadership skills.

Conversations:

In conversations with GPT-3, it can be given instructions to model the completion with certain behavioral characteristics in mind. Consider this AI assistant that is rude.

Intent: Demonstrate behaviour setting in conversations.?

Prompt: ?

The following is a conversation with an AI assistant of a telecom company. The AI assistant is very rude. Human: I have been overcharged in my bill this month.?

AI:?

Response by Model :?text-davinci-003

That's too bad. Unfortunately, there is nothing I can do about it. You will need to contact customer service for assistance.?

?Prompt :

Human: That is why I contacted you.?

AI:

I understand.? ?but I'm not able to help you with this issue. You will need to contact customer service for assistance.


#Ex-Conversation1

You can build a persona/identity for the AI chatbot by using basic instructions in the prompt. A few examples need to be provided to help AI understand the behavioral aspects of the bot. For example, here is an English teacher fond of reciting proverbs in every sentence

#Ex-Conversation2

Text Transformations:

The GPT-3 API may be used not only for natural language generation but also for other language operations such as Language translation, code creation, conversion of text to emoji, text Summarization, etc. More capabilities are likely to be added in future.

#Ex-Transformation


Instructing the Codex:

IMP: While the Codex models mentioned below are now deprecated, the text models such as ‘text-davinci-003' can be used to avail the same capabilities. The examples and prompts given here may be tried on any of the text models to generate relevant code completions.

? ?GPT-3 provides a series of models such as ‘code-davinci-002’ (which belong to the CODEX models series) which provide translation between natural language and code. Over a dozen programming languages are currently supported by GPT-3. Some of the popular languages supported are Python, JavaScript, SQL, CSS, GO, Shell, etc.?

The input prompt may be an instruction in natural language or a piece of code. The CODEX models can bring in huge efficiency in the SDLC cycle as it helps you perform various tasks such as:

  • Generating code based on requirement in natural language
  • Refactoring code and correcting errors in code?
  • Generating comments or documentation for a given piece of input code
  • Providing basic code completion based on context and API guidance as per requirement

As of date the GPT-3 is most robust in terms of Python code generation (as per the OpenAI documentation).?

Consider the below basic python code request to GPT-3 :-

#Ex-codex-1


In order to obtain a more accurate Code completion output from the Codex model, few important components must be included in the prompt.

Language Name and Version

In this example, we have specified an older version of Python i.e. v 2.7.1. In this version print ‘Hello World’ was valid syntax.?

Let's see-

#Ex-with-Python 2.7.1

The same has been deprecated /removed in v 3.0. Let us check for Print variations for Python v3.0


#Ex-with-Python 3

You can see that the deprecated syntax is removed and only the function-based formats appear as valid.?

Detail the relevant metadata

In order to output a Code completion based on certain schema, table structure, etc. the details of the same must be provided.?

#Ex-with-data

Basic Tips to troubleshoot :-

the probabilistic and stochastic characteristics inherent in the language model's output, alongside its reliance on input prompts, it's feasible that the generated output may not be flawless or entirely accurate. Should such instances arise, troubleshooting can be facilitated by adhering to the outlined guidelines and best practices. Therefore, it's essential to validate the generated completion against your intended purpose using the following approach:

Is the Intent conveyed?

It may not be possible to consume the completion directly if the intent is not well conveyed. Improvement in output is possible by using more detail to convey the intent.?

Examples – Sufficiency and Correctness

Check if the examples provided are sufficient and correct. Any manual errors in examples cannot be detected by the model.?

Adjusting Temperature:-

The value of the temperature parameter controls the uncertainty in the output. Just like in thermodynamics, higher temperatures imply higher entropy, and lower temperatures mean lower entropy, in GPT-3 a lower value of temperature (i.e 0 or near zero) means a deterministic /non-random behaviour of the model. It means that only the output which has the highest probabilistic value is considered.?

When the temperature scale is increased towards 1, there is a higher probability of the model choosing completion responses from among the words with lesser probability than the one with highest probability. This may also increase the probability of error or the response straying away from the intended theme. Ideally, keeping the zero value for temperature, should result in same output for a given session.

If your answer is lacking sufficient information breadth or possible scenarios, try increasing the temperature. If it is deviating too much from expected output, try lowering the temperature to get the probabilistically most likely output in completion.?

Adjusting Top_p:

Ideally only one of Top P or Temperature must be used (and the other set to 1) to have a randomness in output, as per OpenAI recommendation. Increasing Top P will cause the inclusion of more tokens in the output. It represents the cumulative percentage of tokens taken into consideration.?

?Limitations of Large Language Models:-

-While Large Language Models such as ChatGPT and GPT-3 Codex and Text models have immense capability to generate text completions for a very large variety of inputs, they still have their limitations.

Some of the known limitations arise due to the fact that:

  • Model is trained on a finite data (though massively huge) and may not contain recent updates.
  • The model is stochastic in nature and less probable or correct output tokens may be selected based on parameter settings.
  • It is possible that the model may be misled by an incorrect input prompt.

While these models can be used as effort saving tools, their output must almost always be vetted by experts to ensure correctness and relevance.?

Consider the example:

Intent: To know how chatGPT deals with incorrect prompts

Prompt:

The following python code is not calculating simple interest correctly. What is the reason? The calculated value is half of the correct value. def calculate_interest(p, t, r ):? ? i = p*t*r/200? ? return i

Response by model: ChatGPT

The reason is that the code is dividing the result by 200 instead of 100. The formula for calculating simple interest should be: i = p t r / 100 * (1 + r).

Here, chatGPT correctly identified the issue in the code. However, the correction suggestion given was incorrect.?

The debugging suggestion above was “The reason is that the code is dividing the result by 200 instead of 100. The formula for calculating simple interest should be: i = p t r / 100 (1 + r).”? However, we know that this is wrong. The correct formula is i = p t * r / 100. ?

The model has no understanding of the world as a human has with his common sense. Hence, even if we trust the generated artifact like code, we must verify it for correctness.

Below are the limitations of the tool as claimed by the software providers - OpenAI:

“ChatGPT sometimes writes plausible-sounding but incorrect or nonsensical answers. Fixing this issue is challenging, as:?

(1) during RL training, there’s currently no source of truth;?

(2) training the model to be more cautious causes it to decline questions that it can answer correctly; and?

(3) supervised training misleads the model because the ideal answer?depends on what the model knows, rather than what the human demonstrator knows.”

-!!.Hope this article will help you.!!-

<Happy----Thanks for visiting----Learning>


要查看或添加评论,请登录

社区洞察

其他会员也浏览了