// 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' } }); } };