Skip to content

Commit

Permalink
refactor: move creation of headers inside openai-request (wip)
Browse files Browse the repository at this point in the history
All the callers of openai-request pass the Content-Type, Authorization
headers and specify the parser as json-read.

This change makes the defaults be

    :headers `(("Content-Type"  . "application/json")
-               ("Authorization" . ,(concat "Bearer " (openai--resolve-key openai-key)))
    :parser 'json-read

This is allow the key resolution call to be common (and private).
  • Loading branch information
sw1nn committed Apr 4, 2023
1 parent d835351 commit fb4785c
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 87 deletions.
3 changes: 0 additions & 3 deletions openai-audio.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ for more information. Arguments here refer to MODEL PROMPT, RESPONSE-FORMAT,
TEMPERATURE, and LANGUAGE."
(openai-request "https://api.openai.com/v1/audio/transcriptions"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("file" . ,file)
("prompt" . ,prompt)
("response_format" . ,response-format)
("temperature" . ,temperature)
("language" . ,language)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
9 changes: 0 additions & 9 deletions openai-chat.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
;;;###autoload
(cl-defun openai-chat ( messages callback
&key
(key openai-key)
(model "gpt-3.5-turbo")
temperature
top-p
Expand All @@ -59,8 +58,6 @@ for more information. Arguments here refer to MODEL, TEMPERATURE, TOP-P, N,
STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
(openai-request "https://api.openai.com/v1/chat/completions"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " (openai--resolve-key key))))
:data (openai--json-encode
`(("model" . ,model)
("messages" . ,messages)
Expand All @@ -74,7 +71,6 @@ STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
("frequency_penalty" . ,frequency-penalty)
("logit_bias" . ,logit-bias)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -92,11 +88,6 @@ STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
:type 'number
:group 'openai)

(defun openai--resolve-key (key)
(if (functionp key)
(funcall key)
key))

;;;###autoload
(defun openai-chat-say ()
"Start making a conversation to OpenAI.
Expand Down
3 changes: 0 additions & 3 deletions openai-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ TEMPERATURE, TOP-P, N, STREAM, LOGPROBS, ECHO, STOP, PRESENCE-PENALTY,
FREQUENCY-PENALTY, BEST-OF, and LOGIT-BIAS."
(openai-request "https://api.openai.com/v1/completions"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("prompt" . ,prompt)
Expand All @@ -84,7 +82,6 @@ FREQUENCY-PENALTY, BEST-OF, and LOGIT-BIAS."
("best_of" . ,best-of)
("logit_bias" . ,logit-bias)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
3 changes: 0 additions & 3 deletions openai-edit.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ The rest of the arugments are optional, please see OpenAI API reference page
for more information. Arguments here refer to TEMPERATURE, TOP-P, and N."
(openai-request "https://api.openai.com/v1/edits"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("input" . ,input)
("instruction" . ,instruction)
("temperature" . ,temperature)
("top_p" . ,top-p)
("n" . ,n)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
3 changes: 0 additions & 3 deletions openai-embedding.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ The rest of the arugments are optional, please see OpenAI API reference page
for more information. Arguments here refer to MODEL."
(openai-request "https://api.openai.com/v1/embeddings"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("input" . ,input)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
6 changes: 0 additions & 6 deletions openai-engine.el
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/engines"
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -67,9 +64,6 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request (format "https://api.openai.com/v1/engines/%s" engine-id)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
15 changes: 0 additions & 15 deletions openai-file.el
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/files"
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -72,12 +69,9 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/files"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("file" . ,file)
("purpose" . ,purpose)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -95,11 +89,8 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/files"
:type "DELETE"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -117,11 +108,8 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request (format "https://api.openai.com/v1/files/%s" file-id)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -139,11 +127,8 @@ Arguments KEY is global option; however, you can overwrite the value by passing
it in."
(openai-request (format "https://api.openai.com/v1/files/%s/content" file-id)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("file_id" . ,file-id)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
18 changes: 0 additions & 18 deletions openai-fine-tune.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ COMPUTE-CLASSIFICATION-METRICS, CLASSIFICATION-N-CLASSES,
CLASSIFICATION-POSITIVE-CLASS, CLASSIFICATION-BETAS, and SUFFIX"
(openai-request "https://api.openai.com/v1/fine-tunes"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("training_file" . ,training-file)
Expand All @@ -80,7 +78,6 @@ CLASSIFICATION-POSITIVE-CLASS, CLASSIFICATION-BETAS, and SUFFIX"
("classification_positive_class" . ,classification-positive-class)
("classification_betas" . ,classification-betas)
("suffix" . ,suffix)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -93,9 +90,6 @@ CLASSIFICATION-POSITIVE-CLASS, CLASSIFICATION-BETAS, and SUFFIX"
The argument CALLBACK is execuated after request is made."
(openai-request "https://api.openai.com/v1/fine-tunes"
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -110,9 +104,6 @@ The FINE-TUNE-ID of the fine-tune job.
The argument CALLBACK is execuated after request is made."
(openai-request (format "https://api.openai.com/v1/fine-tunes/%s" fine-tune-id)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -127,9 +118,6 @@ The FINE-TUNE-ID of the fine-tune job to cancel.
The argument CALLBACK is execuated after request is made."
(openai-request (format "https://api.openai.com/v1/fine-tunes/%s/cancel" fine-tune-id)
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -144,9 +132,6 @@ The FINE-TUNE-ID of the fine-tune job to get events for.
The argument CALLBACK is execuated after request is made."
(openai-request (format "https://api.openai.com/v1/fine-tunes/%s/events" fine-tune-id)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -161,9 +146,6 @@ The MODEL to delete.
The argument CALLBACK is execuated after request is made."
(openai-request (format "https://api.openai.com/v1/models/%s" model)
:type "DELETE"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
7 changes: 0 additions & 7 deletions openai-image.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ The rest of the arugments are optional, please see OpenAI API reference page
for more information. Arguments here refer to N, SIZE, and RESPONSE-FORMAT."
(openai-request "https://api.openai.com/v1/images/generations"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("prompt" . ,prompt)
("n" . ,n)
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -86,7 +83,6 @@ for more information. Arguments here refer to MASK, N, SIZE, and
RESPONSE-FORMAT."
(openai-request "https://api.openai.com/v1/images/edits"
:type "POST"
:headers `(("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("image" . ,image)
("prompt" . ,prompt)
Expand All @@ -95,7 +91,6 @@ RESPONSE-FORMAT."
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -121,15 +116,13 @@ for more information. Arguments here refer to MASK, N, SIZE, and
RESPONSE-FORMAT."
(openai-request "https://api.openai.com/v1/images/variations"
:type "POST"
:headers `(("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("image" . ,image)
("mask" . ,mask)
("n" . ,n)
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
6 changes: 0 additions & 6 deletions openai-model.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ Arguments KEY is global options; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/models"
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand All @@ -54,9 +51,6 @@ Arguments KEY is global options; however, you can overwrite the value by passing
it in."
(openai-request (format "https://api.openai.com/v1/models/%s" model)
:type "GET"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
3 changes: 0 additions & 3 deletions openai-moderation.el
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ Arguments KEY is global options; however, you can overwrite the value by passing
it in."
(openai-request "https://api.openai.com/v1/embeddings"
:type "POST"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " key)))
:data (openai--json-encode
`(("model" . ,model)
("input" . ,input)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
44 changes: 33 additions & 11 deletions openai.el
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,20 @@
(funcall (plist-get (car auth-info) :secret))
(error "OpenAI API key not found in auth-source")))

(defun openai--resolve-key (key)
"If the given KEY is a function call it and return the result, otherwise return KEY."
(cond
((functionp key) (funcall key))
((not (string-empty-p key)) key)
(t (user-error "[INFO] Invalid API key, please set it to the correct value: %s" openai-key))))

(defvar openai-key ""
"Variable storing the openai key or a function name to retrieve it.
The function should take no arguments and return a string containing the key.
A function, `openai-key-auth-source', that retrieves the key from
auth-source is provided for convenience.
")
auth-source is provided for convenience.")


(defvar openai-user ""
Expand Down Expand Up @@ -116,20 +122,36 @@ See https://beta.openai.com/docs/guides/error-codes/api-errors."
(defvar openai-error nil
"Records for the last error.")

(defun openai--add-missing-defaults (p)
"Add Content-Type and Authorization headers to BODY if not present."

(message "%s" p)
(setq headers (plist-get p :headers))
(unless (assq "Content-Type" headers)
(setq headers (cons '("Content-Type" . "application/json") headers)))
(unless (assq "Authorization" headers)
(setq headers (cons `("Authorization" . ,(concat "Bearer " (openai--resolve-key openai-key))) headers)))
(setq parser (if-let ((parser (plist-get p :parser)))
parser
'json-read))
(thread-first p
(plist-put :parser parser)
(plist-put :headers `(quote ,headers))))

(defmacro openai-request (url &rest body)
"Wrapper for `request' function.
The URL is the url for `request' function; then BODY is the arguments for rest."
(declare (indent 1))
`(if (string-empty-p openai-key)
(user-error "[INFO] Invalid API key, please set it to the correct value: %s" openai-key)
(setq openai-error nil)
(request ,url
:error (cl-function
(lambda (&key response &allow-other-keys)
(setq openai-error response)
(openai--handle-error response)))
,@body)))
`(progn
(setq openai-error nil)
(request ,url
:error (cl-function
(lambda (&key response &allow-other-keys)
(setq openai-error response)
(openai--handle-error response)))
,@(openai--add-missing-defaults body)
)))

;;
;;; Util
Expand Down

0 comments on commit fb4785c

Please sign in to comment.