Skip to content

Latest commit

 

History

History
164 lines (141 loc) · 5.82 KB

3. Generate code with Azure OpenAI Service.MD

File metadata and controls

164 lines (141 loc) · 5.82 KB

Construct code from natural language

  • One of the capabilities of Azure OpenAI models is to generate code from NL 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.

Write functions

  • Azure OpenAI models can create functions and apps in several languages by just describing what you want. 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.
def binary_search(array, target):  
    low = 0  
    high = len(array) - 1  
  
    while low <= high:  
        mid = (low + high) // 2  
        if array[mid] == target:  
            return mid  
        elif array[mid] < target:  
            low = mid + 1  
        else:  
            high = mid - 1  
  
    return -1  

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.
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.

C#
public void PrintSquares(int n)  
{  
    for (int i = 1; i <= n; i++)  
    {  
        Console.WriteLine(i * i);  
    }  
}  

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));
    }
}
  • 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:
Sure, I'd be happy to explain what this code is doing!
....

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

  • For example, when given the following prompt:
prompt

complete the following function    
"""    
// calculate the average of the numbers in an array, but only if they're even    

C#
// calculate the average of the even numbers in an array  
public double CalculateEvenAverage(int[] numbers) {  
 generated code here
}
  • Similar functionality can be seen by providing partial code to the model, without any comments. Take the following prompt for example.
prompt

complete the following code  
"""  
public int Func1(int n) {  
    if (n == 0) { 

C#
public int Func1(int n) {  
    if (n == 0) {  
... code here
}
  • 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.

Write unit tests

C#
public static int BinarySearch(int[] arr, int target)
{
... code here
}

Provide that function along with the prompt "write three unit tests for this function", and you get a response similar to the following.

[Test]  
public void TestBinarySearch1()  
{  
    ...  
}  

[Test]  
public void TestBinarySearch2()  
{  
    ...  
}  

[Test]  
public void TestBinarySearch3()  
{  
    ...  
}  

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.
C#
public static List<List<int>> Permutations(List<int> lst)  
{  
    ...
}  

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.

public static List<List<int>> Permutations(List<int> lst)  
{  
    // Check if the input list is empty  
   ...
}  

Fix bugs and improve your code

Refactor inefficient code

Knowledge check

  1. What are some benefits of using Azure OpenAI to generate code?
    1. Increase in efficiency and productivity - Correct. Azure OpenAI can make you more efficient and productive by generating code for you.
    2. Increase in bugs and readability
    3. Increase in time spent coding
  2. What is the purpose of providing more context to the Azure OpenAI model when completing partial code?
    1. Providing more context makes the model less accurate.
    2. Providing more context doesn't affect the accuracy of the model.
    3. The more context provided to the model, the more accurate the response likely is. - Correct. 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?
    1. Azure OpenAI models can only translate code from one language to another if the code is written in C#.
    2. If you have code in one language, but need it in another, Azure OpenAI can translate that for you in several languages. - Correct. Azure OpenAI models can translate code from one language to another regardless of the original language.
    3. Azure OpenAI models can only translate code from Python to C# or Java.