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

Using $_ENV[""] in the OpenAIChat Class for e.g. Smyfony Error: You have to provide a OPENAI_API_KEY env var to request OpenAI . #1

Open
RHosono opened this issue Apr 16, 2024 · 0 comments
Assignees
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@RHosono
Copy link
Contributor

RHosono commented Apr 16, 2024

For example, if you use a framework such as Symfony, you cannot use this part of the code, as Symfony blocks the getenv because it is not thread safe.

- "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.)
src/Symfony/Component/Runtime/SymfonyRuntime.php

->usePutenv($options['use_putenv'] ?? false)
src/Symfony/Component/Runtime/SymfonyRuntime.php

Should we therefore add another check to use the global $_ENV[""] for example to prevent duplicate configuration? Any opinions on this?

public function __construct(?OpenAIConfig $config = null)
{
	if ($config instanceof OpenAIConfig && $config->client instanceof Client) {
		$this->client = $config->client;
	} else {
		$apiKey = $config->apiKey ?? getenv('OPENAI_API_KEY');
		if (! $apiKey) {
			throw new Exception('You have to provide a OPENAI_API_KEY env var to request OpenAI .');
		}

		$this->client = OpenAI::client($apiKey);
	}
	$this->model = $config->model ?? OpenAIChatModel::Gpt4Turbo->getModelName();
	$this->modelOptions = $config->modelOptions ?? [];
}
@RHosono RHosono added help wanted Extra attention is needed question Further information is requested labels Apr 16, 2024
@RHosono RHosono self-assigned this Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant