-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathReasoningExample.cs
More file actions
184 lines (156 loc) · 7.06 KB
/
ReasoningExample.cs
File metadata and controls
184 lines (156 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Azure;
using Azure.AI.Inference;
using System.Text;
namespace GenAIStarter
{
/// <summary>
/// Reasoning Example - Complex problem solving scenarios
/// Converted from sample-reasoning.js
/// Run with: dotnet run --project ReasoningExample.csproj
/// </summary>
public class ReasoningExample
{
private static readonly (string Name, string Prompt)[] Scenarios = new[]
{
("Mathematical Reasoning",
"A train travels 60 miles per hour for 2 hours, then 80 miles per hour for 1.5 hours. What is the average speed for the entire trip?"),
("Logic Puzzle",
"Three people are wearing hats that are either red or blue. Each person can see the other two hats but not their own. They are told that at least one of them is wearing a red hat. If they are asked in turn if they know the color of their own hat, what logical reasoning can they use to figure it out?"),
("Complex Problem Solving",
"You are organizing a conference with three sessions and four speakers. Each speaker can only attend two sessions, and no session can have more than two speakers. How would you assign the speakers to sessions?"),
("Ethical Reasoning",
"You see a runaway trolley heading towards five people tied up on the tracks. You can pull a lever to divert the trolley onto another track, where it will hit one person. What should you do, and why?")
};
public static async Task Main(string[] args)
{
Console.WriteLine("=== Reasoning Examples ===");
Console.WriteLine("Test AI's ability to solve complex problems\n");
// Load environment variables from .env file
LoadEnvFile();
// Get API configuration from .env file
var token = Environment.GetEnvironmentVariable("API_TOKEN");
var endpointUrl = Environment.GetEnvironmentVariable("API_ENDPOINT");
if (string.IsNullOrWhiteSpace(token))
{
Console.Error.WriteLine("Error: API_TOKEN not found!");
Console.WriteLine("Please create a .env file with your API configuration:");
Console.WriteLine("1. Copy .env.example to .env");
Console.WriteLine("2. Add your API token and endpoint to the .env file");
return;
}
if (string.IsNullOrWhiteSpace(endpointUrl))
{
Console.Error.WriteLine("Error: API_ENDPOINT not found!");
Console.WriteLine("Please add API_ENDPOINT to your .env file");
return;
}
try
{
// Initialize the client
var endpoint = new Uri(endpointUrl);
var client = new ChatCompletionsClient(endpoint, new AzureKeyCredential(token));
var model = "gpt-4o";
while (true)
{
// Show menu
Console.WriteLine("Choose a reasoning scenario:");
for (int i = 0; i < Scenarios.Length; i++)
{
Console.WriteLine($"{i + 1}. {Scenarios[i].Name}");
}
Console.WriteLine($"{Scenarios.Length + 1}. Exit");
Console.Write("\nEnter your choice: ");
var choice = Console.ReadLine();
if (choice == $"{Scenarios.Length + 1}" || choice?.ToLower() == "exit")
{
Console.WriteLine("Goodbye!");
break;
}
if (int.TryParse(choice, out int index) && index >= 1 && index <= Scenarios.Length)
{
var scenario = Scenarios[index - 1];
await RunScenario(client, model, scenario);
}
else
{
Console.WriteLine("Invalid choice. Please try again.\n");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
private static async Task RunScenario(ChatCompletionsClient client, string model, (string Name, string Prompt) scenario)
{
Console.Clear();
Console.WriteLine($"=== {scenario.Name} ===\n");
Console.WriteLine($"Problem: {scenario.Prompt}\n");
Console.WriteLine("AI is thinking...\n");
var requestOptions = new ChatCompletionsOptions
{
Model = model,
Messages =
{
new ChatRequestUserMessage(scenario.Prompt)
},
Temperature = 0.7f,
MaxTokens = 2000
};
var response = await client.CompleteAsync(requestOptions);
Console.WriteLine("AI Response:");
Console.WriteLine(CleanMarkdownFormatting(response.Value.Content));
Console.WriteLine("\n" + new string('=', 60));
Console.WriteLine("Press any key to return to menu...");
Console.ReadKey();
Console.Clear();
}
// Utility method to clean markdown formatting for better console output
private static string CleanMarkdownFormatting(string text)
{
if (string.IsNullOrEmpty(text)) return text;
var sb = new StringBuilder(text);
// Remove bold markdown
sb.Replace("**", "");
sb.Replace("__", "");
// Remove italic markdown
sb.Replace("*", "");
sb.Replace("_", "");
// Remove inline code
sb.Replace("`", "");
// Remove headers
sb.Replace("### ", "");
sb.Replace("## ", "");
sb.Replace("# ", "");
// Clean up bullet points
sb.Replace("- ", "• ");
return sb.ToString().Trim();
}
private static void LoadEnvFile()
{
try
{
if (File.Exists(".env"))
{
foreach (var line in File.ReadAllLines(".env"))
{
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
continue;
var parts = line.Split('=', 2);
if (parts.Length == 2)
{
Environment.SetEnvironmentVariable(parts[0].Trim(), parts[1].Trim());
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Warning: Could not load .env file: {ex.Message}");
}
}
}
}