diff --git a/readme.md b/readme.md index b36d56e..024ffa4 100644 --- a/readme.md +++ b/readme.md @@ -9,10 +9,158 @@ ## Extensions + +Extensions for Microsoft.Extensions.AI + +## Grok + +Full support for Grok [Live Search](https://docs.x.ai/docs/guides/live-search) +and [Reasoning](https://docs.x.ai/docs/guides/reasoning) model options. + +```csharp +// 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 ReasoningEffort.Low + Search = GrokSearch.Auto, // or GrokSearch.On or GrokSearch.Off +}; + +var response = await grok.GetResponseAsync(messages, options); +``` + +Search can alternatively be configured using a regular `ChatOptions` +and adding the `HostedWebSearchTool` to the tools collection, which +sets the live search mode to `auto` like above: + +```csharp +var messages = new Chat() +{ + { "system", "You are an AI assistant that knows how to search the web." }, + { "user", "What's Tesla stock worth today? Search X and the news for latest info." }, +}; + +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); + +var options = new ChatOptions +{ + ModelId = "grok-3", + Tools = [new HostedWebSearchTool()] +}; + +var response = await grok.GetResponseAsync(messages, options); +``` + +## Console Logging + +Additional `UseJsonConsoleLogging` extension for rich JSON-formatted console logging of AI requests +are provided at two levels: + +* Chat pipeline: similar to `UseLogging`. +* HTTP pipeline: lowest possible layer before the request is sent to the AI service, + can capture all requests and responses. Can also be used with other Azure SDK-based + clients that leverage `ClientPipelineOptions`. + +> [!NOTE] +> Rich JSON formatting is provided by [Spectre.Console](https://spectreconsole.net/) + +The HTTP pipeline logging can be enabled by calling `UseJsonConsoleLogging` on the +client options passed to the client constructor: + +```csharp +var openai = new OpenAIClient( + Env.Get("OPENAI_API_KEY")!, + new OpenAIClientOptions().UseJsonConsoleLogging()); +``` + +For a Grok client with search-enabled, a request would look like the following: + +![](https://raw.githubusercontent.com/devlooped/Extensions.AI/main/assets/img/chatmessage.png) + +Both alternatives receive an optional `JsonConsoleOptions` instance to configure +the output, including truncating or wrapping long messages, setting panel style, +and more. + +The chat pipeline logging is added similar to other pipeline extensions: + +```csharp +IChatClient client = new GrokClient(Env.Get("XAI_API_KEY")!) + .AsBuilder() + .UseOpenTelemetry() + // other extensions... + .UseJsonConsoleLogging(new JsonConsoleOptions() + { + // Formatting options... + Border = BoxBorder.None, + WrapLength = 80, + }) + .Build(); +``` + + + + ## Weaving + +Run AI-powered C# files with the power of Microsoft.Extensions.AI and Devlooped.Extensions.AI + +```csharp +#:package Weaving@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?" }, +}; + +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); + +var options = new GrokChatOptions +{ + ModelId = "grok-3-mini", // or "grok-3-mini-fast" + ReasoningEffort = ReasoningEffort.High, // or ReasoningEffort.Low + Search = GrokSearch.Auto, // or GrokSearch.On or 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. + + + # Sponsors