From ef8b9093d2a0a957be52437956245d2b3ffeaf98 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Wed, 7 Feb 2024 19:00:26 -0800 Subject: [PATCH] gptel-gemini: Use permissive API safety settings * gptel-gemini.el (gptel-make-gemini, gptel--request-data): The Gemini API misclassifies harmless questions (like "What's 2+2", see #208) as harmful. Use the most permissive safety settings the API offers. Also respect the value of `:stream` used when defining Gemini backends. --- gptel-gemini.el | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/gptel-gemini.el b/gptel-gemini.el index 27ce987..90119a3 100644 --- a/gptel-gemini.el +++ b/gptel-gemini.el @@ -60,7 +60,15 @@ (cl-defmethod gptel--request-data ((_backend gptel-gemini) prompts) "JSON encode PROMPTS for sending to Gemini." (let ((prompts-plist - `(:contents [,@prompts])) + `(:contents [,@prompts] + :safetySettings [(:category "HARM_CATEGORY_HARASSMENT" + :threshold "BLOCK_NONE") + (:category "HARM_CATEGORY_SEXUALLY_EXPLICIT" + :threshold "BLOCK_NONE") + (:category "HARM_CATEGORY_DANGEROUS_CONTENT" + :threshold "BLOCK_NONE") + (:category "HARM_CATEGORY_HATE_SPEECH" + :threshold "BLOCK_NONE")])) params) (when gptel-temperature (setq params @@ -103,7 +111,7 @@ ;;;###autoload (cl-defun gptel-make-gemini - (name &key header key stream + (name &key header key (stream nil) (host "generativelanguage.googleapis.com") (protocol "https") (models '("gemini-pro")) @@ -146,12 +154,17 @@ function that returns the key." :stream stream :key key :url - (lambda () - (concat protocol "://" host endpoint - (if gptel-stream - "streamGenerateContent" - "generateContent") - "?key=" (gptel--get-api-key)))))) + (if stream + (lambda () + (concat protocol "://" host endpoint + (if gptel-stream + "streamGenerateContent" + "generateContent") + "?key=" (gptel--get-api-key))) + (lambda () + (concat protocol "://" host endpoint + "generateContent" "?key=" + (gptel--get-api-key))))))) (prog1 backend (setf (alist-get name gptel--known-backends nil nil #'equal)