Last active
March 22, 2025 09:38
-
-
Save charlesteh/723f2daf51b041287e02b9f89c1e02c7 to your computer and use it in GitHub Desktop.
Cloudflare Workers AI baai/bge-small deployment script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Made by @charlestehio: https://x.com/charlestehio | |
// Usage: https://abc.workers.dev/?query=your%20embedding%20query | |
import { Ai } from './vendor/@cloudflare/ai.js'; | |
export default { | |
async fetch(request, env) { | |
// Parse the URL to get query parameters | |
const url = new URL(request.url); | |
var query = url.searchParams.get('query'); | |
// Check if the query parameter exists and is not empty | |
// If the query parameter does not exist or is empty, return {"response": null} | |
if (!query) { | |
return new Response(JSON.stringify({ response: null }), { | |
status: 400, | |
headers: { 'Content-Type': 'application/json' } | |
}); | |
} | |
// Clean the query using regex. This regex filters the query string to remove any characters | |
// that are not letters (a-z, A-Z), numbers (0-9), whitespace (spaces, tabs, etc.), commas, or periods. | |
// For example, if the input query is "Hello! How are you? #Cloudflare", the regex will remove | |
// the exclamation mark (!), question mark (?), and hash (#), resulting in the cleaned query | |
// "Hello How are you Cloudflare". | |
query = query.replace(/[^a-zA-Z0-9\s,\.]/g, '').trim(); | |
const ai = new Ai(env.AI); | |
const input = { | |
text: `${query}` | |
}; | |
const response = await ai.run('@cf/baai/bge-small-en-v1.5', input); | |
return new Response(JSON.stringify({ response }), { headers: { 'Content-Type': 'application/json' } }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment