Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.60.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.PgVector" Version="1.60.0-preview" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.3" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.3" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta6.25358.103" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Octokit" Version="14.0.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
</ItemGroup>
</Project>
</Project>
20 changes: 20 additions & 0 deletions EssentialCSharp.Chat.Shared/EssentialCSharp.Chat.Common.csproj
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>
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)
{
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);
}
}
34 changes: 34 additions & 0 deletions EssentialCSharp.Chat.Shared/Models/AIOptions.cs
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;
}
54 changes: 54 additions & 0 deletions EssentialCSharp.Chat.Shared/Models/BookContentChunk.cs
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; }
}
Loading