0% found this document useful (0 votes)
82 views46 pages

CSC1002 Week3 AI Prompt

Uploaded by

ridef90785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views46 pages

CSC1002 Week3 AI Prompt

Uploaded by

ridef90785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

CSC1002 - AI-Assisted

Programming
Design - Tradeoff
Problem Decomposition - Function
Clean Code - Single Responsibilty
• Clearly de ned behaviour, with one clear task to perform
• Short in number of lines of code, say < 15 lines
• Clear Input and Output
• Prefer a function with fewer parameters, no more than three
fi
Goal #1 - AI Assisted Coding
Future Programming - AI as Programming Copilot
• When rst learning to program, learners often dedicated a signi cant amount of time working
with the syntax and fundamental structure of programs.

• As AI technology advances, the focus for learners will shift towards design, coding strucrture
and testing (Clean Code)

• In today’s lecture, we will introduce students to the use of Open AI technology as a


programming copilot to aid in the development of a game program called Number Guessing.

• Through guided interaction called prompt, we will demonstrate how the AI Assistant
facilitates the completion of coding in each function based on the descirbed behavior from
the prompt,

• By the end of the lecture, students will have gained a good understanding of how AI
technology is reshaping the future of programming development, including how to integrate
AI technology as a programming helper and how to leverage the AI Assistant to bootstrap
software development.

• Additionally, we will present instances where the AI Assistant falls short.


fi
fi
AI Assisted Programming Tasks
• Code Completion - Auto complete logic as programmers type to reduce errors and to save time by
providing suggestions for method names, parameters, and even entire code segment (for, if, while,
…).

• Code Generation - Generate logic for the entire function/Program based on user-provided
speci cation and user-speci ed constraints using latest generative AI technology.

• Code Re nement - Re ne logic with speci c requirements


• Code Summarization - Generate a text description at function level or a selected code segment; the
description can be used to understand the logic structure and to make further re nement as needed.

• Code Translation - Translate logic from one language to another, or one format to another (ex: JSON
to Class)

• Defeat Detection - Complexity tends to increase due to CHANGES, it aims to detect unknown errors
and error-prone logic.

• Clone Detection - Identify similar code fragments (repeated logic)


• Testing - Generate test cases for your logic
fi
fi
fi
fi
fi
fi
AI Assisted Programming - Limitations
• Wrong Logic
• Obsolete or Deprecated Logic
• Outdated Features
• Unlicensed Logic
• Missing Library/Module
AI-Assisted Programming Flow
De ne the Alllow AI
Write a prompt Examine the
desired Assistant to
that describes code to see if it
behavior of the generate the
the function is reasonable
fucntion code

Test the code


Incorrect
to see if it is
correct

correct

Done
fi
AI Assisted Prompt
Prompt - Input to Generative AI
Text in natural language

• In the context of Generative AI, the manner by which we engage AI system is conversation
called PROMPT, simply a piece of text in natural language.

• The QUALITY of prompt directly dictates the accuracy of the responses from the AI system.
• In summary, a well-de ned prompt is the recipe for a successful conversation that facilitates
understanding of the topics of interest. For examples:

• “Tell me about python programming”, or


• I am a freshmen student and I am about to take my rst programming course in python.
Give me an introduction to python programming, assumming I have no prior
programming experience. Show me the top 10 useful features and describe it with
examples and explanation.
fi
fi
Prompt - General Guidelines
• Clarify
• use simple sentences and instructions; favor short prompt and short instructions
• add examples with context (background) as needed
• Conciseness
• breakdown instructions into smaller sentences, in steps in proper sequence.
• follow standard formats/symbols recognized by AI assistant:
• (), [], :, “”,_, |
• Focus
• be speci c on the topics of interest
• Role
• control tone, style and depth of the response
fi
Prompt - AntiPatterns
• Information Overloaded
• Too much information might reduce accuracy of the response
• Open-ended questions
• Avoid too general questions such as “Help me to become Kinley!”
• Lack of Constraints
• Specify any speci c requirements, if any.
• Format Ouput, Response Style, Tone, and so on.
fi
Prompt - Components
Not all
Role components are
required

Context

Example 1 Order are not


mandatory but best
to leave instruction
Example 2
to end

Example 3

Instructions

Question
Prompt - Role
Prompt - Examples
Great product, 10/10: positive
Didn't work very well: negative
Super helpful, worth it: positive
It doesnt work!:

Add 3+3: 6
Add 5+5: 10
Add 2+2:

1. Laura Simmons [FARMER]


2. Kevin Alvarez [DANCE INSTRUCTOR]
3. Rachel O'Connor [VOLUNTEER]
4. Kinley [?]
Prompt - Fine Tuning
• Start with a concise, clear, and focused prompt with speci c context.
• an overview of the topic of interest
• Elaborate further with more speci c prompt
• ask speci c question based on previous response
• Stay within the context
• Ensure AI Assistant stays within the topic of interest at all time
• Re-prime prompt if needed
fi
fi
fi
Effective Prompt - Programming
• Context
• Open Files
• History
• Overall Description
• Instructions
• Logic Decomposition Steps
• step-by-step line comments
• Speci c Requirements
• Be Predictable
• Consistent names, input and output
• Parameters and Returns
• Examples
fi
VSC - Open AI tools
Microsoft Copilot
Cody AI
Prompt - pick_a_number()
Prompt 1 - general description
Prompt 2 - with typing hint
Sample AI-generated source code

Try calling these Functionally they are


functions with length identical
> 10 ????
Prompt 3 - specific requirements
Requirements: Specific Method

Guide AI to use
Sample() method to
generate the random
string
Prompt Format - Function
Input and Return
Types

Meaningful name

Parameter
Description

Return
Description

Specifc
Requirements

Examples
Implementation - AI-Assisted
Cody AI - Demo
Preparation

• Create an empty python le, say s1_prompt.py


• On BB, open the “prompt” le.
• Copy and Paste the generation description (top portion)
• Copy and Paste one of the pick_a_number() function, and perform Code
Generation using Cody AI

• Try di erent description of pick_a_number() function from the template le


• Compare the code generated
ff
fi
fi
fi
Code Generation - Edit (without selection)
Code Generation - Edit (with selection)
Code Generation - Edit (with selection)
Complete Implementation - AI
prompt_guess() - Cody AI

is this correct ???


get_correct_count() - Cody AI
get_misplaced_count() - Cody AI
display_hints() - Cody AI
play() - Cody AI

is this correct ???


Bonus - Unit Testing
Why Testing
• Without a doubt, testing is essential especially in the software industry (cost,
quality, customer satisfaction, sales, support, time, revenue, …etc)

• Types of Testing depends on Size and Scope


• Unit Testing - Small, Fast, Simple (usually on function basis, component basis)
• Integrated - Across all components to ensure end-to-end connectivity under
testbed platform

• System/Application - Complete testing with business cases close to production


platform

• Others - stress test, performance, benchmark, longetivity, ….etc


• For CSC1002: ONLY Unit Testing
Unit Testing
Closed vs Open
Unit Testing - Examples

Open-Box
Testing

Closed-Box
Testing

Closed-Box
Testing
Another Open-Box Testing

To Ensure the Logic


within the While loop
is correct
Unit Testing - Design
• Standard Cases
• Based on the function’s behaviour/speci cation
• Common Inputs with expected results to assure the basic functionality of the function
is passed

• Ex: a function foo() that expects 2 integers as input: foo(+ve, +ve), foo(-ve, +ve),
foo(-ve, ve), foo(-ve, -ve), foo(0,0), foo(0,+ve), foo(+ve, 0)

• Exception Cases
• These are inputs that might test some of the rules for the function in more depth or
contain unexpected inputs

• Out of Range Input, Empty, Invalid Input, Large Value, List with Large Items, …etc
fi
The End - Thank you

You might also like