镜像自地址
https://github.com/binary-husky/gpt_academic.git
已同步 2025-12-08 07:26:48 +00:00
RAG interactive prompts added, issues resolved
这个提交包含在:
@@ -59,7 +59,7 @@ class SaveLoad():
|
||||
def purge(self):
|
||||
import shutil
|
||||
shutil.rmtree(self.checkpoint_dir, ignore_errors=True)
|
||||
self.vs_index = self.create_new_vs()
|
||||
self.vs_index = self.create_new_vs(self.checkpoint_dir)
|
||||
|
||||
|
||||
class LlamaIndexRagWorker(SaveLoad):
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import os
|
||||
from llama_index.core import SimpleDirectoryReader
|
||||
|
||||
import markdown
|
||||
# 保留你原有的自定义解析函数
|
||||
from PyPDF2 import PdfReader
|
||||
from bs4 import BeautifulSoup
|
||||
from docx import Document as DocxDocument
|
||||
|
||||
|
||||
def extract_text_from_pdf(file_path):
|
||||
reader = PdfReader(file_path)
|
||||
@@ -13,35 +11,24 @@ def extract_text_from_pdf(file_path):
|
||||
text += page.extract_text() + "\n"
|
||||
return text
|
||||
|
||||
def extract_text_from_docx(file_path):
|
||||
doc = DocxDocument(file_path)
|
||||
return "\n".join([para.text for para in doc.paragraphs])
|
||||
|
||||
def extract_text_from_txt(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
return f.read()
|
||||
|
||||
def extract_text_from_md(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
md_content = f.read()
|
||||
return markdown.markdown(md_content)
|
||||
|
||||
def extract_text_from_html(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
soup = BeautifulSoup(f, 'html.parser')
|
||||
return soup.get_text()
|
||||
|
||||
# 修改后的 extract_text 函数,结合 SimpleDirectoryReader 和自定义解析逻辑
|
||||
def extract_text(file_path):
|
||||
_, ext = os.path.splitext(file_path.lower())
|
||||
|
||||
# 使用 SimpleDirectoryReader 处理它支持的文件格式
|
||||
if ext in ['.txt', '.md', '.pdf', '.docx', '.html']:
|
||||
try:
|
||||
reader = SimpleDirectoryReader(input_files=[file_path])
|
||||
documents = reader.load_data()
|
||||
if len(documents) > 0:
|
||||
return documents[0].text
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
# 如果 SimpleDirectoryReader 失败,或文件格式不支持,使用自定义解析逻辑
|
||||
if ext == '.pdf':
|
||||
return extract_text_from_pdf(file_path)
|
||||
elif ext in ['.docx', '.doc']:
|
||||
return extract_text_from_docx(file_path)
|
||||
elif ext == '.txt':
|
||||
return extract_text_from_txt(file_path)
|
||||
elif ext == '.md':
|
||||
return extract_text_from_md(file_path)
|
||||
elif ext == '.html':
|
||||
return extract_text_from_html(file_path)
|
||||
else:
|
||||
raise ValueError(f"Unsupported file extension: {ext}")
|
||||
try:
|
||||
return extract_text_from_pdf(file_path)
|
||||
except Exception as e:
|
||||
pass
|
||||
return None
|
||||
|
||||
在新工单中引用
屏蔽一个用户