diff --git a/include/openai/openai.hpp b/include/openai/openai.hpp index 18ae49f..73c9fc9 100644 --- a/include/openai/openai.hpp +++ b/include/openai/openai.hpp @@ -372,17 +372,21 @@ class OpenAI { OpenAI(const std::string& token = "", const std::string& organization = "", bool throw_exception = true, const std::string& api_base_url = "") : session_{throw_exception}, token_{token}, organization_{organization}, throw_exception_{throw_exception} { if (token.empty()) { - if(const char* env_p = std::getenv("OPENAI_API_KEY")) { + char* env_p; + if(_dupenv_s(&env_p, NULL, "OPENAI_API_KEY") == 0 && env_p != NULL) { token_ = std::string{env_p}; } + free(env_p); } if (api_base_url.empty()) { - if(const char* env_p = std::getenv("OPENAI_API_BASE")) { + char* env_p; + if(_dupenv_s(&env_p, NULL, "OPENAI_API_BASE") == 0 && env_p != NULL) { base_url = std::string{env_p} + "/"; } else { base_url = "https://api.openai.com/v1/"; } + free(env_p); } else { base_url = api_base_url;