|
9 | 9 | ## Extensions |
10 | 10 |
|
11 | 11 | <!-- include src/AI/readme.md#content --> |
| 12 | +<!-- #content --> |
| 13 | +Extensions for Microsoft.Extensions.AI |
| 14 | + |
| 15 | +## Grok |
| 16 | + |
| 17 | +Full support for Grok [Live Search](https://docs.x.ai/docs/guides/live-search) |
| 18 | +and [Reasoning](https://docs.x.ai/docs/guides/reasoning) model options. |
| 19 | + |
| 20 | +```csharp |
| 21 | +// Sample X.AI client usage with .NET |
| 22 | +var messages = new Chat() |
| 23 | +{ |
| 24 | + { "system", "You are a highly intelligent AI assistant." }, |
| 25 | + { "user", "What is 101*3?" }, |
| 26 | +}; |
| 27 | + |
| 28 | +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); |
| 29 | + |
| 30 | +var options = new GrokChatOptions |
| 31 | +{ |
| 32 | + ModelId = "grok-3-mini", // or "grok-3-mini-fast" |
| 33 | + Temperature = 0.7f, |
| 34 | + ReasoningEffort = ReasoningEffort.High, // or ReasoningEffort.Low |
| 35 | + Search = GrokSearch.Auto, // or GrokSearch.On or GrokSearch.Off |
| 36 | +}; |
| 37 | + |
| 38 | +var response = await grok.GetResponseAsync(messages, options); |
| 39 | +``` |
| 40 | + |
| 41 | +Search can alternatively be configured using a regular `ChatOptions` |
| 42 | +and adding the `HostedWebSearchTool` to the tools collection, which |
| 43 | +sets the live search mode to `auto` like above: |
| 44 | + |
| 45 | +```csharp |
| 46 | +var messages = new Chat() |
| 47 | +{ |
| 48 | + { "system", "You are an AI assistant that knows how to search the web." }, |
| 49 | + { "user", "What's Tesla stock worth today? Search X and the news for latest info." }, |
| 50 | +}; |
| 51 | + |
| 52 | +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); |
| 53 | + |
| 54 | +var options = new ChatOptions |
| 55 | +{ |
| 56 | + ModelId = "grok-3", |
| 57 | + Tools = [new HostedWebSearchTool()] |
| 58 | +}; |
| 59 | + |
| 60 | +var response = await grok.GetResponseAsync(messages, options); |
| 61 | +``` |
| 62 | + |
| 63 | +## Console Logging |
| 64 | + |
| 65 | +Additional `UseJsonConsoleLogging` extension for rich JSON-formatted console logging of AI requests |
| 66 | +are provided at two levels: |
| 67 | + |
| 68 | +* Chat pipeline: similar to `UseLogging`. |
| 69 | +* HTTP pipeline: lowest possible layer before the request is sent to the AI service, |
| 70 | + can capture all requests and responses. Can also be used with other Azure SDK-based |
| 71 | + clients that leverage `ClientPipelineOptions`. |
| 72 | + |
| 73 | +> [!NOTE] |
| 74 | +> Rich JSON formatting is provided by [Spectre.Console](https://spectreconsole.net/) |
| 75 | +
|
| 76 | +The HTTP pipeline logging can be enabled by calling `UseJsonConsoleLogging` on the |
| 77 | +client options passed to the client constructor: |
| 78 | + |
| 79 | +```csharp |
| 80 | +var openai = new OpenAIClient( |
| 81 | + Env.Get("OPENAI_API_KEY")!, |
| 82 | + new OpenAIClientOptions().UseJsonConsoleLogging()); |
| 83 | +``` |
| 84 | + |
| 85 | +For a Grok client with search-enabled, a request would look like the following: |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +Both alternatives receive an optional `JsonConsoleOptions` instance to configure |
| 90 | +the output, including truncating or wrapping long messages, setting panel style, |
| 91 | +and more. |
| 92 | + |
| 93 | +The chat pipeline logging is added similar to other pipeline extensions: |
| 94 | + |
| 95 | +```csharp |
| 96 | +IChatClient client = new GrokClient(Env.Get("XAI_API_KEY")!) |
| 97 | + .AsBuilder() |
| 98 | + .UseOpenTelemetry() |
| 99 | + // other extensions... |
| 100 | + .UseJsonConsoleLogging(new JsonConsoleOptions() |
| 101 | + { |
| 102 | + // Formatting options... |
| 103 | + Border = BoxBorder.None, |
| 104 | + WrapLength = 80, |
| 105 | + }) |
| 106 | + .Build(); |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +<!-- #content --> |
| 111 | +<!-- src/AI/readme.md#content --> |
12 | 112 |
|
13 | 113 | ## Weaving |
14 | 114 |
|
15 | 115 | <!-- include src/Weaving/readme.md#content --> |
| 116 | +<!-- #content --> |
| 117 | +Run AI-powered C# files with the power of Microsoft.Extensions.AI and Devlooped.Extensions.AI |
| 118 | + |
| 119 | +```csharp |
| 120 | +#:package Weaving@0.* |
| 121 | + |
| 122 | +// Sample X.AI client usage with .NET |
| 123 | +var messages = new Chat() |
| 124 | +{ |
| 125 | + { "system", "You are a highly intelligent AI assistant." }, |
| 126 | + { "user", "What is 101*3?" }, |
| 127 | +}; |
| 128 | + |
| 129 | +var grok = new GrokClient(Env.Get("XAI_API_KEY")!); |
| 130 | + |
| 131 | +var options = new GrokChatOptions |
| 132 | +{ |
| 133 | + ModelId = "grok-3-mini", // or "grok-3-mini-fast" |
| 134 | + ReasoningEffort = ReasoningEffort.High, // or ReasoningEffort.Low |
| 135 | + Search = GrokSearch.Auto, // or GrokSearch.On or GrokSearch.Off |
| 136 | +}; |
| 137 | + |
| 138 | +var response = await grok.GetResponseAsync(messages, options); |
| 139 | + |
| 140 | +AnsiConsole.MarkupLine($":robot: {response.Text}"); |
| 141 | +``` |
| 142 | + |
| 143 | +> [!NOTE] |
| 144 | +> The most useful namespaces and dependencies for developing Microsoft.Extensions.AI- |
| 145 | +> powered applications are automatically referenced and imported when using this package. |
| 146 | +
|
| 147 | +## Configuration / Environment Variables |
| 148 | + |
| 149 | +The `Env` class provides access to the following variables/configuration automatically: |
| 150 | + |
| 151 | +* `.env` files: in local and parent directories |
| 152 | +* `~/.env` file: in the user's home directory (`%userprofile%\.env` on Windows) |
| 153 | +* All default configuration sources from [App Builder](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host?tabs=appbuilder#host-builder-settings): |
| 154 | + * Environment variables prefixed with DOTNET_. |
| 155 | + * Command-line arguments. |
| 156 | + * appsettings.json. |
| 157 | + * appsettings.{Environment}.json. |
| 158 | + * Secret Manager when the app runs in the Development environment. |
| 159 | + * Environment variables. |
| 160 | + * Command-line arguments. |
| 161 | + |
| 162 | +<!-- #content --> |
| 163 | +<!-- src/Weaving/readme.md#content --> |
16 | 164 |
|
17 | 165 | <!-- include https://github.com/devlooped/sponsors/raw/main/footer.md --> |
18 | 166 | # Sponsors |
|
0 commit comments