自然言語からコードを構築する

完了

Azure OpenAI モデルの機能の 1 つは、自然言語プロンプトからコードを生成することです。 タスクの範囲は、シンプルな 1 行のコマンドから完全なアプリケーションまでとすることができます。 AI モデルでは、指定されたコードまたは以前の応答を編集および更新して、要求されたタスクを完了することもできます。

コード生成用の AI モデル

以前の世代の gpt モデルの一部は、コードで使うために特別にトレーニングされました (多くの場合、Codex モデルと呼ばれます)。 新しい世代のモデルが進化すると共に、基本モデルのパフォーマンスおよび言語とコード両方の理解が大幅に向上しており、特定のコード モデルは必要なくなっています。 この改善により、自然言語とコードの両方に使用できる、より新しい世代 (gpt-35-turbogpt-4 など) のモデルが 1 つだけとなります。

このモジュールの例では、gpt-35-turbo を使用しています。

関数を記述する

Azure OpenAI モデルでは、必要なものを記述するだけで、複数の言語で関数とアプリを作成できます。 たとえば、バイナリ検索の実装が必要だが、その方法を思い出せないとします。 プロンプト write a function for binary search in python が表示されたら、関数とコードの説明を含む応答が返される可能性があります。

Sure, here's an example implementation of binary search algorithm in Python:

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  
 
This function takes in two arguments - an array of integers and a target integer that we want to search
for in the array. The function initializes two pointers - low and high - to the first and last indices
of the array, respectively.

Inside the while loop, the function calculates the middle index using integer division (//). If the 
middle element is equal to the target, the function returns the index of the middle element. If the 
middle element is less than the target, we update the low pointer to mid + 1. Otherwise, we update the 
high pointer to mid - 1. We continue these steps until we find the target or the pointers cross each 
other (i.e., low > high).

If the target is not found in the array, the function returns -1.

Note that the binary search algorithm assumes that the input array is sorted in ascending order. If 
the array is not sorted, the algorithm may not return the correct result.

要求は関数よりも優先され、一連の関数またはアプリ全体を要求することができます。 応答は、達成しようとしているタスクの適切な出発点となります。

コーディング言語を変更する

ある言語で記述されたコードがあるが、それを別の言語にする必要がある場合、Azure OpenAI を使用すれば翻訳できます。 たとえば、この print_squares 関数が Python で記述されているとします。

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

そのコードをプロンプト convert this code to C#と共に指定すると、次の関数のような結果が得られる可能性があります。

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

モデルでは、さまざまな言語での for ループと関数の定義方法 (C# のアクセス修飾子を含む) の違いを理解し、コードを適切に変更している、ことに注目してください。

不明なコードを理解する

Azure OpenAI モデルは、意味がわからないコードや、馴染みのない言語で使用されているコードを理解するのに役立ちます。 たとえば、(架空のコーディング言語による) 次の関数が与えられたが、それを理解する方法を知らなかったとします。

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));
    }
}

プロンプト could you explain what this code is doing? とともにモデルに指定すると、次のような応答が返されます。

Sure, I'd be happy to explain what this code is doing!

This code is implementing the Ackermann function, which is a well-known mathematical function that 
grows very quickly in terms of computation time as its inputs increase. The function takes two integer 
arguments (n and m) and returns another integer.

Here's how the function works:
- If n is equal to 0, the function returns m + 1.
- If m is equal to 0, the function calls itself recursively with n - 1 and 1 as arguments.
- Otherwise, the function calls itself recursively with n - 1 and the result of calling itself with n 
and m - 1 as arguments.

The function is implemented in a way that is easy to understand and follow, but it has a very high 
computational complexity. In fact, it grows so quickly that it can't be computed for values of n and m 
greater than about 4 or 5.

説明と使い慣れた言語への翻訳を組み合わせると、この機能はさらに便利になります。