karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.04k stars 111 forks source link

Retrieve the Anthropics API key using 'auth-source'. #273

Closed marcuskammer closed 3 months ago

marcuskammer commented 3 months ago

I wrote a function to retrieve the API secret for anthropics from auth source. Maybe you want to include it into your library?!

(defun get-anthropic-key ()
  "Retrieve the Anthropics API key using 'auth-source'."
  (require 'auth-source)
  (let* ((auth-source-creation-prompts
          '((secret . "API key for %u@%h: ")))
         (found (nth 0 (auth-source-search
                        :max 1
                        :host "api.anthropic.com"
                        :user "apikey"
                        :require '(:secret)
                        :create t)))
         (secret (plist-get found :secret)))
    (if (functionp secret)
        (funcall secret)
      secret)))
karthink commented 3 months ago

@marcuskammer Thanks!

Note that auth-source support for all backends is already provided by gptel-api-key-from-auth-source, which is the default value of gptel-api-key, so get-anthropic-key should not be required.

You can test this by setting the gptel backend to Anthropic, then running (gptel-api-key-from-auth-source). It should return the key.