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

Change how OpenAI class is instantiated #534

Merged
merged 3 commits into from
Nov 11, 2023
Merged

Change how OpenAI class is instantiated #534

merged 3 commits into from
Nov 11, 2023

Conversation

franciscodr
Copy link
Contributor

@franciscodr franciscodr commented Nov 11, 2023

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:

  • Make the token argument for the OpenAI constructor non-nullable, so a valid token must be provided to use this class.
  • Change the FromEnvironment from a field to a function, which we would use when we want to provide the token through an environment variable.

Those changes are reflected in the codebase in this way:

  • Replace the use of FromEnvironment by calling fromEnvironment()
  • If we don't want to provide the token explicitly, we will use OpenAI.fromEnvironment() instead of calling the constructor without arguments. In fact, that is no longer possible because the token 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

@franciscodr franciscodr changed the title Change how OoenAI class is instantiated Change how OpenAI class is instantiated Nov 11, 2023
@franciscodr franciscodr merged commit bd5d7da into main Nov 11, 2023
5 checks passed
@franciscodr franciscodr deleted the open-ai-builder branch November 11, 2023 12:09
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.

None yet

2 participants