@luisfun/cloudflare-ai-plugin
v0.2.4
Published
This plugin enables you to code REST API or AI Gateway in the same way as the binding AI when using Cloudflare's Workers AI
Downloads
30
Maintainers
Readme
Cloudflare AI Plugin
- REST API or AI Gateway - You can code the same way as the binding AI.
- MD Translator - You can translate while maintaining the structure of Markdown to some extent.
- TypeScript - TypeScript support.
Install
npm i @luisfun/cloudflare-ai-plugin
npm i -D @cloudflare/ai # When using TypeScript
How to Use
import { Ai } from '@luisfun/cloudflare-ai-plugin'
// const ai = env.AI
// const ai = new Ai(env.AI) // When extending binding AI
const ai = new Ai(env.AI_API_URL, env.AI_API_TOKEN)
const response = await ai.run(model, inputs)
Extension
API Options
Gateway: cf-cache
const options = {
'cf-skip-cache': true, // Gateway
'cf-cache-ttl': 60, // Gateway
timeout: 30000, // fetch timeout (millisecond)
}
const response = await ai.run(model, inputs, options)
MD Translator
⚠️ AI translation requests will increase.
// const { translated_text } = await ai.run('@cf/meta/m2m100-1.2b', inputs)
const { translated_text } = await ai.mdt('@cf/meta/m2m100-1.2b', inputs)
fetch Response Object
// response is Response Object
// outputs is the same `await ai.run()` outputs
const { response, outputs } = await ai.fetch(model, inputs)
Builder
URL Builder
import { Ai, restUrl, gatewayUrl } from '@luisfun/cloudflare-ai-plugin'
const restAi = new Ai(restUrl(env.ACCOUNT_ID), env.AI_API_TOKEN)
const gatewayAi = new Ai(gatewayUrl(env.ACCOUNT_ID, env.GATEWAY_SLUG), env.AI_API_TOKEN)
Examples
References
The following repositories were referenced in the making of this project: