DeepSeek API 是一个强大的人工智能接口,让开发者能够在自己的应用中集成高级的自然语言处理能力。DeepSeek 提供两种主要模型:
通过本教程,你将学习如何在HTML网页中集成DeepSeek API,让你的网站具备AI对话能力!
在开始之前,你需要获取DeepSeek API Key:
首先,我们需要创建一个基本的HTML页面结构:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DeepSeek API 演示</title> <style> body { font-family: 'Microsoft YaHei', sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } .container { border: 1px solid #ddd; padding: 20px; border-radius: 5px; } #apiKey { width: 100%; padding: 10px; margin: 10px 0; } #promptInput { width: 100%; padding: 10px; margin: 10px 0; } #response { background-color: #f5f5f5; padding: 15px; margin-top: 15px; min-height: 100px; white-space: pre-wrap; } </style> </head> <body> <h1>DeepSeek API 演示</h1> <div class="container"> <h2>配置</h2> <label for="apiKey">输入你的 DeepSeek API Key:</label> <input type="password" id="apiKey" placeholder="sk-..."> <h2>发送请求</h2> <label for="promptInput">输入你的提示词:</label> <textarea id="promptInput" rows="4" placeholder="请在这里输入你的问题..."></textarea> <button id="sendBtn">发送请求</button> <div id="loading" class="loading">处理中...</div> <h2>响应结果</h2> <div id="response"></div> </div> <!-- 在这里我们将添加 JavaScript 代码 --> </body> </html>
这段代码创建了一个简单的页面,包含输入API Key的字段、提示词输入框和发送按钮,以及一个显示响应结果的区域。
现在,我们需要添加JavaScript代码来调用DeepSeek API。将以下代码添加到HTML文件的底部,在<!-- 在这里我们将添加 JavaScript 代码 -->
注释的位置:
<script> document.getElementById('sendBtn').addEventListener('click', async function() { // 获取用户输入 const apiKey = document.getElementById('apiKey').value; const promptText = document.getElementById('promptInput').value; const responseElement = document.getElementById('response'); const loadingElement = document.getElementById('loading'); // 验证输入 if (!apiKey || !promptText) { responseElement.textContent = "请输入API Key和提示词"; return; } // 显示加载提示 loadingElement.style.display = 'block'; responseElement.textContent = ""; try { // 准备请求数据 const requestData = { model: "deepseek-chat", // 使用deepseek-chat模型,你也可以换成deepseek-reasoner messages: [ { role: "system", content: "你是一个有帮助的助手。" }, { role: "user", content: promptText } ], stream: false // 设置为true可以获得流式响应 }; // 发送请求到DeepSeek API const response = await fetch('https://api.deepseek.com/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }, body: JSON.stringify(requestData) }); // 处理响应 if (!response.ok) { const errorData = await response.json(); throw new Error(`API错误: ${errorData.error?.message || '未知错误'}`); } const data = await response.json(); responseElement.textContent = data.choices[0].message.content; } catch (error) { responseElement.textContent = `发生错误: ${error.message}`; console.error('API调用错误:', error); } finally { // 隐藏加载提示 loadingElement.style.display = 'none'; } }); </script>
这段JavaScript代码完成了以下任务:
DeepSeek API兼容OpenAI的API格式,因此我们也可以使用OpenAI的JavaScript SDK来调用它。这种方法在某些情况下更简洁。
首先,我们需要在HTML头部添加OpenAI SDK:
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>
然后,替换之前的JavaScript代码为:
<script> document.getElementById('sendBtn').addEventListener('click', async function() { // 获取用户输入 const apiKey = document.getElementById('apiKey').value; const promptText = document.getElementById('promptInput').value; const responseElement = document.getElementById('response'); const loadingElement = document.getElementById('loading'); // 验证输入 if (!apiKey || !promptText) { responseElement.textContent = "请输入API Key和提示词"; return; } // 显示加载提示 loadingElement.style.display = 'block'; responseElement.textContent = ""; try { // 使用OpenAI SDK创建客户端 const openai = new OpenAI({ apiKey: apiKey, baseURL: 'https://api.deepseek.com', dangerouslyAllowBrowser: true // 注意:在生产环境中不建议在浏览器中直接使用API Key }); // 发送请求 const completion = await openai.chat.completions.create({ messages: [ { role: "system", content: "你是一个有帮助的助手。" }, { role: "user", content: promptText } ], model: "deepseek-chat", }); // 显示响应 responseElement.textContent = completion.choices[0].message.content; } catch (error) { responseElement.textContent = `发生错误: ${error.message}`; console.error('API调用错误:', error); } finally { // 隐藏加载提示 loadingElement.style.display = 'none'; } }); </script>
dangerouslyAllowBrowser: true
参数,这在生产环境中是不安全的,因为它会在客户端暴露你的API Key。实际应用中,应该通过服务器端代码来处理API请求。
现在你可以保存HTML文件并在浏览器中打开它,测试你的DeepSeek API集成了。
在实际应用中,应遵循以下最佳实践:
注意: 这个演示界面不会实际工作,因为这只是一个静态HTML示例。要测试功能,请将完整代码保存为HTML文件并在本地浏览器中打开。
恭喜!你已经学会了如何在HTML网页中集成DeepSeek API。如果你想进一步提升你的集成,可以考虑:
要了解更多信息,请访问DeepSeek API官方文档。