refactor: (GeminiProvider) optimize safety settings handling

- Extract safety threshold logic into getModelSafetySetting method
- gemini-exp-* models not support 'OFF', must use 'BLOCK_NONE'
This commit is contained in:
magicdmer 2025-01-15 16:49:30 +08:00 committed by kangfenmao
parent 57718dda6f
commit 42908e8834

View File

@ -112,10 +112,15 @@ export default class GeminiProvider extends BaseProvider {
} }
} }
private getModelSafetySetting(modelId: string): HarmBlockThreshold {
return modelId.includes('gemini-exp-') ? HarmBlockThreshold.BLOCK_NONE : "OFF" as HarmBlockThreshold
}
public async completions({ messages, assistant, onChunk, onFilterMessages }: CompletionsParams) { public async completions({ messages, assistant, onChunk, onFilterMessages }: CompletionsParams) {
const defaultModel = getDefaultModel() const defaultModel = getDefaultModel()
const model = assistant.model || defaultModel const model = assistant.model || defaultModel
const { contextCount, maxTokens, streamOutput } = getAssistantSettings(assistant) const { contextCount, maxTokens, streamOutput } = getAssistantSettings(assistant)
const safetyThreshold = this.getModelSafetySetting(model.id)
const userMessages = filterContextMessages(takeRight(messages, contextCount + 2)) const userMessages = filterContextMessages(takeRight(messages, contextCount + 2))
onFilterMessages(userMessages) onFilterMessages(userMessages)
@ -145,14 +150,26 @@ export default class GeminiProvider extends BaseProvider {
...this.getCustomParameters(assistant) ...this.getCustomParameters(assistant)
}, },
safetySettings: [ safetySettings: [
{ category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: 'OFF' as HarmBlockThreshold }, {
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: safetyThreshold
},
{ {
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: 'OFF' as HarmBlockThreshold threshold: safetyThreshold
}, },
{ category: HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: 'OFF' as HarmBlockThreshold }, {
{ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: 'OFF' as HarmBlockThreshold }, category: HarmCategory.HARM_CATEGORY_HARASSMENT,
{ category: 'HARM_CATEGORY_CIVIC_INTEGRITY' as HarmCategory, threshold: 'BLOCK_NONE' as HarmBlockThreshold } threshold: safetyThreshold
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: safetyThreshold
},
{
category: 'HARM_CATEGORY_CIVIC_INTEGRITY' as HarmCategory,
threshold: safetyThreshold
}
] ]
}, },
this.requestOptions this.requestOptions