diff --git a/readme.md b/readme.md index f19ce12..735b19e 100644 --- a/readme.md +++ b/readme.md @@ -3,32 +3,51 @@ [![Version](https://img.shields.io/nuget/vpre/Smith.svg?color=royalblue)](https://www.nuget.org/packages/Smith) [![Downloads](https://img.shields.io/nuget/dt/Smith.svg?color=green)](https://www.nuget.org/packages/Smith) -[![License](https://img.shields.io/github/license/devlooped/Smith.svg?color=blue)](https://github.com//devlooped/Smith/blob/main/license.txt) -[![Build](https://github.com/devlooped/Smith/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/devlooped/Smith/actions) +[![License](https://img.shields.io/github/license/devlooped/smith.svg?color=blue)](https://github.com//devlooped/smith/blob/main/license.txt) +[![Build](https://github.com/devlooped/smith/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/devlooped/smith/actions) +Run AI-powered C# files using Microsoft.Extensions.AI and Devlooped.Extensions.AI -An opinionated meta-package for doing AI agents using Microsoft.Extensions.AI and MCP and dotnet run file. - -Example Claude-based agent: - -![](https://raw.githubusercontent.com/devlooped/smith/main/assets/run.png) +Example leveraging [Grok](https://console.x.ai/): ```csharp #:package Smith@0.* -var configuration = new ConfigurationBuilder() - .AddEnvironmentVariables() - .AddUserSecrets() - .AddJsonFile("appsettings.json", optional: true) - .AddIniFile("appsettings.ini", optional: true) - .Build(); +// Sample X.AI client usage with .NET +var messages = new Chat() +{ + { "system", "You are a highly intelligent AI assistant." }, + { "user", "What is 101*3?" }, +}; + +IChatClient grok = new GrokClient(Throw.IfNullOrEmpty(Env.Get("XAI_API_KEY"))) + .GetChatClient("grok-3-mini") + .AsIChatClient(); + +var options = new GrokChatOptions +{ + ReasoningEffort = ReasoningEffort.High, // or ReasoningEffort.Low + Search = GrokSearch.Auto, // or GrokSearch.On/GrokSearch.Off +}; + +var response = await grok.GetResponseAsync(messages, options); + +AnsiConsole.MarkupLine($":robot: {response.Text}"); +``` -var services = new ServiceCollection(); +> [!NOTE] +> The most useful namespaces and dependencies for developing Microsoft.Extensions.AI-powered +> applications are automatically referenced and imported when using this package. -services.AddHttpClient("ai").AddStandardResilienceHandler(); +Example using Claude: -services.AddChatClient(services => new Anthropic.AnthropicClient( +![](https://raw.githubusercontent.com/devlooped/smith/main/assets/run.png) + +```csharp +#:package Smith@0.* + +var chat = new Anthropic.AnthropicClient(Throw. configuration["Claude:Key"] ?? throw new InvalidOperationException("Missing Claude:Key configuration."), services.GetRequiredService().CreateClient("ai"))) .UseLogging() @@ -93,6 +112,24 @@ static class Prompts } ``` + +## 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. + + + # Sponsors diff --git a/src/Smith/AppInitializer.cs b/src/Smith/AppInitializer.cs index c9111e9..1e4fa5e 100644 --- a/src/Smith/AppInitializer.cs +++ b/src/Smith/AppInitializer.cs @@ -2,7 +2,7 @@ using System.Runtime.InteropServices; using System.Text; -namespace Weaving; +namespace Smith; class AppInitializer { diff --git a/src/Smith/Env.cs b/src/Smith/Env.cs index 4fc8ba8..3713f37 100644 --- a/src/Smith/Env.cs +++ b/src/Smith/Env.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -namespace Weaving; +namespace Smith; /// /// Allows retrieving configuration settings for the app environment using the diff --git a/src/Smith/HostExtensions.cs b/src/Smith/HostExtensions.cs index 6cc547a..78a6c98 100644 --- a/src/Smith/HostExtensions.cs +++ b/src/Smith/HostExtensions.cs @@ -1,8 +1,9 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -namespace Weaving; +namespace Smith; /// /// Extensions for configuring the host application builder and accessing services. diff --git a/src/Smith/Smith.csproj b/src/Smith/Smith.csproj index 9de1521..fdc6956 100644 --- a/src/Smith/Smith.csproj +++ b/src/Smith/Smith.csproj @@ -13,7 +13,8 @@ - + + diff --git a/src/Smith/readme.md b/src/Smith/readme.md index 18e2456..2bf6101 100644 --- a/src/Smith/readme.md +++ b/src/Smith/readme.md @@ -1,50 +1,3 @@ - -Run AI-powered C# files using Microsoft.Extensions.AI and Devlooped.Extensions.AI - -```csharp -#:package Smith@0.* - -// Sample X.AI client usage with .NET -var messages = new Chat() -{ - { "system", "You are a highly intelligent AI assistant." }, - { "user", "What is 101*3?" }, -}; - -IChatClient grok = new GrokClient(Throw.IfNullOrEmpty(Env.Get("XAI_API_KEY"))) - .GetChatClient("grok-3-mini") - .AsIChatClient(); - -var options = new GrokChatOptions -{ - ReasoningEffort = ReasoningEffort.High, // or ReasoningEffort.Low - Search = GrokSearch.Auto, // or GrokSearch.On/GrokSearch.Off -}; - -var response = await grok.GetResponseAsync(messages, options); - -AnsiConsole.MarkupLine($":robot: {response.Text}"); -``` - -> [!NOTE] -> 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