DEV Community

Luis
Luis

Posted on • Edited 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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more