Change how OpenAI class is instantiated #534
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request tries to fix an issue detected when we wanted to used the OpenAI module by providing the API token explicitly instead of using an environment variable.
It should be possible to pass the API token as a constructor argument. However, when the application is launched, it crashes with an error message indicating that it could not locate the environment variable. Based on the code, the class attempts to read the environment variable only if the constructor does not provide the token.
We discovered during debugging that the companion object in our code has a field named
FromEnvironment
. This field invokes the OpenAI constructor without any arguments. As a result, when the class instance is initialized, it reads the token from the environment variable. Therefore, even though we explicitly provide a token, the code still relies on the environment variable.I've made a couple of changes to resolve that:
Those changes are reflected in the codebase in this way:
FromEnvironment
by callingfromEnvironment()
OpenAI.fromEnvironment()
instead of calling the constructor without arguments. In fact, that is no longer possible because thetoken
argument is no longer optional, so you must at least pass the token to call the constructor:OpenAI(token = t)
PS: the
main
branch is broken. I have disabled the new OpenAI client modules for now