如何为 Vertex AI 的批量预测作业设置安全设置?
似乎无法在谷歌/文档上找到任何关于此的内容。
TypeError: BatchPredictionJob.submit() got an unexpected keyword argument 'model_parameters'
# File Format
# {"request":{"contents": [{"role": "user", "parts": [{"text": "Translate this sentence to French: Hello, how are you today?"}]}]}}
import os
import vertexai
from vertexai.preview.batch_prediction import BatchPredictionJob
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
def batch_predict_gemini_createjob(
input_uri: str, output_uri: str
) -> BatchPredictionJob:
"""Perform batch text prediction using a Gemini AI model.
Args:
input_uri (str): URI of the input file in BigQuery table or Google Cloud Storage.
Example: "gs://[BUCKET]/[DATASET].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]"
output_uri (str): URI of the output folder, in BigQuery table or Google Cloud Storage.
Example: "gs://[BUCKET]/[OUTPUT].jsonl" OR "bq://[PROJECT].[DATASET].[TABLE]"
Returns:
batch_prediction_job: The batch prediction job object containing details of the job.
"""
import time
input_uri ="gs://ge/in/vertex_batch_input.jsonl"
output_uri ="gs://ge/out/"
# Initialize vertexai
vertexai.init(project=PROJECT_ID, location="us-central1")
# Submit a batch prediction job with Gemini model
batch_prediction_job = BatchPredictionJob.submit(
source_model="gemini-1.5-flash-002",
input_dataset=input_uri,
output_uri_prefix=output_uri,
model_parameters={
"safety_settings": [
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"method": "SEVERITY",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"method": "SEVERITY",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"method": "SEVERITY",
"threshold": "BLOCK_ONLY_HIGH"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"method": "SEVERITY",
"threshold": "BLOCK_ONLY_HIGH"
}
]
}
)
基于这些代码示例,在创建批量预测作业时,创建了单独的变量:
model_parameters_dict = {} model_parameters = json_format.ParseDict(model_parameters_dict, Value())
尝试使用此格式设置安全过滤器:
safety_settings = [
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_ONLY_HIGH
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_ONLY_HIGH
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_ONLY_HIGH
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_ONLY_HIGH
),
]