-
Notifications
You must be signed in to change notification settings - Fork 8
feat: AI Chat and embeddings #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bfd4851
Add naming conventions table and anchors to guideline blocks
BenjaminMichaelis d9dc06e
PR Feedback
BenjaminMichaelis 9a1adf0
WIP
BenjaminMichaelis 1294edc
Basic POC of chunking
BenjaminMichaelis 04e3e3a
Initial Markdown Chunking
BenjaminMichaelis e0b871b
Markdown chunking and embedding service setup
BenjaminMichaelis 41915c9
Embeddings working
BenjaminMichaelis cfd6f3c
Begin Chat feature work
BenjaminMichaelis f9f502d
Initial Chat AI
BenjaminMichaelis 848cd5e
Basic UI Chat working
BenjaminMichaelis ff36223
Persist chat across navigation, separate css
BenjaminMichaelis 4005cea
Vuetify
BenjaminMichaelis 4ea884c
Chat history, cleanup
BenjaminMichaelis 645e419
Update
BenjaminMichaelis 9054f6b
Replace "thinking" message with response
BenjaminMichaelis 0a51883
Auth and Rate Limiting
BenjaminMichaelis 1c00f56
Convert to Vuetify
BenjaminMichaelis e44cb70
Cleanup :)
BenjaminMichaelis f385438
Cleanup
BenjaminMichaelis faa5833
cleanup
BenjaminMichaelis fa882bb
Merge branch 'main' into AIChat
BenjaminMichaelis 27204c7
Cleanup
BenjaminMichaelis 4c572c8
PR Feedback
BenjaminMichaelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
EssentialCSharp.Chat.Shared/EssentialCSharp.Chat.Common.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.SemanticKernel" /> | ||
| <PackageReference Include="Microsoft.SemanticKernel.Connectors.PgVector" /> | ||
| <PackageReference Include="ModelContextProtocol" /> | ||
| <PackageReference Include="ModelContextProtocol.AspNetCore" /> | ||
| <PackageReference Include="Microsoft.SourceLink.GitHub"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
80 changes: 80 additions & 0 deletions
80
EssentialCSharp.Chat.Shared/Extensions/ServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using EssentialCSharp.Chat.Common.Services; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.SemanticKernel; | ||
|
|
||
| namespace EssentialCSharp.Chat.Common.Extensions; | ||
|
|
||
| public static class ServiceCollectionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds Azure OpenAI and related AI services to the service collection | ||
| /// </summary> | ||
| /// <param name="services">The service collection to add services to</param> | ||
| /// <param name="aiOptions">The AI configuration options</param> | ||
| /// <returns>The service collection for chaining</returns> | ||
| public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, AIOptions aiOptions) | ||
| { | ||
| // Validate required configuration | ||
| if (aiOptions == null) | ||
BenjaminMichaelis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| throw new InvalidOperationException("AIOptions cannot be null."); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(aiOptions.Endpoint) || | ||
| string.IsNullOrEmpty(aiOptions.ApiKey)) | ||
| { | ||
| throw new InvalidOperationException("Azure OpenAI Endpoint and ApiKey must be properly configured in AIOptions. Please update your configuration with valid values."); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(aiOptions.PostgresConnectionString) || | ||
| aiOptions.PostgresConnectionString.Contains("your-postgres-connection-string")) | ||
| { | ||
| throw new InvalidOperationException("PostgreSQL connection string must be properly configured in AIOptions for vector store. Please update your configuration with a valid connection string."); | ||
| } | ||
|
|
||
| #pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
|
|
||
| // Register Azure OpenAI services | ||
| services.AddAzureOpenAIEmbeddingGenerator( | ||
| aiOptions.VectorGenerationDeploymentName, | ||
| aiOptions.Endpoint, | ||
| aiOptions.ApiKey); | ||
|
|
||
| services.AddAzureOpenAIChatClient( | ||
| aiOptions.ChatDeploymentName, | ||
| aiOptions.Endpoint, | ||
| aiOptions.ApiKey); | ||
|
|
||
| // Add PostgreSQL vector store | ||
| services.AddPostgresVectorStore(aiOptions.PostgresConnectionString); | ||
|
|
||
| #pragma warning restore SKEXP0010 | ||
|
|
||
| // Register shared AI services | ||
| services.AddSingleton<EmbeddingService>(); | ||
| services.AddSingleton<AISearchService>(); | ||
| services.AddSingleton<AIChatService>(); | ||
| services.AddSingleton<MarkdownChunkingService>(); | ||
|
|
||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds Azure OpenAI and related AI services to the service collection using configuration | ||
| /// </summary> | ||
| /// <param name="services">The service collection to add services to</param> | ||
| /// <param name="configuration">The configuration to read AIOptions from</param> | ||
| /// <returns>The service collection for chaining</returns> | ||
| public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, IConfiguration configuration) | ||
| { | ||
| // Configure AI options from configuration | ||
| services.Configure<AIOptions>(configuration.GetSection("AIOptions")); | ||
|
|
||
| var aiOptions = configuration.GetSection("AIOptions").Get<AIOptions>(); | ||
|
|
||
| return aiOptions == null | ||
| ? throw new InvalidOperationException("AIOptions section is missing from configuration.") | ||
| : services.AddAzureOpenAIServices(aiOptions); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| namespace EssentialCSharp.Chat; | ||
|
|
||
| public class AIOptions | ||
| { | ||
| /// <summary> | ||
| /// The Azure OpenAI deployment name for text embedding generation. | ||
| /// </summary> | ||
| public string VectorGenerationDeploymentName { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The Azure OpenAI deployment name for chat completions. | ||
| /// </summary> | ||
| public string ChatDeploymentName { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The system prompt to use for the chat model. | ||
| /// </summary> | ||
| public string SystemPrompt { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The Azure OpenAI endpoint URL. | ||
| /// </summary> | ||
| public string Endpoint { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The API key for accessing Azure OpenAI services. | ||
| /// </summary> | ||
| public string ApiKey { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The PostgreSQL connection string for the vector store. | ||
| /// </summary> | ||
| public string PostgresConnectionString { get; set; } = string.Empty; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using Microsoft.Extensions.VectorData; | ||
|
|
||
| namespace EssentialCSharp.Chat.Common.Models; | ||
|
|
||
| /// <summary> | ||
| /// Represents a chunk of book content for vector search | ||
| /// </summary> | ||
| public sealed class BookContentChunk | ||
| { | ||
| /// <summary> | ||
| /// Unique identifier for the chunk - serves as the vector store key | ||
| /// </summary> | ||
| [VectorStoreKey] | ||
| public string Id { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Original source file name | ||
| /// </summary> | ||
| [VectorStoreData] | ||
| public string FileName { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Heading or title of the markdown chunk | ||
| /// </summary> | ||
| [VectorStoreData] | ||
| public string Heading { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// The actual markdown content text for this chunk | ||
| /// </summary> | ||
| [VectorStoreData] | ||
| public string ChunkText { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Chapter number extracted from filename (e.g., "Chapter01.md" -> 1) | ||
| /// </summary> | ||
| [VectorStoreData] | ||
| public int? ChapterNumber { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// SHA256 hash of the chunk content for change detection | ||
| /// </summary> | ||
| [VectorStoreData] | ||
| public string ContentHash { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Vector embedding for the chunk text - will be generated by embedding service | ||
| /// Using 1536 dimensions for Azure OpenAI text-embedding-3-small-v1 | ||
| /// Use CosineSimilarity distance function since we are using text-embedding-3 (https://platform.openai.com/docs/guides/embeddings#which-distance-function-should-i-use) | ||
| /// 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 | ||
| /// </summary> | ||
| [VectorStoreVector(Dimensions: 1536, DistanceFunction = DistanceFunction.CosineSimilarity, IndexKind = IndexKind.Hnsw)] | ||
| public ReadOnlyMemory<float>? TextEmbedding { get; set; } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.