代码生成

Codey for Code Generation (code-bison) 是支持代码生成的模型的名称。这是一个可根据自然语言描述生成代码的基础模型。Codey for Code Generation 可以创建的内容类型包括函数、网页和单元测试。Codey for Code Generation 由代码生成 Codey API 提供支持。Codey API 属于 PaLM API 系列。

如需在控制台中探索此模型,请参阅 Model Garden 中的 Codey for Code Generation 模型卡片。
<a{: class="button button-primary" l10n-attrs-original-order="href,target,class,track-name,track-type" l10n-encrypted-href="kDsq5VTi6ASK/vNFlrWmnltigmNHgUGOXn/QVSGplOi71dheYhG9dKuv3S+0ajmQkfzB9oP/Mo2x7xIe1klR5YSKTX7LV1jkkg0C2Ndofq2g0LY5rER9QL0JoE/A8FHO" target="console" track-name="consoleLink" track-type="tasks" }="">转到 Model Garden</a{:>

使用场景

以下是代码生成的一些常见应用场景:

  • 单元测试:使用提示以请求对函数进行单元测试。

  • 编写函数:将问题传递给模型,以获取可以解决该问题的函数。

  • 创建类:使用提示来说明类的用途,并具有用于定义返回的类的代码。

HTTP 请求

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

模型版本

如需使用最新的模型版本,请指定不含版本号的模型名称,例如 code-bison

如需使用稳定的模型版本,请指定模型版本号,例如 code-bison@002。 每个稳定版本会在后续稳定版发布日期后的六个月内可用。

下表包含可用的稳定模型版本:

code-bison 模型 发布日期 终止日期
code-bison@002 2023 年 12 月 6 日 2024 年 10 月 9 日
code-bison@001 2023 年 6 月 29 日 2024 年 7 月 6 日

如需了解详情,请参阅模型版本和生命周期

请求正文

{
  "instances": [
    { "prefix": string }
  ],
  "parameters": {
    "temperature": number,
    "maxOutputTokens": integer,
    "candidateCount": integer,
    "stopSequences": [ string ]
    "logprobs": integer,
    "presencePenalty": float,
    "frequencyPenalty": float,
    "echo": boolean,
    "seed": integer
  }
}

以下是代码生成模型 code-bison 的参数。code-bison 模型是 Codey 中的模型之一。您可以使用这些参数来帮助优化代码补全提示。如需了解详情,请参阅代码模型概览创建代码补全提示

参数 说明 可接受的值

prefix

(必填)

对于代码模型,prefix 表示一条有意义的编程代码的开头,或描述要生成的代码的自然语言提示。 有效的文本字符串

temperature

温度 (temperature) 在生成回复期间用于采样。温度可以控制词元选择的随机性。较低的温度有利于需要更少开放性或创造性回复的提示,而较高的温度可以带来更具多样性或创造性的结果。温度为 0 表示始终选择概率最高的词元。在这种情况下,给定提示的回复大多是确定的,但可能仍然有少量变化。

0.0–1.0

Default: 0.2

maxOutputTokens

回复中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 60-80 个单词。

指定较低的值可获得较短的回复,指定较高的值可获得可能较长的回复。

1–2048

Default: 1024

candidateCount

(可选)

要返回的响应变体数量。

1-4

Default: 1

stopSequences

(可选)

指定一个字符串列表,告知模型在响应中遇到其中一个字符串时,停止生成文本。如果某个字符串在响应中多次出现,则响应会在首次出现的位置截断。字符串区分大小写。

例如,未指定 stopSequences 时,如果下面的内容是返回的回复:

public static string reverse(string myString)

则返回的回复为以下内容,其中 stopSequences 设置为 ["Str", "reverse"]

public static string
字符串列表

logprobs

(可选)

返回每个生成步骤中排名靠前的 logprobs 最可能候选词元及其对数概率。系统会始终返回每个步骤的选定词元及其对数概率。选择的词元不一定是排名靠前的 logprobs 最可能候选词元。

0-5

frequencyPenalty

(可选)

正值会惩罚生成的文本中反复出现的词元,从而降低重复内容概率。可接受的值为 -2.0-2.0

Minimum value: -2.0 Maximum value: 2.0

presencePenalty

(可选)

正值会惩罚已生成文本中已存在的词元,从而增加生成更多样化内容的概率。可接受的值为 -2.0-2.0

Minimum value: -2.0 Maximum value: 2.0

echo

(可选)

如果为 true,则提示会在生成的文本中回显。

Optional

seed

解码器使用伪随机数生成器生成随机噪声,温度 * 噪声在采样之前添加到 logits。伪随机数生成器 (prng) 将种子作为输入,并使用同一种子生成相同的输出。

如果未设置种子,则解码器中使用的种子具有不确定性,因此生成的随机噪声具有不确定性。如果设置了种子,则生成的随机噪声具有确定性。

Optional

示例请求

REST

如需使用 Vertex AI API 测试文本提示,请向发布方模型端点发送 POST 请求。

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID
  • 如需了解其他字段,请查看请求正文表。

    HTTP 方法和网址:

    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 响应。

Python

如需了解如何安装或更新 Python 版 Vertex AI SDK,请参阅安装 Python 版 Vertex AI SDK。如需了解详情,请参阅 Python API 参考文档

from vertexai.language_models import CodeGenerationModel

def generate_a_function(temperature: float = 0.5) -> object:
    """Example of using Codey for Code Generation to write a function."""

    # TODO developer - override these parameters as needed:
    parameters = {
        "temperature": temperature,  # 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}")

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

响应正文

{
  "predictions": [
    {
      "content": string,
      "citationMetadata": {
        "citations": [
          {
            "startIndex": integer,
            "endIndex": integer,
            "url": string,
            "title": string,
            "license": string,
            "publicationDate": string
          }
        ]
      },
      "logprobs": {
        "tokenLogProbs": [ float ],
        "tokens": [ string ],
        "topLogProbs": [ { map<string, float> } ]
      },
      "safetyAttributes": {
        "categories": [ string ],
        "blocked": false,
        "scores": [ float ],
        "errors": [ int ]
      },
      "score": float
    }
  ]
}
响应元素 说明
blocked 与安全属性关联的 boolean 标志,用于指示模型的输入或输出是否被阻止。如果 blockedtrue,则响应中的 errors 字段包含一个或多个错误代码。如果 blockedfalse,则响应不包含 errors 字段。
categories 与所生成内容关联的安全属性类别名称的列表。scores 参数中的得分顺序与类别的顺序匹配。例如,scores 参数中的第一个得分表示回复违反 categories 列表中第一个类别的可能性。
citationMetadata 包含引用数组的元素。
citations 引用数组。每个引用都包含其元数据。
content 模型使用输入文本生成的结果。
endIndex 一个整数,用于指定引用在 content 中的结束位置。
errors 错误代码数组。仅当响应中的 blocked 字段为 true 时,响应中才会包含 errors 响应字段。如需了解如何理解错误代码,请参阅安全错误
license 与引用关联的许可。
publicationDate 引用的发布日期。其有效格式为 YYYYYYYY-MMYYYY-MM-DD
safetyAttributes 一个安全属性阵列。该阵列包含每个响应候选项的一个安全属性。
score 小于零的 float 值。score 的值越高,模型回复的置信度就越高。
startIndex 一个整数,用于指定引用在内容中的起始位置。
title 引用来源的标题。来源标题的示例可能是新闻报道或书籍标题。
url 引用来源的网址。网址来源的示例可能是新闻网站或 GitHub 代码库。
tokens 采样词元。
tokenLogProbs 采样词元的对数概率。
topLogProbs 每个步骤中最可能的候选词元及其对数概率。
logprobs “logprobs”参数的结果。1-1 映射到“候选”。

示例响应

{
  "predictions":[
    {
      "citationMetadata": {
        "citations": [ ]
      },
      "safetyAttributes": {
        "scores": [ ],
        "categories": [ ],
        "blocked": false
      },
      "content": "CONTENT",
      "score": -1.1161688566207886
    }
  ]
}

流式传输来自生成式 AI 模型的响应

对于 API 的流式传输请求和非流式传输请求,这些参数是相同的。

如需使用 REST API 查看示例代码请求和响应,请参阅使用流式传输 REST API 的示例

如需使用 Python 版 Vertex AI SDK 查看示例代码请求和响应,请参阅使用 Python 版 Vertex AI SDK 进行流式传输的示例