创建提示以与发布方代码模型搭配使用,进而创建测试函数代码补全建议。
代码示例
C#
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 C# 设置说明执行操作。如需了解详情,请参阅 Vertex AI C# API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
using Google.Cloud.AIPlatform.V1;
using System;
using System.Collections.Generic;
using System.Linq;
using Value = Google.Protobuf.WellKnownTypes.Value;
public class PredictCodeCompletionTestFunctionSample
{
public string PredictTestFunction(
string projectId = "your-project-id",
string locationId = "us-central1",
string publisher = "google",
string model = "code-gecko@001")
{
// Initialize client that will be used to send requests.
// This client only needs to be created once,
// and can be reused for multiple requests.
var client = new PredictionServiceClientBuilder
{
Endpoint = $"{locationId}-aiplatform.googleapis.com"
}.Build();
// Configure the parent resource.
var endpoint = EndpointName.FromProjectLocationPublisherModel(projectId, locationId, publisher, model);
var prefix = @"
public static string ReverseString(string s)
{
char[] chars = s.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
public static void TestEmptyInputString()";
var instances = new List<Value>
{
Value.ForStruct(new()
{
Fields =
{
["prefix"] = Value.ForString(prefix),
}
})
};
var parameters = Value.ForStruct(new()
{
Fields =
{
{ "temperature", new Value { NumberValue = 0.2 } },
{ "maxOutputTokens", new Value { NumberValue = 64 } }
}
});
// Make the request.
var response = client.Predict(endpoint, instances, parameters);
// Parse and return the content.
var content = response.Predictions.First().StructValue.Fields["content"].StringValue;
Console.WriteLine($"Content: {content}");
return content;
}
}
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 PredictCodeCompletionTestFunctionSample {
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 create code completion suggestions:
// https://cloud.google.com/vertex-ai/docs/generative-ai/code/code-completion-prompts
String instance =
"{ \"prefix\": \""
+ "def reverse_string(s):\n"
+ " return s[::-1]\n"
+ "def test_empty_input_string()"
+ "}";
String parameters = "{\n" + " \"temperature\": 0.2,\n" + " \"maxOutputTokens\": 64,\n" + "}";
String location = "us-central1";
String publisher = "google";
String model = "code-gecko@001";
predictTestFunction(instance, parameters, project, location, publisher, model);
}
// Use Codey for Code Completion to complete a test function
public static void predictTestFunction(
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();
}
}
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-gecko@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:
'def reverse_string(s): \
return s[::-1] \
def test_empty_input_string()',
};
const instanceValue = helpers.toValue(prompt);
const instances = [instanceValue];
const parameter = {
temperature: 0.2,
maxOutputTokens: 64,
};
const parameters = helpers.toValue(parameter);
const request = {
endpoint,
instances,
parameters,
};
// Predict request
const [response] = await predictionServiceClient.predict(request);
console.log('Get code completion response');
const predictions = response.predictions;
console.log('\tPredictions :');
for (const prediction of predictions) {
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
}
}
callPredict();
Python
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。如需了解详情,请参阅 Vertex AI Python API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
from vertexai.language_models import CodeGenerationModel
# TODO developer - override these parameters as needed:
parameters = {
"temperature": temperature, # Temperature controls the degree of randomness in token selection.
"max_output_tokens": 64, # Token limit determines the maximum amount of text output.
}
code_completion_model = CodeGenerationModel.from_pretrained("code-gecko@001")
response = code_completion_model.predict(
prefix="""def reverse_string(s):
return s[::-1]
def test_empty_input_string()""",
**parameters,
)
print(f"Response from Model: {response.text}")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。