使用发布方文本模型从文本中提取信息。
代码示例
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;
// Extractive Question Answering with a Large Language Model
public class PredictTextExtractionSample
{
public string PredictTextExtraction(
string projectId = "your-project-id",
string locationId = "us-central1",
string publisher = "google",
string model = "text-bison@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);
// Initialize request argument(s).
var content = @"
Background: There is evidence that there have been significant changes
in Amazon rainforest vegetation over the last 21,000 years through the Last
Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment
deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that
rainfall in the basin during the LGM was lower than for the present, and this
was almost certainly associated with reduced moist tropical vegetation cover
in the basin. There is debate, however, over how extensive this reduction
was. Some scientists argue that the rainforest was reduced to small, isolated
refugia separated by open forest and grassland; other scientists argue that
the rainforest remained largely intact but extended less far to the north,
south, and east than is seen today. This debate has proved difficult to
resolve because the practical limitations of working in the rainforest mean
that data sampling is biased away from the center of the Amazon basin, and
both explanations are reasonably well supported by the available data.
Q: What does LGM stands for?
A: Last Glacial Maximum.
Q: What did the analysis from the sediment deposits indicate?
A: Rainfall in the basin during the LGM was lower than for the present.
Q: What are some of scientists arguments?
A: The rainforest was reduced to small, isolated refugia separated by open forest
and grassland.
Q: There have been major changes in Amazon rainforest vegetation over the last how many years?
A: 21,000.
Q: What caused changes in the Amazon rainforest vegetation?
A: The Last Glacial Maximum (LGM) and subsequent deglaciation
Q: What has been analyzed to compare Amazon rainfall in the past and present?
A: Sediment deposits.
Q: What has the lower rainfall in the Amazon during the LGM been attributed to?
A:";
var instances = new List<Value>
{
Value.ForStruct(new()
{
Fields =
{
["content"] = Value.ForString(content),
}
})
};
var parameters = Value.ForStruct(new()
{
Fields =
{
{ "temperature", new Value { NumberValue = 0 } },
{ "maxDecodeSteps", new Value { NumberValue = 32 } },
{ "topP", new Value { NumberValue = 0 } },
{ "topK", new Value { NumberValue = 1 } }
}
});
// Make the request.
var response = client.Predict(endpoint, instances, parameters);
// Parse and return the content.
var responseContent = response.Predictions.First().StructValue.Fields["content"].StringValue;
Console.WriteLine($"Content: {responseContent}");
return responseContent;
}
}
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.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// Extractive Question Answering with a Large Language Model
public class PredictTextExtractionSample {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
// Details about designing prompts that extract information from text:
// https://cloud.google.com/vertex-ai/docs/generative-ai/text/extraction-prompts
String instance =
"{\"content\": \"Background: There is evidence that there have been significant changes \n"
+ "in Amazon rainforest vegetation over the last 21,000 years through the Last \n"
+ "Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment \n"
+ "deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that \n"
+ "rainfall in the basin during the LGM was lower than for the present, and this \n"
+ "was almost certainly associated with reduced moist tropical vegetation cover \n"
+ "in the basin. There is debate, however, over how extensive this reduction \n"
+ "was. Some scientists argue that the rainforest was reduced to small, isolated \n"
+ "refugia separated by open forest and grassland; other scientists argue that \n"
+ "the rainforest remained largely intact but extended less far to the north, \n"
+ "south, and east than is seen today. This debate has proved difficult to \n"
+ "resolve because the practical limitations of working in the rainforest mean \n"
+ "that data sampling is biased away from the center of the Amazon basin, and \n"
+ "both explanations are reasonably well supported by the available data.\n"
+ "\n"
+ "Q: What does LGM stands for?\n"
+ "A: Last Glacial Maximum.\n"
+ "\n"
+ "Q: What did the analysis from the sediment deposits indicate?\n"
+ "A: Rainfall in the basin during the LGM was lower than for the present.\n"
+ "\n"
+ "Q: What are some of scientists arguments?\n"
+ "A: The rainforest was reduced to small, isolated refugia separated by open forest"
+ " and grassland.\n"
+ "\n"
+ "Q: There have been major changes in Amazon rainforest vegetation over the last how"
+ " many years?\n"
+ "A: 21,000.\n"
+ "\n"
+ "Q: What caused changes in the Amazon rainforest vegetation?\n"
+ "A: The Last Glacial Maximum (LGM) and subsequent deglaciation\n"
+ "\n"
+ "Q: What has been analyzed to compare Amazon rainfall in the past and present?\n"
+ "A: Sediment deposits.\n"
+ "\n"
+ "Q: What has the lower rainfall in the Amazon during the LGM been attributed to?\n"
+ "A:\"}";
String parameters =
"{\n"
+ " \"temperature\": 0,\n"
+ " \"maxDecodeSteps\": 32,\n"
+ " \"topP\": 0,\n"
+ " \"topK\": 1\n"
+ "}";
String project = "YOUR_PROJECT_ID";
String location = "us-central1";
String publisher = "google";
String model = "text-bison@001";
predictTextExtraction(instance, parameters, project, location, publisher, model);
}
static void predictTextExtraction(
String instance,
String parameters,
String project,
String location,
String publisher,
String model)
throws IOException {
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);
// Use Value.Builder to convert instance to a dynamically typed value that can be
// processed by the service.
Value.Builder instanceValue = Value.newBuilder();
JsonFormat.parser().merge(instance, instanceValue);
List<Value> instances = new ArrayList<>();
instances.add(instanceValue.build());
// Use Value.Builder to convert parameter to a dynamically typed value that can be
// processed by the service.
Value.Builder parameterValueBuilder = Value.newBuilder();
JsonFormat.parser().merge(parameters, parameterValueBuilder);
Value parameterValue = parameterValueBuilder.build();
PredictResponse predictResponse =
predictionServiceClient.predict(endpointName, instances, parameterValue);
System.out.println("Predict Response");
System.out.println(predictResponse);
}
}
}
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 = 'text-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 instance = {
content: `Background: There is evidence that there have been significant changes \
in Amazon rainforest vegetation over the last 21,000 years through the Last \
Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment \
deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that \
rainfall in the basin during the LGM was lower than for the present, and this \
was almost certainly associated with reduced moist tropical vegetation cover \
in the basin. There is debate, however, over how extensive this reduction \
was. Some scientists argue that the rainforest was reduced to small, isolated \
refugia separated by open forest and grassland; other scientists argue that \
the rainforest remained largely intact but extended less far to the north, \
south, and east than is seen today. This debate has proved difficult to \
resolve because the practical limitations of working in the rainforest mean \
that data sampling is biased away from the center of the Amazon basin, and \
both explanations are reasonably well supported by the available data.
Q: What does LGM stands for?
A: Last Glacial Maximum.
Q: What did the analysis from the sediment deposits indicate?
A: Rainfall in the basin during the LGM was lower than for the present.
Q: What are some of scientists arguments?
A: The rainforest was reduced to small, isolated refugia separated by open forest and grassland.
Q: There have been major changes in Amazon rainforest vegetation over the last how many years?
A: 21,000.
Q: What caused changes in the Amazon rainforest vegetation?
A: The Last Glacial Maximum (LGM) and subsequent deglaciation
Q: What has been analyzed to compare Amazon rainfall in the past and present?
A: Sediment deposits.
Q: What has the lower rainfall in the Amazon during the LGM been attributed to?
A:
`,
};
const instanceValue = helpers.toValue(instance);
const instances = [instanceValue];
const parameter = {
temperature: 0.2,
maxOutputTokens: 256,
topP: 0,
topK: 1,
};
const parameters = helpers.toValue(parameter);
const request = {
endpoint,
instances,
parameters,
};
// Predict request
const [response] = await predictionServiceClient.predict(request);
console.log('Get text extraction 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 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
import vertexai
from vertexai.language_models import TextGenerationModel
# TODO (developer): update project_id
vertexai.init(project=PROJECT_ID, location="us-central1")
parameters = {
"temperature": 0, # Temperature controls the degree of randomness in token selection.
"max_output_tokens": 256, # Token limit determines the maximum amount of text output.
"top_p": 0, # Tokens are selected from most probable to least until the sum of their probabilities equals the top_p value.
"top_k": 1, # A top_k of 1 means the selected token is the most probable among all tokens.
}
model = TextGenerationModel.from_pretrained("text-bison@002")
response = model.predict(
prompt="""Background: There is evidence that there have been significant changes \
in Amazon rainforest vegetation over the last 21,000 years through the Last \
Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment \
deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that \
rainfall in the basin during the LGM was lower than for the present, and this \
was almost certainly associated with reduced moist tropical vegetation cover \
in the basin. There is debate, however, over how extensive this reduction \
was. Some scientists argue that the rainforest was reduced to small, isolated \
refugia separated by open forest and grassland; other scientists argue that \
the rainforest remained largely intact but extended less far to the north, \
south, and east than is seen today. This debate has proved difficult to \
resolve because the practical limitations of working in the rainforest mean \
that data sampling is biased away from the center of the Amazon basin, and \
both explanations are reasonably well supported by the available data.
Q: What does LGM stands for?
A: Last Glacial Maximum.
Q: What did the analysis from the sediment deposits indicate?
A: Rainfall in the basin during the LGM was lower than for the present.
Q: What are some of scientists arguments?
A: The rainforest was reduced to small, isolated refugia separated by open forest and grassland.
Q: There have been major changes in Amazon rainforest vegetation over the last how many years?
A: 21,000.
Q: What caused changes in the Amazon rainforest vegetation?
A: The Last Glacial Maximum (LGM) and subsequent deglaciation
Q: What has been analyzed to compare Amazon rainfall in the past and present?
A: Sediment deposits.
Q: What has the lower rainfall in the Amazon during the LGM been attributed to?
A:""",
**parameters,
)
print(f"Response from Model: {response.text}")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。