-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
58 lines (44 loc) · 1.58 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
using Microsoft.Extensions.AI;
using Azure.AI.Inference;
using Azure;
using ChatRole = Microsoft.Extensions.AI.ChatRole;
IChatClient client =
new ChatCompletionsClient(
endpoint: new Uri("https://models.inference.ai.azure.com"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("GITHUB_TOKEN")))
.AsChatClient("gpt-4o-mini");
var filePaths = Directory.GetFiles("data");
var systemPrompt =
"""
You are an AI assistant that extracts underlined, highlighted, and marked passages from book page images.
When passages have a natural continuation between pages, merge them and assign the page number where the first passage starts.
""";
var passages = new List<AIBookmark>();
foreach(var path in filePaths)
{
var file = await File.ReadAllBytesAsync(path);
var messages = new List<ChatMessage>
{
new ChatMessage(ChatRole.System, systemPrompt),
new ChatMessage(ChatRole.User, new AIContent[] {
new ImageContent(file, "image/jpeg"),
new TextContent("Extract the marked passages from the image"),
})
};
var response = await client.CompleteAsync<List<AIBookmark>>(messages, options: new ChatOptions {Temperature = 0.1f});
passages.AddRange(response.Result);
}
var sortedPassages =
passages
.OrderBy(p => p.PageNumber)
.Select(p => $"> {p.Text} (pg. {p.PageNumber})");
foreach(var passage in sortedPassages)
{
Console.WriteLine(passage);
Console.WriteLine("");
}
class AIBookmark
{
public string Text {get;set;}
public int PageNumber {get;set;}
}