|
| 1 | +#pragma warning disable MEAI001 |
| 2 | +using System.Linq; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using MarkItDown; |
| 8 | +using MarkItDown.Intelligence; |
| 9 | +using MarkItDown.Conversion.Middleware; |
| 10 | + |
| 11 | +using MarkItDown.Tests.Fixtures; |
| 12 | +using Microsoft.Extensions.AI; |
| 13 | +using Shouldly; |
| 14 | + |
| 15 | +namespace MarkItDown.Tests.Conversion; |
| 16 | + |
| 17 | +public class AiModelPipelineTests |
| 18 | +{ |
| 19 | + [Fact] |
| 20 | + public async Task ImageEnrichment_UsesInjectedChatClient() |
| 21 | + { |
| 22 | + var chatClient = new RecordingChatClient("SONAR diagram with layered services"); |
| 23 | + var options = new MarkItDownOptions |
| 24 | + { |
| 25 | + AiModels = new StaticAiModelProvider(chatClient, null), |
| 26 | + EnableAiImageEnrichment = true |
| 27 | + }; |
| 28 | + |
| 29 | + var client = new MarkItDownClient(options); |
| 30 | + |
| 31 | + await using var stream = DocxInlineImageFactory.Create(); |
| 32 | + var streamInfo = new StreamInfo( |
| 33 | + mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 34 | + extension: ".docx", |
| 35 | + fileName: "inline-images.docx"); |
| 36 | + |
| 37 | + var result = await client.ConvertAsync(stream, streamInfo); |
| 38 | + |
| 39 | + chatClient.Requests.Count.ShouldBeGreaterThan(0); |
| 40 | + result.Artifacts.Images.ShouldNotBeEmpty(); |
| 41 | + result.Artifacts.Images[0].DetailedDescription.ShouldBe("SONAR diagram with layered services"); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task CustomMiddleware_UsesSpeechToTextClient() |
| 46 | + { |
| 47 | + var speechClient = new RecordingSpeechClient("Mission control acknowledges receipt of telemetry."); |
| 48 | + var invoked = false; |
| 49 | + var pipeline = new ConversionPipeline( |
| 50 | + new IConversionMiddleware[] { new SpeechAnnotationMiddleware(new byte[] { 1, 2, 3 }, () => invoked = true) }, |
| 51 | + new StaticAiModelProvider(null, speechClient), |
| 52 | + logger: null); |
| 53 | + |
| 54 | + var artifacts = new ConversionArtifacts(); |
| 55 | + var segments = new List<DocumentSegment>(); |
| 56 | + var streamInfo = new StreamInfo(mimeType: "audio/wav", extension: ".wav", fileName: "sample.wav"); |
| 57 | + |
| 58 | + await pipeline.ExecuteAsync(streamInfo, artifacts, segments, CancellationToken.None); |
| 59 | + |
| 60 | + invoked.ShouldBeTrue(); |
| 61 | + speechClient.InvocationCount.ShouldBe(1); |
| 62 | + segments.ShouldContain(segment => |
| 63 | + segment.Type == SegmentType.Audio && |
| 64 | + segment.Markdown.Contains("Mission control acknowledges receipt of telemetry.")); |
| 65 | + } |
| 66 | + |
| 67 | + private sealed class RecordingChatClient(string responseText) : IChatClient |
| 68 | + { |
| 69 | + public List<IReadOnlyList<ChatMessage>> Requests { get; } = new(); |
| 70 | + |
| 71 | + public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellationToken = default) |
| 72 | + { |
| 73 | + var recorded = new List<ChatMessage>(messages); |
| 74 | + Requests.Add(recorded); |
| 75 | + var reply = new ChatMessage(ChatRole.Assistant, responseText); |
| 76 | + return Task.FromResult(new ChatResponse(new List<ChatMessage> { reply })); |
| 77 | + } |
| 78 | + |
| 79 | + IAsyncEnumerable<ChatResponseUpdate> IChatClient.GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellationToken) |
| 80 | + { |
| 81 | + throw new NotSupportedException(); |
| 82 | + } |
| 83 | + |
| 84 | + public object? GetService(Type serviceType, object? serviceKey = null) => null; |
| 85 | + |
| 86 | + public void Dispose() |
| 87 | + { |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private sealed class RecordingSpeechClient(string transcript) : ISpeechToTextClient |
| 92 | + { |
| 93 | + public int InvocationCount { get; private set; } |
| 94 | + |
| 95 | + public Task<SpeechToTextResponse> GetTextAsync(System.IO.Stream audio, SpeechToTextOptions? options, CancellationToken cancellationToken = default) |
| 96 | + { |
| 97 | + InvocationCount++; |
| 98 | + return Task.FromResult(new SpeechToTextResponse(transcript)); |
| 99 | + } |
| 100 | + |
| 101 | + IAsyncEnumerable<SpeechToTextResponseUpdate> ISpeechToTextClient.GetStreamingTextAsync(System.IO.Stream audio, SpeechToTextOptions? options, CancellationToken cancellationToken) |
| 102 | + { |
| 103 | + throw new NotSupportedException(); |
| 104 | + } |
| 105 | + |
| 106 | + public object? GetService(Type serviceType, object? serviceKey = null) => null; |
| 107 | + |
| 108 | + public void Dispose() |
| 109 | + { |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private sealed class SpeechAnnotationMiddleware(byte[] audioBytes, Action onInvoke) : IConversionMiddleware |
| 114 | + { |
| 115 | + public async Task InvokeAsync(ConversionPipelineContext context, CancellationToken cancellationToken) |
| 116 | + { |
| 117 | + onInvoke(); |
| 118 | + var speechClient = context.AiModels.SpeechToTextClient; |
| 119 | + if (speechClient is null || audioBytes.Length == 0) |
| 120 | + { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + await using var buffer = new System.IO.MemoryStream(audioBytes, writable: false); |
| 125 | + var response = await speechClient.GetTextAsync(buffer, new SpeechToTextOptions |
| 126 | + { |
| 127 | + ModelId = "gpt-4o-transcribe", |
| 128 | + SpeechLanguage = "en-US" |
| 129 | + }, cancellationToken); |
| 130 | + |
| 131 | + if (string.IsNullOrWhiteSpace(response.Text)) |
| 132 | + { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + context.Segments.Add(new DocumentSegment( |
| 137 | + markdown: response.Text!, |
| 138 | + type: SegmentType.Audio, |
| 139 | + label: "AI Speech Transcript")); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | +#pragma warning restore MEAI001 |
0 commit comments