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

Conversation

sw1nn
Copy link

@sw1nn sw1nn commented Apr 4, 2023

Enhance the handling of the openai-key variable. If it is a string it will be interpreted as the raw key. If it is a function then that function will be called to retrieve the key. This allows storing the key in some secure store (e.g. auth-source)

Enhance the handling of the openai-key variable. If it is a string it
will be interpreted as the raw key. If it is a function then that
function will be called to retrieve the key. This allows storing the
key in some secure store (e.g. auth-source)
Copy link
Member

@jcs090218 jcs090218 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits. Overall looks good. :D

Edit: Can you update the document README regarding the new function openai-key-auth-source? Thanks!

openai.el Outdated Show resolved Hide resolved
openai.el Outdated Show resolved Hide resolved
openai-chat.el Outdated
@@ -60,7 +60,7 @@ 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)))
("Authorization" . ,(concat "Bearer " (openai--resolve-key key))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple entrances for this, you might want to apply it to every entrances. For example,

("Authorization" . ,(concat "Bearer " key)))

("Authorization" . ,(concat "Bearer " key)))

and all other files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I also note that this would be required in the other repos that depend on openai, are you amenable to similar PRs in those repos

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think perhaps moving the creation of the default headers into openai-request might be bettter, so the :headers could be removed from all those callsites ? I guess any headers that were specified could be merged on top.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I also note that this would be required in the other repos that depend on openai, are you amenable to similar PRs in those repos

Other packages fully rely on this package, I don't think we will need to change anything from those packages.

I think perhaps moving the creation of the default headers into openai-request might be bettter, so the :headers could be removed from all those callsites ? I guess any headers that were specified could be merged on top.

This sounds reasonable. Some endpoints (mostly GET) don't require the key; but now, every endpoint seems to require it. 😕 As of right now, I might prefer to leave it as it is since I do not know when they are going to change it (require key or don't require key). WDYT?

Copy link
Author

@sw1nn sw1nn Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I also note that this would be required in the other repos that depend on openai, are you amenable to similar PRs in those repos

The call to resolve key should move to inside openai-request - then no call sites need to change, but this is awkward since the key has already been put in a Authorization header by this point. By moving the creation of the authorization header into openai-request we can tidy that up.

openai-chat.el Outdated Show resolved Hide resolved
openai.el Outdated Show resolved Hide resolved
@sw1nn sw1nn force-pushed the openai-key-from-auth-source branch from 3622942 to d835351 Compare April 4, 2023 09:52
@sw1nn
Copy link
Author

sw1nn commented Apr 4, 2023

baba8fc moves the headers construction around, but the change is getting bigger now than I envisioned at first. It somewhat cleans up all the calls to openai-request but possibly some clarity is lost.

@sw1nn sw1nn marked this pull request as draft April 4, 2023 12:48
@sw1nn sw1nn force-pushed the openai-key-from-auth-source branch 2 times, most recently from fb4785c to 7b4474e Compare April 4, 2023 14:16
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).
@sw1nn sw1nn force-pushed the openai-key-from-auth-source branch from 7b4474e to 5c26a3c Compare April 4, 2023 14:19
Need to rework the macro a little to ensure that the openai-key is
bound at runtime not compile time.

TODO - does this even need to be a macro?
@sw1nn
Copy link
Author

sw1nn commented Apr 5, 2023

Change has become bigger than I thought - I'll keep chipping away in my local repo, but probably not worth a PR until the major restructuring discussed in #5 is fleshed out. Closing.

@sw1nn sw1nn closed this Apr 5, 2023
@jcs090218
Copy link
Member

Sorry for not being able to merge this feature request! I will make sure to add this feature once we've done restructuring the code base! 😓

@jcs090218
Copy link
Member

I've used some of your code, and the feature is integrated in #13. Hope you like it! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants