Skip to content

Commit 7b4474e

Browse files
committed
refactor: move creation of headers inside openai-request (wip)
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).
1 parent d835351 commit 7b4474e

12 files changed

+47
-142
lines changed

openai-audio.el

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
;;;###autoload
3535
(cl-defun openai-audio-create-transcription ( file callback
3636
&key
37-
(key openai-key)
3837
(model "whisper-1")
3938
prompt
4039
response-format
@@ -54,24 +53,20 @@ for more information. Arguments here refer to MODEL PROMPT, RESPONSE-FORMAT,
5453
TEMPERATURE, and LANGUAGE."
5554
(openai-request "https://api.openai.com/v1/audio/transcriptions"
5655
:type "POST"
57-
:headers `(("Content-Type" . "application/json")
58-
("Authorization" . ,(concat "Bearer " key)))
5956
:data (openai--json-encode
6057
`(("model" . ,model)
6158
("file" . ,file)
6259
("prompt" . ,prompt)
6360
("response_format" . ,response-format)
6461
("temperature" . ,temperature)
6562
("language" . ,language)))
66-
:parser 'json-read
6763
:complete (cl-function
6864
(lambda (&key data &allow-other-keys)
6965
(funcall callback data)))))
7066

7167
;;;###autoload
7268
(cl-defun openai-audio-create-translation ( file callback
7369
&key
74-
(key openai-key)
7570
(model "whisper-1")
7671
prompt
7772
response-format
@@ -89,15 +84,12 @@ for more information. Arguments here refer to MODEL PROMPT, RESPONSE-FORMAT,
8984
and TEMPERATURE."
9085
(openai-request "https://api.openai.com/v1/audio/transcriptions"
9186
:type "POST"
92-
:headers `(("Content-Type" . "application/json")
93-
("Authorization" . ,(concat "Bearer " key)))
9487
:data (openai--json-encode
9588
`(("model" . ,model)
9689
("file" . ,file)
9790
("prompt" . ,prompt)
9891
("response_format" . ,response-format)
9992
("temperature" . ,temperature)))
100-
:parser 'json-read
10193
:complete (cl-function
10294
(lambda (&key data &allow-other-keys)
10395
(funcall callback data)))))

openai-chat.el

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
;;;###autoload
3535
(cl-defun openai-chat ( messages callback
3636
&key
37-
(key openai-key)
3837
(model "gpt-3.5-turbo")
3938
temperature
4039
top-p
@@ -59,8 +58,6 @@ for more information. Arguments here refer to MODEL, TEMPERATURE, TOP-P, N,
5958
STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
6059
(openai-request "https://api.openai.com/v1/chat/completions"
6160
:type "POST"
62-
:headers `(("Content-Type" . "application/json")
63-
("Authorization" . ,(concat "Bearer " (openai--resolve-key key))))
6461
:data (openai--json-encode
6562
`(("model" . ,model)
6663
("messages" . ,messages)
@@ -74,7 +71,6 @@ STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
7471
("frequency_penalty" . ,frequency-penalty)
7572
("logit_bias" . ,logit-bias)
7673
("user" . ,user)))
77-
:parser 'json-read
7874
:complete (cl-function
7975
(lambda (&key data &allow-other-keys)
8076
(funcall callback data)))))
@@ -92,11 +88,6 @@ STREAM, STOP, MAX-TOKENS, PRESENCE-PENALTY, FREQUENCY-PENALTY, and LOGIT-BIAS."
9288
:type 'number
9389
:group 'openai)
9490

95-
(defun openai--resolve-key (key)
96-
(if (functionp key)
97-
(funcall key)
98-
key))
99-
10091
;;;###autoload
10192
(defun openai-chat-say ()
10293
"Start making a conversation to OpenAI.

openai-completion.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
;;;###autoload
3535
(cl-defun openai-completion ( prompt callback
3636
&key
37-
(key openai-key)
3837
(model "text-davinci-003")
3938
suffix
4039
max-tokens
@@ -65,8 +64,6 @@ TEMPERATURE, TOP-P, N, STREAM, LOGPROBS, ECHO, STOP, PRESENCE-PENALTY,
6564
FREQUENCY-PENALTY, BEST-OF, and LOGIT-BIAS."
6665
(openai-request "https://api.openai.com/v1/completions"
6766
:type "POST"
68-
:headers `(("Content-Type" . "application/json")
69-
("Authorization" . ,(concat "Bearer " key)))
7067
:data (openai--json-encode
7168
`(("model" . ,model)
7269
("prompt" . ,prompt)
@@ -84,7 +81,6 @@ FREQUENCY-PENALTY, BEST-OF, and LOGIT-BIAS."
8481
("best_of" . ,best-of)
8582
("logit_bias" . ,logit-bias)
8683
("user" . ,user)))
87-
:parser 'json-read
8884
:complete (cl-function
8985
(lambda (&key data &allow-other-keys)
9086
(funcall callback data)))))

openai-edit.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
(cl-defun openai-edit-create ( input instruction callback
3636
&key
37-
(key openai-key)
3837
(model "text-davinci-edit-001")
3938
temperature
4039
top-p
@@ -53,16 +52,13 @@ The rest of the arugments are optional, please see OpenAI API reference page
5352
for more information. Arguments here refer to TEMPERATURE, TOP-P, and N."
5453
(openai-request "https://api.openai.com/v1/edits"
5554
:type "POST"
56-
:headers `(("Content-Type" . "application/json")
57-
("Authorization" . ,(concat "Bearer " key)))
5855
:data (openai--json-encode
5956
`(("model" . ,model)
6057
("input" . ,input)
6158
("instruction" . ,instruction)
6259
("temperature" . ,temperature)
6360
("top_p" . ,top-p)
6461
("n" . ,n)))
65-
:parser 'json-read
6662
:complete (cl-function
6763
(lambda (&key data &allow-other-keys)
6864
(funcall callback data)))))

openai-embedding.el

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
(cl-defun openai-embedding-create ( input callback
3636
&key
37-
(key openai-key)
3837
(model "text-embedding-ada-002")
3938
(user openai-user))
4039
"Creates an embedding vector representing the input text.
@@ -53,13 +52,10 @@ The rest of the arugments are optional, please see OpenAI API reference page
5352
for more information. Arguments here refer to MODEL."
5453
(openai-request "https://api.openai.com/v1/embeddings"
5554
:type "POST"
56-
:headers `(("Content-Type" . "application/json")
57-
("Authorization" . ,(concat "Bearer " key)))
5855
:data (openai--json-encode
5956
`(("model" . ,model)
6057
("input" . ,input)
6158
("user" . ,user)))
62-
:parser 'json-read
6359
:complete (cl-function
6460
(lambda (&key data &allow-other-keys)
6561
(funcall callback data)))))

openai-engine.el

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
;;
3535
;;; API
3636

37-
(cl-defun openai-engine-list ( callback
38-
&key
39-
(key openai-key))
37+
(cl-defun openai-engine-list (callback)
4038
"Lists the currently available (non-finetuned) models, and provides basic
4139
information about each one such as the owner and availability.
4240
@@ -46,16 +44,11 @@ Arguments KEY is global option; however, you can overwrite the value by passing
4644
it in."
4745
(openai-request "https://api.openai.com/v1/engines"
4846
:type "GET"
49-
:headers `(("Content-Type" . "application/json")
50-
("Authorization" . ,(concat "Bearer " key)))
51-
:parser 'json-read
5247
:complete (cl-function
5348
(lambda (&key data &allow-other-keys)
5449
(funcall callback data)))))
5550

56-
(cl-defun openai-engine-retrieve ( engine-id callback
57-
&key
58-
(key openai-key))
51+
(cl-defun openai-engine-retrieve (engine-id callback)
5952
"Retrieves a model instance, providing basic information about it such as the
6053
owner and availability.
6154
@@ -67,9 +60,6 @@ Arguments KEY is global option; however, you can overwrite the value by passing
6760
it in."
6861
(openai-request (format "https://api.openai.com/v1/engines/%s" engine-id)
6962
:type "GET"
70-
:headers `(("Content-Type" . "application/json")
71-
("Authorization" . ,(concat "Bearer " key)))
72-
:parser 'json-read
7363
:complete (cl-function
7464
(lambda (&key data &allow-other-keys)
7565
(funcall callback data)))))

openai-file.el

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
;;
3333
;;; API
3434

35-
(cl-defun openai-file-list ( callback
36-
&key
37-
(key openai-key))
35+
(cl-defun openai-file-list (callback)
3836
"Return a list of files that belong to the user's organization.
3937
4038
The argument CALLBACK is execuated after request is made.
@@ -43,16 +41,11 @@ Arguments KEY is global option; however, you can overwrite the value by passing
4341
it in."
4442
(openai-request "https://api.openai.com/v1/files"
4543
:type "GET"
46-
:headers `(("Content-Type" . "application/json")
47-
("Authorization" . ,(concat "Bearer " key)))
48-
:parser 'json-read
4944
:complete (cl-function
5045
(lambda (&key data &allow-other-keys)
5146
(funcall callback data)))))
5247

53-
(cl-defun openai-file-upload ( file purpose callback
54-
&key
55-
(key openai-key))
48+
(cl-defun openai-file-upload (file purpose callback)
5649
"Upload a file that contain document(s) to be used across various
5750
endpoints/features.
5851
@@ -72,19 +65,14 @@ Arguments KEY is global option; however, you can overwrite the value by passing
7265
it in."
7366
(openai-request "https://api.openai.com/v1/files"
7467
:type "POST"
75-
:headers `(("Content-Type" . "application/json")
76-
("Authorization" . ,(concat "Bearer " key)))
7768
:data (openai--json-encode
7869
`(("file" . ,file)
7970
("purpose" . ,purpose)))
80-
:parser 'json-read
8171
:complete (cl-function
8272
(lambda (&key data &allow-other-keys)
8373
(funcall callback data)))))
8474

85-
(cl-defun openai-file-delete ( file-id callback
86-
&key
87-
(key openai-key))
75+
(cl-defun openai-file-delete (file-id callback)
8876
"Delete a file.
8977
9078
The arument FILE-ID is id of the file to use for this request.
@@ -95,18 +83,13 @@ Arguments KEY is global option; however, you can overwrite the value by passing
9583
it in."
9684
(openai-request "https://api.openai.com/v1/files"
9785
:type "DELETE"
98-
:headers `(("Content-Type" . "application/json")
99-
("Authorization" . ,(concat "Bearer " key)))
10086
:data (openai--json-encode
10187
`(("file_id" . ,file-id)))
102-
:parser 'json-read
10388
:complete (cl-function
10489
(lambda (&key data &allow-other-keys)
10590
(funcall callback data)))))
10691

107-
(cl-defun openai-file-retrieve ( file-id callback
108-
&key
109-
(key openai-key))
92+
(cl-defun openai-file-retrieve (file-id callback)
11093
"Return information about a specific file.
11194
11295
The arument FILE-ID is id of the file to use for this request.
@@ -117,18 +100,13 @@ Arguments KEY is global option; however, you can overwrite the value by passing
117100
it in."
118101
(openai-request (format "https://api.openai.com/v1/files/%s" file-id)
119102
:type "GET"
120-
:headers `(("Content-Type" . "application/json")
121-
("Authorization" . ,(concat "Bearer " key)))
122103
:data (openai--json-encode
123104
`(("file_id" . ,file-id)))
124-
:parser 'json-read
125105
:complete (cl-function
126106
(lambda (&key data &allow-other-keys)
127107
(funcall callback data)))))
128108

129-
(cl-defun openai-file-retrieve-content ( file-id callback
130-
&key
131-
(key openai-key))
109+
(cl-defun openai-file-retrieve-content (file-id callback)
132110
"Return the contents of the specified file
133111
134112
The arument FILE-ID is id of the file to use for this request.
@@ -139,11 +117,8 @@ Arguments KEY is global option; however, you can overwrite the value by passing
139117
it in."
140118
(openai-request (format "https://api.openai.com/v1/files/%s/content" file-id)
141119
:type "GET"
142-
:headers `(("Content-Type" . "application/json")
143-
("Authorization" . ,(concat "Bearer " key)))
144120
:data (openai--json-encode
145121
`(("file_id" . ,file-id)))
146-
:parser 'json-read
147122
:complete (cl-function
148123
(lambda (&key data &allow-other-keys)
149124
(funcall callback data)))))

0 commit comments

Comments
 (0)