semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python: Remove dependencies to .env file

Open sbaidachni opened this issue 2 years ago • 5 comments

https://github.com/microsoft/semantic-kernel/blob/16d52c155a2c8193fef7207a8816ac8c4a8eed2c/python/semantic_kernel/utils/settings.py#L16C7-L16C7

The current implementation of openai_settings_from_dot_env() and azure_openai_settings_from_dot_env() uses dotenv_values(".env") to initialize configuration parameters from the .env file. At the same time, there are many ways to initialize environment variables not using the .env file. For example, in Azure DevOps we can use variable groups and nobody host keys in .env file in a repository. Therefore, we would like to see an implementation that is not stick to .env directly. Here is an example, how it can be implemented:

load_dotenv() deployment = os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME") api_key = os.environ.get("AZURE_OPENAI_API_KEY") endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT")

In this implementation I can use .env locally to host variables or I can setup environment variables in any other way (like in DevOps or GitHub) and it will work.

sbaidachni avatar Jun 29 '23 19:06 sbaidachni

Note for internal SK: keeping this older work item open as it is pertinent and on our backlog.

moonbox3 avatar Mar 08 '24 19:03 moonbox3

Let's investigate using pydantic-settings: https://lewoudar.medium.com/my-favorite-way-to-configure-a-python-project-5a222c579d4c

eavanvalkenburg avatar Mar 11 '24 10:03 eavanvalkenburg

Same issue. I am deploying an app on an Azure App Service with Environment Variables configured, but it fails since it cannot find the .env file. image

MaxiPigna avatar Mar 13 '24 10:03 MaxiPigna

Thanks for reporting @MaxiPigna this is indeed one of the issues with the current approach, unfortunately for you, you will have to directly use env without using the settings helpers. But we will fix it!

eavanvalkenburg avatar Mar 13 '24 14:03 eavanvalkenburg

Saving this: https://docs.pydantic.dev/latest/concepts/pydantic_settings/

moonbox3 avatar May 08 '24 18:05 moonbox3