5
5
using System . ClientModel . Primitives ;
6
6
using Azure . Projects . Core ;
7
7
using Azure . Provisioning . CognitiveServices ;
8
- using Azure . Provisioning . Primitives ;
9
8
10
9
namespace Azure . Projects . OpenAI ;
11
10
@@ -20,7 +19,9 @@ public class OpenAIModelFeature : AzureProjectFeature
20
19
/// <param name="model"></param>
21
20
/// <param name="modelVersion"></param>
22
21
/// <param name="kind"></param>
23
- public OpenAIModelFeature ( string model , string modelVersion , AIModelKind kind = AIModelKind . Chat ) {
22
+ public OpenAIModelFeature ( string model , string modelVersion , AIModelKind kind = AIModelKind . Chat )
23
+ : base ( $ "{ typeof ( OpenAIModelFeature ) . FullName } _{ model } ")
24
+ {
24
25
Kind = kind ;
25
26
Model = model ;
26
27
ModelVersion = modelVersion ;
@@ -50,49 +51,51 @@ public ClientConnection CreateConnection(string cmId)
50
51
ClientConnection connection = new ( "Azure.AI.OpenAI.AzureOpenAIClient" , $ "https://{ cmId } .openai.azure.com", ClientAuthenticationMethod . Credential ) ;
51
52
return connection ;
52
53
}
54
+
53
55
/// <summary>
54
56
/// Emit the feature.
55
57
/// </summary>
56
- /// <param name="features"></param>
57
- /// <param name="cmId"></param>
58
- protected override void AddImplicitFeatures ( FeatureCollection features , string cmId )
58
+ protected override void EmitFeatures ( ProjectInfrastructure infrastructure )
59
59
{
60
- // TODO: is it OK that we return the first one?
61
-
60
+ FeatureCollection features = infrastructure . Features ;
62
61
if ( ! features . TryGet ( out OpenAIAccountFeature ? openAI ) )
63
62
{
64
63
openAI = new OpenAIAccountFeature ( ) ;
65
64
features . Append ( openAI ) ;
66
65
}
67
66
68
67
Account = openAI ! ;
68
+
69
69
features . Append ( this ) ;
70
70
}
71
71
72
72
/// <summary>
73
73
/// Emit the resources.
74
74
/// </summary>
75
- /// <param name="cm"></param>
76
- /// <returns></returns>
75
+ /// <param name="infrastructure"></param>
77
76
/// <exception cref="InvalidOperationException"></exception>
78
77
/// <exception cref="NotImplementedException"></exception>
79
- protected override ProvisionableResource EmitResources ( ProjectInfrastructure cm )
78
+ protected override void EmitConstructs ( ProjectInfrastructure infrastructure )
80
79
{
81
- if ( Account == null ) throw new InvalidOperationException ( "Account must be set before emitting" ) ;
82
- if ( Account . Resource == null ) throw new InvalidOperationException ( "Account must be emitted before emitting" ) ;
80
+ string suffix = Kind switch
81
+ {
82
+ AIModelKind . Chat => "chat" ,
83
+ AIModelKind . Embedding => "embedding" ,
84
+ _ => throw new NotImplementedException ( )
85
+ } ;
83
86
84
- string name = Kind switch
87
+ string resourceName = Kind switch
85
88
{
86
- AIModelKind . Chat => $ "{ cm . ProjectId } _chat ",
87
- AIModelKind . Embedding => $ "{ cm . ProjectId } _embedding ",
89
+ AIModelKind . Chat => $ "{ infrastructure . ProjectId } _ { suffix } ",
90
+ AIModelKind . Embedding => $ "{ infrastructure . ProjectId } _ { suffix } ",
88
91
_ => throw new NotImplementedException ( )
89
92
} ;
90
93
91
- CognitiveServicesAccount parent = ( CognitiveServicesAccount ) Account . Resource ;
94
+ CognitiveServicesAccount parent = infrastructure . GetConstruct < CognitiveServicesAccount > ( Account . Id ) ;
92
95
93
- CognitiveServicesAccountDeployment deployment = new ( $ "openai_{ name } ", "2024-06-01-preview" ) {
96
+ CognitiveServicesAccountDeployment deployment = new ( $ "openai_{ suffix } ", "2024-06-01-preview" ) {
94
97
Parent = parent ,
95
- Name = name ,
98
+ Name = resourceName ,
96
99
Properties = new CognitiveServicesAccountDeploymentProperties ( )
97
100
{
98
101
Model = new CognitiveServicesAccountDeploymentModel ( )
@@ -111,27 +114,23 @@ protected override ProvisionableResource EmitResources(ProjectInfrastructure cm)
111
114
} ,
112
115
} ;
113
116
114
- // deployments need to have dependson set!
115
- OpenAIModelFeature ? previous = FindPrevious ( cm , this ) ;
117
+ // deployments need to have DependsOn property set!
118
+ OpenAIModelFeature ? previous = FindPrevious ( infrastructure , this ) ;
116
119
if ( previous != null )
117
120
{
118
- if ( previous . Resource == null ) throw new InvalidOperationException ( "Previous must be emitted" ) ;
119
- CognitiveServicesAccountDeployment previousDeployment = ( CognitiveServicesAccountDeployment ) previous . Resource ;
121
+ CognitiveServicesAccountDeployment previousDeployment = infrastructure . GetConstruct < CognitiveServicesAccountDeployment > ( previous . Id ) ;
120
122
deployment . DependsOn . Add ( previousDeployment ) ;
121
123
}
122
124
123
- cm . AddConstruct ( deployment ) ;
124
-
125
- string key = Kind == AIModelKind . Chat ? "OpenAI.Chat.ChatClient" : "OpenAI.Embeddings.EmbeddingClient" ;
126
- string locator = Kind == AIModelKind . Chat ? $ "{ cm . ProjectId } _chat" : $ "{ cm . ProjectId } _embedding";
127
- EmitConnection ( cm , key , locator ) ;
125
+ infrastructure . AddConstruct ( Id , deployment ) ;
128
126
129
- return deployment ;
127
+ string clientId = Kind == AIModelKind . Chat ? "OpenAI.Chat.ChatClient" : "OpenAI.Embeddings.EmbeddingClient" ;
128
+ EmitConnection ( infrastructure , clientId , resourceName ) ;
130
129
131
- OpenAIModelFeature ? FindPrevious ( ProjectInfrastructure cm , OpenAIModelFeature current )
130
+ OpenAIModelFeature ? FindPrevious ( ProjectInfrastructure infrastructure , OpenAIModelFeature current )
132
131
{
133
132
OpenAIModelFeature ? previous = default ;
134
- foreach ( var feature in cm . Features )
133
+ foreach ( var feature in infrastructure . Features )
135
134
{
136
135
if ( feature == current )
137
136
return previous ;
0 commit comments