04 Generate Code With Azure OpenAI Service
04 Generate Code With Azure OpenAI Service
• The Azure OpenAI service enables you to use large language models (LLMs) to
generate content, including programming code.
• In this module, you'll learn how to use Azure OpenAI to generate code and help
various development tasks.
Construct code from natural
language
• 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).
• 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.
• 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.
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.
• The more context you can provide the model, the more accurate the response
likely is.
• 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.
• 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.
• 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.
• Azure OpenAI models can help identify ways to improve and provide suggestions
on how to write better code.
• 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.
• This code calculates the total price of a particular item based on its name and
quantity.
• 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?
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.