Streamtext-Generierung

In diesem Codebeispiel wird gezeigt, wie Sie Text per Streaming generieren.

Codebeispiel

Python

Bevor Sie dieses Beispiel anwenden, folgen Sie den Python-Einrichtungsschritten in der Vertex AI-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Informationen finden Sie in der Referenzdokumentation zur Vertex AI Python API.

Richten Sie zur Authentifizierung bei Vertex AI Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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
# ...

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud -Produkte finden Sie im Google Cloud Beispielbrowser.