使用 Node.js 快速构建接入 Taotoken 的 AI 客服原型1. 环境准备与初始化在开始构建 AI 客服原型前请确保已安装 Node.js 16 或更高版本。创建一个新项目目录并初始化 npm 包管理mkdir ai-customer-service cd ai-customer-service npm init -y npm install openai express body-parser dotenv创建.env文件用于存储敏感信息内容如下TAOTOKEN_API_KEYyour_api_key_here PORT30002. 配置 Taotoken 连接使用 OpenAI 官方 JavaScript SDK 连接 Taotoken 端点时关键配置是正确设置baseURL和apiKey。创建taotoken-client.js文件import OpenAI from openai; import dotenv from dotenv; dotenv.config(); const client new OpenAI({ apiKey: process.env.TAOTOKEN_API_KEY, baseURL: https://taotoken.net/api, }); export default client;3. 实现基础聊天接口创建server.js文件实现 Express 服务处理用户消息并返回 AI 响应import express from express; import bodyParser from body-parser; import client from ./taotoken-client.js; const app express(); app.use(bodyParser.json()); app.post(/chat, async (req, res) { try { const { messages, model claude-sonnet-4-6 } req.body; const completion await client.chat.completions.create({ model, messages, temperature: 0.7, }); res.json({ reply: completion.choices[0].message.content }); } catch (error) { res.status(500).json({ error: error.message }); } }); app.listen(process.env.PORT, () { console.log(Server running on port ${process.env.PORT}); });4. 添加流式响应支持对于需要实时显示响应的场景可以修改接口支持流式传输app.post(/chat-stream, async (req, res) { try { const { messages, model claude-sonnet-4-6 } req.body; res.setHeader(Content-Type, text/event-stream); res.setHeader(Cache-Control, no-cache); res.setHeader(Connection, keep-alive); const stream await client.chat.completions.create({ model, messages, temperature: 0.7, stream: true, }); for await (const chunk of stream) { const content chunk.choices[0]?.delta?.content || ; res.write(data: ${JSON.stringify({ content })}\n\n); } res.end(); } catch (error) { res.status(500).json({ error: error.message }); } });5. 前端界面集成创建简单的 HTML 界面与后端交互在public/index.html中!DOCTYPE html html head titleAI 客服演示/title script async function sendMessage() { const input document.getElementById(message); const output document.getElementById(output); output.innerHTML div用户: ${input.value}/div; const response await fetch(/chat, { method: POST, headers: { Content-Type: application/json }, body: JSON.stringify({ messages: [{ role: user, content: input.value }] }) }); const { reply } await response.json(); output.innerHTML divAI: ${reply}/div; input.value ; } /script /head body div idoutput styleheight: 300px; overflow-y: scroll; border: 1px solid #ccc;/div input idmessage typetext placeholder输入消息... button onclicksendMessage()发送/button /body /html6. 模型切换与扩展Taotoken 支持通过修改model参数切换不同模型。可以在前端添加模型选择器select idmodel option valueclaude-sonnet-4-6Claude Sonnet/option option valuegpt-3.5-turboGPT-3.5 Turbo/option !-- 其他模型选项可在 Taotoken 模型广场查看 -- /select然后修改sendMessage函数const model document.getElementById(model).value; const response await fetch(/chat, { method: POST, headers: { Content-Type: application/json }, body: JSON.stringify({ messages: [{ role: user, content: input.value }], model }) });现在您已经完成了一个支持多模型切换的 AI 客服原型。要开始使用 Taotoken请访问 Taotoken 获取 API Key 并探索更多可用模型。