コード生成プロンプトをテストする

適切に機能するプロンプトを設計するには、さまざまなバージョンのプロンプトをテストし、プロンプト パラメータを使用してテストを行い、最適なレスポンスが得られるかどうかを判断します。プロンプトは、Codey API を使用してプログラムでテストできます。また、Google Cloud コンソールの Vertex AI Studio でテストすることもできます。

コード生成プロンプトをテストする

コード生成プロンプトをテストするには、次のいずれかの方法を選択します。

REST

Vertex AI API でコード生成プロンプトをテストするには、パブリッシャー モデル エンドポイントに POST リクエストを送信します。

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID
  • PREFIX: コードモデルの場合、prefix は、意味のあるプログラミング コードの一部、または生成されるコードを記述する自然言語プロンプトの開始を表します。
  • TEMPERATURE: 温度は、レスポンス生成時のサンプリングに使用されます。温度は、トークン選択のランダム性の度合いを制御します。温度が低いほど、確定的で自由度や創造性を抑えたレスポンスが求められるプロンプトに適しています。一方、温度が高いと、より多様で創造的な結果を導くことができます。温度が 0 の場合、確率が最も高いトークンが常に選択されます。この場合、特定のプロンプトに対するレスポンスはほとんど確定的ですが、わずかに変動する可能性は残ります。
  • MAX_OUTPUT_TOKENS: レスポンスで生成できるトークンの最大数。1 トークンは約 4 文字です。100 トークンは約 60~80 語に相当します。

    レスポンスを短くしたい場合は小さい値を、長くしたい場合は大きい値を指定します。

  • CANDIDATE_COUNT: レスポンスのバリエーションの数。リクエストごとに、すべての候補の出力トークンが課金されますが、入力トークンは 1 回のみ課金されます。

    複数の候補を指定する機能は、generateContent で動作するプレビュー機能です(streamGenerateContent はサポートされていません)。次のモデルがサポートされています。

    • Gemini 1.5 Flash: 18、デフォルト: 1
    • Gemini 1.5 Pro: 18、デフォルト: 1
    • Gemini 1.0 Pro: 18、デフォルト: 1
    有効な値の範囲は 1~4 の int です。

HTTP メソッドと URL:

POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/code-bison:predict

リクエストの本文(JSON):

{
  "instances": [
    { "prefix": "PREFIX" }
  ],
  "parameters": {
    "temperature": TEMPERATURE,
    "maxOutputTokens": MAX_OUTPUT_TOKENS,
    "candidateCount": CANDIDATE_COUNT
  }
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/code-bison:predict"

PowerShell

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/code-bison:predict" | Select-Object -Expand Content

次のような JSON レスポンスが返されます。

Vertex AI SDK for Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、Vertex AI SDK for Python API のリファレンス ドキュメントをご覧ください。

from vertexai.language_models import CodeGenerationModel

parameters = {
    "temperature": 0.1,  # Temperature controls the degree of randomness in token selection.
    "max_output_tokens": 256,  # Token limit determines the maximum amount of text output.
}

code_generation_model = CodeGenerationModel.from_pretrained("code-bison@001")
response = code_generation_model.predict(
    prefix="Write a function that checks if a year is a leap year.", **parameters
)

print(f"Response from Model: {response.text}")
# Example response:
# Response from Model: I will write a function to check if a year is a leap year.
# **The function will take a year as input and return a boolean value**.
# **The function will first check if the year is divisible by 4.**
# ...

return response

Node.js

このサンプルを試す前に、Vertex AI クイックスタート: クライアント ライブラリの使用にある Node.js の設定手順を完了してください。詳細については、Vertex AI Node.js API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

/**
 * TODO(developer): Uncomment these variables before running the sample.\
 * (Not necessary if passing values as arguments)
 */
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction service client
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects.
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
const publisher = 'google';
const model = 'code-bison@001';

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function callPredict() {
  // Configure the parent resource
  const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;

  const prompt = {
    prefix: 'Write a function that checks if a year is a leap year.',
  };
  const instanceValue = helpers.toValue(prompt);
  const instances = [instanceValue];

  const parameter = {
    temperature: 0.5,
    maxOutputTokens: 256,
  };
  const parameters = helpers.toValue(parameter);

  const request = {
    endpoint,
    instances,
    parameters,
  };

  // Predict request
  const [response] = await predictionServiceClient.predict(request);
  console.log('Get code generation response');
  const predictions = response.predictions;
  console.log('\tPredictions :');
  for (const prediction of predictions) {
    console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
  }
}

callPredict();

Java

このサンプルを試す前に、Vertex AI クイックスタート: クライアント ライブラリの使用にある Java の設定手順を完了してください。詳細については、Vertex AI Java API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。


import com.google.cloud.aiplatform.v1.EndpointName;
import com.google.cloud.aiplatform.v1.PredictResponse;
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PredictCodeGenerationFunctionSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace this variable before running the sample.
    String project = "YOUR_PROJECT_ID";

    // Learn how to create prompts to work with a code model to generate code:
    // https://cloud.google.com/vertex-ai/docs/generative-ai/code/code-generation-prompts
    String instance = "{ \"prefix\": \"Write a function that checks if a year is a leap year.\"}";
    String parameters = "{\n" + "  \"temperature\": 0.5,\n" + "  \"maxOutputTokens\": 256,\n" + "}";
    String location = "us-central1";
    String publisher = "google";
    String model = "code-bison@001";

    predictFunction(instance, parameters, project, location, publisher, model);
  }

  // Use Codey for Code Generation to generate a code function
  public static void predictFunction(
      String instance,
      String parameters,
      String project,
      String location,
      String publisher,
      String model)
      throws IOException {
    final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
    PredictionServiceSettings predictionServiceSettings =
        PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (PredictionServiceClient predictionServiceClient =
        PredictionServiceClient.create(predictionServiceSettings)) {
      final EndpointName endpointName =
          EndpointName.ofProjectLocationPublisherModelName(project, location, publisher, model);

      Value instanceValue = stringToValue(instance);
      List<Value> instances = new ArrayList<>();
      instances.add(instanceValue);

      Value parameterValue = stringToValue(parameters);

      PredictResponse predictResponse =
          predictionServiceClient.predict(endpointName, instances, parameterValue);
      System.out.println("Predict Response");
      System.out.println(predictResponse);
    }
  }

  // Convert a Json string to a protobuf.Value
  static Value stringToValue(String value) throws InvalidProtocolBufferException {
    Value.Builder builder = Value.newBuilder();
    JsonFormat.parser().merge(value, builder);
    return builder.build();
  }
}

コンソール

Google Cloud コンソールで Vertex AI Studio を使用してコード生成プロンプトをテストする手順は次のとおりです。

  1. Google Cloud コンソールの [Vertex AI] セクションで、[Vertex AI Studio] に移動します。

    Vertex AI Studio に移動

  2. [開始] をクリックします。
  3. [ Create prompt] をクリックします。
  4. [モデル] で、名前が code-bison で始まるモデルを選択します。code-bison の後の 3 桁の数字は、モデルのバージョン番号を示します。たとえば、code-bison@002 はコード生成モデルのバージョン 1 の名前です。
  5. [Prompt] に、コード生成プロンプトを入力します。
  6. [Temperature] と [トークンの上限] を調整して、レスポンスへの影響をテストします。詳細については、コード生成モデル パラメータをご覧ください。
  7. [送信] をクリックしてレスポンスを生成します。
  8. プロンプトを保存する場合は [保存] をクリックします。
  9. [ コードを表示] をクリックして、プロンプトの Python コードまたは curl コマンドを表示します。

curl コマンドの例

MODEL_ID="code-bison"
PROJECT_ID=PROJECT_ID

curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:predict -d \
$"{
  'instances': [
    { 'prefix': 'Write a function that checks if a year is a leap year.' }
  ],
  'parameters': {
    'temperature': 0.2,
    'maxOutputTokens': 1024,
    'candidateCount': 1
  }
}"

コード生成のプロンプト設計の詳細については、コード生成のプロンプトを作成するをご覧ください。

コードモデルからのレスポンスをストリーミングする

REST API を使用してサンプルコードのリクエストとレスポンスを表示するには、ストリーミング REST API の使用例をご覧ください。

Vertex AI SDK for Python を使用してサンプルコードのリクエストとレスポンスを表示するには、ストリーミングでの Vertex AI SDK for Python の使用例をご覧ください。

次のステップ