ChatApi/Program.cs
2024-11-28 22:49:03 -03:00

158 lines
5.9 KiB
C#
Raw Blame History

using ChatApi;
using ChatApi.Services;
using ChatApi.Services.Bot;
using ChatApi.Services.Bot.Structs;
using ChatApi.Services.Classifier;
using ChatApi.Services.ResponseService;
using ChatApi.Settings;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using OllamaSharp;
#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.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<DomvsDatabaseSettings>(
builder.Configuration.GetSection("DomvsDatabase"));
builder.Services.Configure<ChatRHSettings>(
builder.Configuration.GetSection("ChatRHSettings"));
builder.Services.AddScoped<ClassifierPersistence>();
builder.Services.AddSingleton<ChatHistoryService>();
builder.Services.AddScoped<SharepointDomvsService>();
builder.Services.AddSingleton<TextFilter>();
builder.Services.AddScoped<ResponseFactory>();
builder.Services.AddScoped<ChatBotRHCall>();
builder.Services.AddScoped<IResponseService, ResponseChatService>();
builder.Services.AddScoped<IResponseService, ResponseCompanyService>();
builder.Services.AddScoped<IResponseService, ResponseBotRHService>();
//builder.Services.AddOllamaChatCompletion("phi3.5", new Uri("http://localhost:11435"));
//builder.Services.AddOllamaChatCompletion("tinydolphin", new Uri("http://localhost:11435"));
//var apiClient = new OllamaApiClient(new Uri("http://localhost:11435"), "tinydolphin");
//builder.Services.AddOllamaChatCompletion("phi3.5", new Uri("http://localhost:11435"));
//builder.Services.AddOllamaChatCompletion("tinydolphin", new Uri("http://localhost:11435"));
//builder.Services.AddOllamaChatCompletion("tinyllama", new Uri("http://localhost:11435"));
//builder.Services.AddOllamaChatCompletion("starling-lm", new Uri("http://localhost:11435"));
//ServerSpace - GPT Service
builder.Services.AddOpenAIChatCompletion("openchat-3.5-0106", new Uri("https://gpt.serverspace.com.br/v1/chat/completions"), "tIAXVf3AkCkkpSX+PjFvktfEeSPyA1ZYam50UO3ye/qmxVZX6PIXstmJsLZXkQ39C33onFD/81mdxvhbGHm7tQ==");
builder.Services.AddOllamaTextEmbeddingGeneration("all-minilm", new Uri("http://localhost:11435"));
//builder.Services.AddOllamaChatCompletion("phi3.5", new Uri("http://localhost:11435"));
//builder.Services.AddOpenAIChatCompletion("gpt-4o-mini", "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");
//builder.Services.AddGoogleAIGeminiChatCompletion("gemini-1.5-flash-latest", "AIzaSyDKBMX5yW77vxJFVJVE-5VLxlQRxCepck8");
//Anthropic / Claude
//builder.Services.AddAnthropicChatCompletion(
// modelId: "claude-3-5-sonnet-latest", // ou outro modelo Claude desejado
// apiKey: "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"
//);
builder.Services.AddKernel();
//builder.Services.AddKernel()
// .AddOllamaChatCompletion("phi3", new Uri("http://localhost:11435"))
// .AddOllamaTextEmbeddingGeneration()
// .Build();
//builder.Services.AddOllamaChatCompletion("phi3.5", new Uri("http://192.168.0.150:11436"));
builder.Services.AddHttpClient();
var tenantId = builder.Configuration.GetSection("AppTenantId");
var clientId = builder.Configuration.GetSection("AppClientID");
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = $"https://login.microsoftonline.com/{tenantId}/v2.0";
options.Audience = "api://" + clientId; // Client ID da sua API
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = $"https://login.microsoftonline.com/{tenantId}/v2.0",
ValidAudience = "api://" + clientId
};
});
builder.Services.AddControllers();
//builder.Services.AddAuthentication(options =>
// {
// options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
// })
// .AddJwtBearer(options =>
// {
// // Configura<72><61>es anteriores...
// // Eventos para log e tratamento de erros
// options.Events = new JwtBearerEvents
// {
// OnAuthenticationFailed = context =>
// {
// // Log de erros de autentica<63><61>o
// Console.WriteLine($"Erro de autentica<63><61>o: {context.Exception.Message}");
// return Task.CompletedTask;
// },
// OnTokenValidated = context =>
// {
// // Valida<64><61>es adicionais se necess<73>rio
// return Task.CompletedTask;
// }
// };
// });
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Use(async (context, next) =>
{
var cookieOpt = new CookieOptions()
{
Path = "/",
Expires = DateTimeOffset.UtcNow.AddDays(1),
IsEssential = true,
HttpOnly = false,
Secure = false,
};
await next();
});
app.UseAuthentication();
app.UseAuthorization();
app.Run();
#pragma warning restore SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.