/
Configure.ps1
212 lines (175 loc) · 6.16 KB
/
Configure.ps1
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
<#
.SYNOPSIS
Configure user secrets, appsettings.Development.json, and webapp/.env for Chat Copilot.
.PARAMETER AIService
The service type used: OpenAI or AzureOpenAI.
.PARAMETER APIKey
The API key for the AI service.
.PARAMETER Endpoint
Set when using Azure OpenAI.
.PARAMETER CompletionModel
The chat completion model to use (e.g., gpt-3.5-turbo or gpt-4).
.PARAMETER EmbeddingModel
The embedding model to use (e.g., text-embedding-ada-002).
.PARAMETER FrontendClientId
The client (application) ID associated with your frontend's AAD app registration.
.PARAMETER BackendClientId
The client (application) ID associated with your backend's AAD app registration.
.PARAMETER TenantId
The tenant (directory) associated with your AAD app registrations.
See https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-client-application-configuration#authority.
.PARAMETER Instance
The Azure cloud instance used for authenticating users. Defaults to https://login.microsoftonline.com.
See https://learn.microsoft.com/en-us/azure/active-directory/develop/authentication-national-cloud#azure-ad-authentication-endpoints.
#>
param(
[Parameter(Mandatory = $true)]
[string]$AIService,
[Parameter(Mandatory = $true)]
[string]$APIKey,
[Parameter(Mandatory = $false)]
[string]$Endpoint,
[Parameter(Mandatory = $false)]
[string]$CompletionModel,
[Parameter(Mandatory = $false)]
[string]$EmbeddingModel,
[Parameter(Mandatory = $false)]
[string] $FrontendClientId,
[Parameter(Mandatory = $false)]
[string] $BackendClientId,
[Parameter(Mandatory = $false)]
[string] $TenantId,
[Parameter(Mandatory = $false)]
[string] $Instance
)
# Get defaults and constants
$varScriptFilePath = Join-Path "$PSScriptRoot" 'Variables.ps1'
. $varScriptFilePath
# Set remaining values from Variables.ps1
if ($AIService -eq $varOpenAI) {
if (!$CompletionModel) {
Write-Host "No completion model provided - Defaulting to $varCompletionModelOpenAI"
$CompletionModel = $varCompletionModelOpenAI
}
# TO DO: Validate model values if set by command line.
}
elseif ($AIService -eq $varAzureOpenAI) {
if (!$CompletionModel) {
Write-Host "No completion model provided - Defaulting to $varCompletionModelAzureOpenAI"
$CompletionModel = $varCompletionModelAzureOpenAI
}
# TO DO: Validate model values if set by command line.
if (!$Endpoint) {
Write-Error "Please specify an endpoint for -Endpoint when using AzureOpenAI."
exit(1)
}
}
else {
Write-Error "Please specify an AI service (AzureOpenAI or OpenAI) for -AIService."
exit(1)
}
if (!$EmbeddingModel) {
$EmbeddingModel = $varEmbeddingModel
# TO DO: Validate model values if set by command line.
}
# Determine authentication type based on arguments
if ($FrontendClientId -and $BackendClientId -and $TenantId) {
$authType = $varAzureAd
if (!$Instance) {
$Instance = $varInstance
}
}
elseif (!$FrontendClientId -and !$BackendClientId -and !$TenantId) {
$authType = $varNone
}
else {
Write-Error "To use Azure AD authentication, please set -FrontendClientId, -BackendClientId, and -TenantId."
exit(1)
}
Write-Host "#########################"
Write-Host "# Backend configuration #"
Write-Host "#########################"
# Install dev certificate
if ($IsLinux) {
dotnet dev-certs https
if ($LASTEXITCODE -ne 0) { exit(1) }
}
else {
# Windows/MacOS
dotnet dev-certs https --trust
if ($LASTEXITCODE -ne 0) { exit(1) }
}
$webapiProjectPath = Join-Path "$PSScriptRoot" '../webapi'
Write-Host "Setting 'APIKey' user secret for $AIService..."
if ($AIService -eq $varOpenAI) {
dotnet user-secrets set --project $webapiProjectPath KernelMemory:Services:OpenAI:APIKey $ApiKey
if ($LASTEXITCODE -ne 0) { exit(1) }
$AIServiceOverrides = @{
OpenAI = @{
TextModel = $CompletionModel;
EmbeddingModel = $EmbeddingModel;
}
};
}
else {
dotnet user-secrets set --project $webapiProjectPath KernelMemory:Services:AzureOpenAIText:APIKey $ApiKey
if ($LASTEXITCODE -ne 0) { exit(1) }
dotnet user-secrets set --project $webapiProjectPath KernelMemory:Services:AzureOpenAIEmbedding:APIKey $ApiKey
if ($LASTEXITCODE -ne 0) { exit(1) }
$AIServiceOverrides = @{
AzureOpenAIText = @{
Endpoint = $Endpoint;
Deployment = $CompletionModel;
};
AzureOpenAIEmbedding = @{
Endpoint = $Endpoint;
Deployment = $EmbeddingModel;
}
};
}
$appsettingsOverrides = @{
Authentication = @{
Type = $authType;
AzureAd = @{
Instance = $Instance;
TenantId = $TenantId;
ClientId = $BackendClientId;
Scopes = $varScopes
}
};
KernelMemory = @{
TextGeneratorType = $AIService;
DataIngestion = @{
EmbeddingGeneratorTypes = @($AIService)
};
Retrieval = @{
EmbeddingGeneratorType = $AIService
};
Services = $AIServiceOverrides;
};
Frontend = @{
AadClientId = $FrontendClientId
};
}
$appSettingsJson = -join ("appsettings.", $varASPNetCore, ".json");
$appsettingsOverridesFilePath = Join-Path $webapiProjectPath $appSettingsJson
Write-Host "Setting up '$appSettingsJson' for $AIService..."
# Setting depth to 100 to avoid truncating the JSON
ConvertTo-Json $appsettingsOverrides -Depth 100 | Out-File -Encoding utf8 $appsettingsOverridesFilePath
Write-Host "($appsettingsOverridesFilePath)"
Write-Host "========"
Get-Content $appsettingsOverridesFilePath | Write-Host
Write-Host "========"
Write-Host ""
Write-Host "##########################"
Write-Host "# Frontend configuration #"
Write-Host "##########################"
$webappProjectPath = Join-Path "$PSScriptRoot" '../webapp'
$webappEnvFilePath = Join-Path "$webappProjectPath" '/.env'
Write-Host "Setting up '.env'..."
Set-Content -Path $webappEnvFilePath -Value "REACT_APP_BACKEND_URI=https://localhost:40443/"
Write-Host "($webappEnvFilePath)"
Write-Host "========"
Get-Content $webappEnvFilePath | Write-Host
Write-Host "========"
Write-Host "Done!"