diff --git a/Extensions.AI.sln b/Extensions.AI.sln index ffed918..c55cb54 100644 --- a/Extensions.AI.sln +++ b/Extensions.AI.sln @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AI.Tests", "src\AI.Tests\AI EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-meai", "src\dotnet-meai\dotnet-meai.csproj", "{58FC11CF-7D61-48B9-B95F-0EAA889D6DED}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weaving", "src\Weaving\Weaving.csproj", "{0DA390D2-2953-4CB6-8939-02E142B11733}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "src\Samples\Samples.csproj", "{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -55,8 +59,35 @@ Global {58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x64.Build.0 = Release|Any CPU {58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x86.ActiveCfg = Release|Any CPU {58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x86.Build.0 = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x64.ActiveCfg = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x64.Build.0 = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x86.ActiveCfg = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x86.Build.0 = Debug|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|Any CPU.Build.0 = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x64.ActiveCfg = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x64.Build.0 = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x86.ActiveCfg = Release|Any CPU + {0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x86.Build.0 = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x64.ActiveCfg = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x64.Build.0 = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x86.ActiveCfg = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x86.Build.0 = Debug|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|Any CPU.Build.0 = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x64.ActiveCfg = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x64.Build.0 = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x86.ActiveCfg = Release|Any CPU + {4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CCA59B79-037D-4847-871D-385CD5E8BB6F} + EndGlobalSection EndGlobal diff --git a/src/Samples/Program.cs b/src/Samples/Program.cs new file mode 100644 index 0000000..26e5140 --- /dev/null +++ b/src/Samples/Program.cs @@ -0,0 +1,20 @@ +// Sample X.AI client usage with .NET +var messages = new Chat() +{ + { "system", "You are a highly intelligent AI assistant." }, + { "user", "What is 101*3?" }, +}; + +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); + +var options = new GrokChatOptions +{ + ModelId = "grok-3-mini", // or "grok-3-mini-fast" + Temperature = 0.7f, + ReasoningEffort = ReasoningEffort.High, // or GrokReasoningEffort.Low + Search = GrokSearch.Auto, // or GrokSearch.On or GrokSearch.Off +}; + +var response = await grok.GetResponseAsync(messages, options); + +AnsiConsole.MarkupLine($":robot: {response.Text}"); \ No newline at end of file diff --git a/src/Samples/Properties/launchSettings.json b/src/Samples/Properties/launchSettings.json new file mode 100644 index 0000000..784aa04 --- /dev/null +++ b/src/Samples/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Samples": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/src/Samples/Samples.csproj b/src/Samples/Samples.csproj new file mode 100644 index 0000000..c15da5d --- /dev/null +++ b/src/Samples/Samples.csproj @@ -0,0 +1,24 @@ + + + + Exe + net10.0 + Preview + true + + + + + + + + + + + + + + + + + diff --git a/src/Samples/appsettings.json b/src/Samples/appsettings.json new file mode 100644 index 0000000..483f473 --- /dev/null +++ b/src/Samples/appsettings.json @@ -0,0 +1,13 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "System": "Warning", + "Azure": "Debug", + "Azure.Core": "Debug", + "Azure.AI.OpenAI": "Debug", + "Devlooped.Extensions.AI": "Debug" + } + } +} diff --git a/src/Weaving/ConsoleInitializer.cs b/src/Weaving/ConsoleInitializer.cs new file mode 100644 index 0000000..4ffc3b5 --- /dev/null +++ b/src/Weaving/ConsoleInitializer.cs @@ -0,0 +1,17 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; + +namespace Weaving; + +class ConsoleInitializer +{ +#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries + [ModuleInitializer] +#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries + public static void Init() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8; + } +} diff --git a/src/Weaving/Env.cs b/src/Weaving/Env.cs new file mode 100644 index 0000000..4fc8ba8 --- /dev/null +++ b/src/Weaving/Env.cs @@ -0,0 +1,30 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; + +namespace Weaving; + +/// +/// Allows retrieving configuration settings for the app environment using the +/// default configuration system provided by +/// +public static class Env +{ + static readonly IConfigurationManager configuration = + Host.CreateApplicationBuilder(Environment.GetCommandLineArgs()).Configuration; + + /// + /// Gets a (possibly null) configuration value for the given key. + /// + /// The key to retrieve, following .NET configuration system conventions. + public static string? Get(string key) => configuration[key]; + + /// + /// Gets a configuration value for the given key, returning a default value if the key is not found. + /// + /// The key to retrieve, following .NET configuration system conventions. + /// Default value to return if the key is not found. + /// Never returns null if is not . + [return: NotNullIfNotNull(nameof(defaultValue))] + public static string? Get(string key, string defaultValue) => configuration[key] ?? defaultValue; +} diff --git a/src/Weaving/HostExtensions.cs b/src/Weaving/HostExtensions.cs new file mode 100644 index 0000000..e4812a7 --- /dev/null +++ b/src/Weaving/HostExtensions.cs @@ -0,0 +1,46 @@ +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Weaving; + +/// +/// Extensions for configuring the host application builder and accessing services. +/// +public static class HostExtensions +{ + extension(TBuilder builder) where TBuilder : IHostApplicationBuilder + { + /// + /// Configures the host configuration. + /// + public TBuilder Configure(Action configure) + { + configure?.Invoke(builder.Configuration); + return builder; + } + + /// + /// Configures services in the host. + /// + public TBuilder ConfigureServices(Action configure) + { + configure?.Invoke(builder.Services); + return builder; + } + } + + extension(IHost app) + { + /// + /// Gest the host app configuration; + /// + public IConfiguration Configuration => app.Services.GetRequiredService(); + + /// + /// Gets the default AI client for the app. + /// + public IChatClient ChatClient => app.Services.GetRequiredService(); + } +} diff --git a/src/Weaving/Visibility.cs b/src/Weaving/Visibility.cs new file mode 100644 index 0000000..6a49ba1 --- /dev/null +++ b/src/Weaving/Visibility.cs @@ -0,0 +1,4 @@ +namespace System +{ + public partial class Throw { } +} \ No newline at end of file diff --git a/src/Weaving/Weaving.csproj b/src/Weaving/Weaving.csproj new file mode 100644 index 0000000..a597c21 --- /dev/null +++ b/src/Weaving/Weaving.csproj @@ -0,0 +1,24 @@ + + + + net10.0 + enable + Preview + Weaving + Weaving + Run AI-powered C# files with the power of Microsoft.Extensions.AI and Devlooped.Extensions.AI + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Weaving/Weaving.props b/src/Weaving/Weaving.props new file mode 100644 index 0000000..2a50111 --- /dev/null +++ b/src/Weaving/Weaving.props @@ -0,0 +1,35 @@ + + + true + enable + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Weaving/Weaving.targets b/src/Weaving/Weaving.targets new file mode 100644 index 0000000..4de98b5 --- /dev/null +++ b/src/Weaving/Weaving.targets @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/Weaving/readme.md b/src/Weaving/readme.md new file mode 100644 index 0000000..a92996f --- /dev/null +++ b/src/Weaving/readme.md @@ -0,0 +1 @@ +Run AI-powered C# files with the power of Microsoft.Extensions.AI and Devlooped.Extensions.AI