-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
235 lines (179 loc) · 7.39 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using Common.AiDevsApi.Contracts;
using Common.AiDevsApi.Models;
using Common.Cache.Contracts;
using Common.FirecrawlService;
using OpenAI;
using OpenAI.Audio;
using OpenAI.Chat;
using S02E05;
using S02E05.Models;
using System.Collections.Immutable;
using System.Text.Json.Nodes;
using S02E05.Services;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddS02E05();
var host = builder.Build();
await using var scope = host.Services.CreateAsyncScope();
var sp = scope.ServiceProvider;
var article = await GetArticleContent();
var modifiedArticle = await ReplaceImagesWithDescriptions(article);
modifiedArticle = await ReplaceAudioWithTranscriptions(modifiedArticle);
var questions = await FetchQuestions();
var answers = await AnswerQuestions(questions, modifiedArticle);
Console.WriteLine(answers);
var aiDevsApiService = sp.GetRequiredService<IAiDevsApiService>();
var taskAnswer = new TaskAnswer<JsonObject>
{
Task = "arxiv",
Answer = answers,
};
var response = await aiDevsApiService.VerifyTaskAnswerAsync(taskAnswer);
Console.WriteLine(response);
async Task<JsonObject> AnswerQuestions(string questions, string modifiedArticle)
{
var chatClient = sp.GetRequiredService<ChatClient>();
List<ChatMessage> messages =
[
new SystemChatMessage(Prompts.AnswerQuestionsBasedOnKnowledge(modifiedArticle)),
new UserChatMessage(ChatMessageContentPart.CreateTextPart(questions))
];
var response = await chatClient.CompleteChatAsync(messages);
return JsonNode.Parse(response.Value.Content[0].Text) as JsonObject
?? throw new InvalidOperationException("Answer is not a JSON object");
}
async Task<string> FetchQuestions()
{
var aiDevsHttpClient = sp.GetRequiredService<IAiDevsApiService>().HttpClient;
var aiDevsApiKey = sp.GetRequiredService<IOptions<AiDevsApiOptions>>().Value.ApiKey;
var response = await aiDevsHttpClient.GetAsync($"data/{aiDevsApiKey}/arxiv.txt");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return content;
}
async Task<string> ReplaceImagesWithDescriptions(string content)
{
var images = MarkdownParser.FindImages(content).ToImmutableArray();
foreach (var image in images.OrderByDescending(x => x.StartIndex))
{
var imageBytes = await DownloadImage(image.ImageUrl);
var description = await DescribeImage(imageBytes, Path.GetFileName(image.ImageUrl));
var structuredDescription =
$"""
<image>
{description}
</image>
""";
content = content[..image.StartIndex] + structuredDescription + content[image.EndIndex..];
}
return content;
}
async Task<string> ReplaceAudioWithTranscriptions(string content)
{
var audioFiles = MarkdownParser.FindAudioFiles(content).ToImmutableArray();
foreach (var audio in audioFiles.OrderByDescending(x => x.StartIndex))
{
var audioBytes = await DownloadAudio(audio.AudioUrl);
var transcription = await TranscribeAudio(audioBytes, Path.GetFileName(audio.AudioUrl));
var structuredTranscription =
$"""
<audio name="{audio.Description}">
{transcription}
</audio>
""";
content = content[..audio.StartIndex] + structuredTranscription + content[audio.EndIndex..];
}
return content;
}
async Task<byte[]> DownloadImage(string url)
{
var cacheService = sp.GetRequiredService<ICacheService>();
var cacheKey = url.Replace("/", "_");
var cachedImage = await cacheService.GetAsyncBytes(cacheKey);
if (cachedImage is not null)
return cachedImage;
var taskOptions = sp.GetRequiredService<IOptions<S02E05Options>>().Value;
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(taskOptions.DataUrl);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var imageBytes = await response.Content.ReadAsByteArrayAsync();
await cacheService.SetAsyncBytes(cacheKey, imageBytes);
return imageBytes;
}
async Task<string> DescribeImage(byte[] imageBytes, string imageName, string mimeType = "image/png")
{
var cacheService = sp.GetRequiredService<ICacheService>();
var cacheKey = $"description_{imageName}";
var cachedDescription = await cacheService.GetAsync(cacheKey);
if (cachedDescription is not null)
return cachedDescription;
var chatClient = sp.GetRequiredService<ChatClient>();
var binaryData = BinaryData.FromBytes(imageBytes);
List<ChatMessage> messages =
[
new SystemChatMessage(Prompts.SystemDescribeImage),
new UserChatMessage(
ChatMessageContentPart.CreateTextPart(Prompts.UserDescribeImage),
ChatMessageContentPart.CreateImagePart(binaryData, mimeType))
];
var response = await chatClient.CompleteChatAsync(messages);
var description = response.Value.Content[0].Text;
await cacheService.SetAsync(cacheKey, description);
return description;
}
async Task<byte[]> DownloadAudio(string url)
{
var cacheService = sp.GetRequiredService<ICacheService>();
var cacheKey = $"audio_{url.Replace("/", "_")}";
var cachedAudio = await cacheService.GetAsyncBytes(cacheKey);
if (cachedAudio is not null)
return cachedAudio;
var taskOptions = sp.GetRequiredService<IOptions<S02E05Options>>().Value;
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(taskOptions.DataUrl);
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var audioBytes = await response.Content.ReadAsByteArrayAsync();
await cacheService.SetAsyncBytes(cacheKey, audioBytes);
return audioBytes;
}
async Task<string> TranscribeAudio(byte[] audioBytes, string audioName)
{
var cacheService = sp.GetRequiredService<ICacheService>();
var cacheKey = $"transcription_{audioName}";
var cachedTranscription = await cacheService.GetAsync(cacheKey);
if (cachedTranscription is not null)
return cachedTranscription;
var audioClient = sp.GetRequiredService<OpenAIClient>().GetAudioClient("whisper-1");
AudioTranscriptionOptions options = new()
{
ResponseFormat = AudioTranscriptionFormat.Simple,
Language = "pl",
};
using var audioStream = new MemoryStream(audioBytes);
var response = await audioClient.TranscribeAudioAsync(audioStream, audioName, options);
var transcription = response.Value.Text;
await cacheService.SetAsync(cacheKey, transcription);
return transcription;
}
async Task<string> GetArticleContent()
{
var cacheService = sp.GetRequiredService<ICacheService>();
var cacheKey = $"article";
var cachedContent = await cacheService.GetAsync(cacheKey);
if (cachedContent is not null)
return cachedContent;
var firecrawlService = sp.GetRequiredService<IFirecrawlService>();
var taskOptions = sp.GetRequiredService<IOptions<S02E05Options>>().Value;
var result = await firecrawlService.ScrapeAsync(new FirecrawlRequest
{
Url = taskOptions.ArticleUrl,
Formats = ["markdown"],
});
var articleMarkdown = result.Data.Markdown
?? throw new InvalidOperationException("Article markdown is null");
await cacheService.SetAsync(cacheKey, articleMarkdown);
return articleMarkdown;
}