-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (77 loc) · 3.25 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (77 loc) · 3.25 KB
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
using Microsoft.EntityFrameworkCore;
using SemanticKernel.ChatBot.Api.Data;
using SemanticKernel.ChatBot.Api.Models;
using SemanticKernel.ChatBot.Api.Services;
var builder = WebApplication.CreateBuilder(args);
// Add configuration
builder.Services.Configure<OpenAIConfiguration>(
builder.Configuration.GetSection(OpenAIConfiguration.SectionName));
builder.Services.Configure<SemanticKernelConfiguration>(
builder.Configuration.GetSection(SemanticKernelConfiguration.SectionName));
builder.Services.Configure<DatabaseConfiguration>(
builder.Configuration.GetSection(DatabaseConfiguration.SectionName));
// Add Entity Framework
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? builder.Configuration.GetSection("Database:ConnectionString").Value
?? "Data Source=chatbot.db";
builder.Services.AddDbContext<ChatBotDbContext>(options =>
options.UseSqlite(connectionString));
// Add application services
builder.Services.AddScoped<IFileReaderService, FileReaderService>();
builder.Services.AddScoped<IChatHistoryService, ChatHistoryService>();
builder.Services.AddScoped<IKernelMemoryService, KernelMemoryService>();
builder.Services.AddScoped<IChatService, ChatService>();
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddControllers();
// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "Semantic Kernel Chat Bot API",
Version = "v1",
Description = "API for chat bot powered by Semantic Kernel with persistent chat history and document memory"
});
});
var app = builder.Build();
// Ensure database is created
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ChatBotDbContext>();
context.Database.EnsureCreated();
}
// Initialize Kernel Memory with documents on startup
try
{
using var scope = app.Services.CreateScope();
var kernelMemoryService = scope.ServiceProvider.GetRequiredService<IKernelMemoryService>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting Kernel Memory initialization...");
await kernelMemoryService.InitializeAsync();
await kernelMemoryService.LoadDocumentsAsync();
var documentCount = await kernelMemoryService.GetDocumentCountAsync();
logger.LogInformation("Kernel Memory initialized successfully with {DocumentCount} documents", documentCount);
}
catch (Exception ex)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Failed to initialize Kernel Memory on startup. The service will still start but document search may not work properly.");
// Don't prevent app startup if Kernel Memory fails
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Semantic Kernel Chat Bot API v1");
c.RoutePrefix = "swagger";
});
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();