-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathinit.go
More file actions
1129 lines (963 loc) · 37.3 KB
/
init.go
File metadata and controls
1129 lines (963 loc) · 37.3 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/cognitiveservices/armcognitiveservices"
"github.com/azure/azure-dev/cli/azd/pkg/azdext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/tools/github"
"github.com/azure/azure-dev/cli/azd/pkg/ux"
"github.com/braydonk/yaml"
"github.com/fatih/color"
"github.com/spf13/cobra"
"azure.ai.finetune/internal/services"
)
type initFlags struct {
rootFlagsDefinition
template string
projectResourceId string
subscriptionId string
projectEndpoint string
jobId string
src string
env string
}
// AiProjectResourceConfig represents the configuration for an AI project resource
type AiProjectResourceConfig struct {
Models []map[string]any `json:"models,omitempty"`
}
type InitAction struct {
azdClient *azdext.AzdClient
//azureClient *azure.AzureClient
azureContext *azdext.AzureContext
//composedResources []*azdext.ComposedResource
console input.Console
credential azcore.TokenCredential
projectConfig *azdext.ProjectConfig
environment *azdext.Environment
flags *initFlags
}
// GitHubUrlInfo holds parsed information from a GitHub URL
type GitHubUrlInfo struct {
RepoSlug string
Branch string
FilePath string
Hostname string
}
const AiFineTuningHost = "azure.ai.finetune"
func newInitCommand(rootFlags rootFlagsDefinition) *cobra.Command {
flags := &initFlags{
rootFlagsDefinition: rootFlags,
}
cmd := &cobra.Command{
Use: "init [-t <fine tuning job template>] [-p <foundry project arm id>]",
Short: fmt.Sprintf("Initialize a new AI Fine-tuning project. %s", color.YellowString("(Preview)")),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := azdext.WithAccessToken(cmd.Context())
azdClient, err := azdext.NewAzdClient()
if err != nil {
return fmt.Errorf("failed to create azd client: %w", err)
}
defer azdClient.Close()
// Wait for debugger if AZD_EXT_DEBUG is set
if err := azdext.WaitForDebugger(ctx, azdClient); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, azdext.ErrDebuggerAborted) {
return nil
}
return fmt.Errorf("failed waiting for debugger: %w", err)
}
azureContext, projectConfig, environment, err := ensureAzureContext(ctx, flags, azdClient)
if err != nil {
return fmt.Errorf("failed to ground into a project context: %w", err)
}
credential, err := azidentity.NewAzureDeveloperCLICredential(&azidentity.AzureDeveloperCLICredentialOptions{
TenantID: azureContext.Scope.TenantId,
AdditionallyAllowedTenants: []string{"*"},
})
if err != nil {
return fmt.Errorf("failed to create azure credential: %w", err)
}
console := input.NewConsole(
false, // noPrompt
true, // isTerminal
input.Writers{Output: os.Stdout},
input.ConsoleHandles{
Stderr: os.Stderr,
Stdin: os.Stdin,
Stdout: os.Stdout,
},
nil, // formatter
nil, // externalPromptCfg
)
action := &InitAction{
azdClient: azdClient,
azureContext: azureContext,
console: console,
credential: credential,
projectConfig: projectConfig,
environment: environment,
flags: flags,
}
if err := action.Run(ctx); err != nil {
return fmt.Errorf("failed to run start action: %w", err)
}
return nil
},
}
cmd.Flags().StringVarP(&flags.template, "template", "t", "",
"URL or path to a fine-tune job template")
cmd.Flags().StringVarP(&flags.projectResourceId, "project-resource-id", "p", "",
"ARM resource ID of the Microsoft Foundry Project (e.g., /subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.CognitiveServices/accounts/{account}/projects/{project})")
cmd.Flags().StringVarP(&flags.subscriptionId, "subscription", "s", "",
"Azure subscription ID")
cmd.Flags().StringVarP(&flags.projectEndpoint, "project-endpoint", "e", "",
"Azure AI Foundry project endpoint URL (e.g., https://account.services.ai.azure.com/api/projects/project-name)")
cmd.Flags().StringVarP(&flags.src, "working-directory", "w", "",
"Local path for project output")
cmd.Flags().StringVarP(&flags.jobId, "from-job", "j", "",
"Clone configuration from an existing job ID")
cmd.Flags().StringVarP(&flags.env, "environment", "n", "", "The name of the azd environment to use.")
return cmd
}
type FoundryProject struct {
TenantId string `json:"tenantId"`
SubscriptionId string `json:"subscriptionId"`
Location string `json:"location"`
ResourceGroupName string `json:"resourceGroupName"`
AiAccountName string `json:"aiAccountName"`
AiProjectName string `json:"aiProjectName"`
}
func extractProjectDetails(projectResourceId string) (*FoundryProject, error) {
resourceId, err := arm.ParseResourceID(projectResourceId)
if err != nil {
return nil, fmt.Errorf("failed to parse project resource ID: %w", err)
}
// Validate that this is a Cognitive Services project resource
if resourceId.ResourceType.Namespace != "Microsoft.CognitiveServices" || len(resourceId.ResourceType.Types) != 2 ||
resourceId.ResourceType.Types[0] != "accounts" || resourceId.ResourceType.Types[1] != "projects" {
return nil, fmt.Errorf(
"the given resource ID is not a Microsoft Foundry project. Expected format: " +
"/subscriptions/[SUBSCRIPTION_ID]/resourceGroups/[RESOURCE_GROUP]/providers/" +
"Microsoft.CognitiveServices/accounts/[ACCOUNT_NAME]/projects/[PROJECT_NAME]")
}
// Extract the components
return &FoundryProject{
SubscriptionId: resourceId.SubscriptionID,
ResourceGroupName: resourceId.ResourceGroupName,
AiAccountName: resourceId.Parent.Name,
AiProjectName: resourceId.Name,
}, nil
}
// parseProjectEndpoint extracts account name and project name from an endpoint URL
// Example: https://account-name.services.ai.azure.com/api/projects/project-name
func parseProjectEndpoint(endpoint string) (accountName string, projectName string, err error) {
parsedURL, err := url.Parse(endpoint)
if err != nil {
return "", "", fmt.Errorf("failed to parse endpoint URL: %w", err)
}
// Extract account name from hostname (e.g., "account-name.services.ai.azure.com")
hostname := parsedURL.Hostname()
hostParts := strings.Split(hostname, ".")
if len(hostParts) < 1 || hostParts[0] == "" {
return "", "", fmt.Errorf("invalid endpoint URL: cannot extract account name from hostname")
}
accountName = hostParts[0]
// Extract project name from path (e.g., "/api/projects/project-name")
pathParts := strings.Split(strings.Trim(parsedURL.Path, "/"), "/")
// Expected path: api/projects/{project-name}
if len(pathParts) >= 3 && pathParts[0] == "api" && pathParts[1] == "projects" {
projectName = pathParts[2]
} else {
return "", "", fmt.Errorf("invalid endpoint URL: cannot extract project name from path. Expected format: /api/projects/{project-name}")
}
return accountName, projectName, nil
}
// findProjectByEndpoint searches for a Foundry project matching the endpoint URL
func findProjectByEndpoint(
ctx context.Context,
subscriptionId string,
accountName string,
projectName string,
credential azcore.TokenCredential,
) (*FoundryProject, error) {
// Create Cognitive Services Accounts client to search for the account
accountsClient, err := armcognitiveservices.NewAccountsClient(subscriptionId, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create Cognitive Services Accounts client: %w", err)
}
// List all accounts in the subscription and find the matching one
pager := accountsClient.NewListPager(nil)
var foundAccount *armcognitiveservices.Account
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list Cognitive Services accounts: %w", err)
}
for _, account := range page.Value {
if account.Name != nil && strings.EqualFold(*account.Name, accountName) {
foundAccount = account
break
}
}
if foundAccount != nil {
break
}
}
if foundAccount == nil {
return nil, fmt.Errorf("could not find Cognitive Services account '%s' in subscription '%s'", accountName, subscriptionId)
}
// Parse the account's resource ID to get resource group
accountResourceId, err := arm.ParseResourceID(*foundAccount.ID)
if err != nil {
return nil, fmt.Errorf("failed to parse account resource ID: %w", err)
}
// Create Projects client to verify the project exists
projectsClient, err := armcognitiveservices.NewProjectsClient(subscriptionId, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create Cognitive Services Projects client: %w", err)
}
// Get the project to verify it exists and get its details
projectResp, err := projectsClient.Get(ctx, accountResourceId.ResourceGroupName, accountName, projectName, nil)
if err != nil {
return nil, fmt.Errorf("could not find project '%s' under account '%s': %w", projectName, accountName, err)
}
return &FoundryProject{
SubscriptionId: subscriptionId,
ResourceGroupName: accountResourceId.ResourceGroupName,
AiAccountName: accountName,
AiProjectName: projectName,
Location: *projectResp.Location,
}, nil
}
func getExistingEnvironment(ctx context.Context, flags *initFlags, azdClient *azdext.AzdClient) *azdext.Environment {
var env *azdext.Environment
if flags.env == "" {
if envResponse, err := azdClient.Environment().GetCurrent(ctx, &azdext.EmptyRequest{}); err == nil {
env = envResponse.Environment
}
} else {
if envResponse, err := azdClient.Environment().Get(ctx, &azdext.GetEnvironmentRequest{
Name: flags.env,
}); err == nil {
env = envResponse.Environment
}
}
return env
}
func ensureEnvironment(ctx context.Context, flags *initFlags, azdClient *azdext.AzdClient) (*azdext.Environment, error) {
var foundryProject *FoundryProject
// Handle project endpoint URL - extract account/project names and find the ARM resource
if flags.projectEndpoint != "" {
accountName, projectName, err := parseProjectEndpoint(flags.projectEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse project endpoint: %w", err)
}
fmt.Printf("Parsed endpoint - Account: %s, Project: %s\n", accountName, projectName)
// Get subscription ID - either from flag or prompt
subscriptionId := flags.subscriptionId
var tenantId string
if subscriptionId == "" {
fmt.Println("Subscription ID is required to find the project. Let's select one.")
subscriptionResponse, err := azdClient.Prompt().PromptSubscription(ctx, &azdext.PromptSubscriptionRequest{})
if err != nil {
return nil, fmt.Errorf("failed to prompt for subscription: %w", err)
}
subscriptionId = subscriptionResponse.Subscription.Id
tenantId = subscriptionResponse.Subscription.UserTenantId
} else {
// Get tenant ID from subscription
tenantResponse, err := azdClient.Account().LookupTenant(ctx, &azdext.LookupTenantRequest{
SubscriptionId: subscriptionId,
})
if err != nil {
return nil, fmt.Errorf("failed to get tenant ID: %w", err)
}
tenantId = tenantResponse.TenantId
}
// Create credential
credential, err := azidentity.NewAzureDeveloperCLICredential(&azidentity.AzureDeveloperCLICredentialOptions{
TenantID: tenantId,
AdditionallyAllowedTenants: []string{"*"},
})
if err != nil {
return nil, fmt.Errorf("failed to create Azure credential: %w", err)
}
// Find the project by searching the subscription
spinner := ux.NewSpinner(&ux.SpinnerOptions{
Text: fmt.Sprintf("Searching for project in subscription %s...", subscriptionId),
})
if err := spinner.Start(ctx); err != nil {
fmt.Printf("failed to start spinner: %v\n", err)
}
foundryProject, err = findProjectByEndpoint(ctx, subscriptionId, accountName, projectName, credential)
_ = spinner.Stop(ctx)
if err != nil {
return nil, fmt.Errorf("failed to find project from endpoint: %w", err)
}
foundryProject.TenantId = tenantId
fmt.Printf("Found project - Resource Group: %s, Account: %s, Project: %s\n",
foundryProject.ResourceGroupName, foundryProject.AiAccountName, foundryProject.AiProjectName)
} else if flags.projectResourceId != "" {
// Parse the Microsoft Foundry project resource ID if provided & Fetch Tenant Id and Location using parsed information
var err error
foundryProject, err = extractProjectDetails(flags.projectResourceId)
if err != nil {
return nil, fmt.Errorf("failed to parse Microsoft Foundry project ID: %w", err)
}
// Get the tenant ID
tenantResponse, err := azdClient.Account().LookupTenant(ctx, &azdext.LookupTenantRequest{
SubscriptionId: foundryProject.SubscriptionId,
})
if err != nil {
return nil, fmt.Errorf("failed to get tenant ID: %w", err)
}
foundryProject.TenantId = tenantResponse.TenantId
credential, err := azidentity.NewAzureDeveloperCLICredential(&azidentity.AzureDeveloperCLICredentialOptions{
TenantID: foundryProject.TenantId,
AdditionallyAllowedTenants: []string{"*"},
})
if err != nil {
return nil, fmt.Errorf("failed to create Azure credential: %w", err)
}
// Create Cognitive Services Projects client
projectsClient, err := armcognitiveservices.NewProjectsClient(foundryProject.SubscriptionId, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create Cognitive Services Projects client: %w", err)
}
// Get the Microsoft Foundry project
projectResp, err := projectsClient.Get(ctx, foundryProject.ResourceGroupName, foundryProject.AiAccountName, foundryProject.AiProjectName, nil)
if err != nil {
return nil, fmt.Errorf("failed to get Microsoft Foundry project: %w", err)
}
foundryProject.Location = *projectResp.Location
}
// Get specified or current environment if it exists
existingEnv := getExistingEnvironment(ctx, flags, azdClient)
if existingEnv == nil {
// Dispatch `azd env new` to create a new environment with interactive flow
fmt.Println("Lets create a new default azd environment for your project.")
envArgs := []string{"env", "new"}
if flags.env != "" {
envArgs = append(envArgs, flags.env)
}
if foundryProject != nil {
envArgs = append(envArgs, "--subscription", foundryProject.SubscriptionId)
envArgs = append(envArgs, "--location", foundryProject.Location)
}
// Add --no-prompt flag if non-interactive mode is requested
if flags.NoPrompt {
envArgs = append(envArgs, "--no-prompt")
}
// Dispatch a workflow to create a new environment
// Handles both interactive and no-prompt flows
workflow := &azdext.Workflow{
Name: "env new",
Steps: []*azdext.WorkflowStep{
{Command: &azdext.WorkflowCommand{Args: envArgs}},
},
}
_, err := azdClient.Workflow().Run(ctx, &azdext.RunWorkflowRequest{
Workflow: workflow,
})
if err != nil {
return nil, fmt.Errorf("failed to create new azd environment: %w", err)
}
// Re-fetch the environment after creation
existingEnv = getExistingEnvironment(ctx, flags, azdClient)
if existingEnv == nil {
return nil, fmt.Errorf("azd environment not found, please create an environment (azd env new) and try again")
}
}
// Set TenantId, SubscriptionId, ResourceGroupName, AiAccountName, and Location in the environment
if foundryProject != nil {
_, err := azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_TENANT_ID",
Value: foundryProject.TenantId,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_TENANT_ID in azd environment: %w", err)
}
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_SUBSCRIPTION_ID",
Value: foundryProject.SubscriptionId,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_SUBSCRIPTION_ID in azd environment: %w", err)
}
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_RESOURCE_GROUP_NAME",
Value: foundryProject.ResourceGroupName,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_RESOURCE_GROUP_NAME in azd environment: %w", err)
}
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_ACCOUNT_NAME",
Value: foundryProject.AiAccountName,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_ACCOUNT_NAME in azd environment: %w", err)
}
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_PROJECT_NAME",
Value: foundryProject.AiProjectName,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_PROJECT_NAME in azd environment: %w", err)
}
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: existingEnv.Name,
Key: "AZURE_LOCATION",
Value: foundryProject.Location,
})
if err != nil {
return nil, fmt.Errorf("failed to set AZURE_LOCATION in environment: %w", err)
}
}
return existingEnv, nil
}
func ensureProject(ctx context.Context, flags *initFlags, azdClient *azdext.AzdClient) (*azdext.ProjectConfig, error) {
projectResponse, err := azdClient.Project().Get(ctx, &azdext.EmptyRequest{})
if err != nil {
fmt.Println("Lets get your project initialized.")
// Environment creation is handled separately in ensureEnvironment
initArgs := []string{"init", "--minimal"}
// Add --no-prompt flag if non-interactive mode is requested
if flags.NoPrompt {
initArgs = append(initArgs, "--no-prompt")
}
// Dispatch a workflow to init the project
workflow := &azdext.Workflow{
Name: "init",
Steps: []*azdext.WorkflowStep{
{Command: &azdext.WorkflowCommand{Args: initArgs}},
},
}
_, err := azdClient.Workflow().Run(ctx, &azdext.RunWorkflowRequest{
Workflow: workflow,
})
if err != nil {
return nil, fmt.Errorf("failed to initialize project: %w", err)
}
projectResponse, err = azdClient.Project().Get(ctx, &azdext.EmptyRequest{})
if err != nil {
return nil, fmt.Errorf("failed to get project: %w", err)
}
fmt.Println()
}
if projectResponse.Project == nil {
return nil, fmt.Errorf("project not found")
}
return projectResponse.Project, nil
}
func ensureAzureContext(
ctx context.Context,
flags *initFlags,
azdClient *azdext.AzdClient,
) (*azdext.AzureContext, *azdext.ProjectConfig, *azdext.Environment, error) {
project, err := ensureProject(ctx, flags, azdClient)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to ensure project: %w", err)
}
env, err := ensureEnvironment(ctx, flags, azdClient)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to ensure environment: %w", err)
}
envValues, err := azdClient.Environment().GetValues(ctx, &azdext.GetEnvironmentRequest{
Name: env.Name,
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get environment values: %w", err)
}
envValueMap := make(map[string]string)
for _, value := range envValues.KeyValues {
envValueMap[value.Key] = value.Value
}
azureContext := &azdext.AzureContext{
Scope: &azdext.AzureScope{
TenantId: envValueMap["AZURE_TENANT_ID"],
SubscriptionId: envValueMap["AZURE_SUBSCRIPTION_ID"],
Location: envValueMap["AZURE_LOCATION"],
ResourceGroup: envValueMap["AZURE_RESOURCE_GROUP_NAME"],
},
Resources: []string{},
}
if azureContext.Scope.SubscriptionId == "" {
fmt.Print()
fmt.Println("It looks like we first need to connect to your Azure subscription.")
subscriptionResponse, err := azdClient.Prompt().PromptSubscription(ctx, &azdext.PromptSubscriptionRequest{})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to prompt for subscription: %w", err)
}
azureContext.Scope.SubscriptionId = subscriptionResponse.Subscription.Id
azureContext.Scope.TenantId = subscriptionResponse.Subscription.UserTenantId
// Set the subscription ID in the environment
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_TENANT_ID",
Value: azureContext.Scope.TenantId,
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to set AZURE_TENANT_ID in environment: %w", err)
}
// Set the tenant ID in the environment
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_SUBSCRIPTION_ID",
Value: azureContext.Scope.SubscriptionId,
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to set AZURE_SUBSCRIPTION_ID in environment: %w", err)
}
}
if azureContext.Scope.ResourceGroup == "" {
fmt.Print()
resourceGroupResponse, err := azdClient.Prompt().
PromptResourceGroup(ctx, &azdext.PromptResourceGroupRequest{
AzureContext: azureContext,
Options: &azdext.PromptResourceGroupOptions{
SelectOptions: &azdext.PromptResourceSelectOptions{
AllowNewResource: new(false),
},
},
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to prompt for resource group: %w", err)
}
azureContext.Scope.ResourceGroup = resourceGroupResponse.ResourceGroup.Name
// Set the subscription ID in the environment
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_RESOURCE_GROUP_NAME",
Value: azureContext.Scope.ResourceGroup,
})
}
if envValueMap["AZURE_ACCOUNT_NAME"] == "" {
foundryProjectResponse, err := azdClient.Prompt().PromptResourceGroupResource(ctx, &azdext.PromptResourceGroupResourceRequest{
AzureContext: azureContext,
Options: &azdext.PromptResourceOptions{
ResourceType: "Microsoft.CognitiveServices/accounts/projects",
ResourceTypeDisplayName: "AI Foundry project",
SelectOptions: &azdext.PromptResourceSelectOptions{
AllowNewResource: new(false),
Message: "Select a Foundry project",
LoadingMessage: "Fetching Foundry projects...",
},
},
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get Microsoft Foundry project: %w", err)
}
fpDetails, err := extractProjectDetails(foundryProjectResponse.Resource.Id)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to parse Microsoft Foundry project ID: %w", err)
}
credential, err := azidentity.NewAzureDeveloperCLICredential(&azidentity.AzureDeveloperCLICredentialOptions{
TenantID: azureContext.Scope.TenantId,
AdditionallyAllowedTenants: []string{"*"},
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create Azure credential: %w", err)
}
// Create Cognitive Services Projects client
projectsClient, err := armcognitiveservices.NewProjectsClient(azureContext.Scope.SubscriptionId, credential, nil)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create Cognitive Services Projects client: %w", err)
}
// Get the Microsoft Foundry project
projectResp, err := projectsClient.Get(ctx, azureContext.Scope.ResourceGroup, fpDetails.AiAccountName, fpDetails.AiProjectName, nil)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get Microsoft Foundry project: %w", err)
}
// Set the subscription ID in the environment
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_ACCOUNT_NAME",
Value: fpDetails.AiAccountName,
})
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_PROJECT_NAME",
Value: fpDetails.AiProjectName,
})
location := *projectResp.Location
// Set the location in the environment
_, err = azdClient.Environment().SetValue(ctx, &azdext.SetEnvRequest{
EnvName: env.Name,
Key: "AZURE_LOCATION",
Value: location,
})
}
return azureContext, project, env, nil
}
func (a *InitAction) Run(ctx context.Context) error {
// Validate that either template or from-job is provided, but not both
if a.flags.template != "" && a.flags.jobId != "" {
return fmt.Errorf("cannot specify both --template and --from-job flags")
}
color.Green("Creating fine-tuning Job definition...")
var cwd string
var err error
// Use src flag if provided, otherwise use current working directory
if a.flags.src != "" {
cwd = a.flags.src
} else {
cwd, err = os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
}
if a.flags.template == "" && a.flags.jobId == "" {
defaultBaseModel := "gpt-4o-mini"
defaultMethod := "supervised"
baseModelForFineTuningInput, err := a.azdClient.Prompt().Prompt(ctx, &azdext.PromptRequest{
Options: &azdext.PromptOptions{
Message: "Enter base model name for fine tuning (defaults to model name)",
IgnoreHintKeys: true,
DefaultValue: defaultBaseModel,
},
})
ftMethodInput, err := a.azdClient.Prompt().Prompt(ctx, &azdext.PromptRequest{
Options: &azdext.PromptOptions{
Message: "Enter fine-tuning method (defaults to supervised)",
IgnoreHintKeys: true,
DefaultValue: defaultMethod,
},
})
if err != nil {
return err
}
fmt.Printf("Base model : %s, Fine-tuning method: %s\n", baseModelForFineTuningInput.Value, ftMethodInput.Value)
// Create YAML file with the fine-tuning job template
yamlContent := fmt.Sprintf(`name: ft-cli-job
description: Template to demonstrate fine-tuning via CLI
model: %s
method:
type: %s
`, baseModelForFineTuningInput.Value, ftMethodInput.Value)
// Determine the output directory (use src flag or current directory)
outputDir := a.flags.src
if outputDir == "" {
var err error
outputDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
}
yamlFilePath := filepath.Join(outputDir, "config", "job.yaml")
//nolint:gosec // project config directory should be readable and traversable
if err := os.MkdirAll(filepath.Dir(yamlFilePath), 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
//nolint:gosec // generated config file should be readable by project tooling
if err := os.WriteFile(yamlFilePath, []byte(yamlContent), 0644); err != nil {
return fmt.Errorf("failed to write job.yaml file: %w", err)
}
fmt.Printf("Created fine-tuning job template at: %s\n", yamlFilePath)
// Set the template flag to the newly created YAML file
a.flags.template = yamlFilePath
} else if a.flags.template != "" {
if a.isGitHubUrl(a.flags.template) {
// For container agents, download the entire parent directory
fmt.Println("Downloading full directory for fine-tuning configuration from GitHub...")
var console input.Console
var urlInfo *GitHubUrlInfo
// Create a simple console and command runner for GitHub CLI
commandRunner := exec.NewCommandRunner(&exec.RunnerOptions{
Stdout: os.Stdout,
Stderr: os.Stderr,
})
console = input.NewConsole(
false, // noPrompt
true, // isTerminal
input.Writers{Output: os.Stdout},
input.ConsoleHandles{
Stderr: os.Stderr,
Stdin: os.Stdin,
Stdout: os.Stdout,
},
nil, // formatter
nil, // externalPromptCfg
)
ghCli := github.NewGitHubCli(console, commandRunner)
if err := ghCli.EnsureInstalled(ctx); err != nil {
return fmt.Errorf("ensuring gh is installed: %w", err)
}
// Create a new AZD client
azdClient, err := azdext.NewAzdClient()
if err != nil {
return fmt.Errorf("failed to create azd client: %w", err)
}
defer azdClient.Close()
// Call the ParseGitHubUrl RPC method
parseResponse, err := azdClient.Project().ParseGitHubUrl(ctx, &azdext.ParseGitHubUrlRequest{
Url: a.flags.template,
})
if err != nil {
return fmt.Errorf("parsing GitHub URL via azd extension: %w", err)
}
// Map the response to GitHubUrlInfo
urlInfo = &GitHubUrlInfo{
RepoSlug: parseResponse.RepoSlug,
Branch: parseResponse.Branch,
FilePath: parseResponse.FilePath,
Hostname: parseResponse.Hostname,
}
if urlInfo.Branch != "" {
fmt.Printf("Downloaded manifest from branch: %s\n", urlInfo.Branch)
}
err = downloadParentDirectory(ctx, urlInfo, cwd, ghCli, console)
if err != nil {
return fmt.Errorf("downloading parent directory: %w", err)
}
} else {
if err := copyDirectory(a.flags.template, cwd); err != nil {
return fmt.Errorf("failed to copy directory: %w", err)
}
}
} else if a.flags.jobId != "" {
fmt.Printf("Cloning fine-tuning job configuration from job ID: %s\n", a.flags.jobId)
fineTuneSvc, err := services.NewFineTuningService(ctx, a.azdClient, nil)
if err != nil {
return fmt.Errorf("failed to create fine-tuning service: %w", err)
}
// Fetch job details
fmt.Printf("Fetching fine-tuning job %s...\n", a.flags.jobId)
job, err := fineTuneSvc.GetFineTuningJobDetails(ctx, a.flags.jobId)
if err != nil {
return fmt.Errorf("failed to fetch fine-tuning job details: %w", err)
}
// Create YAML file with job configuration
yamlContent := fmt.Sprintf(`name: %s
description: Cloned configuration from job %s
model: %s
seed: %d
method:
type: %s
`, a.flags.jobId, a.flags.jobId, job.Model, job.Seed, job.Method)
// Add hyperparameters nested under method type if present
if job.Hyperparameters != nil {
yamlContent += fmt.Sprintf(` %s:
hyperparameters:
epochs: %d
batch_size: %d
learning_rate_multiplier: %f
`, job.Method, job.Hyperparameters.NEpochs, job.Hyperparameters.BatchSize, job.Hyperparameters.LearningRateMultiplier)
// Add beta parameter only for DPO method
if strings.ToLower(job.Method) == "dpo" {
yamlContent += fmt.Sprintf(" beta: %v\n", job.Hyperparameters.Beta)
}
// Add reinforcement-specific hyperparameters
if strings.ToLower(job.Method) == "reinforcement" {
yamlContent += fmt.Sprintf(" compute_multiplier: %f\n", job.Hyperparameters.ComputeMultiplier)
yamlContent += fmt.Sprintf(" eval_interval: %d\n", job.Hyperparameters.EvalInterval)
yamlContent += fmt.Sprintf(" eval_samples: %d\n", job.Hyperparameters.EvalSamples)
yamlContent += fmt.Sprintf(" reasoning_effort: %s\n", job.Hyperparameters.ReasoningEffort)
}
}
// Add grader for reinforcement method if present
if len(job.Grader) > 0 && strings.ToLower(job.Method) == "reinforcement" {
var graderMap map[string]any
if err := json.Unmarshal(job.Grader, &graderMap); err == nil {
graderYaml, err := yaml.Marshal(graderMap)
if err == nil {
// Indent the grader YAML to be nested under method.reinforcement.grader
var indentedGrader strings.Builder
for line := range strings.SplitSeq(string(graderYaml), "\n") {
if line != "" {
indentedGrader.WriteString(" " + line + "\n")
}
}
yamlContent += " grader:\n" + indentedGrader.String()
}
}
}
// Add training and validation files
yamlContent += fmt.Sprintf("training_file: %s\n", job.TrainingFile)
if job.ValidationFile != "" {
yamlContent += fmt.Sprintf("validation_file: %s\n", job.ValidationFile)
}
// Add extra_body with trainingType if present
if trainingType, ok := job.ExtraFields["trainingType"]; ok {
yamlContent += fmt.Sprintf("extra_body:\n trainingType: %v\n", trainingType)
}
// Determine the output directory (use src flag or current directory)
outputDir := a.flags.src
if outputDir == "" {
var err error
outputDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
}
yamlFilePath := filepath.Join(outputDir, "config", "job.yaml")
//nolint:gosec // project config directory should be readable and traversable
if err := os.MkdirAll(filepath.Dir(yamlFilePath), 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
//nolint:gosec // generated config file should be readable by project tooling
if err := os.WriteFile(yamlFilePath, []byte(yamlContent), 0644); err != nil {
return fmt.Errorf("failed to write job.yaml file: %w", err)
}
fmt.Printf("Created fine-tuning job configuration at: %s\n", yamlFilePath)
// Set the template flag to the newly created YAML file
a.flags.template = yamlFilePath
}
fmt.Println()
color.Green("Initialized fine-tuning Project.")
return nil
}
func (a *InitAction) isGitHubUrl(manifestPointer string) bool {
// Check if it's a GitHub URL based on the patterns from downloadGithubManifest
parsedURL, err := url.Parse(manifestPointer)
if err != nil {
return false
}
hostname := parsedURL.Hostname()
// Check for GitHub URL patterns as defined in downloadGithubManifest
return strings.HasPrefix(hostname, "raw.githubusercontent") ||
strings.HasPrefix(hostname, "api.github") ||
strings.Contains(hostname, "github")
}
func downloadParentDirectory(
ctx context.Context, urlInfo *GitHubUrlInfo, targetDir string, ghCli *github.Cli, console input.Console) error {
// Get parent directory by removing the filename from the file path
pathParts := strings.Split(urlInfo.FilePath, "/")
if len(pathParts) <= 1 {
fmt.Println("The file agent.yaml is at repository root, no parent directory to download")
return nil
}
parentDirPath := strings.Join(pathParts[:len(pathParts)-1], "/")
fmt.Printf("Downloading parent directory '%s' from repository '%s', branch '%s'\n", parentDirPath, urlInfo.RepoSlug, urlInfo.Branch)
// Download directory contents
if err := downloadDirectoryContents(ctx, urlInfo.Hostname, urlInfo.RepoSlug, parentDirPath, urlInfo.Branch, targetDir, ghCli, console); err != nil {
return fmt.Errorf("failed to download directory contents: %w", err)
}