INTEGRITY Cloudflare Docs

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.AI

Examples

Converting files

In 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",
					}),
				},
			]),
		);
	},
};

Getting supported file formats

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());
	},
};

Methods

async env.AI.toMarkdown()

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]),
});

Parameter

Return values

MarkdownDocument definition

ConversionResult definition

async env.AI.toMarkdown().transform()

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]),
});

async env.AI.toMarkdown().supported()

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();

Return values

SupportedFormat definition