开发html5网页,通过JavaScript访问deepseek服务。
1.在线版本:
1 // script.js 2 document.getElementById('askButton').addEventListener('click', function() { 3 const question = document.getElementById('questionInput').value; 4 if (question.trim() === '') { 5 alert('请输入问题!'); 6 return; 7 } 8 9 // 这里假设DeepSeek API的端点是 'https://api.deepseek.com/v1/ask' 10 const apiUrl = 'https://api.deepseek.com/v1/chat/completions'; 11 const apiKey = 'sk-f4711749144249028??'; // 替换为你的DeepSeek API密钥 12 13 fetch(apiUrl, { 14 method: 'POST', 15 headers: { 16 'Content-Type': 'application/json', 17 'Authorization': `Bearer ${apiKey}` 18 //'Access-Control-Allow-Origin':true 19 }, 20 body: JSON.stringify({ 21 model: "deepseek-chat", // 根据实际模型修改 22 messages: [{ 23 role: "user", 24 content: question 25 }], 26 "stream": false 27 }) 28 }) 29 .then(response => response.json()) 30 .then(data => { 31 document.getElementById('response').innerText = data.choices[0].message.content;; 32 }) 33 .catch(error => { 34 console.error('Error:', error); 35 document.getElementById('response').innerText = '获取答案时出错,请稍后重试。'; 36 }); 37 });
2.本地部署的ollama,deepseek-r1:14b
1 // script.js 2 document.getElementById('askButton').addEventListener('click', function() { 3 const question = document.getElementById('questionInput').value; 4 if (question.trim() === '') { 5 alert('请输入问题!'); 6 return; 7 } 8 9 // 这里假设DeepSeek API的端点是 'https://api.deepseek.com/v1/ask' 10 const apiUrl = 'http://localhost:11434/v1/chat/completions'; 11 const apiKey = ''; // 替换为你的DeepSeek API密钥 12 13 fetch(apiUrl, { 14 method: 'POST', 15 headers: { 16 'Content-Type': 'application/json', 17 //'Authorization': `Bearer ${apiKey}` 18 //'Access-Control-Allow-Origin':true 19 }, 20 body: JSON.stringify({ 21 model: "deepseek-r1:14b", // 根据实际模型修改 22 messages: [{ 23 role: "user", 24 content: question 25 }], 26 "stream": false 27 }) 28 }) 29 .then(response => response.json()) 30 .then(data => { 31 document.getElementById('response').innerText = data.choices[0].message.content;; 32 }) 33 .catch(error => { 34 console.error('Error:', error); 35 document.getElementById('response').innerText = '获取答案时出错,请稍后重试。'; 36 }); 37 });