Skip to content

Commit

Permalink
ai flow upload (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
robch authored Nov 7, 2023
1 parent c109977 commit 95627a4
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/ai/.x/help/flow
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COMMANDS
ai flow invoke [...] (see: ai help flow invoke)
ai flow serve [...] (see: ai help flow serve)

ai flow upload [...] (see: ai help flow upload)
ai flow package [...] (see: ai help flow package)
ai flow deploy [...] (see: ai help flow deploy)

Expand Down
1 change: 1 addition & 0 deletions src/ai/.x/help/flow.invoke
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ SEE ALSO

ai help flow
ai help flow new
ai help flow upload
ai help flow examples
ai help find topics flow
1 change: 1 addition & 0 deletions src/ai/.x/help/flow.new
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ SEE ALSO

ai help flow
ai help flow invoke
ai help flow upload
ai help flow examples
ai help find topics flow
26 changes: 26 additions & 0 deletions src/ai/.x/help/flow.upload
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FLOW PACKAGE

@include.the.flow.upload.command

USAGE: ai flow upload [...]

AZURE
--subscription SUBSCRIPTION (see: ai help subscription)
--group GROUP (see: ai help resource group)
--project NAME (see: ai help project name)

FLOW
--name NAME

EXAMPLE

ai flow new --name my_flow
ai flow upload --name my_flow

SEE ALSO

ai help flow
ai help flow new
ai help flow invoke
ai help flow examples
ai help find topics flow
1 change: 1 addition & 0 deletions src/ai/.x/help/include.the.flow.upload.command
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `ai flow upload` command uploads an existing flow to AI Studio.
47 changes: 23 additions & 24 deletions src/ai/commands/flow_command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,94 +45,88 @@ private async Task<bool> RunFlowCommand()

private async Task DoCommand(string command)
{
CheckPath();
StartCommand();

switch (command)
{
case "flow.new": await DoNewFlow(); break;
case "flow.invoke": await DoInvokeFlow(); break;
case "flow.serve": await DoServeFlow(); break;
case "flow.upload": await DoUploadFlow(); break;
case "flow.package": await DoPackageFlow(); break;
// case "flow.deploy": await DoDeployFlow(); break;

default:
_values.AddThrowError("WARNING:", $"'{command.Replace('.', ' ')}' NOT YET IMPLEMENTED!!");
break;
}

StopCommand();
DisposeAfterStop();
DeleteTemporaryFiles();
}

private async Task DoNewFlow()
{
var action = "Creating flow";
var command = "flow new";

StartCommand();

var flow = FlowNameToken.Data().Demand(_values, action, command);
var prompt = SystemPromptTemplateToken.Data().GetOrDefault(_values);
var function = FunctionToken.Data().GetOrDefault(_values, "");
SplitFunctionReference(function, out var module, out function);

await DoFlowInitConsoleGui(flow, prompt, function, module);

StopCommand();
DisposeAfterStop();
DeleteTemporaryFiles();
}

private async Task DoInvokeFlow()
{
var action = "Invoking flow";
var command = "flow invoke";

StartCommand();

var flowName = FlowNameToken.Data().Demand(_values, action, command);
var nodeName = FlowNodeToken.Data().GetOrDefault(_values);
var inputs = string.Join(" ", InputWildcardToken.GetNames(_values)
.Select(name => $"{name}={InputWildcardToken.Data(name).GetOrDefault(_values)}"));

await DoFlowTestConsoleGui(flowName, inputs, nodeName);

StopCommand();
DisposeAfterStop();
DeleteTemporaryFiles();
}

private async Task DoServeFlow()
{
var action = "Serving flow";
var command = "flow serve";

StartCommand();

var flowName = FlowNameToken.Data().Demand(_values, action, command);
var port = PortToken.Data().GetOrDefault(_values);
var host = HostToken.Data().GetOrDefault(_values);
var env = EnvironmentVariablesToken.Data().GetOrDefault(_values);

await DoFlowServeConsoleGui(flowName, port, host, env);
}

StopCommand();
DisposeAfterStop();
DeleteTemporaryFiles();
private async Task DoUploadFlow()
{
var action = "Uploading flow";
var command = "flow upload";

var flowName = FlowNameToken.Data().Demand(_values, action, command);
var subscription = SubscriptionToken.Data().Demand(_values, action, command, "subscription");
var resourceGroup = ResourceGroupNameToken.Data().Demand(_values, action, command, "group");
var projectName = ProjectNameToken.Data().Demand(_values, action, command, "project");

await DoFlowUpload(subscription, resourceGroup, projectName, flowName);
}

private async Task DoPackageFlow()
{
var action = "Packaging flow";
var command = "flow package";

StartCommand();

var flowName = FlowNameToken.Data().Demand(_values, action, command);
var dockerBuildContext = DockerBuildContextToken.Data().Demand(_values, action, command);

await DoFlowBuild(flowName, dockerBuildContext, "docker");

StopCommand();
DisposeAfterStop();
DeleteTemporaryFiles();
}

private async Task DoFlowInitConsoleGui(string flow, string prompt, string function, string module)
Expand Down Expand Up @@ -162,6 +156,11 @@ private async Task DoFlowServeConsoleGui(string flowName, string port, string ho
}
}

private Task DoFlowUpload(string subscription, string resourceGroup, string projectName, string flowName)
{
return PfCli.FlowUpload(subscription, resourceGroup, projectName, flowName);
}

private async Task DoFlowBuild(string flowName, string dockerBuildContext, string format)
{
var response = await PfCli.FlowBuild(flowName, dockerBuildContext, format);
Expand Down
11 changes: 11 additions & 0 deletions src/ai/commands/parsers/flow_command_parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static readonly (string name, bool valuesRequired)[] _commands = {
("flow.new", true),
("flow.invoke", true),
("flow.serve", true),
("flow.upload", true),
("flow.package", true),
("flow.deploy", true),
};
Expand All @@ -40,6 +41,7 @@ private static IEnumerable<INamedValueTokenParser> GetCommandParsers(ICommandVal
case "flow.new": return _flowNewParsers;
case "flow.invoke": return _flowInvokeParsers;
case "flow.deploy": return _flowDeployParsers;
case "flow.upload": return _flowUploadParsers;
case "flow.package": return _flowPackageParsers;
case "flow.serve": return _flowServeParsers;
}
Expand Down Expand Up @@ -99,6 +101,15 @@ public CommonFlowNamedValueTokenParsers() : base(
FlowNameToken.Parser()
};

private static INamedValueTokenParser[] _flowUploadParsers = {
new CommonFlowNamedValueTokenParsers(),
SubscriptionToken.Parser(),
ResourceGroupNameToken.Parser(),
ProjectNameToken.Parser(),
FlowNameToken.Parser(),
};


private static INamedValueTokenParser[] _flowPackageParsers = {
new CommonFlowNamedValueTokenParsers(),
FlowNameToken.Parser(),
Expand Down
12 changes: 12 additions & 0 deletions src/common/details/pfcli/PfCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public static async Task<ProcessOutput> FlowTest(string flowPath, string inputs,
return await ProcessHelpers.RunShellCommandAsync("pf", $"{cmdPart} {argsPart}", null, StdOutputHandler(), StandardErrorHandler());
}

public static Task FlowUpload(string subscription, string resourceGroup, string projectName, string flowName)
{
var cmdPart = "flow create";
var argsPart = CliHelpers.BuildCliArgs(
"--subscription", subscription,
"--resource-group", resourceGroup,
"--workspace-name", projectName,
"--flow", flowName);

return ProcessHelpers.RunShellCommandAsync("pfazure", $"{cmdPart} {argsPart}", null, StdOutputHandler(), StandardErrorHandler());
}

public static async Task<ProcessOutput> FlowBuild(string flowPath, string output, string format, string variant = null, bool verbose = false, bool debug = false)
{
var cmdPart = "flow build";
Expand Down

0 comments on commit 95627a4

Please sign in to comment.