-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (126 loc) · 6.76 KB
/
Program.cs
File metadata and controls
147 lines (126 loc) · 6.76 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Orchestration.GroupChat;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using RedTeamingTool.Extensions;
using RedTeamingTool.Agents;
using RedTeamingTool.Config;
using RedTeamingTool.Constants;
using RedTeamingTool.Services;
using RedTeamingTool.Services.Interfaces;
using RedTeamingTool.UI;
using Spectre.Console;
namespace RedTeamingTool
{
public class Program
{
public static async Task Main(string[] args)
{
try
{
AnsiConsole.Write(
new FigletText("Red Teaming Tool")
.Color(Color.Red));
AnsiConsole.MarkupLine("[yellow]Starting up...[/]");
var host = CreateHostBuilder(args).Build();
// Get the ConsoleUI service and run it
var consoleUI = host.Services.GetRequiredService<ConsoleUI>();
await consoleUI.RunAsync();
}
catch (Exception ex)
{
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenPaths | ExceptionFormats.ShortenTypes |
ExceptionFormats.ShortenMethods | ExceptionFormats.ShowLinks);
AnsiConsole.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true);
config.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
// Register configuration options
services.Configure<AzureOpenAIConfig>(hostContext.Configuration.GetSection("AzureOpenAI"));
services.Configure<OllamaConfig>(hostContext.Configuration.GetSection("Ollama"));
services.Configure<RedTeamingConfig>(hostContext.Configuration.GetSection("RedTeaming"));
// Register HTTP client
services.AddHttpClient();
// Register Semantic Kernel
var azureConfig = hostContext.Configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfig>();
var ollamaConfig = hostContext.Configuration.GetSection("Ollama").Get<OllamaConfig>();
// Create kernel with named services
services.AddSingleton<Kernel>(sp =>
{
var builder = Kernel.CreateBuilder();
// Add Azure OpenAI with a service ID
if (azureConfig != null)
{
builder.AddAzureOpenAIChatCompletion(
deploymentName: azureConfig.DeploymentName ?? "gpt-4",
endpoint: azureConfig.Endpoint ?? "https://api.openai.com/",
apiKey: azureConfig.ApiKey ?? "default-key",
serviceId: ServiceIds.AzureOpenAI);
}
// Add Ollama with a service ID
if (ollamaConfig != null)
{
builder.Services.AddTransient(s =>
{
var client = new HttpClient(new HttpClientHandler());
client.BaseAddress = new Uri("http://localhost:11434/");
client.Timeout = TimeSpan.FromMinutes(20);
return client;
});
builder.Services.AddOllamaChatCompletion(modelId: "gpt-oss:20b", serviceId: ServiceIds.Ollama);
//builder.AddOllamaChatCompletion(
// modelId: ollamaConfig.Model ?? "gpt-oss:20b",
// endpoint: new Uri(ollamaConfig.Endpoint ?? "http://localhost:11434/"),
// serviceId: ServiceIds.Ollama);
//builder.AddOpenAIChatCompletion("gpt-oss:20b",
// new Uri("http://localhost:11434/"),
// ServiceIds.Ollama);
}
return builder.Build();
});
// Register the red teaming service directly
services.AddSingleton<IRedTeamingService, RedTeamingService>();
// Register prompt service
services.AddSingleton<PromptService>();
// Register the group chat
services.AddSingleton<RedTeamingGroupChat>();
// Register Ollama test service
services.AddSingleton<OllamaTestService>();
// Register attack summary service
services.AddSingleton<AttackSummaryService>();
// Register email tool misuse test service
services.AddSingleton<EmailToolMisuseTest>();
// Register findings export service
services.AddSingleton<FindingsExportService>();
// Register UI
services.AddSingleton<ConsoleUI>();
})
.ConfigureLogging((hostContext, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
// Set minimum log level from configuration
logging.AddFilter("Microsoft", LogLevel.Warning);
logging.AddFilter("System", LogLevel.Warning);
if (hostContext.HostingEnvironment.IsDevelopment())
{
logging.AddDebug();
}
});
}
}