-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProgram.cs
97 lines (80 loc) · 2.72 KB
/
Program.cs
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
94
95
96
97
using AnthonyPuppo.SemanticKernel.NL2EF.Data;
using AnthonyPuppo.SemanticKernel.NL2EF.Extensions;
using AnthonyPuppo.SemanticKernel.NL2EF.Options;
using AnthonyPuppo.SemanticKernel.NL2EF.Skills;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors((options) =>
{
options.AddDefaultPolicy((policy) =>
{
policy
.WithOrigins("https://chat.openai.com", "https://localhost:7012")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services
.AddOptions<SemanticKernelOptions>()
.Bind(builder.Configuration.GetSection(SemanticKernelOptions.SemanticKernel));
builder.Services.AddDbContext<AppDbContext>((options) =>
{
options.UseSqlite("Data Source=movies.sqlite;Mode=ReadOnly");
});
builder.Services.AddSemanticKernel();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen((options) =>
{
options.SwaggerDoc("v1", new OpenApiInfo()
{
Title = "NL2EF",
Version = "v1",
Description = "This is a sample API for NL2EF",
});
});
var app = builder.Build();
app.UseCors();
app.UseSwagger((options) =>
{
options.PreSerializeFilters.Add((swaggerDocument, httpRequest) =>
{
swaggerDocument.Servers = new List<OpenApiServer>()
{
new OpenApiServer()
{
Url = $"{httpRequest.Scheme}://{httpRequest.Host.Value}"
}
};
});
});
app.UseSwaggerUI((options) =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.yaml", "NL2EF v1");
});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), ".well-known")),
RequestPath = "/.well-known"
});
app
.MapGet("/query", async ([FromServices] IKernel kernel, string question) =>
{
var context = kernel.CreateNewContext();
var function = kernel.Skills.GetFunction(nameof(NL2EFSkill), nameof(NL2EFSkill.Query));
context.Variables.Update(question);
context = await function.InvokeAsync(context);
return context.Result;
})
.WithName("Query")
.WithOpenApi((operation) =>
{
operation.Description = "Translates a question into SQL, fetches relevant data from the database, and formulates a response based on the retrieved data.";
var parameter = operation.Parameters[0];
parameter.Description = "The question to query the database for";
return operation;
});
app.Run();