텍스트 생성 스트리밍

이 코드 샘플은 스트리밍 방식으로 텍스트를 생성하는 방법을 보여줍니다.

코드 샘플

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 샘플 브라우저를 참조하세요.