Skip to content

Commit 9c1396d

Browse files
feat: AI Chat and embeddings (#763)
A bunch of files were just deleted :) Fixes #168 Fixes IntelliTect-dev/EssentialCSharp.Tooling#901 <details> <summary>PR Summary</summary> This pull request introduces significant enhancements to integrate Azure OpenAI services and vector search capabilities into the project. Key changes include adding dependencies for AI and vector search tools, creating new services for AI chat and search functionality, and defining models and configurations to support these features. ### Dependency and Configuration Updates: * [`Directory.Packages.props`](diffhunk://#diff-5baf5f9e448ad54ab25a091adee0da05d4d228481c9200518fcb1b53a65d4156R39-R47): Added new package dependencies, including `Microsoft.SemanticKernel`, `ModelContextProtocol`, and `System.CommandLine`, to enable AI and vector search functionality. * [`EssentialCSharp.Chat.Common.csproj`](diffhunk://#diff-f4e3de9b154fa0162fee1cd9a75b70d7ffff3b3ca7b34028a3a36b2345a0bca8R1-R20): Configured project properties and added package references for Azure OpenAI and vector search integration. ### AI Service Implementation: * [`EssentialCSharp.Chat.Shared/Services/AIChatService.cs`](diffhunk://#diff-2e3a8204c3630d2aad08cd1583dd37bb97b89bebdb390cd9684fbdd60615f710R1-R348): Introduced `AIChatService`, which handles AI chat completions using Azure OpenAI. It supports both single and streaming responses, contextual prompt enrichment via vector search, and function call execution. * [`EssentialCSharp.Chat.Shared/Services/AISearchService.cs`](diffhunk://#diff-ab2dec801c787671ce9cf9e09dec9932c670b5f54e02f866a094bccc27e399a8R1-R27): Added `AISearchService` to execute vector searches on book content chunks using embeddings generated by the `EmbeddingService`. ### Data Models and Extensions: * [`EssentialCSharp.Chat.Shared/Models/BookContentChunk.cs`](diffhunk://#diff-6c4785393661a1f777dce26db779b2c20341b7a0eb8b1bd954d157d3c669d82cR1-R54): Defined `BookContentChunk` to represent chunks of book content for vector search, including metadata and vector embeddings. * [`EssentialCSharp.Chat.Shared/Models/AIOptions.cs`](diffhunk://#diff-66e64dc8790347f86e8ebce8a3e7fc8b25a9694ddb514ea3f90522bb81310395R1-R34): Created `AIOptions` to centralize configuration for Azure OpenAI services and PostgreSQL vector store. * [`EssentialCSharp.Chat.Shared/Extensions/ServiceCollectionExtensions.cs`](diffhunk://#diff-44e37562fe51903c9f707ccc04677a25b2e37691b1a7dead5d62cc2f76004d83R1-R80): Added extension methods to register Azure OpenAI services and PostgreSQL vector store in the dependency injection container. </details> Screenshot: <img width="1342" height="1144" alt="image" src="https://github.com/user-attachments/assets/71dbf972-7ff4-45ed-b161-f80e9c342ff2" />
1 parent 9d25818 commit 9c1396d

63 files changed

Lines changed: 3667 additions & 55003 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/Build-Test-And-Deploy.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,20 @@ jobs:
147147
emailsender-secret=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-secretkey,identityref:$MANAGEDIDENTITYID emailsender-name=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-sendfromname,identityref:$MANAGEDIDENTITYID \
148148
emailsender-email=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-sendfromemail,identityref:$MANAGEDIDENTITYID connectionstring=keyvaultref:$KEYVAULTURI/secrets/connectionstrings-essentialcsharpwebcontextconnection,identityref:$MANAGEDIDENTITYID \
149149
captcha-sitekey=keyvaultref:$KEYVAULTURI/secrets/captcha-sitekey,identityref:$MANAGEDIDENTITYID captcha-secretkey=keyvaultref:$KEYVAULTURI/secrets/captcha-secretkey,identityref:$MANAGEDIDENTITYID \
150-
appinsights-connectionstring=keyvaultref:$KEYVAULTURI/secrets/applicationinsights-connectionstring,identityref:$MANAGEDIDENTITYID
150+
appinsights-connectionstring=keyvaultref:$KEYVAULTURI/secrets/applicationinsights-connectionstring,identityref:$MANAGEDIDENTITYID \
151+
ai-endpoint=keyvaultref:$KEYVAULTURI/secrets/AIOptions--Endpoint,identityref:$MANAGEDIDENTITYID ai-apikey=keyvaultref:$KEYVAULTURI/secrets/AIOptions--ApiKey,identityref:$MANAGEDIDENTITYID \
152+
ai-vectordeployment=keyvaultref:$KEYVAULTURI/secrets/AIOptions--VectorGenerationDeploymentName,identityref:$MANAGEDIDENTITYID ai-chatdeployment=keyvaultref:$KEYVAULTURI/secrets/AIOptions--ChatDeploymentName,identityref:$MANAGEDIDENTITYID \
153+
ai-systemprompt=keyvaultref:$KEYVAULTURI/secrets/AIOptions--SystemPrompt,identityref:$MANAGEDIDENTITYID \
154+
postgres-vectorstore-connectionstring=keyvaultref:$KEYVAULTURI/secrets/connectionstrings--PostgresVectorDb,identityref:$MANAGEDIDENTITYID
151155
az containerapp update --name $CONTAINER_APP_NAME --resource-group $RESOURCEGROUP --replace-env-vars Authentication__github__clientId=secretref:github-clientid Authentication__github__clientSecret=secretref:github-clientsecret \
152156
Authentication__microsoft__clientId=secretref:msft-clientid Authentication__microsoft__clientSecret=secretref:msft-clientsecret AuthMessageSender__ApiKey=secretref:emailsender-apikey AuthMessageSender__SecretKey=secretref:emailsender-secret \
153157
AuthMessageSender__SendFromName=secretref:emailsender-name AuthMessageSender__SendFromEmail=secretref:emailsender-email ConnectionStrings__EssentialCSharpWebContextConnection=secretref:connectionstring ASPNETCORE_ENVIRONMENT=Staging \
154-
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring
158+
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring \
159+
AIOptions__Endpoint=secretref:ai-endpoint AIOptions__ApiKey=secretref:ai-apikey AIOptions__VectorGenerationDeploymentName=secretref:ai-vectordeployment AIOptions__ChatDeploymentName=secretref:ai-chatdeployment \
160+
AIOptions__SystemPrompt=secretref:ai-systemprompt ConnectionStrings__PostgresVectorStore=secretref:postgres-vectorstore-connectionstring
155161
156162
- name: Logout of Azure CLI
157-
if: "always()"
163+
if: always()
158164
uses: azure/CLI@v2
159165
with:
160166
inlineScript: |
@@ -233,14 +239,19 @@ jobs:
233239
emailsender-secret=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-secretkey,identityref:$MANAGEDIDENTITYID emailsender-name=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-sendfromname,identityref:$MANAGEDIDENTITYID \
234240
emailsender-email=keyvaultref:$KEYVAULTURI/secrets/authmessagesender-sendfromemail,identityref:$MANAGEDIDENTITYID connectionstring=keyvaultref:$KEYVAULTURI/secrets/connectionstrings-essentialcsharpwebcontextconnection,identityref:$MANAGEDIDENTITYID \
235241
captcha-sitekey=keyvaultref:$KEYVAULTURI/secrets/captcha-sitekey,identityref:$MANAGEDIDENTITYID captcha-secretkey=keyvaultref:$KEYVAULTURI/secrets/captcha-secretkey,identityref:$MANAGEDIDENTITYID \
236-
appinsights-connectionstring=keyvaultref:$KEYVAULTURI/secrets/applicationinsights-connectionstring,identityref:$MANAGEDIDENTITYID
242+
appinsights-connectionstring=keyvaultref:$KEYVAULTURI/secrets/applicationinsights-connectionstring,identityref:$MANAGEDIDENTITYID \
243+
ai-endpoint=keyvaultref:$KEYVAULTURI/secrets/AIOptions--Endpoint,identityref:$MANAGEDIDENTITYID ai-apikey=keyvaultref:$KEYVAULTURI/secrets/AIOptions--ApiKey,identityref:$MANAGEDIDENTITYID \
244+
ai-vectordeployment=keyvaultref:$KEYVAULTURI/secrets/AIOptions--VectorGenerationDeploymentName,identityref:$MANAGEDIDENTITYID ai-chatdeployment=keyvaultref:$KEYVAULTURI/secrets/AIOptions--ChatDeploymentName,identityref:$MANAGEDIDENTITYID \
245+
ai-systemprompt=keyvaultref:$KEYVAULTURI/secrets/AIOptions--SystemPrompt,identityref:$MANAGEDIDENTITYID \
246+
postgres-vectorstore-connectionstring=keyvaultref:$KEYVAULTURI/secrets/connectionstrings--PostgresVectorDb,identityref:$MANAGEDIDENTITYID
237247
az containerapp update --name $CONTAINER_APP_NAME --resource-group $RESOURCEGROUP --replace-env-vars Authentication__github__clientId=secretref:github-clientid Authentication__github__clientSecret=secretref:github-clientsecret \
238248
Authentication__microsoft__clientId=secretref:msft-clientid Authentication__microsoft__clientSecret=secretref:msft-clientsecret AuthMessageSender__ApiKey=secretref:emailsender-apikey AuthMessageSender__SecretKey=secretref:emailsender-secret \
239249
AuthMessageSender__SendFromName=secretref:emailsender-name AuthMessageSender__SendFromEmail=secretref:emailsender-email ConnectionStrings__EssentialCSharpWebContextConnection=secretref:connectionstring ASPNETCORE_ENVIRONMENT=Production \
240-
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring
250+
AZURE_CLIENT_ID=$AZURECLIENTID HCaptcha__SiteKey=secretref:captcha-sitekey HCaptcha__SecretKey=secretref:captcha-secretkey ApplicationInsights__ConnectionString=secretref:appinsights-connectionstring \
251+
AIOptions__Endpoint=secretref:ai-endpoint AIOptions__ApiKey=secretref:ai-apikey ConnectionStrings__PostgresVectorStore=secretref:postgres-vectorstore-connectionstring
241252
242253
- name: Logout of Azure CLI
243-
if: "always()"
254+
if: always()
244255
uses: azure/CLI@v2
245256
with:
246257
inlineScript: |

Directory.Packages.props

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@
3636
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
3737
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
3838
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
39+
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.60.0" />
40+
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.PgVector" Version="1.60.0-preview" />
41+
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
3942
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
4043
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
44+
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.3" />
45+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.3" />
46+
<PackageVersion Include="Moq" Version="4.20.72" />
47+
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta6.25358.103" />
4148
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
4249
<PackageVersion Include="Octokit" Version="14.0.0" />
4350
<PackageVersion Include="DotnetSitemapGenerator" Version="1.0.4" />
4451
<PackageVersion Include="xunit" Version="2.9.3" />
4552
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
4653
</ItemGroup>
47-
</Project>
54+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.SemanticKernel" />
9+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.PgVector" />
10+
<PackageReference Include="ModelContextProtocol" />
11+
<PackageReference Include="ModelContextProtocol.AspNetCore" />
12+
<PackageReference Include="Microsoft.SourceLink.GitHub">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Azure.AI.OpenAI;
2+
using EssentialCSharp.Chat.Common.Services;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.SemanticKernel;
6+
7+
namespace EssentialCSharp.Chat.Common.Extensions;
8+
9+
public static class ServiceCollectionExtensions
10+
{
11+
/// <summary>
12+
/// Adds Azure OpenAI and related AI services to the service collection
13+
/// </summary>
14+
/// <param name="services">The service collection to add services to</param>
15+
/// <param name="aiOptions">The AI configuration options</param>
16+
/// <param name="postgresConnectionString">The PostgreSQL connection string for the vector store</param>
17+
/// <returns>The service collection for chaining</returns>
18+
public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, AIOptions aiOptions, string postgresConnectionString)
19+
{
20+
if (string.IsNullOrEmpty(aiOptions.Endpoint) ||
21+
string.IsNullOrEmpty(aiOptions.ApiKey))
22+
// Register Azure OpenAI services
23+
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
24+
services.AddAzureOpenAIEmbeddingGenerator(
25+
aiOptions.VectorGenerationDeploymentName,
26+
aiOptions.Endpoint,
27+
aiOptions.ApiKey);
28+
29+
services.AddAzureOpenAIChatClient(
30+
aiOptions.ChatDeploymentName,
31+
aiOptions.Endpoint,
32+
aiOptions.ApiKey);
33+
34+
services.AddSingleton(provider =>
35+
new AzureOpenAIClient(new Uri(aiOptions.Endpoint), new Azure.AzureKeyCredential(aiOptions.ApiKey)));
36+
37+
// Register Azure OpenAI services
38+
services.AddAzureOpenAIEmbeddingGenerator(
39+
aiOptions.VectorGenerationDeploymentName,
40+
aiOptions.Endpoint,
41+
aiOptions.ApiKey);
42+
43+
services.AddAzureOpenAIChatCompletion(
44+
aiOptions.ChatDeploymentName,
45+
aiOptions.Endpoint,
46+
aiOptions.ApiKey);
47+
48+
// Add PostgreSQL vector store
49+
services.AddPostgresVectorStore(postgresConnectionString);
50+
51+
#pragma warning restore SKEXP0010
52+
53+
// Register shared AI services
54+
services.AddSingleton<EmbeddingService>();
55+
services.AddSingleton<AISearchService>();
56+
services.AddSingleton<AIChatService>();
57+
services.AddSingleton<MarkdownChunkingService>();
58+
59+
return services;
60+
}
61+
62+
/// <summary>
63+
/// Adds Azure OpenAI and related AI services to the service collection using configuration
64+
/// </summary>
65+
/// <param name="services">The service collection to add services to</param>
66+
/// <param name="configuration">The configuration to read AIOptions from</param>
67+
/// <returns>The service collection for chaining</returns>
68+
public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, IConfiguration configuration)
69+
{
70+
// Configure AI options from configuration
71+
services.Configure<AIOptions>(configuration.GetSection("AIOptions"));
72+
73+
var aiOptions = configuration.GetSection("AIOptions").Get<AIOptions>();
74+
if (aiOptions == null)
75+
{
76+
throw new InvalidOperationException("AIOptions section is missing from configuration.");
77+
}
78+
79+
// Get PostgreSQL connection string using the standard method
80+
var postgresConnectionString = configuration.GetConnectionString("PostgresVectorStore") ??
81+
throw new InvalidOperationException("Connection string 'PostgresVectorStore' not found.");
82+
83+
return services.AddAzureOpenAIServices(aiOptions, postgresConnectionString);
84+
}
85+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace EssentialCSharp.Chat;
2+
3+
public class AIOptions
4+
{
5+
/// <summary>
6+
/// The Azure OpenAI deployment name for text embedding generation.
7+
/// </summary>
8+
public string VectorGenerationDeploymentName { get; set; } = string.Empty;
9+
10+
/// <summary>
11+
/// The Azure OpenAI deployment name for chat completions.
12+
/// </summary>
13+
public string ChatDeploymentName { get; set; } = string.Empty;
14+
15+
/// <summary>
16+
/// The system prompt to use for the chat model.
17+
/// </summary>
18+
public string SystemPrompt { get; set; } = string.Empty;
19+
20+
/// <summary>
21+
/// The Azure OpenAI endpoint URL.
22+
/// </summary>
23+
public string Endpoint { get; set; } = string.Empty;
24+
25+
/// <summary>
26+
/// The API key for accessing Azure OpenAI services.
27+
/// </summary>
28+
public string ApiKey { get; set; } = string.Empty;
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.Extensions.VectorData;
2+
3+
namespace EssentialCSharp.Chat.Common.Models;
4+
5+
/// <summary>
6+
/// Represents a chunk of book content for vector search
7+
/// </summary>
8+
public sealed class BookContentChunk
9+
{
10+
/// <summary>
11+
/// Unique identifier for the chunk - serves as the vector store key
12+
/// </summary>
13+
[VectorStoreKey]
14+
public string Id { get; set; } = string.Empty;
15+
16+
/// <summary>
17+
/// Original source file name
18+
/// </summary>
19+
[VectorStoreData]
20+
public string FileName { get; set; } = string.Empty;
21+
22+
/// <summary>
23+
/// Heading or title of the markdown chunk
24+
/// </summary>
25+
[VectorStoreData]
26+
public string Heading { get; set; } = string.Empty;
27+
28+
/// <summary>
29+
/// The actual markdown content text for this chunk
30+
/// </summary>
31+
[VectorStoreData]
32+
public string ChunkText { get; set; } = string.Empty;
33+
34+
/// <summary>
35+
/// Chapter number extracted from filename (e.g., "Chapter01.md" -> 1)
36+
/// </summary>
37+
[VectorStoreData]
38+
public int? ChapterNumber { get; set; }
39+
40+
/// <summary>
41+
/// SHA256 hash of the chunk content for change detection
42+
/// </summary>
43+
[VectorStoreData]
44+
public string ContentHash { get; set; } = string.Empty;
45+
46+
/// <summary>
47+
/// Vector embedding for the chunk text - will be generated by embedding service
48+
/// Using 1536 dimensions for Azure OpenAI text-embedding-3-small-v1
49+
/// Use CosineSimilarity distance function since we are using text-embedding-3 (https://platform.openai.com/docs/guides/embeddings#which-distance-function-should-i-use)
50+
/// Postgres supports only Hnsw: https://learn.microsoft.com/en-us/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors/postgres-connector?pivots=programming-language-csharp&WT.mc_id=8B97120A00B57354
51+
/// </summary>
52+
[VectorStoreVector(Dimensions: 1536, DistanceFunction = DistanceFunction.CosineSimilarity, IndexKind = IndexKind.Hnsw)]
53+
public ReadOnlyMemory<float>? TextEmbedding { get; set; }
54+
}

0 commit comments

Comments
 (0)