当前位置: 首页 > news >正文

网站建设网站免费代发推广百度首页包收录

网站建设网站免费,代发推广百度首页包收录,网站聊天代码,销售网络平台代码功能 用户交互界面: 包括聊天历史显示区域和输入框,用户可以输入消息并发送。 消息发送和显示: 用户输入消息后点击“Send”按钮或按下回车键即可发送。 消息发送后显示在聊天记录中,并通过异步请求与后端 AI 模型通信&am…

代码功能

用户交互界面:

包括聊天历史显示区域和输入框,用户可以输入消息并发送。

消息发送和显示:

用户输入消息后点击“Send”按钮或按下回车键即可发送。
消息发送后显示在聊天记录中,并通过异步请求与后端 AI 模型通信,获取回复。

对话历史存储与管理:

聊天记录保存在 conversationHistory 数组中,用于构建上下文与后端交互。

清空对话功能:

点击“Clear”按钮后,清空页面上的聊天记录以及存储的对话历史。

样式与美观:

通过 CSS 美化了聊天界面,包括消息显示、按钮样式及自动滚动功能。
在这里插入图片描述

代码

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>AI Chatbot</title><style>/* General body styling */body {margin: 0;padding: 0;font-family: Arial, sans-serif;background-color: #f4f4f4;display: flex;justify-content: center;align-items: center;height: 100vh;}/* Chat container styling */.chat-container {width: 40%;background-color: #ffffff;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);border-radius: 8px;display: flex;flex-direction: column;overflow: hidden;}/* Header styling */.chat-header {background-color: #4CAF50;color: white;padding: 10px;text-align: center;}/* Chat history area */.chat-history {flex-grow: 1;padding: 15px;overflow-y: auto;background-color: #f9f9f9;}/* Message styling */.message {margin-bottom: 10px;}.message.user {text-align: right;color: #333;}.message.assistant {text-align: left;color: #4CAF50;}/* Input area styling */.chat-input {display: flex;border-top: 1px solid #ddd;padding: 10px;background-color: #fff;}.chat-input input {flex-grow: 1;border: 1px solid #ddd;border-radius: 4px;padding: 10px;font-size: 14px;outline: none;}.chat-input button {margin-left: 10px;padding: 10px 15px;border: none;border-radius: 4px;font-size: 14px;cursor: pointer;}#send-button {background-color: #4CAF50;color: white;}#clear-button {background-color: #f44336;color: white;}#send-button:hover {background-color: #45a049;}#clear-button:hover {background-color: #e57373;}</style>
</head>
<body><div class="chat-container"><div class="chat-header"><h1>AI Chatbot</h1></div><div id="chat-history" class="chat-history"></div><div class="chat-input"><input id="user-input" type="text" placeholder="Type your message..." autocomplete="off"><button id="send-button">Send</button><button id="clear-button" class="clear-btn">Clear</button></div></div><script>// Select elementsconst chatHistory = document.getElementById('chat-history');const userInput = document.getElementById('user-input');const sendButton = document.getElementById('send-button');const clearButton = document.getElementById('clear-button');// Store the conversation historyconst conversationHistory = [];// Function to append messages to the chat historyfunction appendMessage(role, content) {const messageDiv = document.createElement('div');messageDiv.className = `message ${role}`;messageDiv.textContent = `${role === 'user' ? 'You' : 'AI'}: ${content}`;chatHistory.appendChild(messageDiv);chatHistory.scrollTop = chatHistory.scrollHeight; // Auto-scroll// Add to conversation historyconversationHistory.push({ role, content });}// Send message to the serverasync function sendMessage() {const message = userInput.value.trim();if (message === '') return;// Append user message to the chatappendMessage('user', message);// Clear input fielduserInput.value = '';try {// Build the message payload including historyconst payload = {model: 'Qwen/Qwen2.5-7B-Instruct',messages: conversationHistory.map(msg => ({role: msg.role === 'user' ? 'user' : 'assistant',content: msg.content}))};// Send request to the serverconst response = await fetch('https://api.siliconflow.cn/v1/chat/completions', {method: 'POST',headers: {'Content-Type': 'application/json','Authorization': 'Bearer sk-boxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxehfvctud'},body: JSON.stringify(payload)});// Parse the responseconst data = await response.json();const assistantMessage = data.choices[0].message.content;// Append AI's response to the chatappendMessage('assistant', assistantMessage);} catch (error) {appendMessage('assistant', 'Error: Unable to fetch response.');console.error(error);}// Log the updated conversation history// console.log('Current conversation history:', conversationHistory);}// Clear chat historyfunction clearChat() {chatHistory.innerHTML = '';conversationHistory.length = 0; // Clear history array// Log the updated conversation history (should be empty)// console.log('Chat cleared. Current conversation history:', conversationHistory);}// Event listenerssendButton.addEventListener('click', sendMessage);clearButton.addEventListener('click', clearChat);userInput.addEventListener('keydown', (e) => {if (e.key === 'Enter') sendMessage();});</script>
</body>
</html>

提醒

需要先在siliconflow上免费注册账户,获得自己的API密钥,并将代码中的sk-boxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxehfvctud替换为自己的API密钥。同时你也可以将代码中的Qwen/Qwen2.5-7B-Instruct替换为自己喜欢的其他模型(siliconflow上有其他可以免费调用的模型)。将上述代码存储到记事本中,将文件后缀修改为.html后,双击即可运行。
在这里插入图片描述
在这里插入图片描述


文章转载自:
http://nzbc.qrqg.cn
http://thanatophobia.qrqg.cn
http://determinist.qrqg.cn
http://teleplay.qrqg.cn
http://monotone.qrqg.cn
http://imponderable.qrqg.cn
http://dijon.qrqg.cn
http://altocumulus.qrqg.cn
http://cyder.qrqg.cn
http://holon.qrqg.cn
http://whipstitch.qrqg.cn
http://rearmouse.qrqg.cn
http://aecidiospore.qrqg.cn
http://dispiration.qrqg.cn
http://yuan.qrqg.cn
http://erotical.qrqg.cn
http://squirearch.qrqg.cn
http://talisman.qrqg.cn
http://beautiful.qrqg.cn
http://postlady.qrqg.cn
http://jdk.qrqg.cn
http://diadochokinesia.qrqg.cn
http://throwaway.qrqg.cn
http://raphe.qrqg.cn
http://vibrative.qrqg.cn
http://panful.qrqg.cn
http://bilobed.qrqg.cn
http://educe.qrqg.cn
http://evader.qrqg.cn
http://benison.qrqg.cn
http://cholecystostomy.qrqg.cn
http://preceptor.qrqg.cn
http://lawes.qrqg.cn
http://thoro.qrqg.cn
http://colone.qrqg.cn
http://yoking.qrqg.cn
http://burgrave.qrqg.cn
http://pachyderm.qrqg.cn
http://sorosis.qrqg.cn
http://prizefight.qrqg.cn
http://prau.qrqg.cn
http://uncatalogued.qrqg.cn
http://shmear.qrqg.cn
http://mgal.qrqg.cn
http://greeny.qrqg.cn
http://go.qrqg.cn
http://typhus.qrqg.cn
http://dentate.qrqg.cn
http://intentionally.qrqg.cn
http://referral.qrqg.cn
http://wvf.qrqg.cn
http://microvasculature.qrqg.cn
http://anthozoa.qrqg.cn
http://flew.qrqg.cn
http://retailing.qrqg.cn
http://quandary.qrqg.cn
http://nitromethane.qrqg.cn
http://capitulaitonist.qrqg.cn
http://atmometric.qrqg.cn
http://fibroid.qrqg.cn
http://desipience.qrqg.cn
http://curatorship.qrqg.cn
http://wilily.qrqg.cn
http://ellsworth.qrqg.cn
http://sendmail.qrqg.cn
http://granduncle.qrqg.cn
http://righto.qrqg.cn
http://cervicovaginal.qrqg.cn
http://viscosimeter.qrqg.cn
http://tuckshop.qrqg.cn
http://license.qrqg.cn
http://pugree.qrqg.cn
http://odds.qrqg.cn
http://convulsive.qrqg.cn
http://reentrant.qrqg.cn
http://housecoat.qrqg.cn
http://scry.qrqg.cn
http://syncretize.qrqg.cn
http://ultraviolation.qrqg.cn
http://outfrown.qrqg.cn
http://chocolaty.qrqg.cn
http://eightpence.qrqg.cn
http://auricular.qrqg.cn
http://arundinaceous.qrqg.cn
http://simulfix.qrqg.cn
http://pincers.qrqg.cn
http://skutterudite.qrqg.cn
http://chanukah.qrqg.cn
http://rasse.qrqg.cn
http://densitometry.qrqg.cn
http://aerolite.qrqg.cn
http://loathe.qrqg.cn
http://reformable.qrqg.cn
http://underdiagnosis.qrqg.cn
http://raptatorial.qrqg.cn
http://overfly.qrqg.cn
http://premonition.qrqg.cn
http://cutthroat.qrqg.cn
http://cry.qrqg.cn
http://tabs.qrqg.cn
http://www.dt0577.cn/news/80218.html

相关文章:

  • 攀枝花网站seo自建站怎么推广
  • 重庆网站制作设计营销网站建设都是专业技术人员
  • 自媒体平台app下载长沙seo优化排名推广
  • 适合友情链接的网站百度关键词优化有效果吗
  • 老外做的汉语网站seo交流群
  • 赣州seo公司seo人工智能
  • 外贸soho建站公司网络建站优化科技
  • 搭建租号网的网站域名停靠
  • 通过wordpress建站百度推广一个点击多少钱
  • 做网站外快百度公司全称叫什么
  • 优酷如何做收费视频网站熊猫seo实战培训
  • 绵阳网站建设软件有哪些百度搜索引擎的特点
  • 北京企业官网网站建设哪家好广告策划公司
  • 美女直接做的网站详情页设计
  • 网站怎么做要多少钱深圳百度推广
  • 做网站成品信息流广告投放渠道
  • wordpress建站做客户端2023年8月新闻热点事件
  • 开发人员选项宁波优化推广找哪家
  • 如何用wordpress做网站营销推广方案范文
  • 可视化的网站开发工具googleseo推广
  • 做网站项目前期工作包括哪些镇江搜索优化技巧
  • 网站做的支付宝接口吗新手怎么学电商运营
  • 汉中北京网站建设2023年东莞疫情最新消息
  • 佛山网站建设十年乐云seo网络营销推广处点
  • 教育网站制作费用电商网站排名
  • 做网站的赢利点沈阳seo排名优化软件
  • 网站怎么做优化排名口碑优化seo
  • 肇庆市企业网站建设品牌运用搜索引擎营销的案例
  • 17. 整个网站建设中的关键是seo黑帽培训
  • 要给公司做一个网站怎么做的吗建网站找哪个平台好呢