|
1 | | -An opinionated meta-package for doing AI agents using Microsoft.Extensions.AI and MCP |
| 1 | +An opinionated meta-package for doing AI agents using Microsoft.Extensions.AI and MCP and dotnet run file. |
| 2 | + |
| 3 | +Examle Claude-based agent: |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +```csharp |
| 8 | +#:package Smith@0.* |
| 9 | + |
| 10 | +var configuration = new ConfigurationBuilder() |
| 11 | + .AddEnvironmentVariables() |
| 12 | + .AddUserSecrets() |
| 13 | + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
| 14 | + .AddIniFile("appsettings.ini", optional: true, reloadOnChange: true) |
| 15 | + .Build(); |
| 16 | + |
| 17 | +var services = new ServiceCollection(); |
| 18 | + |
| 19 | +services.AddHttpClient("ai").AddStandardResilienceHandler(); |
| 20 | + |
| 21 | +services.AddChatClient(services => new Anthropic.AnthropicClient( |
| 22 | + configuration["Claude:Key"] ?? throw new InvalidOperationException("Missing Claude:Key configuration."), |
| 23 | + services.GetRequiredService<IHttpClientFactory>().CreateClient("ai"))) |
| 24 | + .UseLogging() |
| 25 | + .UseFunctionInvocation(); |
| 26 | + |
| 27 | +var provider = services.BuildServiceProvider(); |
| 28 | +var history = new List<ChatMessage> { new ChatMessage(ChatRole.System, Prompts.System) }; |
| 29 | +var chat = provider.GetRequiredService<IChatClient>(); |
| 30 | +var options = new ChatOptions |
| 31 | +{ |
| 32 | + ModelId = "claude-sonnet-4-20250514", |
| 33 | + MaxOutputTokens = 1000, |
| 34 | + Temperature = 0.7f, |
| 35 | + Tools = [AIFunctionFactory.Create(() => DateTime.Now, "get_datetime", "Gets the current date and time on the user's local machine.")] |
| 36 | +}; |
| 37 | + |
| 38 | +AnsiConsole.MarkupLine($":robot: Ready v{ThisAssembly.Info.Version}"); |
| 39 | +AnsiConsole.Markup($":person_beard: "); |
| 40 | +while (true) |
| 41 | +{ |
| 42 | + var input = Console.ReadLine()?.Trim(); |
| 43 | + if (string.IsNullOrEmpty(input)) |
| 44 | + continue; |
| 45 | + |
| 46 | + history.Add(new ChatMessage(ChatRole.User, input)); |
| 47 | + |
| 48 | + try |
| 49 | + { |
| 50 | + var response = await AnsiConsole.Status().StartAsync(":robot: Thinking...", |
| 51 | + ctx => chat.GetResponseAsync(input, options)); |
| 52 | + |
| 53 | + history.AddRange(response.Messages); |
| 54 | + // Try rendering as formatted markup |
| 55 | + try |
| 56 | + { |
| 57 | + if (response.Text is { Length: > 0 }) |
| 58 | + AnsiConsole.MarkupLine($":robot: {response.Text}"); |
| 59 | + } |
| 60 | + catch (Exception) |
| 61 | + { |
| 62 | + // Fallback to escaped markup text if rendering fails |
| 63 | + AnsiConsole.MarkupInterpolated($":robot: {response.Text}"); |
| 64 | + } |
| 65 | + |
| 66 | + AnsiConsole.WriteLine(); |
| 67 | + AnsiConsole.Markup($":person_beard: "); |
| 68 | + } |
| 69 | + catch (Exception e) |
| 70 | + { |
| 71 | + AnsiConsole.WriteException(e); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +static class Prompts |
| 76 | +{ |
| 77 | + public const string System = |
| 78 | + """ |
| 79 | + Your responses will be rendered using Spectre.Console.AnsiConsole.Write(new Markup(string text))). |
| 80 | + This means that you can use rich text formatting, colors, and styles in your responses, but you must |
| 81 | + ensure that the text is valid markup syntax. |
| 82 | + """; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md --> |
0 commit comments