Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the error caused by file format when docx loading files #2061

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions dbgpt/rag/knowledge/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Any, Dict, List, Optional, Union

import docx
from docx.opc.oxml import parse_xml
from docx.opc.pkgreader import _SerializedRelationship, _SerializedRelationships

from dbgpt.core import Document
from dbgpt.rag.knowledge.base import (
Expand All @@ -12,6 +14,21 @@
)


def load_from_xml_v2(base_uri, rels_item_xml):
"""Return |_SerializedRelationships| instance loaded with the relationships.

contained in *rels_item_xml*.collection if *rels_item_xml* is |None|.
"""
srels = _SerializedRelationships()
if rels_item_xml is not None:
rels_elm = parse_xml(rels_item_xml)
for rel_elm in rels_elm.Relationship_lst:
if rel_elm.target_ref in ("../NULL", "NULL"):
continue
srels._srels.append(_SerializedRelationship(base_uri, rel_elm))
return srels


class DocxKnowledge(Knowledge):
"""Docx Knowledge."""

Expand Down Expand Up @@ -47,8 +64,10 @@ def _load(self) -> List[Document]:
documents = self._loader.load()
else:
docs = []
_SerializedRelationships.load_from_xml = load_from_xml_v2 # type: ignore
doc = docx.Document(self._path)
content = []

for i in range(len(doc.paragraphs)):
para = doc.paragraphs[i]
text = para.text
Expand Down
Loading