// ==================== 配置区 ==================== // 在此处填入你的 DeepSeek API Key const DEEPSEEK_API_KEY = "sk-d9a9bc17d41a4c128a3027c6fccb6d01"; // =============================================== export default { async fetch(request, env) { const url = new URL(request.url); // 1. 处理聊天请求:由 Worker 后端自动注入 Key 并转发 if (url.pathname === '/api/chat') { if (request.method !== 'POST') { return new Response('Method Not Allowed', { status: 405 }); } try { const body = await request.json(); // 在服务端自动加上 Authorization 头,浏览器端无需感知 Key const response = await fetch('https://api.deepseek.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DEEPSEEK_API_KEY}`, }, body: JSON.stringify({ model: body.model || 'deepseek-chat', messages: body.messages, stream: false }) }); const data = await response.text(); return new Response(data, { status: response.status, headers: { 'Content-Type': 'application/json' } }); } catch (err) { return new Response(JSON.stringify({ error: { message: err.message } }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } } // 2. 访问网站根目录时,返回免填 Key 的前端页面 return new Response(HTML_PAGE, { headers: { 'content-type': 'text/html;charset=UTF-8' }, }); }, }; // 内置前端界面(已精简移除右上角 Key 输入框) const HTML_PAGE = ` DeepSeek Playground
DeepSeek Hub
API 已内置就绪,直接在下方输入内容即可开始对话。
`;