JinaAI ReaderLM
Jina AI 推出的 Reader-LM 是一系列将 HTML 内容转换为 Markdown 内容的模型,可用于内容转换任务。该模型在精选的 HTML 内容集合及其对应的 Markdown 内容上进行训练,可以将原始 HTML 转换为 markdown 或 JSON 的小型语言模型。
输入 → 文本 HTML → 输出 → 文本 Markdown → 文本 JSON
ReaderLM-v2 是一个 1.5B 参数语言模型,可将原始 HTML 转换为 markdown 或 JSON,处理最多 512K 个词元组合输入/输出长度,支持 29 种语言。与将 HTML 到 markdown 视为“选择性复制”任务的前身不同,v2 将其视为翻译过程,从而能够出色地处理代码围栏、嵌套列表、表格和 LaTeX 方程式等复杂元素。该模型在不同的上下文长度下保持一致的性能,并引入了具有预定义架构的直接 HTML 到 JSON 生成功能。
ReaderLM I / O 图
ReaderLM I / O 图
ReaderLM I / O 图
ReaderLM-v2 以 Qwen2.5-1.5B-Instruction 为基础,其训练涉及 html-markdown-1m 数据集,其中包含一百万个 HTML 文档,平均每个文档有 56,000 个词元。训练过程包括:1) 使用 ring-zag 注意力和 RoPE 进行长上下文预训练,将上下文从 32K 扩展到 256K 个词元,2) 使用精炼数据集进行监督微调,3) 直接偏好优化以实现输出对齐,以及 4) 自我游戏强化调整。数据准备遵循由 Qwen2.5-32B-Instruction 提供支持的三步流程(草稿-精炼-批评),使用针对特定任务训练的专用模型,然后通过线性参数插值进行合并。
在综合基准测试中,ReaderLM-v2 在 HTML 到 Markdown 任务上的表现优于 Qwen2.5-32B-Instruct 和 Gemini2-flash-expr 等大型模型。对于主要内容提取,它实现了 0.84 的 ROUGE-L、0.82 的 Jaro-Winkler,并且与竞争对手相比,Levenshtein 距离 (0.22) 明显更低。在 HTML 到 JSON 任务中,它保持了具有竞争力的性能,F1 得分为 0.81,通过率为 98%。该模型在 T4 GPU 上以 67 个 token/s 的输入和 36 个 token/s 的输出进行处理,通过对比损失训练显著减少了退化问题。
<html> <body> <h3>Why is the sky blue?</h3> <p>The sky appears blue because of the way light from the sun is reflected by the atmosphere. The atmosphere is made up of gases, including nitrogen and oxygen, which scatter light in all directions. This scattering causes the sunlight to appear as a rainbow of colors, with red light scattered more than other colors. </p> </body> </html>
JinaAI reader-lm 输出 markdown
### Why is the sky blue? The sky appears blue because of the way light from the sun is reflected by the atmosphere. The atmosphere is made up of gases, including nitrogen and oxygen, which scatter light in all directions. This scattering causes the sunlight to appear as a rainbow of colors, with red light scattered more than other colors.
pip install transformers<=4.43.4
# pip install transformers from transformers import AutoModelForCausalLM, AutoTokenizer checkpoint = "jinaai/reader-lm-1.5b" device = "cuda" # for GPU usage or "cpu" for CPU usage tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device) # example html content html_content = "<html><body><h1>Hello, world!</h1></body></html>" messages = [{"role": "user", "content": html_content}] input_text=tokenizer.apply_chat_template(messages, tokenize=False) print(input_text) inputs = tokenizer.encode(input_text, return_tensors="pt").to(device) outputs = model.generate(inputs, max_new_tokens=1024, temperature=0, do_sample=False, repetition_penalty=1.08) print(tokenizer.decode(outputs[0]))