Skip to content

Commit

Permalink
Allow skip OpenAI deployments and/or Cog Search resource creation in …
Browse files Browse the repository at this point in the history
…appropriate locations in `ai init` flows (#91)

* allow skip model deployment

* allow skip cog search

* formatting
  • Loading branch information
robch authored Nov 3, 2023
1 parent c14e1e1 commit 10cfe4e
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 69 deletions.
6 changes: 3 additions & 3 deletions src/ai/commands/scenario_wizard_command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ private async Task ChatWithYourDataScenarioAsync(string scenario)
StartCommand();

var subscription = await AzCliConsoleGui.PickSubscriptionAsync(true, true);
var openAiResource = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResource(true, subscription.Id);
var cogSearchResource = await AzCliConsoleGui.PickOrCreateAndConfigCogSearchResource(subscription.Id, openAiResource.RegionLocation, openAiResource.Group);
var openAiResource = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResource(true, false, subscription.Id);
var cogSearchResource = await AzCliConsoleGui.PickOrCreateAndConfigCogSearchResource(false, subscription.Id, openAiResource.RegionLocation, openAiResource.Group);
// var aiHubResource = await AiSdkConsoleGui.PickOrCreateAiHubResource(_values, subscription.Id);
// var aiHubProject = AiSdkConsoleGui.PickOrCreateAndConfigAiHubProject(true, true, _values, subscription.Id, aiHubResource.Id, openAiResource.Group, openAiResource.Endpoint, openAiResource.Key, cogSearchResource.Endpoint, cogSearchResource.Key);

Expand Down Expand Up @@ -166,7 +166,7 @@ private async Task ChatWithYourDataScenarioAsync(string scenario)

Console.WriteLine("*** UPDATING ***");

var kernel = CreateSemanticKernel(cogSearchResource.Endpoint, cogSearchResource.Key, openAiResource.Endpoint, openAiResource.EmbeddingsDeployment, openAiResource.Key);
var kernel = CreateSemanticKernel(cogSearchResource.Value.Endpoint, cogSearchResource.Value.Key, openAiResource.Endpoint, openAiResource.EmbeddingsDeployment, openAiResource.Key);
await StoreMemoryAsync(kernel, indexName, files.Select(x => new KeyValuePair<string, string>(x, FileHelpers.ReadAllHelpText(x, Encoding.UTF8))));

Console.Write("\r*** UPDATED *** ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Azure.AI.Details.Common.CLI
{
public partial class AiSdkConsoleGui
{
public static async Task<AiHubProjectInfo> PickOrCreateAndConfigAiHubProject(bool allowCreate, bool allowPick, ICommandValues values, string subscription, string resourceId, string groupName, string openAiEndpoint, string openAiKey, string searchEndpoint, string searchKey)
public static async Task<AiHubProjectInfo> PickOrCreateAndConfigAiHubProject(bool allowCreate, bool allowPick, bool allowSkipDeployments, bool allowSkipSearch, ICommandValues values, string subscription, string resourceId, string groupName, string openAiEndpoint, string openAiKey, string searchEndpoint, string searchKey)
{
var createdProject = false;
var project = allowCreate && allowPick
Expand All @@ -27,20 +27,20 @@ public static async Task<AiHubProjectInfo> PickOrCreateAndConfigAiHubProject(boo
? PickAiHubProject(values, subscription, resourceId)
: throw new ApplicationException($"CANCELED: No project selected");

var createdSearch = false;
var createdOrPickedSearch = false;
if (!createdProject)
{
var (hubName, openai, search) = await AiSdkConsoleGui.VerifyResourceConnections(values, subscription, project.Group, project.Name);

if (!string.IsNullOrEmpty(openai?.Name))
{
var (chatDeployment, embeddingsDeployment, evaluationDeployment, keys) = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResourceDeployments("AZURE OPENAI RESOURCE", true, subscription, openai.Value);
var (chatDeployment, embeddingsDeployment, evaluationDeployment, keys) = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResourceDeployments("AZURE OPENAI RESOURCE", true, allowSkipDeployments, subscription, openai.Value);
openAiEndpoint = openai.Value.Endpoint;
openAiKey = keys.Key1;
}
else
{
var openAiResource = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResource(true, subscription);
var openAiResource = await AzCliConsoleGui.PickOrCreateAndConfigCognitiveServicesOpenAiKindResource(true, allowSkipDeployments, subscription);
openAiEndpoint = openAiResource.Endpoint;
openAiKey = openAiResource.Key;
}
Expand All @@ -54,14 +54,17 @@ public static async Task<AiHubProjectInfo> PickOrCreateAndConfigAiHubProject(boo
}
else
{
var pickedOrCreated = await AzCliConsoleGui.PickOrCreateAndConfigCogSearchResource(subscription, null, null, project.Name, "aiproj");
searchEndpoint = pickedOrCreated.Endpoint;
searchKey = pickedOrCreated.Key;
createdSearch = true;
var pickedOrCreated = await AzCliConsoleGui.PickOrCreateAndConfigCogSearchResource(allowSkipSearch, subscription, null, null, project.Name, "aiproj");
createdOrPickedSearch = pickedOrCreated != null;
if (createdOrPickedSearch)
{
searchEndpoint = pickedOrCreated.Value.Endpoint;
searchKey = pickedOrCreated.Value.Key;
}
}
}

GetOrCreateAiHubProjectConnections(values, createdProject || createdSearch, subscription, project.Group, project.Name, openAiEndpoint, openAiKey, searchEndpoint, searchKey);
GetOrCreateAiHubProjectConnections(values, createdProject || createdOrPickedSearch, subscription, project.Group, project.Name, openAiEndpoint, openAiKey, searchEndpoint, searchKey);
CreateAiHubProjectConfigJsonFile(subscription, project.Group, project.Name);

return project;
Expand Down
13 changes: 12 additions & 1 deletion src/common/details/azcli/AzCliConsoleGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class AzCliConsoleGui
return keys.Payload;
}

public static async Task<AzCli.CognitiveSearchResourceInfo> PickOrCreateCognitiveSearchResource(string subscription, string location, string groupName, string smartName = null, string smartNameKind = null)
public static async Task<AzCli.CognitiveSearchResourceInfo?> PickOrCreateCognitiveSearchResource(bool allowSkip, string subscription, string location, string groupName, string smartName = null, string smartNameKind = null)
{
ConsoleHelpers.WriteLineWithHighlight($"\n`AI SEARCH RESOURCE`");
Console.Write("\rName: *** Loading choices ***");
Expand All @@ -48,6 +48,11 @@ public partial class AzCliConsoleGui
var choices = resources.Select(x => $"{x.Name} ({x.RegionLocation})").ToList();
choices.Insert(0, "(Create new)");

if (allowSkip)
{
choices.Add("(Skip)");
}

Console.Write("\rName: ");

var picked = ListBoxPicker.PickIndexOf(choices.ToArray());
Expand All @@ -56,6 +61,12 @@ public partial class AzCliConsoleGui
throw new ApplicationException($"CANCELED: No resource selected");
}

if (allowSkip && picked == choices.Count - 1)
{
Console.WriteLine($"\rName: (Skipped)");
return null;
}

Console.WriteLine($"\rName: {choices[picked]}");
var resource = picked > 0 ? resources[picked - 1] : new AzCli.CognitiveSearchResourceInfo();
if (picked == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ namespace Azure.AI.Details.Common.CLI
{
public partial class AzCliConsoleGui
{
public static async Task<AzCli.CognitiveServicesDeploymentInfo> PickOrCreateCognitiveServicesResourceDeployment(bool interactive, string deploymentExtra, string subscriptionId, string groupName, string resourceRegionLocation, string resourceName, string deploymentFilter)
public static async Task<AzCli.CognitiveServicesDeploymentInfo?> PickOrCreateCognitiveServicesResourceDeployment(bool interactive, bool allowSkipDeployment, string deploymentExtra, string subscriptionId, string groupName, string resourceRegionLocation, string resourceName, string deploymentFilter)
{
ConsoleHelpers.WriteLineWithHighlight($"\n`AZURE OPENAI DEPLOYMENT ({deploymentExtra.ToUpper()})`");

var createNewItem = !string.IsNullOrEmpty(deploymentFilter)
? $"(Create `{deploymentFilter}`)"
: interactive ? "(Create new)" : null;

var deployment = await FindCognitiveServicesResourceDeployment(interactive, deploymentExtra, subscriptionId, groupName, resourceName, deploymentFilter, createNewItem);
var deployment = await FindCognitiveServicesResourceDeployment(interactive, allowSkipDeployment, deploymentExtra, subscriptionId, groupName, resourceName, deploymentFilter, createNewItem);
if (deployment == null && allowSkipDeployment) return null;

if (deployment != null && deployment.Value.Name == null)
{
deployment = await TryCreateCognitiveServicesResourceDeployment(interactive, deploymentExtra, subscriptionId, groupName, resourceRegionLocation, resourceName, deploymentFilter);
Expand All @@ -35,7 +37,7 @@ public partial class AzCliConsoleGui
return deployment.Value;
}

public static async Task<AzCli.CognitiveServicesDeploymentInfo?> FindCognitiveServicesResourceDeployment(bool interactive, string deploymentExtra, string subscriptionId, string groupName, string resourceName, string deploymentFilter, string allowCreateDeploymentOption)
public static async Task<AzCli.CognitiveServicesDeploymentInfo?> FindCognitiveServicesResourceDeployment(bool interactive, bool allowSkipDeployment, string deploymentExtra, string subscriptionId, string groupName, string resourceName, string deploymentFilter, string allowCreateDeploymentOption)
{
var allowCreateDeployment = !string.IsNullOrEmpty(allowCreateDeploymentOption);

Expand Down Expand Up @@ -97,7 +99,7 @@ public partial class AzCliConsoleGui
select = Math.Max(0, select);

return interactive
? ListBoxPickDeployment(choices, allowCreateDeploymentOption, select)
? ListBoxPickDeployment(choices, allowCreateDeploymentOption, allowSkipDeployment, select)
: null;
}

Expand Down Expand Up @@ -198,13 +200,18 @@ private static AzCli.CognitiveServicesModelInfo[] FilterModelsByUsage(AzCli.Cogn
return filtered.ToArray();
}

private static AzCli.CognitiveServicesDeploymentInfo? ListBoxPickDeployment(AzCli.CognitiveServicesDeploymentInfo[] deployments, string p0, int select = 0)
private static AzCli.CognitiveServicesDeploymentInfo? ListBoxPickDeployment(AzCli.CognitiveServicesDeploymentInfo[] deployments, string p0, bool allowSkipDeployment = false, int select = 0)
{
var list = deployments.Select(x => $"{x.Name} ({x.ModelName})").ToList();

var hasP0 = !string.IsNullOrEmpty(p0);
if (hasP0) list.Insert(0, p0);

if (allowSkipDeployment)
{
list.Add("(Skip)");
}

var picked = ListBoxPicker.PickIndexOf(list.ToArray(), select);
if (picked < 0)
{
Expand All @@ -217,6 +224,12 @@ private static AzCli.CognitiveServicesModelInfo[] FilterModelsByUsage(AzCli.Cogn
return new AzCli.CognitiveServicesDeploymentInfo();
}

if (allowSkipDeployment && picked == list.Count - 1)
{
Console.WriteLine("(Skip)");
return null;
}

if (hasP0) picked--;
Console.WriteLine($"{deployments[picked].Name}");
return deployments[picked];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ namespace Azure.AI.Details.Common.CLI
{
public partial class AzCliConsoleGui
{
public static async Task<AzCli.CognitiveSearchResourceInfoEx> PickOrCreateAndConfigCogSearchResource(string subscription, string location, string groupName, string smartName = null, string smartNameKind = null)
public static async Task<AzCli.CognitiveSearchResourceInfoEx?> PickOrCreateAndConfigCogSearchResource(bool allowSkip, string subscription, string location, string groupName, string smartName = null, string smartNameKind = null)
{
var resource = await AzCliConsoleGui.PickOrCreateCognitiveSearchResource(subscription, location, groupName, smartName, smartNameKind);
var keys = await AzCliConsoleGui.LoadSearchResourceKeys(subscription, resource);
var resource = await AzCliConsoleGui.PickOrCreateCognitiveSearchResource(allowSkip, subscription, location, groupName, smartName, smartNameKind);
if (resource == null) return null;

ConfigSetHelpers.ConfigSearchResource(resource.Endpoint, keys.Key1);
var keys = await AzCliConsoleGui.LoadSearchResourceKeys(subscription, resource.Value);

ConfigSetHelpers.ConfigSearchResource(resource.Value.Endpoint, keys.Key1);

return new AzCli.CognitiveSearchResourceInfoEx
{
Id = resource.Id,
Group = resource.Group,
Name = resource.Name,
RegionLocation = resource.RegionLocation,
Endpoint = resource.Endpoint,
Id = resource.Value.Id,
Group = resource.Value.Group,
Name = resource.Value.Name,
RegionLocation = resource.Value.RegionLocation,
Endpoint = resource.Value.Endpoint,
Key = keys.Key1,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ namespace Azure.AI.Details.Common.CLI
{
public partial class AzCliConsoleGui
{
public static async Task<AzCli.CognitiveServicesResourceInfoEx> PickOrCreateAndConfigCognitiveServicesAiServicesKindResource(bool interactive, string subscriptionId, string regionFilter = null, string groupFilter = null, string resourceFilter = null, string kinds = null, string sku = null, bool yes = false)
public static async Task<AzCli.CognitiveServicesResourceInfoEx> PickOrCreateAndConfigCognitiveServicesAiServicesKindResource(bool interactive, bool allowSkipDeployments, string subscriptionId, string regionFilter = null, string groupFilter = null, string resourceFilter = null, string kinds = null, string sku = null, bool yes = false)
{
kinds ??= "AIServices";
var sectionHeader = "AI SERVICES";

var regionLocation = !string.IsNullOrEmpty(regionFilter) ? await AzCliConsoleGui.PickRegionLocationAsync(interactive, regionFilter) : new AzCli.AccountRegionLocationInfo();
var resource = await AzCliConsoleGui.PickOrCreateCognitiveResource(sectionHeader, interactive, subscriptionId, regionLocation.Name, groupFilter, resourceFilter, kinds, sku, yes);

var (chatDeployment, embeddingsDeployment, evaluationDeployment, keys) = await PickOrCreateAndConfigCognitiveServicesOpenAiKindResourceDeployments(sectionHeader, interactive, subscriptionId, resource);
var (chatDeployment, embeddingsDeployment, evaluationDeployment, keys) = await PickOrCreateAndConfigCognitiveServicesOpenAiKindResourceDeployments(sectionHeader, interactive, allowSkipDeployments, subscriptionId, resource);

return new AzCli.CognitiveServicesResourceInfoEx
{
Expand All @@ -38,9 +38,9 @@ public partial class AzCliConsoleGui
RegionLocation = resource.RegionLocation,
Endpoint = resource.Endpoint,
Key = keys.Key1,
ChatDeployment = chatDeployment.Name,
EmbeddingsDeployment = embeddingsDeployment.Name,
EvaluationDeployment = evaluationDeployment.Name
ChatDeployment = chatDeployment.HasValue ? chatDeployment.Value.Name : null,
EmbeddingsDeployment = embeddingsDeployment.HasValue ? embeddingsDeployment.Value.Name : null,
EvaluationDeployment = evaluationDeployment.HasValue ? evaluationDeployment.Value.Name : null
};
}
}
Expand Down
Loading

0 comments on commit 10cfe4e

Please sign in to comment.