DEV Community

Luis
Luis

Posted on • Updated on

Plugin for Cloudflare AI API

I created a module for @luisfun/cloudflare-ai-plugin, and this is its introduction.

What I Built

This plugin enables you to write REST API or AI Gateway in the same way as binding AI (env.AI.run) when using Cloudflare's Workers AI.
Also, it supports Typescript.

How to Use

Install

npm i @luisfun/cloudflare-ai-plugin
npm i -D @cloudflare/ai # When using TypeScript
Enter fullscreen mode Exit fullscreen mode

Coding

import { Ai } from '@luisfun/cloudflare-ai-plugin'

// const ai = env.AI
const ai = new Ai(env.AI_API_URL, env.AI_API_TOKEN)

const response = await ai.run(model, inputs)
Enter fullscreen mode Exit fullscreen mode

How to get API URL and TOKEN

There are also some extensions available.
Here, I will include the cache control of the Gateway.
If you are interested in other extensions, please check the repository.

const options = {
  'cf-skip-cache': true,
  'cf-cache-ttl': 60,
}
const response = await ai.run(model, inputs, options)
Enter fullscreen mode Exit fullscreen mode

Repository

https://github.com/LuisFun/cloudflare-ai-plugin

Top comments (0)