Last active
May 31, 2023 10:59
-
-
Save kkrishnan90/161b262f6a4473e77ed8fc6455ae3fc8 to your computer and use it in GitHub Desktop.
Leveraging Google Cloud GenAI Python API using service account key file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a service account and associate a "Vertex AI User" role | |
import google.auth | |
import google.auth.transport.requests | |
from google.oauth2 import service_account | |
import vertexai | |
from vertexai.preview.language_models import TextGenerationModel | |
key_path = "<YOUR_KEY_PATH>" | |
credentials = service_account.Credentials.from_service_account_file(key_path,scopes=['https://www.googleapis.com/auth/cloud-platform']) | |
auth_req = google.auth.transport.requests.Request() | |
credentials.refresh(auth_req) | |
def predict_large_language_model_sample( | |
project_id: str, | |
model_name: str, | |
temperature: float, | |
max_decode_steps: int, | |
top_p: float, | |
top_k: int, | |
content: str, | |
location: str = "us-central1", | |
tuned_model_name: str = "", | |
) : | |
"""Predict using a Large Language Model.""" | |
vertexai.init(project=project_id, location=location, credentials=credentials) | |
model = TextGenerationModel.from_pretrained(model_name) | |
if tuned_model_name: | |
model = model.get_tuned_model(tuned_model_name) | |
response = model.predict( | |
content, | |
temperature=temperature, | |
max_output_tokens=max_decode_steps, | |
top_k=top_k, | |
top_p=top_p,) | |
print(f"Response from Model: {response.text}") | |
predict_large_language_model_sample("<PROJECT_ID>", "text-bison@001", 0.2, 768, 0.8, 40, '<YOUR_PROMPT_HERE>', "us-central1") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment