xenodium / chatgpt-shell

A multi-llm Emacs shell (ChatGPT, Claude, Gemini, Ollama, Perplexity) + editing integrations
https://lmno.lol/alvaro
GNU General Public License v3.0
866 stars 77 forks source link

Add support for Azure-OpenAI service in chatgpt-shell.el #145

Closed doctorguile closed 5 months ago

doctorguile commented 1 year ago

update the chatgpt-shell-auth-header function to generate the appropriate header based on the selected service.

Also, add new custom variables chatgpt-shell-azure-openai-deployment and chatgpt-shell-azure-openai-api-version for Azure-OpenAI configuration.

Modify chatgpt-shell--api-url function to construct the appropriate API URL based on the selected service.

xenodium commented 1 year ago

Thanks a lot for the PR! I'm not an Azure user, though I'm thinking we may want to try to consolidate service configurations. Maybe something along the lines of:

(defcustom chatgpt-shell-provider (chatgpt-shell-make-openai-provider)
  "ChatGPT service provider.

Defaults to `chatgpt-shell-make-openai-provider'.

Alternatively, use `chatgpt-shell-make-azure-openai-provider'.")

(cl-defun chatgpt-shell-make-openai-provider (&key (api-url-base "https://api.openai.com")
                                                  (api-url-path "/v1/chat/completions"))
  (list
   (cons 'header
         (format "Authorization: Bearer %s" (chatgpt-shell-openai-key)))
   (cons 'url
         (concat api-url-base chatgpt-shell-api-url-path))))

(cl-defun chatgpt-shell-make-azure-openai-provider (&key (api-url-base "https://api.openai.com")
                                                         deployment-name
                                                         (version "2023-03-15-preview"))
  (unless deployment-name
    (user-error "`deployment-name' must be set"))
  (list
   (cons 'header
         (format "api-key: %s" (chatgpt-shell-openai-key)))
   (cons 'url
         (format "/openai/deployments/%s/chat/completions?api-version=%s"
                 deployment-name version))))

Client code avoids switching with cond, and we use something like:

(map-elt chatgpt-shell-provider  'header)
(map-elt chatgpt-shell-provider 'url)

I've not tested this code. We may need to swap some of the values with lambdas and (funcall (map-elt chatgpt-shell-provider 'header)) to prevent issues at package load time.

xenodium commented 5 months ago

IIRC this is supported these days. Closing, but please reopen if otherwise.