1
- import { Command } from "./types" ;
1
+ import { Command , HarmCategory , SafetyThreshold } from "./types" ;
2
2
3
3
import type {
4
4
ChatAskOptions ,
@@ -15,7 +15,7 @@ import type {
15
15
QueryResponseMap ,
16
16
} from "./types" ;
17
17
18
- import { getFileType , handleReader , pairToMessage } from "./utils" ;
18
+ import { SafetyError , getFileType , handleReader , pairToMessage } from "./utils" ;
19
19
20
20
const uploadFile = async ( {
21
21
file,
@@ -162,6 +162,7 @@ class Gemini {
162
162
163
163
static TEXT = "text" as const ;
164
164
static JSON = "json" as const ;
165
+ static SafetyThreshold = SafetyThreshold ;
165
166
166
167
constructor ( key : string , options : Partial < GeminiOptions > = { } ) {
167
168
if ( ! options . fetch && typeof fetch !== "function" ) {
@@ -281,7 +282,7 @@ class Gemini {
281
282
< F extends Format > ( format : F = Gemini . TEXT as F ) =>
282
283
( response : GeminiResponse ) : FormatType < F > => {
283
284
if ( response . candidates [ 0 ] . finishReason === "SAFETY" ) {
284
- throw new Error (
285
+ throw new SafetyError (
285
286
`Your prompt was blocked by Google. Here are the Harm Categories: \n${ JSON . stringify (
286
287
response . candidates [ 0 ] . safetyRatings ,
287
288
null ,
@@ -337,10 +338,35 @@ class Gemini {
337
338
maxOutputTokens : 2048 ,
338
339
data : [ ] ,
339
340
messages : [ ] ,
341
+ safetySettings : {
342
+ hate : Gemini . SafetyThreshold . BLOCK_SOME ,
343
+ sexual : Gemini . SafetyThreshold . BLOCK_SOME ,
344
+ harassment : Gemini . SafetyThreshold . BLOCK_SOME ,
345
+ dangerous : Gemini . SafetyThreshold . BLOCK_SOME ,
346
+ } ,
340
347
} ,
341
348
...options ,
342
349
} ;
343
350
351
+ const safety_settings = [
352
+ {
353
+ category : HarmCategory . HateSpeech ,
354
+ threshold : parsedOptions . safetySettings . hate ,
355
+ } ,
356
+ {
357
+ category : HarmCategory . SexuallyExplicit ,
358
+ threshold : parsedOptions . safetySettings . sexual ,
359
+ } ,
360
+ {
361
+ category : HarmCategory . Harassment ,
362
+ threshold : parsedOptions . safetySettings . harassment ,
363
+ } ,
364
+ {
365
+ category : HarmCategory . DangerousContent ,
366
+ threshold : parsedOptions . safetySettings . dangerous ,
367
+ } ,
368
+ ] ;
369
+
344
370
const command = parsedOptions . stream
345
371
? Command . StreamGenerate
346
372
: Command . Generate ;
@@ -378,6 +404,7 @@ class Gemini {
378
404
topP : parsedOptions . topP ,
379
405
topK : parsedOptions . topK ,
380
406
} ,
407
+ safety_settings,
381
408
} ;
382
409
383
410
const response : Response = await this . query (
@@ -440,6 +467,12 @@ class Chat {
440
467
...{
441
468
data : [ ] ,
442
469
format : Gemini . TEXT as F ,
470
+ safetySettings : {
471
+ hate : Gemini . SafetyThreshold . BLOCK_SOME ,
472
+ sexual : Gemini . SafetyThreshold . BLOCK_SOME ,
473
+ harassment : Gemini . SafetyThreshold . BLOCK_SOME ,
474
+ dangerous : Gemini . SafetyThreshold . BLOCK_SOME ,
475
+ } ,
443
476
} ,
444
477
...options ,
445
478
} ;
0 commit comments