流式传输文本生成

此代码示例演示了如何以流式方式生成文本。

代码示例

Python

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import vertexai
from vertexai import language_models

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

text_generation_model = language_models.TextGenerationModel.from_pretrained(
    "text-bison"
)
parameters = {
    # Temperature controls the degree of randomness in token selection.
    "temperature": 0.2,
    # Token limit determines the maximum amount of text output.
    "max_output_tokens": 256,
    # Tokens are selected from most probable to least until the
    # sum of their probabilities equals the top_p value.
    "top_p": 0.8,
    # A top_k of 1 means the selected token is the most probable among
    # all tokens.
    "top_k": 40,
}

responses = text_generation_model.predict_streaming(
    prompt="Give me ten interview questions for the role of program manager.",
    **parameters,
)

results = []
for response in responses:
    print(response)
    results.append(str(response))
results = "\n".join(results)
print(results)
# Example response:
# 1. **Tell me about your experience as a program manager.**
# 2. **What are your strengths and weaknesses as a program manager?**
# 3. **What do you think are the most important qualities for a successful program manager?**
# 4. **How do you manage
# ...

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器