Skip to content

Instantly share code, notes, and snippets.

@Lulzx
Forked from charlesteh/cf-workers-ai-bge-small.js
Created January 29, 2024 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lulzx/1de78876861c17ea524dd30c0e4252ec to your computer and use it in GitHub Desktop.
Save Lulzx/1de78876861c17ea524dd30c0e4252ec to your computer and use it in GitHub Desktop.
Cloudflare Workers AI baai/bge-small deployment script
// 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