-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathProgram_AI.cs
284 lines (248 loc) · 12.4 KB
/
Program_AI.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Azure.AI.CLI.Clients.AzPython;
using Azure.AI.CLI.Common.Clients;
using Azure.AI.Details.Common.CLI.Telemetry;
using Azure.AI.Details.Common.CLI.Telemetry.Events;
namespace Azure.AI.Details.Common.CLI
{
public class AiProgram
{
static async Task<int> Main(string[] args)
{
bool isDebug = args.Length > 0 && args[0] == "debug";
if (isDebug) Console.WriteLine($"StopWatch: Started at {DateTime.Now}");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var exitCode = TryCatchHelpers.TryCatchNoThrow(() => Program.Main(new AiProgramData(), args), -1, out var ex);
stopwatch.Stop();
var elapsed = stopwatch.Elapsed;
if (isDebug) Console.WriteLine($"StopWatch: Stopped at {DateTime.Now} ({GetStopWatchElapsedAsString(elapsed)})");
stopwatch.Restart();
await LogExitEventAsync(elapsed, exitCode);
stopwatch.Stop();
if (isDebug) Console.WriteLine($"StopWatch: Telemetry completed at {DateTime.Now} ({GetStopWatchElapsedAsString(stopwatch.Elapsed)})");
if (ex != null) throw ex;
return exitCode;
}
static async Task LogExitEventAsync(TimeSpan elapsed, int exitCode)
{
if (Program.Telemetry != null)
{
Program.Telemetry.LogEvent(new ExitedTelemetryEvent()
{
ExitCode = exitCode,
Elapsed = elapsed
});
await Program.Telemetry.DisposeAsync();
}
}
static string GetStopWatchElapsedAsString(TimeSpan elapsed)
{
var elapsedMilliseconds = elapsed.TotalMilliseconds;
var elapsedSeconds = elapsed.TotalSeconds;
var elapsedMinutes = elapsed.TotalMinutes;
var elapsedHours = elapsed.TotalHours;
var elapsedString = elapsedSeconds < 1 ? $"{elapsedMilliseconds} ms"
: elapsedMinutes < 1 ? $"{elapsedSeconds:0.00} sec"
: elapsedHours < 1 ? $"{elapsedMinutes:0.00} min"
: $"{elapsedHours:0.00} hr";
return elapsedString;
}
}
public class AiProgramData : IProgramData
{
private readonly Lazy<ITelemetry> _telemetry;
public AiProgramData()
{
_telemetry = new Lazy<ITelemetry>(
() => TelemetryHelpers.InstantiateFromConfig(this),
System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
var environmentVariables = LegacyAzCli.GetUserAgentEnv();
LoginManager = new AzConsoleLoginManager(
() => Values?.GetOrDefault("init.service.interactive", true) ?? true,
environmentVariables);
var azCliClient = new AzCliClient(environmentVariables);
SubscriptionsClient = azCliClient;
CognitiveServicesClient = azCliClient;
SearchClient = azCliClient;
}
#region name data
public string Name => "ai";
public string DisplayName => "Azure AI CLI";
public string WarningBanner => "`#e_;This PUBLIC PREVIEW version may change at any time.\nSee: https://aka.ms/azure-ai-cli-public-preview";
public string TelemetryUserAgent => "ai-cli 0.0.1";
#endregion
#region assembly data
public string Exe => "ai.exe";
public string Dll => "ai.dll";
public Type ResourceAssemblyType => typeof(AiProgramData);
public Type BindingAssemblySdkType => typeof(AiProgramData);
#endregion
#region init command data
public string SERVICE_RESOURCE_DISPLAY_NAME_ALL_CAPS => "AZURE OPENAI RESOURCE";
public string CognitiveServiceResourceKind => "OpenAI";
public string CognitiveServiceResourceSku => "s0";
public bool InitConfigsEndpoint => true;
public bool InitConfigsSubscription => true;
#endregion
#region help command data
public string HelpCommandTokens => "wizard;dev;test;init;config;chat;speech;vision;language;search;service;tool;samples;eval;run";
#endregion
#region config command data
public string ConfigScopeTokens => $"wizard;dev;test;init;chat;speech;vision;language;search;service;tool;samples;eval;run;*";
#endregion
#region zip option data
public string[] ZipIncludes => new string[]
{
"LICENSE.txt",
// "THIRD_PARTY_NOTICE.txt",
"Azure.Core.dll",
"Azure.Identity.dll",
"Azure.AI.CLI.Common.dll",
"Azure.AI.CLI.Extensions.HelperFunctions.dll",
"Azure.AI.CLI.Extensions.Templates.dll",
"Azure.AI.OpenAI.dll",
"Azure.Core.dll",
"Azure.Search.Documents.dll",
"JmePath.Net.Parser.dll",
"JmesPath.Net.dll",
"Microsoft.Bcl.AsyncInterfaces.dll",
"Microsoft.Bcl.HashCode.dll",
"Microsoft.CognitiveServices.Speech.core.dll",
"Microsoft.CognitiveServices.Speech.csharp.dll",
"Microsoft.CognitiveServices.Speech.extension.audio.sys.dll",
"Microsoft.CognitiveServices.Speech.extension.kws.dll",
"Microsoft.CognitiveServices.Speech.extension.kws.ort.dll",
"Microsoft.CognitiveServices.Speech.extension.lu.dll",
"Microsoft.CognitiveServices.Speech.extension.codec.dll",
"Microsoft.CognitiveServices.Speech.extension.silk_codec.dll",
"Microsoft.Extensions.Logging.Abstractions.dll",
"Microsoft.SemanticKernel.Abstractions.dll",
"Microsoft.SemanticKernel.Connectors.AI.OpenAI.dll",
"Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch.dll",
"Microsoft.SemanticKernel.Core.dll",
"Microsoft.SemanticKernel.dll",
"Microsoft.SemanticKernel.Planning.ActionPlanner.dll",
"Microsoft.SemanticKernel.Planning.SequentialPlanner.dll",
"Microsoft.SemanticKernel.Skills.Core.dll",
"System.Diagnostics.DiagnosticSource.dll",
"System.Interactive.Async.dll",
"System.Linq.Async.dll",
"System.Memory.Data.dll",
"System.Text.Json.dll"
};
#endregion
public bool DispatchRunCommand(ICommandValues values)
{
var command = values.GetCommand();
var root = command.Split('.').FirstOrDefault();
return root switch {
"init" => (new InitCommand(values)).RunCommand(),
"chat" => (new ChatCommand(values)).RunCommand(),
"speech" => (new SpeechCommand(values)).RunCommand(),
"vision" => (new VisionCommand(values)).RunCommand(),
"language" => (new LanguageCommand(values)).RunCommand(),
"search" => (new SearchCommand(values)).RunCommand(),
"service" => (new ServiceCommand(values)).RunCommand(),
"tool" => (new ToolCommand(values)).RunCommand(),
"eval" => (new EvalCommand(values)).RunCommand(),
"wizard" => (new ScenarioWizardCommand(values)).RunCommand(),
"dev" => (new DevCommand(values)).RunCommand(),
"test" => (new TestCommand(values)).RunCommand(),
"run" => (new RunJobCommand(values)).RunCommand(),
"version" => (new VersionCommand(values)).RunCommand(),
"update" => (new VersionCommand(values)).RunCommand(),
_ => false
};
}
public bool DispatchParseCommand(INamedValueTokens tokens, ICommandValues values)
{
var command = values.GetCommand(null) ?? tokens.PeekNextToken();
var root = command.Split('.').FirstOrDefault();
return root switch
{
"help" => HelpCommandParser.ParseCommand(tokens, values),
"init" => InitCommandParser.ParseCommand(tokens, values),
"config" => ConfigCommandParser.ParseCommand(tokens, values),
"chat" => ChatCommandParser.ParseCommand(tokens, values),
"eval" => EvalCommandParser.ParseCommand(tokens, values),
"speech" => SpeechCommandParser.ParseCommand(tokens, values),
"vision" => VisionCommandParser.ParseCommand(tokens, values),
"language" => LanguageCommandParser.ParseCommand(tokens, values),
"search" => SearchCommandParser.ParseCommand(tokens, values),
"service" => ServiceCommandParser.ParseCommand(tokens, values),
"tool" => ToolCommandParser.ParseCommand(tokens, values),
"wizard" => ScenarioWizardCommandParser.ParseCommand(tokens, values),
"version" => VersionCommandParser.ParseCommand(tokens, values),
"update" => UpdateCommandParser.ParseCommand(tokens, values),
"dev" => DevCommandParser.ParseCommand(tokens, values),
"test" => TestCommandParser.ParseCommand(tokens, values),
"run" => RunJobCommandParser.ParseCommand(tokens, values),
_ => false
};
}
public bool DispatchParseCommandValues(INamedValueTokens tokens, ICommandValues values)
{
var root = values.GetCommandRoot();
return root switch
{
"init" => InitCommandParser.ParseCommandValues(tokens, values),
"config" => ConfigCommandParser.ParseCommandValues(tokens, values),
"chat" => ChatCommandParser.ParseCommandValues(tokens, values),
"eval" => EvalCommandParser.ParseCommandValues(tokens, values),
"speech" => SpeechCommandParser.ParseCommandValues(tokens, values),
"vision" => VisionCommandParser.ParseCommandValues(tokens, values),
"language" => LanguageCommandParser.ParseCommandValues(tokens, values),
"search" => SearchCommandParser.ParseCommandValues(tokens, values),
"service" => ServiceCommandParser.ParseCommandValues(tokens, values),
"tool" => ToolCommandParser.ParseCommandValues(tokens, values),
"wizard" => ScenarioWizardCommandParser.ParseCommandValues(tokens, values),
"dev" => DevCommandParser.ParseCommandValues(tokens, values),
"test" => TestCommandParser.ParseCommandValues(tokens, values),
"run" => RunJobCommandParser.ParseCommandValues(tokens, values),
"version" => VersionCommandParser.ParseCommandValues(tokens, values),
"update" => UpdateCommandParser.ParseCommandValues(tokens, values),
_ => false
};
}
public bool DisplayKnownErrors(ICommandValues values, Exception ex)
{
var message = ex.Message;
if (message.Contains("az login"))
{
ErrorHelpers.WriteLineMessage(
"ERROR:", $"Azure CLI credential not found.",
"",
"TRY:", $"az login",
"",
"SEE:", "https://docs.microsoft.com/cli/azure/authenticate-azure-cli");
values.Reset("x.verbose", "false");
return true;
}
if (message.Contains("refresh token") && message.Contains("expired"))
{
ErrorHelpers.WriteLineMessage(
"ERROR:", $"Refresh token expired.",
"",
"TRY:", $"az login");
values.Reset("x.verbose", "false");
return true;
}
return false;
}
public IEventLoggerHelpers EventLoggerHelpers => new AiEventLoggerHelpers();
public ITelemetry Telemetry => _telemetry.Value;
public ILoginManager LoginManager { get; }
public ISubscriptionsClient SubscriptionsClient { get; }
public ICognitiveServicesClient CognitiveServicesClient { get; }
public ISearchClient SearchClient { get; }
public ICommandValues Values { get; } = new CommandValues();
}
}