2023-10-11 21:40:35 +02:00
|
|
|
using System.Text.Json;
|
2023-11-10 14:22:09 +00:00
|
|
|
using Azure;
|
|
|
|
using Azure.AI.OpenAI;
|
2023-10-26 22:09:18 +02:00
|
|
|
using Microsoft.AI.DevTeam;
|
2023-10-11 21:40:35 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Microsoft.SemanticKernel;
|
2023-11-10 15:30:24 +00:00
|
|
|
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
|
2023-10-11 21:40:35 +02:00
|
|
|
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
|
|
|
|
using Microsoft.SemanticKernel.Memory;
|
2023-11-10 15:30:24 +00:00
|
|
|
using Microsoft.SemanticKernel.Plugins.Memory;
|
2023-11-09 16:03:38 +00:00
|
|
|
using Microsoft.SemanticKernel.Reliability.Basic;
|
2023-10-11 21:40:35 +02:00
|
|
|
using Octokit.Webhooks;
|
|
|
|
using Octokit.Webhooks.AspNetCore;
|
2023-10-26 22:09:18 +02:00
|
|
|
using Orleans.Configuration;
|
2023-10-11 21:40:35 +02:00
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddSingleton<WebhookEventProcessor, GithubWebHookProcessor>();
|
|
|
|
builder.Services.AddTransient(CreateKernel);
|
2023-11-10 15:30:24 +00:00
|
|
|
builder.Services.AddTransient(CreateMemory);
|
2023-11-03 21:46:25 +01:00
|
|
|
builder.Services.AddHttpClient();
|
2023-10-11 21:40:35 +02:00
|
|
|
|
|
|
|
builder.Services.AddSingleton(s =>
|
|
|
|
{
|
|
|
|
var ghOptions = s.GetService<IOptions<GithubOptions>>();
|
2023-11-09 16:03:38 +00:00
|
|
|
var logger = s.GetService<ILogger<GithubAuthService>>();
|
|
|
|
var ghService = new GithubAuthService(ghOptions, logger);
|
2023-10-11 21:40:35 +02:00
|
|
|
var client = ghService.GetGitHubClient().Result;
|
|
|
|
return client;
|
|
|
|
});
|
|
|
|
builder.Services.AddSingleton<GithubAuthService>();
|
|
|
|
|
|
|
|
builder.Services.AddApplicationInsightsTelemetry();
|
|
|
|
builder.Services.AddOptions<GithubOptions>()
|
|
|
|
.Configure<IConfiguration>((settings, configuration) =>
|
|
|
|
{
|
|
|
|
configuration.GetSection("GithubOptions").Bind(settings);
|
|
|
|
});
|
|
|
|
builder.Services.AddOptions<AzureOptions>()
|
|
|
|
.Configure<IConfiguration>((settings, configuration) =>
|
|
|
|
{
|
|
|
|
configuration.GetSection("AzureOptions").Bind(settings);
|
|
|
|
});
|
|
|
|
builder.Services.AddOptions<OpenAIOptions>()
|
|
|
|
.Configure<IConfiguration>((settings, configuration) =>
|
|
|
|
{
|
|
|
|
configuration.GetSection("OpenAIOptions").Bind(settings);
|
|
|
|
});
|
|
|
|
builder.Services.AddOptions<QdrantOptions>()
|
|
|
|
.Configure<IConfiguration>((settings, configuration) =>
|
|
|
|
{
|
|
|
|
configuration.GetSection("QdrantOptions").Bind(settings);
|
|
|
|
});
|
2023-11-03 21:46:25 +01:00
|
|
|
builder.Services.AddOptions<ServiceOptions>()
|
|
|
|
.Configure<IConfiguration>((settings, configuration) =>
|
|
|
|
{
|
|
|
|
configuration.GetSection("ServiceOptions").Bind(settings);
|
|
|
|
});
|
2023-10-11 21:40:35 +02:00
|
|
|
|
|
|
|
builder.Services.AddSingleton<IManageAzure, AzureService>();
|
|
|
|
builder.Services.AddSingleton<IManageGithub, GithubService>();
|
2023-11-03 21:46:25 +01:00
|
|
|
builder.Services.AddSingleton<IAnalyzeCode, CodeAnalyzer>();
|
2023-10-11 21:40:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
builder.Host.UseOrleans(siloBuilder =>
|
|
|
|
{
|
2023-10-26 22:09:18 +02:00
|
|
|
|
|
|
|
if (builder.Environment.IsDevelopment())
|
2023-10-11 21:40:35 +02:00
|
|
|
{
|
2023-10-26 22:09:18 +02:00
|
|
|
var connectionString = builder.Configuration.GetValue<string>("AzureOptions:CosmosConnectionString");
|
2024-02-18 20:21:44 +00:00
|
|
|
siloBuilder.AddMemoryStreams("StreamProvider")
|
|
|
|
.AddMemoryGrainStorage("PubSubStore");
|
2023-10-26 22:09:18 +02:00
|
|
|
siloBuilder.UseCosmosReminderService( o =>
|
|
|
|
{
|
|
|
|
o.ConfigureCosmosClient(connectionString);
|
|
|
|
o.ContainerName = "reminders";
|
|
|
|
o.DatabaseName = "devteam";
|
|
|
|
o.IsResourceCreationEnabled = true;
|
|
|
|
});
|
|
|
|
siloBuilder.AddCosmosGrainStorage(
|
|
|
|
name: "messages",
|
|
|
|
configureOptions: o =>
|
|
|
|
{
|
|
|
|
o.ConfigureCosmosClient(connectionString);
|
|
|
|
o.ContainerName = "persistence";
|
|
|
|
o.DatabaseName = "devteam";
|
|
|
|
o.IsResourceCreationEnabled = true;
|
|
|
|
});
|
|
|
|
siloBuilder.UseLocalhostClustering();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var cosmosDbconnectionString = builder.Configuration.GetValue<string>("AzureOptions:CosmosConnectionString");
|
|
|
|
siloBuilder.Configure<ClusterOptions>(options =>
|
2023-10-11 21:40:35 +02:00
|
|
|
{
|
2023-10-26 22:09:18 +02:00
|
|
|
options.ClusterId = "ai-dev-cluster";
|
|
|
|
options.ServiceId = "ai-dev-cluster";
|
2023-10-11 21:40:35 +02:00
|
|
|
});
|
2023-10-26 22:09:18 +02:00
|
|
|
siloBuilder.Configure<SiloMessagingOptions>(options =>
|
|
|
|
{
|
|
|
|
options.ResponseTimeout = TimeSpan.FromMinutes(3);
|
|
|
|
options.SystemResponseTimeout = TimeSpan.FromMinutes(3);
|
|
|
|
});
|
|
|
|
siloBuilder.Configure<ClientMessagingOptions>(options =>
|
|
|
|
{
|
|
|
|
options.ResponseTimeout = TimeSpan.FromMinutes(3);
|
|
|
|
});
|
|
|
|
siloBuilder.UseCosmosClustering( o =>
|
|
|
|
{
|
|
|
|
o.ConfigureCosmosClient(cosmosDbconnectionString);
|
|
|
|
o.ContainerName = "devteam";
|
|
|
|
o.DatabaseName = "clustering";
|
|
|
|
o.IsResourceCreationEnabled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
siloBuilder.UseCosmosReminderService( o =>
|
|
|
|
{
|
|
|
|
o.ConfigureCosmosClient(cosmosDbconnectionString);
|
|
|
|
o.ContainerName = "devteam";
|
|
|
|
o.DatabaseName = "reminders";
|
|
|
|
o.IsResourceCreationEnabled = true;
|
|
|
|
});
|
|
|
|
siloBuilder.AddCosmosGrainStorage(
|
|
|
|
name: "messages",
|
|
|
|
configureOptions: o =>
|
|
|
|
{
|
|
|
|
o.ConfigureCosmosClient(cosmosDbconnectionString);
|
|
|
|
o.ContainerName = "devteam";
|
|
|
|
o.DatabaseName = "persistence";
|
|
|
|
o.IsResourceCreationEnabled = true;
|
|
|
|
});
|
2024-01-12 20:06:35 +00:00
|
|
|
|
|
|
|
//TODO: Add streaming here
|
2023-10-26 22:09:18 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 21:40:35 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
builder.Services.Configure<JsonSerializerOptions>(options =>
|
|
|
|
{
|
|
|
|
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
|
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapGitHubWebhooks();
|
|
|
|
});
|
|
|
|
|
2023-11-03 21:46:25 +01:00
|
|
|
|
2023-10-11 21:40:35 +02:00
|
|
|
app.Run();
|
|
|
|
|
2023-11-10 15:30:24 +00:00
|
|
|
static ISemanticTextMemory CreateMemory(IServiceProvider provider)
|
2023-10-11 21:40:35 +02:00
|
|
|
{
|
|
|
|
var openAiConfig = provider.GetService<IOptions<OpenAIOptions>>().Value;
|
|
|
|
var qdrantConfig = provider.GetService<IOptions<QdrantOptions>>().Value;
|
|
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.SetMinimumLevel(LogLevel.Debug)
|
|
|
|
.AddConsole()
|
|
|
|
.AddDebug();
|
|
|
|
});
|
|
|
|
|
2023-11-10 15:30:24 +00:00
|
|
|
var memoryBuilder = new MemoryBuilder();
|
|
|
|
return memoryBuilder.WithLoggerFactory(loggerFactory)
|
|
|
|
.WithQdrantMemoryStore(qdrantConfig.Endpoint, qdrantConfig.VectorSize)
|
|
|
|
.WithAzureTextEmbeddingGenerationService(openAiConfig.EmbeddingDeploymentOrModelId, openAiConfig.Endpoint, openAiConfig.ApiKey)
|
|
|
|
.Build();
|
|
|
|
}
|
|
|
|
|
|
|
|
static IKernel CreateKernel(IServiceProvider provider)
|
|
|
|
{
|
|
|
|
var openAiConfig = provider.GetService<IOptions<OpenAIOptions>>().Value;
|
2023-10-11 21:40:35 +02:00
|
|
|
|
2023-11-10 15:30:24 +00:00
|
|
|
var loggerFactory = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.SetMinimumLevel(LogLevel.Debug)
|
|
|
|
.AddConsole()
|
|
|
|
.AddDebug();
|
|
|
|
});
|
2023-11-10 14:22:09 +00:00
|
|
|
|
|
|
|
var clientOptions = new OpenAIClientOptions();
|
|
|
|
clientOptions.Retry.NetworkTimeout = TimeSpan.FromMinutes(5);
|
|
|
|
var openAIClient = new OpenAIClient(new Uri(openAiConfig.Endpoint), new AzureKeyCredential(openAiConfig.ApiKey), clientOptions);
|
|
|
|
|
2023-10-11 21:40:35 +02:00
|
|
|
return new KernelBuilder()
|
|
|
|
.WithLoggerFactory(loggerFactory)
|
2023-11-10 14:22:09 +00:00
|
|
|
.WithAzureChatCompletionService(openAiConfig.DeploymentOrModelId, openAIClient)
|
2023-11-09 16:03:38 +00:00
|
|
|
.WithRetryBasic(new BasicRetryConfig {
|
|
|
|
MaxRetryCount = 5,
|
|
|
|
UseExponentialBackoff = true
|
2023-11-10 15:30:24 +00:00
|
|
|
}).Build();
|
2023-10-11 21:40:35 +02:00
|
|
|
}
|