From a500c76a685c12dd7dc37d3a999eb4d5fbab3152 Mon Sep 17 00:00:00 2001 From: algal Date: Thu, 30 Mar 2023 17:34:11 -0700 Subject: [PATCH] Encode a lambda-provided API key as utf-8 If the user provides the OpenAPI key via a function, as is the case by default if the user puts credentials in an auth-sources resource like .authinfo or .authinfo.gpg, then it is necessary to encode the function's returned value into utf-8 before passing it onward to build the HTTP request. This commit ensures that happen. Why is this necessary, given that the API key contains only alphanumeric characters and therefore should be byte-for-byte the same in utf-8 as in us-acii? I don't know. It may because emacs's url-http.el library concats many strings together, and they all need to be identically encoded before they can be combined correctly. Whatever the reason, this fix works and allows you to send prompts which include Unicode characters that require multibyte encodings in UTF-8 --- gptel.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gptel.el b/gptel.el index 606bc63..bdbd6c0 100644 --- a/gptel.el +++ b/gptel.el @@ -202,7 +202,9 @@ By default, \"openai.com\" is used as HOST and \"apikey\" as USER." :host (or host "openai.com") :user (or user "apikey"))) :secret))) - (if (functionp secret) (funcall secret) secret) + (if (functionp secret) + (encode-coding-string (funcall secret) 'utf-8) + secret) (user-error "No `gptel-api-key' found in the auth source"))) (defun gptel--api-key ()