- 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.
npm i @luisfun/cloudflare-ai-plugin
npm i -D @cloudflare/ai # When using TypeScript
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)
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
// 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)
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)
The following repositories were referenced in the making of this project: