| Name | Message | Date |
|---|---|---|
| 📄 AiDtos.cs | 4 days ago | |
| 📄 ClipDescriber.cs | 4 days ago | |
| 📄 ClipDescriberOptions.cs | 4 days ago | |
| 📄 ClipDescriberServiceCollectionExtensions.cs | 4 days ago | |
| 📄 ClipDescription.cs | 4 days ago |
📄
src/Domain/Describer/ClipDescriber.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
using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; using Microsoft.Extensions.Options; namespace Slopper.Domain.Describer; public sealed class ClipDescriber( IFrameExtractor frameExtractor, ISubtitleReader subtitleReader, IChatClient chatClient, IOptionsMonitor<ClipDescriberOptions> options ) { public async Task<ClipDescription> DescribeClip( MediaItem media, TimeSpan start, TimeSpan duration, CancellationToken cancellationToken ) { var frames = await frameExtractor.ExtractFrames(media, [start, start + duration], cancellationToken); var subtitles = await subtitleReader.ReadSubtitles(media, start, duration, cancellationToken); List<AIContent> contents = []; foreach (var frame in frames) { contents.Add(new DataContent(frame, "image/png")); } contents.Add(new TextContent(options.CurrentValue.Prompt)); if (subtitles.Count > 0) { contents.Add( new TextContent( $"Subtitles from this clip:\n{string.Join("\n", subtitles.Select(s => string.Join(" / ", s.Lines)))}" ) ); } var response = await chatClient.GetResponseAsync( [new ChatMessage(ChatRole.User, contents)], new() { ResponseFormat = ChatResponseFormat.ForJsonSchema<ClipDescriptionAiDto>( AiDtoSerializerContext.Default.Options ), }, cancellationToken ); var result = JsonSerializer.Deserialize(response.Text, AiDtoSerializerContext.Default.ClipDescriptionAiDto) ?? throw new Exception("Literal null response from description agent."); return new(result.Caption, result.Tags.Select(t => new Tag(t)).ToHashSet()); } }