diff --git a/src/Samples/.env b/src/Samples/.env new file mode 100644 index 0000000..15c36f5 --- /dev/null +++ b/src/Samples/.env @@ -0,0 +1 @@ +FOO=BAR \ No newline at end of file diff --git a/src/Samples/Program.cs b/src/Samples/Program.cs index 28e20ae..e3ca628 100644 --- a/src/Samples/Program.cs +++ b/src/Samples/Program.cs @@ -5,6 +5,7 @@ { "user", "What's Tesla stock worth today? Search X and the news for latest info." }, }; +// Env supports .env as well as all standard .NET configuration sources var grok = new GrokClient(Env.Get("XAI_API_KEY")!, new GrokClientOptions() .UseJsonConsoleLogging(new() { WrapLength = 80 })); diff --git a/src/Weaving/ConsoleInitializer.cs b/src/Weaving/AppInitializer.cs similarity index 57% rename from src/Weaving/ConsoleInitializer.cs rename to src/Weaving/AppInitializer.cs index 4ffc3b5..c9111e9 100644 --- a/src/Weaving/ConsoleInitializer.cs +++ b/src/Weaving/AppInitializer.cs @@ -4,7 +4,7 @@ namespace Weaving; -class ConsoleInitializer +class AppInitializer { #pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries [ModuleInitializer] @@ -13,5 +13,13 @@ public static void Init() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8; + + // Load environment variables from .env files in current dir and above. + DotNetEnv.Env.TraversePath().Load(); + + // Load environment variables from user profile directory. + var userEnv = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".env"); + if (File.Exists(userEnv)) + DotNetEnv.Env.Load(userEnv); } } diff --git a/src/Weaving/Weaving.csproj b/src/Weaving/Weaving.csproj index 078a1ae..e61e8c9 100644 --- a/src/Weaving/Weaving.csproj +++ b/src/Weaving/Weaving.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Weaving/Weaving.props b/src/Weaving/Weaving.props index 2a50111..24de5bf 100644 --- a/src/Weaving/Weaving.props +++ b/src/Weaving/Weaving.props @@ -30,6 +30,7 @@ + \ No newline at end of file diff --git a/src/Weaving/readme.md b/src/Weaving/readme.md index d022264..ed0a0d5 100644 --- a/src/Weaving/readme.md +++ b/src/Weaving/readme.md @@ -29,6 +29,21 @@ AnsiConsole.MarkupLine($":robot: {response.Text}"); > The most useful namespaces and dependencies for developing Microsoft.Extensions.AI- > powered applications are automatically referenced and imported when using this package. +## Configuration / Environment Variables + +The `Env` class provides access to the following variables/configuration automatically: + +* `.env` files: in local and parent directories +* `~/.env` file: in the user's home directory (`%userprofile%\.env` on Windows) +* All default configuration sources from [App Builder](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host?tabs=appbuilder#host-builder-settings): + * Environment variables prefixed with DOTNET_. + * Command-line arguments. + * appsettings.json. + * appsettings.{Environment}.json. + * Secret Manager when the app runs in the Development environment. + * Environment variables. + * Command-line arguments. + \ No newline at end of file