Session 14 Generative AI - For Software Engineering
Session 14 Generative AI - For Software Engineering
Code generation
Code Review.
Automated Refactoring.
Style Consistency.
Vulnerability Detection.
Leverage Gen AI to Improve Quality and Productivity
• Now we run the translate.py file with src_lang file and tgt_lang file. The command below may take some
time to run
python transcoder/translate.py --src_lang java \
--tgt_lang python \
--model_path model_1.pth > code2.java
Code Generation Prompts
Code Generation Prompts
Generate a Docker script
• The following prompt generates a Docker script to create a Linux machine with specific libraries
installed:
Generate a Docker script to create a simple linux machine that has python 3.10
installed with following libraries: pandas, tensorflow, numpy
Code Generation Prompts
Generate a function
• The following prompt generates a function that inputs a year and determines if it's a
leap year or not:
You are a helpful code assistant that can teach a junior developer
how to code. Your language of choice is Python. Don't explain the
code, just generate the code block itself.
• The User Message will then be the prompt itself with the instruction of the specific code you want the
model to generate.
Basic Example
Here is the prompt if you want to try it yourself:
Write code that asks the user for their name and say "Hello"
Turn Comments Into Code
• You can also use the code generation capabilities of these LLMs to generate code from comments alone.
Prompt:
"""1. Create a list of movies
2. Create a list of ratings for these movies
3. Combine them to make a json object of 10 movies with their ratings."""
MySQL Query Generation
• Let's say you have a dataset with some information which you can include
as part of the prompt and then instruct it to generate a specific query.
For example:
"""Table departments,columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science department""”
Example 2: Enum
Let's do some more parsing with it
• If you are testing this example, it could be useful to have this database created.
• One approach is using a modified version of the prompt above with slightly different instructions to
generate a database schema:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a valid database schema with the above tables and columns
Add Dummy Data..
You will also need dummy data to test if the original generated query was valid. :
CREATE TABLE students ( DepartmentId INT, StudentId INT PRIMARY KEY, StudentName VARCHAR(50), FOREIGN KEY
(DepartmentId) REFERENCES departments(DepartmentId));
Given the database schema above, generate valid insert statements include 4 rows for each table.
Explain Code
• If you are learning to program in a certain language, it might be useful to prompt the model to explain
certain bits of code.
• Let's reuse the query generated above and ask the model to explain it.
• If you are using the same PREAMBLE from before, be careful of how you are instructing the model.
• E.g. if we use the following example, the model may complain that it is a code assistant in Python but it
still provides an explanation.