This commit is contained in:
wangjiming
2026-08-19 10:33:06 +08:00
11 changed files with 466 additions and 14 deletions

View File

@@ -104,7 +104,9 @@ def extract_pdf_page_texts(raw: bytes) -> tuple[PdfPageText, ...]:
)
has_text = True
if not has_text:
raise ValueError("PDF contains no extractable text; scanned PDF requires OCR")
raise ValueError(
"PDF contains no extractable text; scanned or image-only PDF files are not supported"
)
return tuple(pages)
def _pdf_page_lines(page: PdfPageText) -> tuple[_PdfLine, ...]:

View File

@@ -90,8 +90,10 @@ def _tokenizer() -> tiktoken.Encoding:
token, rank = line.split()
mergeable_ranks[base64.b64decode(token)] = int(rank)
# 构造 Encoding 对象
import tiktoken.core
# 构造 Encoding 对象(模块顶部已 import tiktoken
# 此处不能再 import tiktoken.core否则会把 tiktoken
# 变成局部变量,使函数开头的 tiktoken.get_encoding 抛
# UnboundLocalError
return tiktoken.core.Encoding(
name="cl100k_base",
pat_str=r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+""",
@@ -260,9 +262,17 @@ def _make_text_chunk(source: str, start: int, end: int) -> DocumentChunk:
@lru_cache(maxsize=1)
def _document_converter():
from docling.document_converter import DocumentConverter
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
return DocumentConverter()
pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = False
return DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options),
}
)
class _MarkdownSerializerProvider(ChunkingSerializerProvider):

View File

@@ -45,6 +45,25 @@ MINIMAX_M3_API_HOSTS = {"api.minimax.io", "api.minimaxi.com"}
MINIMAX_M3_MIN_COMPLETION_TOKENS = 4096
logger = logging.getLogger(__name__)
# 问题表述风格规则:防止模型产出“请描述/请说明”式模板化问句。
_QUESTION_STYLE_RULE = (
"各条问题必须覆盖不同的信息点并使用不同的句式,只替换关键词套用同一句式视为重复。"
"问题表述要像真实用户自然提出的问题:具体、口语化、直奔信息点,"
"避免“请描述”“请说明”“根据文档”等模板化开头,"
"也不要把原文句子直接改成问句;多条问题时交替使用直接疑问、场景式提问、追问式等句式。"
"表述示例(仅示意风格,不要照搬内容):"
"避免——“请描述系统的权限控制机制”;"
"推荐——“不同角色能看到的菜单不一样,平台是怎么控制的?”"
)
# 任务配置未提供提示语时的兜底,与前端内置默认提示语保持同等信息量。
_DEFAULT_GENERATION_PROMPT = (
"你是一名专业的数据生成专家。请基于来源内容生成高质量、"
"可直接用于监督微调的问答数据:问题聚焦核心信息点、"
"表述像真实用户自然提出的问题,具体、口语化,多条问题使用不同句式;"
"答案严格依据来源内容,准确、完整、语言自然,不引入来源之外的信息。"
)
def _is_retryable_generation_error(exc: Exception) -> bool:
if isinstance(exc, _TerminalModelGenerationError):
@@ -310,11 +329,11 @@ def _prompt_messages(
)
schema_instruction = (
f"必须只返回 JSON 对象,格式为 {schema}items 必须包含 {count} 条。"
f"这是总计 {total_count} 条中的第 {start_index}-{end_index}"
"各条必须使用不同的提问角度和表述,避免重复。"
f"{output_rule}不要输出 Markdown 代码围栏或 JSON 之外的说明。"
f"这是总计 {total_count} 条中的第 {start_index}-{end_index}"
f"{_QUESTION_STYLE_RULE}{output_rule}"
"不要输出 Markdown 代码围栏或 JSON 之外的说明。"
)
base_prompt = normalize_text(prompt) or "请根据来源内容生成可用于监督微调的问答数据。"
base_prompt = normalize_text(prompt) or _DEFAULT_GENERATION_PROMPT
if "{{ content }}" in base_prompt:
user_prompt = base_prompt.replace("{{ content }}", content)
return [