Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

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

License

Notifications You must be signed in to change notification settings

luisfun/cloudflare-ai-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare AI Plugin npm v Bundle Size

  • 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)

How to get API URL and TOKEN

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:

About

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

Topics

Resources

License

Stars

Watchers

Forks