mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
23 KiB
23 KiB
In [ ]:
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.In [ ]:
! pip3 install --upgrade --quiet google-cloud-aiplatformIn [ ]:
# Automatically restart kernel after installs so that your environment can access the new packages
# import IPython
# app = IPython.Application.instance()
# app.kernel.do_shutdown(True)In [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
# Set the project id
! gcloud config set project {PROJECT_ID} --quietIn [ ]:
REGION = "us-central1" # @param {type: "string"}In [ ]:
# ! gcloud auth loginIn [ ]:
# from google.colab import auth
# auth.authenticate_user()In [ ]:
BUCKET_URI = f"gs://your-bucket-name-{PROJECT_ID}-unique" # @param {type:"string"}In [ ]:
! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}In [ ]:
import vertexai
from vertexai.preview.language_models import TextGenerationModelIn [ ]:
vertexai.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)In [ ]:
model = TextGenerationModel.from_pretrained("google/text-bison@001")
print(
model.predict(
"What is the best recipe for banana bread? Recipe:",
# "Brainstorm some ideas combining VR and fitness:",
# Optional:
# max_output_tokens=128,
# temperature=0,
# top_p=1,
# top_k=5,
)
)In [ ]:
import datetime
text_generation_model = TextGenerationModel.from_pretrained("text-bison")
print("Start: ", datetime.datetime.now())
for response in text_generation_model.predict_streaming(
prompt="Count to 100", max_output_tokens=1000
):
print(datetime.datetime.now())
print(response)
print("End: ", datetime.datetime.now())In [ ]:
from vertexai.language_models import ChatModel, InputOutputTextPair
chat_model2 = ChatModel.from_pretrained("google/chat-bison@001")
chat2 = chat_model2.start_chat(
# Optional:
context="My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.",
examples=[
InputOutputTextPair(
input_text="Who do you work for?",
output_text="I work for Ned.",
),
InputOutputTextPair(
input_text="What do I like?",
output_text="Ned likes watching movies.",
),
],
)
print(chat2.send_message("Are my favorite movies based on a book series?"))In [ ]:
print(chat2.send_message("When where these books published?"))In [ ]:
from vertexai.language_models import TextEmbeddingModel
model = TextEmbeddingModel.from_pretrained("google/textembedding-gecko@001")
embeddings = model.get_embeddings(["What is life?"])
for embedding in embeddings:
vector = embedding.values
print(len(vector))In [ ]:
dataset = "gs://cloud-samples-data/vertex-ai/prediction/llm/test_table.jsonl"
destination_uri_prefix = f"{BUCKET_URI}/text-bison@001_"
! gsutil cp -r gs://cloud-samples-data/vertex-ai/prediction/llm/text-bison@001_/ {destination_uri_prefix}
from vertexai.language_models import TextGenerationModel
text_generation_model = TextGenerationModel.from_pretrained("text-bison")
batch_job_1 = text_generation_model.batch_predict(
dataset=dataset,
destination_uri_prefix=destination_uri_prefix,
model_parameters={},
)In [ ]:
dataset = "gs://cloud-samples-data/vertex-ai/prediction/llm/embedding_input.jsonl"
destination_uri_prefix = f"{BUCKET_URI}/textembedding-gecko@001_"
from vertexai.preview.language_models import TextEmbeddingModel
text_embedding_model = TextEmbeddingModel.from_pretrained("textembedding-gecko@001")
batch_job_2 = text_embedding_model.batch_predict(
dataset=dataset,
destination_uri_prefix=destination_uri_prefix,
# Optional:
model_parameters={},
)In [ ]:
model3 = TextGenerationModel.from_pretrained("google/text-bison@001")
model3.list_tuned_model_names()In [ ]:
! gsutil cp gs://cloud-samples-data/vertex-ai/prediction/llm/q_a_train_with_context.jsonl {BUCKET_URI}/q_a_train_with_context.jsonl
tuning_job = model3.tune_model(
training_data=f"{BUCKET_URI}/q_a_train_with_context.jsonl",
# Optional:
train_steps=1,
tuning_job_location="europe-west4",
tuned_model_location="us-central1",
)In [ ]:
tuned_model = tuning_job.get_tuned_model()
print(tuned_model.predict("Tell me some ideas combining VR and fitness:"))In [ ]:
model3.list_tuned_model_names()In [ ]:
tuned_model4 = model3.get_tuned_model(
tuned_model_name=model3.list_tuned_model_names()[0]
)In [ ]:
print(tuned_model4.predict("Brainstorm some ideas combining VR and fitness:"))In [ ]:
import pandas
training_data = pandas.DataFrame(
data=[
{"input_text": "Input 1", "output_text": "Output 1"},
{"input_text": "Input 2", "output_text": "Output 2"},
{"input_text": "Input 3", "output_text": "Output 3"},
{"input_text": "Input 4", "output_text": "Output 4"},
{"input_text": "Input 5", "output_text": "Output 5"},
{"input_text": "Input 6", "output_text": "Output 6"},
{"input_text": "Input 7", "output_text": "Output 7"},
{"input_text": "Input 8", "output_text": "Output 8"},
{"input_text": "Input 9", "output_text": "Output 9"},
{"input_text": "Input 10", "output_text": "Output 10"},
]
)
training_dataIn [ ]:
model4 = TextGenerationModel.from_pretrained("google/text-bison@001")
tuning_job = model4.tune_model(
training_data=training_data,
# Optional:
train_steps=10,
tuning_job_location="europe-west4",
tuned_model_location="us-central1",
)In [ ]:
tuned_model = tuning_job.get_tuned_model()
print(tuned_model.predict("Tell me some ideas combining VR and fitness:"))In [ ]:
import os
batch_job_1.delete()
batch_job_2.delete()
delete_bucket = False
if delete_bucket or os.getenv("IS_TESTING"):
! gsutil rm -rf {BUCKET_URI}
View on GitHub
Run in Colab