Skip to content

Commit b1428ae

Browse files
jeffhandleystephentoub
authored andcommitted
Use computed symbols for all bool checks in the template. Sort usings in Program.cs.
1 parent a98c301 commit b1428ae

File tree

18 files changed

+80
-74
lines changed

18 files changed

+80
-74
lines changed

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/.template.config/template.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
]
8989
},
9090
{
91-
"condition": "(!UseLocalVectorStore)",
91+
"condition": "(!IsLocalVectorStore)",
9292
"exclude": [
9393
"ChatWithCustomData-CSharp.Web/Services/JsonVectorStore.cs"
9494
]
@@ -185,6 +185,10 @@
185185
"defaultValue": "false",
186186
"description": "Create the project as a distributed application using Aspire."
187187
},
188+
"IsManagedIdentity": {
189+
"type": "computed",
190+
"value": "(UseManagedIdentity)"
191+
},
188192
"IsAspire": {
189193
"type": "computed",
190194
"value": "(UseAspire || VectorStore == \"qdrant\")"
@@ -209,21 +213,21 @@
209213
"type": "computed",
210214
"value": "(AiServiceProvider == \"azureaifoundry\")"
211215
},
212-
"UseAzureAISearch": {
216+
"IsAzureAISearch": {
213217
"type": "computed",
214218
"value": "(VectorStore == \"azureaisearch\")"
215219
},
216-
"UseLocalVectorStore": {
220+
"IsLocalVectorStore": {
217221
"type": "computed",
218222
"value": "(VectorStore == \"local\")"
219223
},
220-
"UseQdrant": {
224+
"IsQdrant": {
221225
"type": "computed",
222226
"value": "(VectorStore == \"qdrant\")"
223227
},
224-
"UseAzure": {
228+
"IsAzure": {
225229
"type": "computed",
226-
"value": "(IsAzureOpenAI || IsAzureAiFoundry || UseAzureAISearch)"
230+
"value": "(IsAzureOpenAI || IsAzureAIFoundry || IsAzureAISearch)"
227231
},
228232
"ChatModel": {
229233
"type": "parameter",

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.AppHost/AppHost.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
modelName: "text-embedding-3-small",
3030
modelVersion: "1");
3131
#endif
32-
#if (UseAzureAISearch)
32+
#if (IsAzureAISearch)
3333

3434
// See https://learn.microsoft.com/dotnet/aspire/azure/local-provisioning#configuration
3535
// for instructions providing configuration values
@@ -42,13 +42,13 @@
4242
var chat = ollama.AddModel("chat", "llama3.2");
4343
var embeddings = ollama.AddModel("embeddings", "all-minilm");
4444
#endif
45-
#if (UseAzureAISearch) // VECTOR DATABASE CONFIGURATION
46-
#elif (UseQdrant)
45+
#if (IsAzureAISearch) // VECTOR DATABASE CONFIGURATION
46+
#elif (IsQdrant)
4747

4848
var vectorDB = builder.AddQdrant("vectordb")
4949
.WithDataVolume()
5050
.WithLifetime(ContainerLifetime.Persistent);
51-
#else // UseLocalVectorStore
51+
#else // IsLocalVectorStore
5252
#endif
5353

5454
var webApp = builder.AddProject<Projects.ChatWithCustomData_CSharp_Web_AspireClassName_Web>("aichatweb-app");
@@ -65,15 +65,15 @@
6565
.WithReference(openai)
6666
.WaitFor(openai);
6767
#endif
68-
#if (UseAzureAISearch) // VECTOR DATABASE REFERENCES
68+
#if (IsAzureAISearch) // VECTOR DATABASE REFERENCES
6969
webApp
7070
.WithReference(search)
7171
.WaitFor(search);
72-
#elif (UseQdrant)
72+
#elif (IsQdrant)
7373
webApp
7474
.WithReference(vectorDB)
7575
.WaitFor(vectorDB);
76-
#else // UseLocalVectorStore
76+
#else // IsLocalVectorStore
7777
#endif
7878

7979
builder.Build().Run();

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.AppHost/ChatWithCustomData-CSharp.AppHost.csproj.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Aspire.Hosting.AppHost" Version="${TemplatePackageVersion_Aspire}" />
15-
<!--#if (UseQdrant)
15+
<!--#if (IsQdrant)
1616
<PackageReference Include="Aspire.Hosting.Qdrant" Version="${TemplatePackageVersion_Aspire}" />
17-
#elif (UseAzureAISearch)
17+
#elif (IsAzureAISearch)
1818
<PackageReference Include="Aspire.Hosting.Azure.Search" Version="${TemplatePackageVersion_Aspire}" />
1919
#endif -->
2020
<!--#if (IsAzureOpenAI) -->

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/ChatWithCustomData-CSharp.Web.csproj.in

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="OllamaSharp" Version="${TemplatePackageVersion_OllamaSharp}" />
1616
#elif ((IsGHModels || IsOpenAI) && !IsAspire)
1717
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="${TemplatePackageVersion_MicrosoftExtensionsAIOpenAI}" />
18-
#elif (IsAzureAiFoundry)
18+
#elif (IsAzureAIFoundry)
1919
<PackageReference Include="Azure.AI.Projects" Version="${TemplatePackageVersion_AzureAIProjects}" />
2020
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="${TemplatePackageVersion_MicrosoftExtensionsAIOpenAI}" />
2121
#endif -->
@@ -26,24 +26,24 @@
2626
<!--#if (IsAzureOpenAI && !IsAspire) -->
2727
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="${TemplatePackageVersion_MicrosoftExtensionsAIOpenAI}" />
2828
<!--#endif -->
29-
<!--#if (UseManagedIdentity) -->
29+
<!--#if (IsManagedIdentity) -->
3030
<PackageReference Include="Azure.Identity" Version="${TemplatePackageVersion_AzureIdentity}" />
3131
<!--#endif -->
3232
<PackageReference Include="Microsoft.Extensions.AI" Version="${TemplatePackageVersion_MicrosoftExtensionsAI}" />
3333
<PackageReference Include="Microsoft.SemanticKernel.Core" Version="${TemplatePackageVersion_MicrosoftSemanticKernel}" />
3434
<PackageReference Include="PdfPig" Version="${TemplatePackageVersion_PdfPig}" />
3535
<PackageReference Include="System.Linq.Async" Version="${TemplatePackageVersion_SystemLinqAsync}" />
36-
<!--#if (UseAzureAISearch && IsAspire)
36+
<!--#if (IsAzureAISearch && IsAspire)
3737
<PackageReference Include="Aspire.Azure.Search.Documents" Version="${TemplatePackageVersion_Aspire}" />
38-
#elif (UseAzureAISearch && !IsAspire)
38+
#elif (IsAzureAISearch && !IsAspire)
3939
<PackageReference Include="Azure.Search.Documents" Version="${TemplatePackageVersion_AzureSearchDocuments}" />
4040
#endif -->
41-
<!--#if (UseAzureAISearch)
41+
<!--#if (IsAzureAISearch)
4242
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AzureAISearch" Version="${TemplatePackageVersion_MicrosoftSemanticKernel_Preview}" />
43-
#elif (UseQdrant)-->
43+
#elif (IsQdrant)-->
4444
<PackageReference Include="Aspire.Qdrant.Client" Version="${TemplatePackageVersion_Aspire}" />
4545
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Qdrant" Version="${TemplatePackageVersion_MicrosoftSemanticKernel_Preview}" />
46-
<!--#elif (UseLocalVectorStore)-->
46+
<!--#elif (IsLocalVectorStore)-->
4747
<PackageReference Include="Microsoft.SemanticKernel.Connectors.SqliteVec" Version="${TemplatePackageVersion_MicrosoftSemanticKernel_Preview}" />
4848
<!--#endif -->
4949
</ItemGroup>

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/Program.Aspire.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using Microsoft.Extensions.AI;
2+
#if (IsOpenAI || IsGHModels)
3+
using OpenAI;
4+
#endif
25
using ChatWithCustomData_CSharp.Web.Components;
36
using ChatWithCustomData_CSharp.Web.Services;
47
using ChatWithCustomData_CSharp.Web.Services.Ingestion;
5-
#if (IsOllama)
6-
#elif (IsOpenAI || IsGHModels)
7-
using OpenAI;
8-
#else // IsAzureOpenAI
9-
#endif
108

119
var builder = WebApplication.CreateBuilder(args);
1210
builder.AddServiceDefaults();
@@ -20,7 +18,7 @@
2018
c.EnableSensitiveData = builder.Environment.IsDevelopment());
2119
builder.AddOllamaApiClient("embeddings")
2220
.AddEmbeddingGenerator();
23-
#elif (IsAzureAiFoundry)
21+
#elif (IsAzureAIFoundry)
2422
#else // (IsOpenAI || IsAzureOpenAI || IsGHModels)
2523
#if (IsOpenAI)
2624
var openai = builder.AddOpenAIClient("openai");
@@ -34,15 +32,15 @@
3432
openai.AddEmbeddingGenerator("text-embedding-3-small");
3533
#endif
3634

37-
#if (UseAzureAISearch)
35+
#if (IsAzureAISearch)
3836
builder.AddAzureSearchClient("search");
3937
builder.Services.AddAzureAISearchCollection<IngestedChunk>("data-ChatWithCustomData-CSharp.Web-chunks");
4038
builder.Services.AddAzureAISearchCollection<IngestedDocument>("data-ChatWithCustomData-CSharp.Web-documents");
41-
#elif (UseQdrant)
39+
#elif (IsQdrant)
4240
builder.AddQdrantClient("vectordb");
4341
builder.Services.AddQdrantCollection<Guid, IngestedChunk>("data-ChatWithCustomData-CSharp.Web-chunks");
4442
builder.Services.AddQdrantCollection<Guid, IngestedDocument>("data-ChatWithCustomData-CSharp.Web-documents");
45-
#else // UseLocalVectorStore
43+
#else // IsLocalVectorStore
4644
var vectorStorePath = Path.Combine(AppContext.BaseDirectory, "vector-store.db");
4745
var vectorStoreConnectionString = $"Data Source={vectorStorePath}";
4846
builder.Services.AddSqliteCollection<string, IngestedChunk>("data-ChatWithCustomData-CSharp.Web-chunks", vectorStoreConnectionString);

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/Program.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
using Microsoft.Extensions.AI;
2-
using ChatWithCustomData_CSharp.Web.Components;
3-
using ChatWithCustomData_CSharp.Web.Services;
4-
using ChatWithCustomData_CSharp.Web.Services.Ingestion;
5-
#if(IsAzureOpenAI || UseAzureAISearch)
1+
#if (!IsOllama)
2+
using System.ClientModel;
3+
#endif
4+
#if (IsAzureAISearch && !IsManagedIdentity)
65
using Azure;
7-
#if (UseManagedIdentity)
6+
#elif (IsManagedIdentity)
7+
using Azure.Core;
88
using Azure.Identity;
99
#endif
10-
#endif
10+
using Microsoft.Extensions.AI;
1111
#if (IsOllama)
1212
using OllamaSharp;
1313
#else
1414
using OpenAI;
15-
using System.ClientModel;
16-
#if (UseManagedIdentity && IsAzureOpenAI)
17-
using Azure.Core;
18-
using Azure.Identity;
19-
#endif
2015
#endif
16+
using ChatWithCustomData_CSharp.Web.Components;
17+
using ChatWithCustomData_CSharp.Web.Services;
18+
using ChatWithCustomData_CSharp.Web.Services.Ingestion;
2119

2220
var builder = WebApplication.CreateBuilder(args);
2321
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
@@ -52,18 +50,18 @@
5250
var chatClient = openAIClient.GetOpenAIResponseClient("gpt-4o-mini").AsIChatClient();
5351
#pragma warning restore OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
5452
var embeddingGenerator = openAIClient.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
55-
#elif (IsAzureAiFoundry)
53+
#elif (IsAzureAIFoundry)
5654

5755
#else // IsAzureOpenAI
5856
// You will need to set the endpoint and key to your own values
5957
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
6058
// cd this-project-directory
6159
// dotnet user-secrets set AzureOpenAI:Endpoint https://YOUR-DEPLOYMENT-NAME.openai.azure.com
62-
#if (!UseManagedIdentity)
60+
#if (!IsManagedIdentity)
6361
// dotnet user-secrets set AzureOpenAI:Key YOUR-API-KEY
6462
#endif
6563
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAi:Endpoint. See the README for details.")), "/openai/v1");
66-
#if (UseManagedIdentity)
64+
#if (IsManagedIdentity)
6765
var tokenCredential = new DefaultAzureCredential();
6866
var token = tokenCredential.GetToken(new TokenRequestContext(["https://cognitiveservices.azure.com/.default"]), default);
6967
var openAIOptions = new OpenAIClientOptions { Endpoint = azureOpenAIEndpoint };
@@ -78,25 +76,25 @@
7876
var embeddingGenerator = azureOpenAi.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
7977
#endif
8078

81-
#if (UseAzureAISearch)
79+
#if (IsAzureAISearch)
8280
// You will need to set the endpoint and key to your own values
8381
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
8482
// cd this-project-directory
8583
// dotnet user-secrets set AzureAISearch:Endpoint https://YOUR-DEPLOYMENT-NAME.search.windows.net
86-
#if (!UseManagedIdentity)
84+
#if (!IsManagedIdentity)
8785
// dotnet user-secrets set AzureAISearch:Key YOUR-API-KEY
8886
#endif
8987
var azureAISearchEndpoint = new Uri(builder.Configuration["AzureAISearch:Endpoint"]
9088
?? throw new InvalidOperationException("Missing configuration: AzureAISearch:Endpoint. See the README for details."));
91-
#if (UseManagedIdentity)
89+
#if (IsManagedIdentity)
9290
var azureAISearchCredential = new DefaultAzureCredential();
9391
#else
9492
var azureAISearchCredential = new AzureKeyCredential(builder.Configuration["AzureAISearch:Key"]
9593
?? throw new InvalidOperationException("Missing configuration: AzureAISearch:Key. See the README for details."));
9694
#endif
9795
builder.Services.AddAzureAISearchCollection<IngestedChunk>("data-ChatWithCustomData-CSharp.Web-chunks", azureAISearchEndpoint, azureAISearchCredential);
9896
builder.Services.AddAzureAISearchCollection<IngestedDocument>("data-ChatWithCustomData-CSharp.Web-documents", azureAISearchEndpoint, azureAISearchCredential);
99-
#else // UseLocalVectorStore
97+
#else // IsLocalVectorStore
10098
var vectorStorePath = Path.Combine(AppContext.BaseDirectory, "vector-store.db");
10199
var vectorStoreConnectionString = $"Data Source={vectorStorePath}";
102100
builder.Services.AddSqliteCollection<string, IngestedChunk>("data-ChatWithCustomData-CSharp.Web-chunks", vectorStoreConnectionString);

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This project is an AI chat application that demonstrates how to chat with custom
55
>[!NOTE]
66
> Before running this project you need to configure the API keys or endpoints for the providers you have chosen. See below for details specific to your choices.
77
8-
#### ---#if (UseAzure)
8+
#### ---#if (IsAzure)
99
### Prerequisites
1010
To use Azure OpenAI or Azure AI Search, you need an Azure account. If you don't already have one, [create an Azure account](https://azure.microsoft.com/free/).
1111

@@ -104,7 +104,7 @@ To use Azure OpenAI, you will need an Azure account and an Azure OpenAI Service
104104
### 2. Deploy the Models
105105
Deploy the `gpt-4o-mini` and `text-embedding-3-small` models to your Azure OpenAI Service resource. When creating those deployments, give them the same names as the models (`gpt-4o-mini` and `text-embedding-3-small`). See the Azure OpenAI documentation to learn how to [Deploy a model](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource?pivots=web-portal#deploy-a-model).
106106
107-
#### ---#if (UseManagedIdentity)
107+
#### ---#if (IsManagedIdentity)
108108
### 3. Configure Azure OpenAI for Keyless Authentication
109109
This template is configured to use keyless authentication (also known as Managed Identity, with Entra ID). In the Azure Portal, when viewing the Azure OpenAI resource you just created, view access control settings and assign yourself the `Azure AI Developer` role. [Learn more about configuring authentication for local development](https://learn.microsoft.com/azure/developer/ai/keyless-connections?tabs=csharp%2Cazure-cli#authenticate-for-local-development).
110110
@@ -160,7 +160,7 @@ Make sure to replace `YOUR-AZURE-OPENAI-KEY` and `YOUR-AZURE-OPENAI-ENDPOINT` wi
160160
161161
#### ---#endif
162162
#### ---#endif
163-
#### ---#if (UseAzureAISearch)
163+
#### ---#if (IsAzureAISearch)
164164
## Configure Azure AI Search
165165
166166
To use Azure AI Search, you will need an Azure account and an Azure AI Search resource. For detailed instructions, see the [Azure AI Search documentation](https://learn.microsoft.com/azure/search/search-create-service-portal).
@@ -170,7 +170,7 @@ Follow the instructions in the [Azure portal](https://portal.azure.com/) to crea
170170
171171
Note that if you previously used the same Azure AI Search resource with different model using this project name, you may need to delete your `data-ChatWithCustomData-CSharp.Web-chunks` and `data-ChatWithCustomData-CSharp.Web-documents` indexes using the [Azure portal](https://portal.azure.com/) first before continuing; otherwise, data ingestion may fail due to a vector dimension mismatch.
172172
173-
#### ---#if (UseManagedIdentity)
173+
#### ---#if (IsManagedIdentity)
174174
### 2. Configure Azure AI Search for Keyless Authentication
175175
This template is configured to use keyless authentication (also known as Managed Identity, with Entra ID). Before continuing, you'll need to configure your Azure AI Search resource to support this. [Learn more](https://learn.microsoft.com/azure/search/keyless-connections). After creation, ensure that you have selected Role-Based Access Control (RBAC) under Settings > Keys, as this is not the default. Assign yourself the roles called out for local development. [Learn more](https://learn.microsoft.com/azure/search/keyless-connections#roles-for-local-development).
176176

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/Services/IngestedChunk.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ public class IngestedChunk
99
#else
1010
private const int VectorDimensions = 1536; // 1536 is the default vector size for the OpenAI text-embedding-3-small model
1111
#endif
12-
#if (UseAzureAISearch || UseQdrant)
12+
#if (IsAzureAISearch || IsQdrant)
1313
private const string VectorDistanceFunction = DistanceFunction.CosineSimilarity;
1414
#else
1515
private const string VectorDistanceFunction = DistanceFunction.CosineDistance;
1616
#endif
1717

1818
[VectorStoreKey]
19-
#if (UseQdrant)
19+
#if (IsQdrant)
2020
public required Guid Key { get; set; }
2121
#else
2222
public required string Key { get; set; }

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/Services/IngestedDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace ChatWithCustomData_CSharp.Web.Services;
55
public class IngestedDocument
66
{
77
private const int VectorDimensions = 2;
8-
#if (UseAzureAISearch || UseQdrant)
8+
#if (IsAzureAISearch || IsQdrant)
99
private const string VectorDistanceFunction = DistanceFunction.CosineSimilarity;
1010
#else
1111
private const string VectorDistanceFunction = DistanceFunction.CosineDistance;
1212
#endif
1313

1414
[VectorStoreKey]
15-
#if (UseQdrant)
15+
#if (IsQdrant)
1616
public required Guid Key { get; set; }
1717
#else
1818
public required string Key { get; set; }

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/Services/Ingestion/DataIngestor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ChatWithCustomData_CSharp.Web.Services.Ingestion;
55

66
public class DataIngestor(
77
ILogger<DataIngestor> logger,
8-
#if (UseQdrant)
8+
#if (IsQdrant)
99
VectorStoreCollection<Guid, IngestedChunk> chunksCollection,
1010
VectorStoreCollection<Guid, IngestedDocument> documentsCollection)
1111
#else

0 commit comments

Comments
 (0)