我正在使用 VertexAI API 开发 RAG 应用程序。 我的一个问题包含“roubar”一词,它不断触发安全过滤器,例如:
...
Candidate:
{
"index": 0,
"finish_reason": "SAFETY",
"safety_ratings": [
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE",
"probability_score": 0.059326172,
"severity": "HARM_SEVERITY_NEGLIGIBLE",
"severity_score": 0.10986328
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "MEDIUM",
"blocked": true,
"probability_score": 0.69921875,
"severity": "HARM_SEVERITY_MEDIUM",
"severity_score": 0.45703125
}
...
按照 VertexAI 文档,我尝试禁用 HARM_CATEGORY_DANGEROUS_CONTENT 或将其更改为仅过滤 HIGH。
这是我的代码:
import os
import json
from dotenv import load_dotenv
import vertexai
from vertexai.generative_models import GenerativeModel, GenerationConfig, SafetySetting
from promtps import SUBJECT_PROMPT, SYSTEM_PROMPT_3
from utils import VectorDB
load_dotenv()
# Configs ------------------------------------------------------------------------------
vertexai.init(
project=os.environ["VERTEXAI_PROJECT_ID"],
location=os.environ["VERTEXAI_REGION"]
)
safety_settings = [
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_NONE,
)
]
# RAG flow -----------------------------------------------------------------------------
def subject_identifier(query: str):
"""
Classify the query subject.
"""
generation_config_subject = GenerationConfig(
response_mime_type="text/plain",
temperature=0
)
model_subject = GenerativeModel(
model_name=os.environ["VERTEXAI_MODEL"],
system_instruction=SUBJECT_PROMPT
)
response = model_subject.generate_content(query, generation_config=generation_config_subject)
return response.text.strip()
def retrieve(subject: str, query:str):
"""
Retrive documents for the given query.
"""
vector_db = VectorDB(database_directory=f"data/db/{subject}")
chroma_collection = vector_db.get_or_create_chroma_collection()
results = chroma_collection.query(query_texts=[query], n_results=4)
return results['documents']
def generate(query):
"""
Embbeds the retrived documents into SYSTEM_PROMPT and generate answers for the given query.
"""
subject = subject_identifier(query=query)
retrieved_documents = retrieve(subject=subject, query=query)
generation_config = GenerationConfig(
response_mime_type="application/json",
temperature=0
)
model = GenerativeModel(
model_name=os.environ["VERTEXAI_MODEL"],
system_instruction=SYSTEM_PROMPT_3.format(context=retrieved_documents),
)
response = model.generate_content(
query,
generation_config=generation_config,
safety_settings=safety_settings
)
return json.loads(response.text)
我做错了什么吗?
根据您的屏幕截图,“roubar”触发了危险内容类别下的安全设置,概率为中等。设置过滤器必须设置为“关闭”或“阻止少数”。代码必须是这样的,threshold=SafetySetting.HarmBlockThreshold.OFF或threshold=SafetySetting.HarmBlockThreshold.BLOCK_ONLY_HIGH而不是.BLOCK_NONE以避免触发安全过滤器“概率”:“MEDIUM”。
根据如何配置安全过滤器:
Google Cloud 控制台可让您为每个安全属性配置阈值。安全过滤器仅使用概率分数。
没有使用严重性分数的选项。 Google Cloud 控制台提供以下阈值:
关闭(默认):不阻止自动响应。
Block Few:当概率得分较高时进行阻止。
阻止某些:当概率分数为中或高时阻止。
阻止最多:当概率分数为低、中或高时阻止。
例如,如果您将阻止设置设置为
对危险内容类别进行阻止,所有很可能是危险内容的内容都会被阻止。任何概率较低的事情都是允许的。默认阈值是阻止某些。