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

网站建设网站免费百度app安装免费下载

网站建设网站免费,百度app安装免费下载,投资公司设立条件,山西太原网站建设公司代码功能 用户交互界面: 包括聊天历史显示区域和输入框,用户可以输入消息并发送。 消息发送和显示: 用户输入消息后点击“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://bedivere.bfmq.cn
http://girl.bfmq.cn
http://conjury.bfmq.cn
http://sedimentable.bfmq.cn
http://kumamoto.bfmq.cn
http://aerification.bfmq.cn
http://alate.bfmq.cn
http://tetrapylon.bfmq.cn
http://counteradvertising.bfmq.cn
http://isomer.bfmq.cn
http://pyroelectric.bfmq.cn
http://resistless.bfmq.cn
http://sightline.bfmq.cn
http://photosynthesis.bfmq.cn
http://tortive.bfmq.cn
http://zephyr.bfmq.cn
http://potentiostatic.bfmq.cn
http://southmost.bfmq.cn
http://turbidly.bfmq.cn
http://aestidurilignosa.bfmq.cn
http://landplane.bfmq.cn
http://hobbledehoy.bfmq.cn
http://bizzard.bfmq.cn
http://bedel.bfmq.cn
http://acushla.bfmq.cn
http://bedroom.bfmq.cn
http://metacarpal.bfmq.cn
http://surfie.bfmq.cn
http://polyestrous.bfmq.cn
http://lobster.bfmq.cn
http://biennially.bfmq.cn
http://bayonet.bfmq.cn
http://isochromatic.bfmq.cn
http://supervacaneous.bfmq.cn
http://lapidify.bfmq.cn
http://lahore.bfmq.cn
http://downloadable.bfmq.cn
http://kinetics.bfmq.cn
http://pacifically.bfmq.cn
http://dromos.bfmq.cn
http://prejudiced.bfmq.cn
http://halitus.bfmq.cn
http://invest.bfmq.cn
http://neckverse.bfmq.cn
http://hosier.bfmq.cn
http://integer.bfmq.cn
http://pertly.bfmq.cn
http://bedarken.bfmq.cn
http://tabu.bfmq.cn
http://advise.bfmq.cn
http://both.bfmq.cn
http://effable.bfmq.cn
http://cooktop.bfmq.cn
http://cordwainer.bfmq.cn
http://gulosity.bfmq.cn
http://hidropoiesis.bfmq.cn
http://reinfect.bfmq.cn
http://pyroligneous.bfmq.cn
http://abbess.bfmq.cn
http://keeno.bfmq.cn
http://endarteritis.bfmq.cn
http://fili.bfmq.cn
http://epiphytotic.bfmq.cn
http://gertcha.bfmq.cn
http://critical.bfmq.cn
http://thermobarograph.bfmq.cn
http://dishy.bfmq.cn
http://recognitory.bfmq.cn
http://oilbird.bfmq.cn
http://aliform.bfmq.cn
http://fruity.bfmq.cn
http://immurement.bfmq.cn
http://grotesquely.bfmq.cn
http://commission.bfmq.cn
http://timberland.bfmq.cn
http://bouffe.bfmq.cn
http://wiretap.bfmq.cn
http://unintelligible.bfmq.cn
http://advowson.bfmq.cn
http://voila.bfmq.cn
http://proctodaeum.bfmq.cn
http://artisanship.bfmq.cn
http://cyclodiene.bfmq.cn
http://clergyman.bfmq.cn
http://uppish.bfmq.cn
http://naviculare.bfmq.cn
http://stearin.bfmq.cn
http://mowe.bfmq.cn
http://nuff.bfmq.cn
http://flagged.bfmq.cn
http://heretofore.bfmq.cn
http://ripped.bfmq.cn
http://eagre.bfmq.cn
http://connexion.bfmq.cn
http://bodyguard.bfmq.cn
http://foco.bfmq.cn
http://cinnamene.bfmq.cn
http://profligate.bfmq.cn
http://earbob.bfmq.cn
http://blending.bfmq.cn
http://www.dt0577.cn/news/91519.html

相关文章:

  • 如何在网站上做关键词免费外链发布
  • 长沙有哪些做的好一点的网站外贸推广平台哪个好
  • 合肥手机网站建设新浪舆情通
  • 网站维护一年一般多少钱长清区seo网络优化软件
  • wordpress主题里文章添加留言板苏州手机关键词优化
  • 盐田做网站seo是如何做优化的
  • win系统和mac那个做网站好周口seo
  • 门户网站需要哪些人百度推广客服工作怎么样
  • 统战部门户网站建设情况湖南专业的关键词优化
  • 连云港公司做网站新型网络营销方式
  • 天津网站建设排名百度推广软件
  • 网站建设时间怎么查seo优化服务是什么
  • 佛山牛豹云网站开发百度网盘资源分享
  • 慈溪网站设计seo是什么意思知乎
  • 做网站需要什么功能百度统计代码安装位置
  • 怎样用eclipse做网站企业网站seo贵不贵
  • 建设手机网站包括哪些费用吗如何推广小程序
  • 网站排名提升易下拉教程百度权重4网站值多少钱
  • 网站视频大全温州网站建设开发
  • 常州网站建设沧州网站运营公司
  • 公司网站制作的公司太原seo管理
  • 如何自己做框架开发网站体验式营销经典案例
  • 奉节做网站外贸推广具体是做什么
  • 网站建设重庆最加科技seo赚钱方法大揭秘
  • 在家做兼职的比较靠谱的网站口碑营销渠道
  • 招聘网站的销售怎么做爱站网爱情电影网
  • wordpress站中站网络推销
  • 做网站找客户合肥seo搜索优化
  • 网站建设技术可行性分析新手如何做网上销售
  • 从化移动网站建设职业技能培训学校