Kaynağa Gözat

feat: add google api safety setting

YeungYeah 1 yıl önce
ebeveyn
işleme
74986803db

+ 11 - 8
app/client/platforms/google.ts

@@ -64,6 +64,9 @@ export class GeminiProApi implements LLMApi {
     // if (visionModel && messages.length > 1) {
     //   options.onError?.(new Error("Multiturn chat is not enabled for models/gemini-pro-vision"));
     // }
+
+    const accessStore = useAccessStore.getState();
+
     const modelConfig = {
       ...useAppConfig.getState().modelConfig,
       ...useChatStore.getState().currentSession().mask.modelConfig,
@@ -85,25 +88,23 @@ export class GeminiProApi implements LLMApi {
       safetySettings: [
         {
           category: "HARM_CATEGORY_HARASSMENT",
-          threshold: "BLOCK_NONE",
+          threshold: accessStore.googleSafetySettings,
         },
         {
           category: "HARM_CATEGORY_HATE_SPEECH",
-          threshold: "BLOCK_NONE",
+          threshold: accessStore.googleSafetySettings,
         },
         {
           category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
-          threshold: "BLOCK_NONE",
+          threshold: accessStore.googleSafetySettings,
         },
         {
           category: "HARM_CATEGORY_DANGEROUS_CONTENT",
-          threshold: "BLOCK_NONE",
+          threshold: accessStore.googleSafetySettings,
         },
       ],
     };
 
-    const accessStore = useAccessStore.getState();
-
     let baseUrl = "";
 
     if (accessStore.useCustomConfig) {
@@ -120,7 +121,9 @@ export class GeminiProApi implements LLMApi {
 
       if (!baseUrl) {
         baseUrl = isApp
-          ? DEFAULT_API_HOST + "/api/proxy/google/" + Google.ChatPath(modelConfig.model)
+          ? DEFAULT_API_HOST +
+            "/api/proxy/google/" +
+            Google.ChatPath(modelConfig.model)
           : this.path(Google.ChatPath(modelConfig.model));
       }
 
@@ -139,7 +142,7 @@ export class GeminiProApi implements LLMApi {
         () => controller.abort(),
         REQUEST_TIMEOUT_MS,
       );
-      
+
       if (shouldStream) {
         let responseText = "";
         let remainText = "";

+ 30 - 0
app/components/settings.tsx

@@ -54,6 +54,7 @@ import {
   Anthropic,
   Azure,
   Google,
+  GoogleSafetySettingsThreshold,
   OPENAI_BASE_URL,
   Path,
   RELEASE_URL,
@@ -1122,6 +1123,35 @@ export function Settings() {
                           }
                         ></input>
                       </ListItem>
+                      <ListItem
+                        title={
+                          Locale.Settings.Access.Google.GoogleSafetySettings
+                            .Title
+                        }
+                        subTitle={
+                          Locale.Settings.Access.Google.GoogleSafetySettings
+                            .SubTitle
+                        }
+                      >
+                        <Select
+                          value={accessStore.googleSafetySettings}
+                          onChange={(e) => {
+                            accessStore.update(
+                              (access) =>
+                                (access.googleSafetySettings = e.target
+                                  .value as GoogleSafetySettingsThreshold),
+                            );
+                          }}
+                        >
+                          {Object.entries(GoogleSafetySettingsThreshold).map(
+                            ([k, v]) => (
+                              <option value={v} key={k}>
+                                {k}
+                              </option>
+                            ),
+                          )}
+                        </Select>
+                      </ListItem>
                     </>
                   )}
                   {accessStore.provider === ServiceProvider.Anthropic && (

+ 9 - 0
app/constant.ts

@@ -72,6 +72,15 @@ export enum ServiceProvider {
   Anthropic = "Anthropic",
 }
 
+// Google API safety settings, see https://ai.google.dev/gemini-api/docs/safety-settings
+// BLOCK_NONE will not block any content, and BLOCK_ONLY_HIGH will block only high-risk content.
+export enum GoogleSafetySettingsThreshold {
+  BLOCK_NONE = "BLOCK_NONE",
+  BLOCK_ONLY_HIGH = "BLOCK_ONLY_HIGH",
+  BLOCK_MEDIUM_AND_ABOVE = "BLOCK_MEDIUM_AND_ABOVE",
+  BLOCK_LOW_AND_ABOVE = "BLOCK_LOW_AND_ABOVE",
+}
+
 export enum ModelProvider {
   GPT = "GPT",
   GeminiPro = "GeminiPro",

+ 4 - 0
app/locales/cn.ts

@@ -346,6 +346,10 @@ const cn = {
           Title: "API 版本(仅适用于 gemini-pro)",
           SubTitle: "选择一个特定的 API 版本",
         },
+        GoogleSafetySettings: {
+          Title: "Google 安全过滤级别",
+          SubTitle: "设置内容过滤级别",
+        },
       },
       CustomModel: {
         Title: "自定义模型名",

+ 4 - 0
app/locales/en.ts

@@ -354,6 +354,10 @@ const en: LocaleType = {
           Title: "API Version (specific to gemini-pro)",
           SubTitle: "Select a specific API version",
         },
+        GoogleSafetySettings: {
+          Title: "Google Safety Settings",
+          SubTitle: "Select a safety filtering level",
+        },
       },
     },
 

+ 2 - 0
app/store/access.ts

@@ -1,6 +1,7 @@
 import {
   ApiPath,
   DEFAULT_API_HOST,
+  GoogleSafetySettingsThreshold,
   ServiceProvider,
   StoreKey,
 } from "../constant";
@@ -36,6 +37,7 @@ const DEFAULT_ACCESS_STATE = {
   googleUrl: "",
   googleApiKey: "",
   googleApiVersion: "v1",
+  googleSafetySettings: GoogleSafetySettingsThreshold.BLOCK_ONLY_HIGH,
 
   // anthropic
   anthropicApiKey: "",