Markdown 对于文本生成和大型语言模型(LLMs)在训练和推理中的应用是必不可少的,因为它可以提供结构化、语义化、人类可读和机器可读的输入。同样,Markdown 有助于将输入数据分块和结构化,以便在 RAGs(检索增强生成)的上下文中更好地检索和合成,并且其简洁性和易于解析和渲染的特点使其成为 AI 代理的理想选择。
由于这些原因,文档转换在设计和开发 AI 应用程序时起着重要作用。Workers AI 提供了toMarkdown
实用方法,开发人员可以从env.AI
绑定或 REST API 中使用,以快速、简单和方便地将多种格式的文档转换为 Markdown 语言。
方法和定义
async env.AI.toMarkdown()
将不同格式的文档列表转换为 Markdown。
参数
documents
: 数组- 一个包含
toMarkdownDocument
的数组。
- 一个包含
返回值
results
: 数组- 一个
toMarkdownDocumentResult
数组。
- 一个
toMarkdownDocument
定义
name
string- 要转换的文档名称。
blob
Blob- 一个新的包含文档内容的 Blob
对象。
- 一个新的包含文档内容的 Blob
toMarkdownDocumentResult
的定义
name
string- 转换后的文档名称。与输入名称匹配。
mimetype
string- 检测到的 文档类型
。
- 检测到的 文档类型
tokens
数量- 转换文档的估计词令牌数。
data
string- 转换后的文档内容以 Markdown 格式呈现。
支持的格式
- 转换后的文档内容以 Markdown 格式呈现。
这是支持格式的列表。我们不断添加新的格式并更新此表格。
格式 | 文件扩展名 | MIME 类型 |
---|---|---|
PDF 文档 | .pdf |
application/pdf |
图片 1 | .jpeg , .jpg , .png , .webp , .svg |
image/jpeg , image/png , image/webp , image/svg+xml |
HTML 文档 | .html |
text/html |
XML 文档 | .xml |
应用/xml |
Microsoft Office 文档 | .xlsx , .xlsm , .xlsb , .xls , .et |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet , application/vnd.ms-excel.sheet.macroenabled.12 , application/vnd.ms-excel.sheet.binary.macroenabled.12 , application/vnd.ms-excel , application/vnd.ms-excel |
开放文档格式 | .ods |
application/vnd.oasis.opendocument.spreadsheet |
CSV | .csv |
text/csv |
Apple 文档 | .numbers |
application/vnd.apple.numbers |
1 图像转换使用两个 Workers AI 模型进行对象检测和总结。如需更多详情,请参见定价。
示例
在这个示例中,我们从 R2 获取一个 PDF 文档和一个图像,并将它们都传递给env.AI.toMarkdown
。结果是一个转换后的文档列表。Workers AI 模型会自动检测并总结图像。
import { Env } from "./env";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
// https://pub-979cb28270cc461d94bc8a169d8f389d.r2.dev/somatosensory.pdf
const pdf = await env.R2.get('somatosensory.pdf');
// https://pub-979cb28270cc461d94bc8a169d8f389d.r2.dev/cat.jpeg
const cat = await env.R2.get('cat.jpeg');
return Response.json(
await env.AI.toMarkdown([
{
name: "somatosensory.pdf",
blob: new Blob([await pdf.arrayBuffer()], { type: "application/octet-stream" }),
},
{
name: "cat.jpeg",
blob: new Blob([await cat.arrayBuffer()], { type: "application/octet-stream" }),
},
]),
);
},
};
这是结果:
[
{
"name": "somatosensory.pdf",
"mimeType": "application/pdf",
"format": "markdown",
"tokens": 0,
"data": "# somatosensory.pdf\n## Metadata\n- PDFFormatVersion=1.4\n- IsLinearized=false\n- IsAcroFormPresent=false\n- IsXFAPresent=false\n- IsCollectionPresent=false\n- IsSignaturesPresent=false\n- Producer=Prince 20150210 (www.princexml.com)\n- Title=Anatomy of the Somatosensory System\n\n## Contents\n### Page 1\nThis is a sample document to showcase..."
},
{
"name": "cat.jpeg",
"mimeType": "image/jpeg",
"format": "markdown",
"tokens": 0,
"data": "The image is a close-up photograph of Grumpy Cat, a cat with a distinctive grumpy expression and piercing blue eyes. The cat has a brown face with a white stripe down its nose, and its ears are pointed upright. Its fur is light brown and darker around the face, with a pink nose and mouth. The cat's eyes are blue and slanted downward, giving it a perpetually grumpy appearance. The background is blurred, but it appears to be a dark brown color. Overall, the image is a humorous and iconic representation of the popular internet meme character, Grumpy Cat. The cat's facial expression and posture convey a sense of displeasure or annoyance, making it a relatable and entertaining image for many people."
}
]
REST API
此外,除了 Workers AI 绑定,你还可以使用 REST API:
Terminal window
curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/tomarkdown \ -H 'Authorization: Bearer {API_TOKEN}' \ -F "[email protected]" \ -F "[email protected]"
定价
toMarkdown 是免费的,用于大多数格式转换。在某些情况下,如图像转换,它可能会使用 Workers AI 模型进行对象检测和总结,如果超出 Workers AI 免费配额限制,则可能会产生额外费用。请参阅定价页面获取更多信息。