langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
46.73k stars 6.61k forks source link

fix: populate app configs to system environment variables #5590

Closed bowenliang123 closed 3 months ago

bowenliang123 commented 3 months ago

Description

Type of Change

Please delete options that are not relevant.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Suggested Checklist:

laipz8200 commented 3 months ago

Hello, Bowen! We need to know which packages need to read our configurations from the system environment variables. Could you give us a more specific example?

bowenliang123 commented 3 months ago

Hi, @laipz8200 thanks for your attention to this PR, which is raised for :

  1. before the removal the config.py in #5507, there's one line to populate all the configs from the .env file to system env vars. see https://github.com/langgenius/dify/pull/5507/files#diff-0e3026d2737181f937735ba5e2023ce7f39a7762c8c2594670f4a944a7e48eafL33
    dotenv.load_dotenv()
    (method comment: "Parse a .env file and then load all the variables found as environment variables...." )

This PR helps to leverage the same efforts to the original behaviours.

  1. some users may does use the .env file to set some env vars for the package. eg. setting CO_API_URL as the base URL for cohere package, which is not directly used in Dify.
laipz8200 commented 3 months ago

@bowenliang123 Since DifyConfig is read from the .env file, I think we can just use dotenv.load_dotenv() here like we did before.

bowenliang123 commented 3 months ago

I would prefer not to reverse couple with the raw dotenv again. For the following reasons,

  1. the configs in DifyConfig is ensured for validation in types and range
  2. dotenv.load_dotenv method has a implicit file path for .env, if we want to change it in future, it's better to make sure it's aligned with pydantic-settings.
  3. Further more pydantic-settings provides various features for arranging the proper order and choices for config sources, like pyproject.toml, JSON files, env files and etc.
  4. No benefits or contribution from reusing the dotenv.load_dotenv , it does replicate the configs into system env vars in set_as_environment_variables method by using os.environ[k] = v
bowenliang123 commented 3 months ago

Thanks.