Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow openai-key to be retrieved via a function #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ You will need to set up your API key before you can use this library.
(setq openai-key "[YOUR API KEY]")
```

Alternatively you can configure a function to retrieve the key from some
external source. A function, `openai-key-auth-source` is provided to retrieve
the key from an auth-source entry under the `:host` key `api.openai.com`

```elisp
(setq openai-key 'openai-key-auth-source)
```

For requests that need your user identifier,

```elisp
Expand Down
8 changes: 0 additions & 8 deletions openai-audio.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
;;;###autoload
(cl-defun openai-audio-create-transcription ( file callback
&key
(key openai-key)
(model "whisper-1")
prompt
response-format
Expand All @@ -54,24 +53,20 @@ 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)))))

;;;###autoload
(cl-defun openai-audio-create-translation ( file callback
&key
(key openai-key)
(model "whisper-1")
prompt
response-format
Expand All @@ -89,15 +84,12 @@ for more information. Arguments here refer to MODEL PROMPT, RESPONSE-FORMAT,
and TEMPERATURE."
(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)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
Expand Down
4 changes: 0 additions & 4 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 " 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 Down
4 changes: 0 additions & 4 deletions openai-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
;;;###autoload
(cl-defun openai-completion ( prompt callback
&key
(key openai-key)
(model "text-davinci-003")
suffix
max-tokens
Expand Down Expand Up @@ -65,8 +64,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 +81,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
4 changes: 0 additions & 4 deletions openai-edit.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

(cl-defun openai-edit-create ( input instruction callback
&key
(key openai-key)
(model "text-davinci-edit-001")
temperature
top-p
Expand All @@ -53,16 +52,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
4 changes: 0 additions & 4 deletions openai-embedding.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

(cl-defun openai-embedding-create ( input callback
&key
(key openai-key)
(model "text-embedding-ada-002")
(user openai-user))
"Creates an embedding vector representing the input text.
Expand All @@ -53,13 +52,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
14 changes: 2 additions & 12 deletions openai-engine.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
;;
;;; API

(cl-defun openai-engine-list ( callback
&key
(key openai-key))
(cl-defun openai-engine-list (callback)
"Lists the currently available (non-finetuned) models, and provides basic
information about each one such as the owner and availability.

Expand All @@ -46,16 +44,11 @@ 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)))))

(cl-defun openai-engine-retrieve ( engine-id callback
&key
(key openai-key))
(cl-defun openai-engine-retrieve (engine-id callback)
"Retrieves a model instance, providing basic information about it such as the
owner and availability.

Expand All @@ -67,9 +60,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
35 changes: 5 additions & 30 deletions openai-file.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
;;
;;; API

(cl-defun openai-file-list ( callback
&key
(key openai-key))
(cl-defun openai-file-list (callback)
"Return a list of files that belong to the user's organization.

The argument CALLBACK is execuated after request is made.
Expand All @@ -43,16 +41,11 @@ 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)))))

(cl-defun openai-file-upload ( file purpose callback
&key
(key openai-key))
(cl-defun openai-file-upload (file purpose callback)
"Upload a file that contain document(s) to be used across various
endpoints/features.

Expand All @@ -72,19 +65,14 @@ 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)))))

(cl-defun openai-file-delete ( file-id callback
&key
(key openai-key))
(cl-defun openai-file-delete (file-id callback)
"Delete a file.

The arument FILE-ID is id of the file to use for this request.
Expand All @@ -95,18 +83,13 @@ 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)))))

(cl-defun openai-file-retrieve ( file-id callback
&key
(key openai-key))
(cl-defun openai-file-retrieve (file-id callback)
"Return information about a specific file.

The arument FILE-ID is id of the file to use for this request.
Expand All @@ -117,18 +100,13 @@ 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)))))

(cl-defun openai-file-retrieve-content ( file-id callback
&key
(key openai-key))
(cl-defun openai-file-retrieve-content (file-id callback)
"Return the contents of the specified file

The arument FILE-ID is id of the file to use for this request.
Expand All @@ -139,11 +117,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
Loading