Workers Binding
Cloudflare’s serverless platform allows you to run code at the edge to build full-stack applications with Workers. A binding enables your Worker or Pages Function to interact with resources on the Cloudflare Developer Platform.
To use our Markdown Conversion service directly from your Workers, create an AI binding either in the Cloudflare dashboard (refer to AI bindings for instructions), or you can update your Wrangler file. Add the following to your Wrangler file:
{ "$schema": "./node_modules/wrangler/config-schema.json", "ai": { "binding": "AI" }}[ai]binding = "AI" # i.e. available in your Worker on env.AIIn this example, we fetch a PDF document and an image from R2 and feed them both to env.AI.toMarkdown. The result is a list of converted documents. Workers AI models are used automatically to detect and summarize the image.
import { Env } from "./env";
export default { async fetch(request, env, ctx) { // 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/pdf", }), }, { name: "cat.jpeg", blob: new Blob([await cat.arrayBuffer()], { type: "image/jpeg", }), }, ]), ); },};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/pdf", }), }, { name: "cat.jpeg", blob: new Blob([await cat.arrayBuffer()], { type: "image/jpeg", }), }, ]), ); },};import { Env } from "./env";
export default { async fetch(request, env, ctx) { return Response.json(await env.AI.toMarkdown().supported()); },};import { Env } from "./env";
export default { async fetch(request: Request, env: Env, ctx: ExecutionContext) { return Response.json(await env.AI.toMarkdown().supported()); },};Takes a document or list of documents in different formats and converts them to Markdown.
const result = await env.AI.toMarkdown({ name: "document.pdf", blob: new Blob([documentBuffer]),});const result = await env.AI.toMarkdown({ name: "document.pdf", blob: new Blob([documentBuffer]),});-
files: MarkdownDocument | MarkdownDocument[]- an instance of or an array ofMarkdownDocuments. -
conversionOptions: ConversionOptions- options that control how conversion happens. See Conversion Options for further details.
results: Promise<ConversionResult | ConversionResult[]>- An instance of or an array ofConversionResults.
-
namestring- Name of the document to convert.
-
blobBlob- A new Blob ↗ object with the document content.
-
idstring- ID associated to this object.
-
namestring- Name of the converted document. Matches the input name.
-
format'markdown' | 'error'- The format of this
ConversionResultobject
- The format of this
-
mimetypestring- The detected mime type ↗ of the document.
-
tokensnumber- The estimated number of tokens of the converted document. Only present if
formatis equal tomarkdown.
- The estimated number of tokens of the converted document. Only present if
-
datastring- The content of the converted document in Markdown format. Only present if
formatis equal tomarkdown.
- The content of the converted document in Markdown format. Only present if
-
errorstring- The error message explaining why this conversion failed. Only present if
formatis equal toerror.
- The error message explaining why this conversion failed. Only present if
This method is similar to env.AI.toMarkdown except that it is exposed through a new handle. It takes the same arguments and returns the same values.
const result = await env.AI.toMarkdown().transform({ name: "document.pdf", blob: new Blob([documentBuffer]),});const result = await env.AI.toMarkdown().transform({ name: "document.pdf", blob: new Blob([documentBuffer]),});Returns a list of file formats that are currently supported for markdown conversion. See Supported formats for the full list of file formats that can be converted into Markdown.
const formats = await env.AI.toMarkdown().supported();const formats = await env.AI.toMarkdown().supported();results: SupportedFormat[]- An array of all formats supported for markdown conversion.
-
extensionstring- Extension of files in this format.
-
mimeTypestring- The mime type ↗ of files of this format