Created
January 18, 2026 11:37
-
-
Save vman/c49b7aeb5e5874fa10f038a5a25a1547 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using OpenAI; | |
| using OpenAI.Responses; | |
| using System.ClientModel; | |
| static string Require(string? v, string name) | |
| => string.IsNullOrWhiteSpace(v) ? throw new InvalidOperationException($"{name} is missing") : v; | |
| var endpoint = Require(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"), "AZURE_OPENAI_ENDPOINT"); | |
| // Example endpoint: https://YOUR-RESOURCE-NAME.openai.azure.com | |
| var apiKey = Require(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY"), "AZURE_OPENAI_API_KEY"); | |
| var deployment = Require(Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT"), "AZURE_OPENAI_DEPLOYMENT"); | |
| // Azure OpenAI v1 base URL (important) | |
| var v1BaseUrl = $"{endpoint.TrimEnd('/')}/openai/v1/"; | |
| var client = new OpenAIClient( | |
| credential: new ApiKeyCredential(apiKey), | |
| options: new OpenAIClientOptions { Endpoint = new Uri(v1BaseUrl) } | |
| ); | |
| var responsesClient = client.GetResponsesClient(deployment); | |
| // Basic reasoning sample | |
| var systemPrompt = "You are a helpful assistant. Provide a short reasoning summary, then a final answer."; | |
| var userPrompt = | |
| "If a train travels at 60 mph for 2.5 hours, then slows down to 40 mph for another 1.5 hours, " + | |
| "what is the total distance traveled and the average speed for the entire journey?"; | |
| var options = new CreateResponseOptions | |
| { | |
| Instructions = systemPrompt, | |
| ReasoningOptions = new ResponseReasoningOptions | |
| { | |
| ReasoningEffortLevel = ResponseReasoningEffortLevel.Medium, | |
| ReasoningSummaryVerbosity = ResponseReasoningSummaryVerbosity.Auto | |
| } | |
| }; | |
| options.InputItems.Add(ResponseItem.CreateUserMessageItem(userPrompt)); | |
| var result = await responsesClient.CreateResponseAsync(options); | |
| var response = result.Value; | |
| // Print reasoning summary (if present) | |
| Console.WriteLine("π§ REASONING SUMMARY"); | |
| Console.WriteLine("ββββββββββββββββββββ"); | |
| var printed = false; | |
| foreach (var item in response.OutputItems) | |
| { | |
| if (item is ReasoningResponseItem reasoningItem) | |
| { | |
| foreach (var part in reasoningItem.SummaryParts) | |
| { | |
| if (part is ReasoningSummaryTextPart textPart) | |
| { | |
| Console.WriteLine(textPart.Text); | |
| printed = true; | |
| } | |
| } | |
| } | |
| } | |
| if (!printed) | |
| { | |
| Console.WriteLine("(No reasoning summary returned for this request)"); | |
| } | |
| // Print final answer | |
| Console.WriteLine(); | |
| Console.WriteLine("β FINAL ANSWER"); | |
| Console.WriteLine("ββββββββββββββββββββ"); | |
| Console.WriteLine(response.GetOutputText()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment