google的vertex ai api如何在cloudflare的worker上作为代理访问?
网友回复
vertex ai有个模型广场,里面有70+模型可以调用,注册google cloud送90天的150美元免费额度。可以白嫖里面的ai模型。
记得先要创建一个项目,然后把开通service account,获取json文件的key,忘记的可以看这个教程点击打开链接
我们以google自己的gemini为例,把他作为代理部署在cloudflare的worker上,其他的模型大同小异,直接把相关的url参数修改一下就好了。
cloudflare的worker代码:
// 配置变量 const API_KEY ="你设定的key"; const PROJECT_ID = ''; const CLIENT_EMAIL = ''; const PRIVATE_KEY =""; const API_ENDPOINT="us-central1-aiplatform.googleapis.com" const LOCATION_ID="us-central1" addEventListener("fetch", (event) => { event.respondWith(handleRequest(event.request)); }); async function handleRequest(request) { let headers = new Headers({ "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", }); if (request.method === "OPTIONS") { return new Response(null, { headers }); } else if (request.method === "GET") { return new Response("404 Not Found", { status: 404 }); // return createErrorResponse(405, "invalid_request_error", "GET method is not allowed"); } const apiKey = request.headers.get("x-api-key"); if (!API_KEY || API_KEY !== apiKey) { return createErrorResponse(401, "authentication_error", "invalid x-api-key"); } const signedJWT = await createSignedJWT(CLIENT_EMAIL, PRIVATE_KEY) const [token, err] = await exchangeJwtForAccessToken(signedJWT) if (token === null) { console.log(`Invalid jwt token: ${err}`) return createErrorResponse(500, "api_error", "invalid authentication credentials"); } try { return handleMessagesEndpoint(request, token); } catch (error) { console.error(error); return createErrorResponse(500, "api_error", "An unexpected error occurred"); } } async function handleMessagesEndpoint(request, api_token) { let payload; try { payload = await request.json(); } catch (err) { return createErrorResponse(400, "invalid_request_error", "The request body is not valid JSON."); } const stream = payload.stream || false; const model = payload.model; const url = `https://${API_ENDPOINT}/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/publishers/google/models/${model}:streamGenerateContent`; delete payload.model; delete payload.stream; let response, contentType try { response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${api_token}` }, body: JSON.stringify(payload) }); contentType = response.headers.get("Content-Type") || "application/json"; } catch (error) { return createErrorResponse(500, "api_error", "Server Error"); } if (stream && contentType.startsWith('text/event-stream')) { if (!(response.body instanceof ReadableStream)) { return createErrorResponse(500, "api_error", "Server Error"); } const encoder = new TextEncoder(); const decoder = new TextDecoder("utf-8"); let buffer = ''; let { readable, writable } = new TransformStream({ transform(chunk, controller) { let decoded = decoder.decode(chunk, { stream: true }); buffer += decoded let eventList = buffer.split(/\r\n\r\n|\r\r|\n\n/g); if (eventList.length === 0) return; buffer = eventList.pop(); for (let event of eventList) { ...
点击查看剩余70%