LLMs and Prompt Engineering
LLMs in Sw Development
To improve productivity (of delivered code), LLMs can be used to do
some of the tasks (or parts of some tasks) of engineers
Benefits can be limited
E.g. if in a 50KLOC delivered code, 40KLOC was reused code, 10K was written code,
then LLM can only be used to reduce effort for 10K
It can also improve quality – if generated code is of good quality (which as of now is
not fully true)
But LLMs can be used for a host of tasks, not just code generation –
help in requirements, design, testing, installation/deployment,
refactoring, re-engineering, …
So companies have aggressively employed them in their work flows
Outputs of LLMs depends significantly on how prompts are given
As context from prompts significantly impacts what LLMs generate
There is randomness built in LLMs generation algorithms
Proper prompting can help in getting the most out of LLM for a task
2
Prompt Engineering
For general queries, we can prompt as we wish
When reliability of the output generated is important
– ad hoc prompting does not work well
In Sw, we want precise output without errors
Prompt engineering has been coined after genAI tools
came – to guide users to prompt the AI suitably to get
the desired results
At the top level, can dividing prompting approaches
into two broad groups
Single prompt approaches
Multi-prompt approaches
Both use prompts – let us understand an effective
prompt
Goal of an effective prompt: To get the desired output
3
Parts of an Effective Prompt
A well structured prompt can guide the LL Model
suitably to generate accurate and relevant output
For somewhat complex tasks, a prompt should
contain the following parts
Task description: Concisely and clearly state what you
want the LLM to do
Input data: Any relevant input data, context, examples,
etc that can help an LLM do the task should be
provided; helps generate output consistent with inputs
Output format: If desired format is clearly specified
(e.g. different parts / sections of the output, ordering,
..), lesser work to be done after it
4
Effective Prompt…
Additional Instructions (if any): Specific
instructions on how to solve a problem, guidelines
or principles to be followed, ..
Evaluation criteria: How the generated output will
be evaluated, i.e. what constitutes a good output
In these parts, many tricks can be used to get the
LLM to do a good job
Discuss a few that may be useful for sw tasks
5
Giving Examples
Provide some examples to guide the LLM
One example – one-shot
Multiple examples – few-shot (not always better)
E.g. if you want a function for computing HCF, you can
give one or two examples
Inputs: 15, 25
Answer: 5
Inputs: 88, 66
Answer: 22
Can give examples as part of instructions - which
you can specify separately in ChatGPT
6
Role Prompting
LLMs have digested a lot of information – also
about how different roles perform some tasks
Hence, role prompting – asking LLM to play the
role of someone – can help getting desired output
You are a requirements analyst, …
You are a cybersecurity expert
Prompt: Imagine you are an end user interacting with a
new e-commerce platform. Describe the features and
functionalities you would expect …
Prompt: You are a systems architect responsible for
designing a scalable back-end for a social media
platform. Describe the components of the application
described below ….
7
Chain-of-Thought
Treating LLMs as a black-box: no visibility on how
the answer has been reached – unsatisfactory
Can ask the LLM to solve some problem in a step
wise manner, giving the output after each step –
better control, more visibility
E.g. Generate code for this problem: …. Pls solve this
problem step-by-step as follows
Step 1: …
Step 2: ..
8
Discussion. Other techniques for
better prompting
Pls share your experience - what can you do in a
single prompt approach to get a better response
9
Multi-Prompt Approaches
Single prompt approaches work well for many
tasks – if the prompts are well structured and
provide the desired information
Their scope will increase
For complex tasks, single prompt insufficient – a
series of prompts needed to solve
Discuss a few approaches
10
Prompt-Chaining
The task is broken into a series of subtasks –
output of a subtask goes as input to the next
LLM prompted for each subtask in sequence (each
prompt well structured)
It simplifies prompt creation (often challenging) –
as simpler prompts for each subtask
Provides control and visibility into problem solving
– incremental problem solving easier to verify
11
Prompt Chaining…
Prompt 1: Generate python code for this problem:
ask for a file name, sum up the numbers,…
Prompt 2: Now enhance the code to handle the
cases where the file does not exist
Prompt 3: Now enhance the code such that if
inputs in the file are not numbers they are skipped
12
Prompt Chaining with Chain of
Thought
Prompt chaining, but each subsequent step
determined based on progress so far
Following steps can be followed
Initial prompt
Evaluate models response – identify how the response
should be improved
Refine prompt for better response – provide more context,
information, …
Repeat till step done well
Then move to the next step in problem solving
Problem is being solved step-by-step
Output by LLM can be reviewed and corrected (at each step)
Corrected output is part of the context for next subtask
13
Exploratory
Can be useful for learning and exploration
Initial prompt: Explain the bubble sort algo in simple
terms
Evaluation: Assess the answer and see where you want
more clarity
Refined prompt: Elaborate on time complexity …
…
14
Multiple Prompts for Consistency
Self-consistency approach – keep refining the
prompt till you get the desired output
Goal is to gradually converge
Sometimes used to check the model output by
giving variations of the same input / prompt to
check the consistency
15
Exploratory Prompting
Initial prompt can be to ask the model to give
multiple possible solutions / answers to the given
task / problem
Then can ask for more information; select one and
then ask for more details on that
Also possible to make it a tree-of-thought
E.g. I have to develop an application: …; Please suggest
at least two mores suitable architecture styles for
building this
This prompt style is expensive for sw tasks as user
has to evaluate different replies
Copilot has one such option
16
Discussion – other approaches
Other approaches you may have tried for sw
related task and which worked well
17
Prompting for Requirements
LLMs can be used for almost any task done by
engineers in a software project
For the requirements activity also, they can be
used – for specification as well as verification
Use in analysis / elicitation – not clear as this is still
largely a human activity
For standard type of applications – might help even in
eliciting requirements
18
For Generating SRS (Specification)
After initial analysis, you have the problem title
(which has semantic information), user categories,
some notes on purpose, scope, goals, … Some
constraints may also become clear
Compile this information (so it is suitable for
prompting) – use LLM help in SRS preparation
For SRS in a specific format – specify the output
format (e.g. ChatGPT allows this)
With these two context information, chatGPT can
be asked “produce an SRS for this application in
the format provided”
19
Generating SRS…
One prompt approach– unlikely to work
Desired output is too much (an SRS may be long)
Better to have LLM generate different sections of the
SRS separately by using prompt chaining
Allows LLM to provide a more detailed output for each
section / part
Allows the analyst to correct the LLM’s hidden assumptions
by correcting the outputs
Eg. Give the context, structure of the output,…
1. Generate the list of all functional requirements as a table
of use cases – one for each user category
2. Generate the non-functional requirements
3. Provide details of use case – give one prompt for each UC
20
For Requirements Validation
Once SRS has been produced, it has to be validated for
key properties of correctness, completeness,
consistency, unambiguity
LLMs can help in this
Single prompt approach unlikely to work – better to
ask LLM to validate each property separately
Some properties are per-requirement: Correctness,
unambiguity
Check for these for portions of the requirements – e.g. first
for functional and then for non-functional
Some are on overall SRS: Consistency, completeness
For this the complete SRS has to be given
21
Looking for Errors…
Providing examples of quality attributes help
E.g. give examples of ambiguous statements along with
unambiguous equivalent
Example 1: Ambiguous text
Unambiguous text: … text
Example 2: Ambiguous text
Unambiguous text: … text
Then give it some of the requirements (numbered)
and ask it to check for ambiguity
Besides identifying errors, can also ask LLM to
suggest fixing the errors / suggest improvement
22
SRS-wide Errors
Consistency and completeness are SRS-wide –
cannot do requirement-by-requirement
Give the whole SRS and ask for inconsistencies – a
single prompt is suitable here
For completeness, give the whole SRS
It will help if LLM is asked to act as a stakeholder,
so it can think from that perspective
Background info on the application
The SRS of this application is: … SRS…
Find the missing requirements for this SRS, think step-
by-step acting like each of the stakeholders
Or you can ask it to check for one type of user, then
another type, ….
23
Which LLM to Use
Interactive like ChatGPT – general purpose and can
be used for any task
Built into the tools used by engineers to assist in
the task
E.g. CoPilot in VS Code – programmer continues
programming, the LLM helps in code completion
(actually can do more)
Focused bots for specific tasks – they are evolving
– often fine tuned for the specific task and hence
are more reliable (and may be “lighter”)
24