microsoft / kernel-memory

RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
https://microsoft.github.io/kernel-memory
MIT License
1.6k stars 308 forks source link

[Bug] Ingesting docx file: DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document. Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package. #382

Closed mamift closed 7 months ago

mamift commented 7 months ago

Context / Scenario

So I'm using the serverless implementation just for testing and I get this error on ingesting word documents that have links with the wrong backslash.

Method used: IKernelMemory.ImportDocumentAsync() (any of the overloads)

What happened?

So ingesting documents returns this error when the documents have links with the wrong slash:

DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document. Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package.

So for instance: 'http://google.com' is the properly formatted URI but users have (and in lots of documents) entered it as 'http:\\google.com'. In my case, there are lots of DOCX files with many links formatted using the wrong slash.

I filed this as a bug because, obviously when a user uses Microsoft Word to enter these wrong-URIs, no error is thrown otherwise these bad-URI links wouldn't be in the documents in the first place. This is also very easy to replicate as well, just create a new Word document and add a 'https:\\' link to some text and try to run any of the IKernelMemory.ImportDocumentAsync() method overloads.

Importance

a fix would make my life easier

Platform, Language, Versions

Kernel memory: 0.32.240307.3+68ee7ef Being used in an app which is: .NET 8, Blazor server, C#, Visual Studio 17.9

Relevant log output

DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document. Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package.
 ---> System.UriFormatException: Invalid URI: The hostname could not be parsed.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString, UriKind uriKind)
   at System.IO.Packaging.InternalRelationshipCollection.ProcessRelationshipAttributes(XmlCompatibilityReader reader)
   at System.IO.Packaging.InternalRelationshipCollection.ParseRelationshipPart(PackagePart part)
   at System.IO.Packaging.InternalRelationshipCollection..ctor(Package package, PackagePart part)
   at System.IO.Packaging.InternalRelationshipCollection..ctor(PackagePart part)
   at System.IO.Packaging.PackagePart.EnsureRelationships()
   at System.IO.Packaging.PackagePart.GetRelationshipsHelper(String filterString)
   at System.IO.Packaging.PackagePart.GetRelationships()
   at DocumentFormat.OpenXml.Packaging.PackagePartRelationshipPropertyCollection..ctor(PackagePart packagePart, IOpenXmlNamespaceResolver resolver)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id, Dictionary`2 loadedParts)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart sourcePart, RelationshipCollection relationshipCollection, Dictionary`2 loadedParts)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load(Package package)
   --- End of inner exception stack trace ---
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load(Package package)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage..ctor(Package package, OpenSettings settings)
   at DocumentFormat.OpenXml.Packaging.TypedOpenXmlPackage..ctor(Package package, OpenSettings settings)
   at DocumentFormat.OpenXml.Packaging.WordprocessingDocument..ctor(Package package, OpenSettings settings)
   at DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(Stream stream, Boolean isEditable, OpenSettings openSettings)
   at DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(Stream stream, Boolean isEditable)
   at Microsoft.KernelMemory.DataFormats.Office.MsWordDecoder.ExtractContent(Stream data)
   at Microsoft.KernelMemory.DataFormats.Office.MsWordDecoder.ExtractContent(BinaryData data)
   at Microsoft.KernelMemory.Handlers.TextExtractionHandler.ExtractTextAsync(FileDetails uploadedFile, BinaryData fileContent, CancellationToken cancellationToken)
   at Microsoft.KernelMemory.Handlers.TextExtractionHandler.InvokeAsync(DataPipeline pipeline, CancellationToken cancellationToken)
   at Microsoft.KernelMemory.Pipeline.InProcessPipelineOrchestrator.RunPipelineAsync(DataPipeline pipeline, CancellationToken cancellationToken)
   at Microsoft.KernelMemory.Pipeline.BaseOrchestrator.ImportDocumentAsync(String index, DocumentUploadRequest uploadRequest, CancellationToken cancellationToken)
   at AiPrototype.Features.DocumentIngestion.DocumentMemoryService.IngestImanageDoc(String docNumber, CancellationToken cancel) in C:\azagent\_work\62\s\AiPrototype\AiPrototype\Features\DocumentIngestion\DocumentMemoryService.cs:line 130
   at AiPrototype.Components.IngestDmsDoc.IngestDocBtnHandler() in C:\azagent\_work\62\s\AiPrototype\AiPrototype\Components\IngestDmsDoc.razor:line 50
   at BootstrapBlazor.Components.Button.HandlerClick()
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at BootstrapBlazor.Components.Button.<SetClickHandler>b__14_0()
   at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at BootstrapBlazor.Components.BootstrapComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
marcominerva commented 7 months ago

I have just tried and I get no exception with a DOCX file that contains an URI like http:\\google.com. I'm using Kernel Memory v0.35.240318.1.

If you still experience the issue, could you please attach a file that showcases the problem?

mamift commented 7 months ago

OK did a bit of more digging. Recent versions of Word (don't know the earliest version exactly,) are autocorrecting the wrong slash with https:\\ and http:\\ protocol prefixes before saving the file. If you use a non-standard protocol prefix, such as quic:\\ or test:\\, Word won't auto correct and persist the \ in the file without allowing you to re-create the exception.

It seems the DOCX files I'm working with go back up to 20 years, and have been made using a range of different Word versions, such as Word 2007, 2010 (2011 for Mac), 2013, 2016, 2019, and 365 versions etc. And the exact pattern that triggers the error has to have at least 3 wrong slashes with non-whitespace characters after the 3rd slash. So these will trigger the error,

quic:\\1234\456
wss:\\blah\12

Strangely the following don't trigger the error; there needs to be trailing non-whitespace chars after a 3rd \:

quic:\\1234\
wss:\\blah\
http:\\intranet\

Edit: OK actually here's an example of a docx file that triggers it on my end. The files I'm working with have links to a specific custom document management system protocol:

bad uri.docx

marcominerva commented 7 months ago

Hi @mamift. With your file, I was able to recreate the problem.

This is an issue of the OpenXml library itself (see https://github.com/dotnet/Open-XML-SDK/issues/715). It seems to be resolved in version 3.x, but unfortunately ClosedXML (that is used to read Excel files), is still on version 2.x. A fix for this has been merged (https://github.com/ClosedXML/ClosedXML/pull/2248), but it has not been published yet. According to the milestone page (https://github.com/ClosedXML/ClosedXML/milestone/26?closed=1), it will be available in the next 0.104 version.

dluc commented 7 months ago

The latest ClosedXML 0.102.2 added a warning in case one uses a non-supported version of OpenXML, see https://github.com/ClosedXML/ClosedXML/pull/2246

Closing this issue since there's nothing we can do until https://github.com/ClosedXML/ClosedXML/issues/2220 is addressed and a new nuget is released. I would suggest following up with ClosedXML project, e.g. supporting with PRs and tests where possible.

mamift commented 7 months ago

OK well for anyone else who comes across this same issue, there's a workaround: just bulk remove all links in the document and keep the text. I wrote an extension method for it:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace Program.Extensions;

public static class OpenXmlExtensions
{
    /// <summary>
    /// Assuming the current <paramref name="docStream"/> is a <see cref="WordprocessingDocument"/>, bulk flatten all hyperlinks (remove the link, but keep the text).
    /// </summary>
    /// <param name="docStream"></param>
    public static void BulkRemoveHyperlinks(this Stream docStream)
    {
        using var doc = WordprocessingDocument.Open(docStream, true, new OpenSettings() {
            AutoSave = true
        });

        doc.BulkRemoveHyperlinks();
    }

    /// <summary>
    /// For the current <see cref="WordprocessingDocument"/>, remove the links on all in-document hyperlinks but keep the text inline.
    /// </summary>
    /// <param name="doc"></param>
    public static void BulkRemoveHyperlinks(this WordprocessingDocument doc)
    {
        if (doc.MainDocumentPart?.Document.Body == null) return;

        foreach (var link in doc.MainDocumentPart.Document.Body.Descendants<Hyperlink>()) {
            if (!string.IsNullOrEmpty(link.Id)) {
                var hr = doc.MainDocumentPart.HyperlinkRelationships.Single(hr => hr.Id == link.Id);
                doc.MainDocumentPart.DeleteReferenceRelationship(hr.Id);
            }

            var linkText = link.InnerText;
            var run = new Run(new Text(linkText));
            link.Parent?.ReplaceChild(run, link);
        }

        if (!doc.AutoSave) {
            doc.Save();
        }
    }
}