0% found this document useful (0 votes)
14 views

04 Generate Code With Azure OpenAI Service

Generate code with Azure OpenAI Service
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

04 Generate Code With Azure OpenAI Service

Generate code with Azure OpenAI Service
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction

• The Azure OpenAI service enables you to use large language models (LLMs) to
generate content, including programming code.

• This functionality allows developers to generate and improve existing


programming code in various languages to increase efficiency and
understanding.

• In this module, you'll learn how to use Azure OpenAI to generate code and help
various development tasks.
Construct code from natural
language

• One of the capabilities of Azure OpenAI models is to generate code from


natural language prompts.

• Tasks can range from a simple one line command to a full application.

• The AI models can also edit and update provided code or previous responses
to complete the requested task.
Construct code from natural
language
AI models for code generation

• In previous generations of gpt models, some were trained specifically for use
with code (often called codex models).

• As new generations of models evolve, the base models drastically improve


their performance and understanding of both language and code, which results
in not needing specific code models.

• This improvement results in just a single model for more recent generations
(such as gpt-35-turbo and gpt-4) that can be used for both natural language
and code.

• The examples in this module are using gpt-35-turbo.


Construct code from natural
language
Write functions

• Azure OpenAI models can create functions and apps in several languages by
just describing what you want.

• For example, say you need an implementation of binary search but can't
remember how it's done.

• Given the prompt write a function for binary search in python, you likely
receive a response with the function and an explanation of the code.
Construct code from natural
language
Write functions
• The request can go further
than functions, and request
a series of functions or an
entire app.

• The response provides a


good starting point for the
task you're trying to
achieve
Construct code from natural
language
Change coding language
• If you have code in one language, but need it in another, Azure OpenAI can
translate that for you.

• For example, say you have this print_squares function in Python.

def print_squares(n):
for i in range(1,
n+1):
print(i**2)

• If you provide that code, along with the prompt convert this code to C#, you
likely get something similar to the following function.
Construct code from natural
language
Change coding language
public void PrintSquares(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(i * i);
}
}

• Notice the model understood the differences between how for loops and
functions are defined in the different languages (including access modifiers in
C#), and changed the code appropriately
Construct code from natural
language
Understand unknown code
• Azure OpenAI models are helpful for understanding code that doesn't make
sense, or may be in a language you aren't familiar with.

• For example, say you were given the following function (in a fictitious coding
language!) and didn't know how to understand it.

fn qzplv(n: i32, m: i32) -> i32 {


if n == 0 {
return m + 1;
} else if m == 0 {
return qzplv(n - 1, 1);
} else {
return qzplv(n - 1, qzplv(n, m -
1));
}
}
Construct code from natural
language
Understand unknown code
• When provided to the model, along with the prompt could you explain what
this code is doing?, you get a response similar to the following:
Complete code and assist the development
process
• Azure OpenAI can assist developers in common software development tasks
such as writing unit tests, completing partial code, commenting code, and
generating documentation.

• Using AI assistance can enable developers to spend more time on complex


programming and problem solving tasks.

Complete partial code


• Azure OpenAI models are capable of generating code based on comments,
function names, and partially written code.

• The more context you can provide the model, the more accurate the response
likely is.

• For example, when given the following prompt:


Complete code and assist the development
process
Complete partial code

• The model takes the comments and start of the function definition, and
complete it from whatever context it has.
Complete code and assist the development
process
Complete partial code
• In this case, it was able to discern what we wanted due to a relatively simple
and complete comment preceding the start of the function.

• With more complex functions or tasks, more context is required to get a useful
response.

• It's important to note that for some languages, like Python, the syntax is clear
enough for the model to assume what language is desired.

• For syntactically similar languages like C# or Java, there's a higher likelihood


that the model uses the wrong language.

• If that is the case, try specifying the language in the prompt or providing more
code for context.

• Similar functionality can be seen by providing partial code to the model, without
any comments. Take the following prompt for example.
Complete code and assist the development
process
Complete partial code
complete the following code
"""
def func1(n)
if n==0:

• The AI model does its best to complete the function with the most likely
completion of the function.

def func1(n):
if n == 0:
return 1
else:
return n * func1(n-1)
Complete code and assist the development
process
Complete partial code
• The response likely also includes a natural language explanation of what that
function is doing.

• In this case, it responded with a recursive implementation of the factorial


function.

• However, the prompt could have also been the start of a Fibonacci function,
which illustrates a case where we need more context in the prompt to better
indicate what we want from the model.

• Tools such as GitHub Copilot utilize OpenAI models to suggest code and function
completions in a similar way to the above examples.

• GitHub Copilot is an excellent example of real-world usage of this functionality


that uses built-in prompts and the context of the code you're writing to act as
your paired programmer.
Complete code and assist the development
process
Write unit tests
• Azure OpenAI models can generate unit tests for functions you write to help
make your code more robust.

• Take for example the binary search function.


Complete code and assist the development
process
Write unit tests
• Provide that function along with the prompt write three
unit tests for this function, and you get a response similar
to the following.

• This functionality is even more


useful if you specify the cases you
want included in your prompt,
which reduces the time it takes
for you to write unit tests for your
code.
Complete code and assist the development
process
Add comments and generate
documentation
• To further improve your code, AI models
can add comments and documentation for
you.

• Take the following function as an example,


which can be a little hard to understand
when first reading it without any code
comments.
Complete code and assist the development
process
Add comments and generate
documentation
• Provide that function to the model, along with a prompt requesting that it add
comments to the code, and you get a response similar to the following.
Complete code and assist the development
process
Add comments and generate
documentation
• Take it one step further, and
request documentation for the
function it just added comments
to.
Fix bugs and improve your code
• Developers sometimes can write code that mostly works, but could be improved
by fixing bugs, syntax, performance, or modularity.

• Azure OpenAI models can help identify ways to improve and provide suggestions
on how to write better code.

Fix bugs in your code


• Azure OpenAI models can help fix bugs in code by analyzing the code and
suggesting changes that can potentially fix the issue.

• This can help developers identify and resolve bugs faster and more efficiently.

• For example, say you have the following function that isn't working for you.
Fix bugs and improve your code
Fix bugs in your code

• Provide that function to the model, along with the prompt Fix the bugs in this
function, and you get a response with the fixed code and an explanation of
what was fixed.
Fix bugs and improve your code
Fix bugs in your code
Fix bugs and improve your code
Improve performance
• While code developers write may work, there might be a more efficient way to
accomplish the task.

• Here's an example of a function that calculates the sum of the first n positive
integers, possibly inefficiently:

• This version works correctly, but its time complexity is O(n). When provided
to the model, here's the response:
Fix bugs and improve your code
Improve performance

• This version still returns the correct result, but its time complexity is now
O(1), which makes it much more efficient.
Fix bugs and improve your code
Refactor inefficient code
• Better code is less prone to bugs and is easier to maintain, and the Azure
OpenAI models can help guide developers on how to refactor their code.

• Consider the following function.

• This code calculates the total price of a particular item based on its name and
quantity.

• However, the code isn't modular and can be difficult to maintain.

• When provided to the model with the request to refactor it, here's the response:
Fix bugs and improve your code
Refactor inefficient code

• Along with the code, the model provides an explanation of the refactoring.
Exercise - Generate and improve code with
Azure OpenAI Service
Knowledge check
1. What are some benefits of using Azure OpenAI to generate code?

a) Increase in efficiency and productivity


b) Increase in bugs and readability
c) Increase in time spent coding
2. What is the purpose of providing more context to the Azure OpenAI model when
completing partial code?

d) Providing more context makes the model less accurate.


e) Providing more context doesn't affect the accuracy of the model.
f) The more context provided to the model, the more accurate the response likely is.
3. What is an example of how Azure OpenAI models can change coding language?

g) Azure OpenAI models can only translate code from one language to another if the
code is written in C#.
h) If you have code in one language, but need it in another, Azure OpenAI can
translate that for you in several languages.
i) Azure OpenAI models can only translate code from Python to C# or Java.

You might also like