
export default { async fetch(request, env, ctx) { // 处理 CORS 预检请求 if (request.method === 'OPTIONS') { return new Response(null, { status: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Max-Age': '86400', }, }); } // 验证 API Key const authHeader = request.headers.get('Authorization'); if (!authHeader || !authHeader.startsWith('Bearer ')) { return new Response(JSON.stringify({ error: { message: 'API key required', type: 'invalid_request_error', code: 'invalid_api_key' } }), { status: 401, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } const apiKey = authHeader.substring(7); const validApiKeys = env.VALID_API_KEYS ? env.VALID_API_KEYS.split(',') : ['your-api-key-here']; if (!validApiKeys.includes(apiKey)) { return new Response(JSON.stringify({ error: { message: 'Invalid API key', type: 'invalid_request_error', code: 'invalid_api_key' } }), { status: 401, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } const url = new URL(request.url); // 模型映射 const modelMap = { 'deepseek-r1': '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b', 'gpt-oss-120b': '@cf/openai/gpt-oss-120b', 'gpt-oss-20b': '@cf/openai/gpt-oss-20b', 'llama-4-scout': '@cf/meta/llama-4-scout-17b-16e-instruct', 'qwen32b': '@cf/qwen/qwq-32b', 'gemma-3': '@cf/google/gemma-3-12b-it', 'qwen3-embedding-0.6b': '@cf/qwen/qwen3-embedding-0.6b' }; // 聊天接口 if (url.pathname === '/v1/chat/completions' && request.method === 'POST') { try { const body = await request.json(); if (!body.messages || !Array.isArray(body.messages)) { return new Response(JSON.stringify({ error: { message: 'Messages must be an array', type: 'invalid_request_error', code: 'invalid_parameter' } }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } const model = body.model || 'deepseek-r1'; const cfModel = modelMap[model]; if (!cfModel) { return new Response(JSON.stringify({ error: { message: `Model '${model}' not supported`, type: 'invalid_request_error', code: 'model_not_found' } }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } // 构造 AI 请求参数 let aiRequest = {}; let useRespOnsesAPI= cfModel.startsWith('@cf/openai/gpt-oss'); if (useResponsesAPI) { // Responses API 格式 const systemMsg = body.messages.find(m => m.role === 'system')?.content || "You are a helpful assistant."; const userMsgs = body.messages.filter(m => m.role === 'user').map(m => m.content).join("\n"); aiRequest = { input: userMsgs, instructions: systemMsg, temperature: body.temperature ?? 0.7, top_p: body.top_p ?? 0.9, max_tokens: body.max_tokens ?? 2048, reasoning: body.reasoning ?? { effort: "medium" } }; } else { // 旧模型:拼接 prompt let prompt = ''; for (const message of body.messages) { if (message.role === 'system') prompt += `System: ${message.content}\n\n`; if (message.role === 'user') prompt += `User: ${message.content}\n\n`; if (message.role === 'assistant') prompt += `Assistant: ${message.content}\n\n`; } prompt += 'Assistant: '; aiRequest = { prompt, temperature: body.temperature ?? 0.7, top_p: body.top_p ?? 0.9, max_tokens: body.max_tokens ?? 4096, }; } // 调用 Cloudflare AI const respOnse= await env.AI.run(cfModel, aiRequest); const completiOnId= 'chatcmpl-' + Math.random().toString(36).substring(2, 15); const timestamp = Math.floor(Date.now() / 1000); // 获取最终回答内容 let assistantCOntent= ""; if (useResponsesAPI) { if (response.output && Array.isArray(response.output)) { assistantCOntent= response.output .flatMap(msg => msg.content .filter(c => c.type === "output_text") .map(c => c.text) ) .join("\n"); } } else { assistantCOntent= response.response ?? ""; } // 流式输出 if (body.stream) { const encoder = new TextEncoder(); const stream = new ReadableStream({ start(controller) { // 开始事件 controller.enqueue(encoder.encode(`data: ${JSON.stringify({ id: completionId, object: 'chat.completion.chunk', created: timestamp, model, choices: [{ index: 0, delta: { role: 'assistant', content: "" }, finish_reason: null }] })}\n\n`)); // 模拟逐块输出 const chunkSize = 20; for (let i = 0; i < assistantContent.length; i += chunkSize) { const chunk = assistantContent.slice(i, i + chunkSize); controller.enqueue(encoder.encode(`data: ${JSON.stringify({ id: completionId, object: 'chat.completion.chunk', created: timestamp, model, choices: [{ index: 0, delta: { content: chunk }, finish_reason: null }] })}\n\n`)); } // 结束事件 controller.enqueue(encoder.encode(`data: ${JSON.stringify({ id: completionId, object: 'chat.completion.chunk', created: timestamp, model, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`)); controller.enqueue(encoder.encode('data: [DONE]\n\n')); controller.close(); } }); return new Response(stream, { headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*' }, }); } // 非流式输出 const chatCompletion = { id: completionId, object: 'chat.completion', created: timestamp, model, choices: [{ index: 0, message: { role: 'assistant', content: assistantContent }, finish_reason: 'stop' }], usage: { prompt_tokens: Math.ceil(JSON.stringify(body.messages).length / 4), completion_tokens: Math.ceil(assistantContent.length / 4), total_tokens: Math.ceil((JSON.stringify(body.messages).length + assistantContent.length) / 4) } }; return new Response(JSON.stringify(chatCompletion), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } catch (error) { console.error('Error:', error); return new Response(JSON.stringify({ error: { message: 'Internal server error', type: 'server_error', code: 'internal_error' } }), { status: 500, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } } // 嵌入模型 if (url.pathname === '/v1/embeddings' && request.method === 'POST') { try { const body = await request.json(); if (!body.input) { return new Response(JSON.stringify({ error: { message: 'Input is required', type: 'invalid_request_error', code: 'invalid_parameter' } }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } const model = body.model; const cfModel = modelMap[model]; if (!cfModel) { return new Response(JSON.stringify({ error: { message: `Model '${model}' not supported`, type: 'invalid_request_error', code: 'model_not_found' } }), { status: 400, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } // 调用 AI 获取 embedding let embeddingResponse; // Responses API 假设支持 embedding embeddingRespOnse= await env.AI.run(cfModel, { text: body.input }); // 构造返回 const embedding = embeddingResponse.embedding || [0]; // 如果返回格式不同,需要根据实际结果调整 return new Response(JSON.stringify(embeddingResponse), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } catch (error) { console.error('Embedding error:', error); return new Response(JSON.stringify({ error: { message: 'Internal server error', type: 'server_error', code: 'internal_error' } }), { status: 500, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } } // 模型列表 if (url.pathname === '/v1/models' && request.method === 'GET') { const models = Object.keys(modelMap).map(id => ({ id, object: 'model', created: Math.floor(Date.now() / 1000), owned_by: 'cloudflare', permission: [{ id: 'modelperm-' + id, object: 'model_permission', created: Math.floor(Date.now() / 1000), allow_create_engine: false, allow_sampling: true, allow_logprobs: false, allow_search_indices: false, allow_view: true, allow_fine_tuning: false, organization: '*', group: null, is_blocking: false }] })); return new Response(JSON.stringify({ object: 'list', data: models }), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } // 健康检查 if (url.pathname === '/health' && request.method === 'GET') { return new Response(JSON.stringify({ status: 'healthy', timestamp: new Date().toISOString(), models: Object.keys(modelMap) }), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); } // 404 return new Response(JSON.stringify({ error: { message: 'Not found', type: 'invalid_request_error', code: 'not_found' } }), { status: 404, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } }); }, }; ]]>不晓得现在的无线蓝牙耳机如何,能否正常进行麦克风的交流?
求 v 友推荐,最好百元以内。
]]>
]]>OpenResty 手动管理的,服务不多的情况下手动加中间件 TinyAuth 还能接受,后面服务多了我怕会很头疼。 但使用 OpenResty 做反代的情况下,感觉加个 Traefik 个也是要手动配置,要是有个类似的玩意就好了
既能自动发现也能手动配置,我是不是想得太美了 XD
]]>clash for windows,下架后还用了很久clash verge,用了一年感觉小毛病挺多的clash在windows,android,ios三个平台的最好的选择是哪些,毕竟现在clashxxx太多了,有点挑花了眼想要下面这些特性:
其他的功能都是添头,什么 token 统计、MCP 管理啥的,都可以没有。
]]>
]]>
]]>解决代码用 cursor ,完胜灵码和元宝,那么比如医学问题应该用什么 AI 呢?法律问题呢?作图做视频又分别有什么 指定 AI 能像 cursor 一样所向披靡呢?
]]>国内想做点东西太难了。
]]>痛定思痛,我们不得不承认,一个科室主任医生无论经验再如何资深,手术水平再高,也会有判断失误的时候。而当一份工作中出现的任意一个失误就会导致整个职业生涯的断送,尤其叠加成为科室主任医生需要的前置时间投入的情况下,我们不难推断出,一个主任医生,尤其是手术相关的主任医生出现的任何失误大概率都要被深深地掩盖起来————因为当前的舆论环境,医患关系以及医疗系统本身就不允许手术科室的主任医生出现判断或操作失误。
而我认为,恰恰是这个原因导致了小洛熙的死亡。当涉事医生在手术台上发现情况和事先检查有误的情况下,选择了不经告知而进一步实施手术。而我们不难想到他当时的心里活动是怎么样的,在自己自我感觉良好,十拿九稳,整个术前讨论以自已一言堂为主,只要自己按部就班实施手术大概率能在冒着患者生命风险弥补掉之前的损失(或者隐瞒损伤)的情况下,选择了一把梭哈。可惜没有如果,小洛熙没有经受住这次赌博,孩子冰冷的遗体创碎本来就危在旦夕的医患关系。
小洛熙永远的离开了,但是在此事件之后的病人和医生该怎么办?那些需要冒一些风险,使用一些激进疗法才有可能治好的病人该怎么办?他们的主治医生会不会因为此次事件的影响而对他们采取低风险的保守、防御性治疗手段?一边是无可指摘但是会耽误患者的病情的保守疗法,一边是把自己职业生涯赌上去救治患者但是可能被起诉被殴打甚至被杀的风险疗法,在当下的情况下,此时的医生会如何选择?
作为一个对医疗手段一无所知的普通人,当我和我的家人如果出现需必须要到医院进行手术救治的情况,除了在手术之前多走访几家医院多咨询几个医生之外,我们还能做什么?
当自己得了一个必须要冒一点风险才可能治好的病,我能想到的的办法: