-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
71 lines (55 loc) · 2.4 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
using Common.OpenAI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenAI.Chat;
using S02E02;
var builder = Host.CreateApplicationBuilder(args);
builder.Services
.AddOpenAIChatClient();
var host = builder.Build();
await using var scope = host.Services.CreateAsyncScope();
var sp = scope.ServiceProvider;
var chatClient = sp.GetRequiredService<ChatClient>();
var mapsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Maps");
var mapFiles = Directory.GetFiles(mapsDirectory, "*.png");
var mapDescriptions = new Dictionary<string, string>();
foreach (var mapFile in mapFiles)
{
using Stream imageStream = File.OpenRead(mapFile);
BinaryData imageBytes = BinaryData.FromStream(imageStream);
List<ChatMessage> messages =
[
new SystemChatMessage(Prompts.DescribeMapSystemPrompt),
new UserChatMessage(
ChatMessageContentPart.CreateTextPart(Prompts.DescribeMapUserPrompt),
ChatMessageContentPart.CreateImagePart(imageBytes, MimeTypes.GetMimeType(mapFile))),
];
ChatCompletion completion = await chatClient.CompleteChatAsync(messages);
mapDescriptions[Path.GetFileName(mapFile)] = completion.Content[0].Text;
}
foreach (var (mapFile, description) in mapDescriptions)
{
var descriptionsLocation = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Descriptions");
await File.WriteAllTextAsync(Path.Combine(descriptionsLocation, $"{Path.GetFileName(mapFile)}.txt"), description);
}
// One of the maps is from the wrong city
foreach (var combination in CombinationsWithOneExcluded(mapDescriptions))
{
var userPrompt = string.Join("\n\n", combination.Select(m => $"{m.Key}: {m.Value}"));
List<ChatMessage> messages =
[
new SystemChatMessage(Prompts.DetectCitySystemPrompt),
new UserChatMessage(userPrompt),
];
ChatCompletion completion = await chatClient.CompleteChatAsync(messages);
Console.WriteLine(completion.Content[0].Text);
Console.WriteLine();
}
static IEnumerable<IReadOnlyDictionary<string, string>> CombinationsWithOneExcluded(IReadOnlyDictionary<string, string> mapDescriptions)
{
foreach (var mapDescription in mapDescriptions)
{
var remainingMaps = mapDescriptions.Where(m => m.Key != mapDescription.Key);
yield return new Dictionary<string, string>(remainingMaps);
}
}