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

LLM+js实现大模型对话

代码运行效果图:


前提是你有一个可用的openai服务,然后用下面一个html页即可启动
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Chat with OpenAI</title>
  <style>
    body {
      font-family: Arial, sans-serif;
    }

    #chat-container {
      width: 400px;
      margin: 0 auto;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 10px;
    }

    #chat-messages {
      height: 300px;
      overflow-y: scroll;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 10px;
      margin-bottom: 10px;
    }

    #user-input {
      width: 70%;
      padding: 5px;
    }

    #send-button {
      padding: 5px 10px;
    }
  </style>
</head>

<body>
  <div id="chat-container">
    <h2>Chat with OpenAI</h2>
    <div id="chat-messages"></div>
    <input type="text" id="user-input" placeholder="Type your message">
    <button id="send-button">Send</button>
  </div>

  <script>
    // 替换为你的OpenAI API密钥
    const apiKey = 'YOUR_API_KEY';
    const apiUrl = 'http://10.130.7.6:8000/v1/chat/completions'; //你的openai服务地址

    document.getElementById('send-button').addEventListener('click', function () {
      const userInput = document.getElementById('user-input').value;
      if (userInput.trim() === '') return;

      const chatMessages = document.getElementById('chat-messages');
      const userMessageElement = document.createElement('p');
      userMessageElement.style.color = 'blue';
      userMessageElement.textContent = `You: ${userInput}`;
      chatMessages.appendChild(userMessageElement);

      fetch(apiUrl, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify({
          "model": "Qwen2.5-7B-Instruct", // 你可以选择其他合适的模型
          "messages": [{"role": "user", "content": userInput}]
        })
      })
      .then(response => response.json())
      .then(data => {
          const assistantReply = data.choices[0].message.content;
          const assistantMessageElement = document.createElement('p');
          assistantMessageElement.style.color = 'green';
          assistantMessageElement.textContent = `Assistant: ${assistantReply}`;
          chatMessages.appendChild(assistantMessageElement);
        })
      .catch(error => {
          console.error('Error:', error);
          const errorMessageElement = document.createElement('p');
          errorMessageElement.style.color ='red';
          errorMessageElement.textContent = 'Error occurred while fetching response.';
          chatMessages.appendChild(errorMessageElement);
        });

      document.getElementById('user-input').value = '';
    });
  </script>
</body>

</html>

运行方式

可直接用python启动建议webui,进入html所在页面(可直接在pycharm控制台里运行):

D:\gradio_project\new_inference0310\0402> python -m http.server 8000

然后打开浏览器访问http://127.0.0.1:8000/test.html即可

相关文章:

  • 判断矩阵A是否可以相似对角化
  • MySQL 在 CentOS 7 环境安装完整步骤
  • 任务调度和安全如何结合
  • WinMerge下载及使用教程(附安装包)
  • AnimateCC基础教学:随机抽取花名册,不能重复
  • Springboot--Kafka客户端参数关键参数的调整方法
  • 【WRF理论第十五期】WPS中输入geogrid二进制格式
  • Johnson算法 流水线问题 java实现
  • 下一代楼宇自控的中枢神经:ARM终端的生态
  • 1.ElasticSearch-入门基础操作
  • 从零开始学java--泛型(1)
  • 辛格迪客户案例 | 北京舒曼德医药实施电子合约系统(eSign)
  • UDP怎么样实现可靠传输?
  • zk源码—3.单机和集群通信原理二
  • Python 字典和集合(泛映射类型)
  • 无缝集成Docker与Maven:docker-maven-plugin实战指南
  • Docker常用命令
  • Microsoft Defender Antivirus Service服务占用CPU过高
  • Ansible(7)——管理机密与事实
  • 按键长按代码
  • 4月证券私募产品备案量创23个月新高,股票策略占比超六成
  • 习近平出席俄罗斯纪念苏联伟大卫国战争胜利80周年庆典
  • 讲座预告|全球贸易不确定情况下企业创新生态构建
  • 一周文化讲座|城市移民与数字时代的新工作
  • 马上评|不再提“智驾”,新能源车企回归理性
  • 姜再冬大使会见巴基斯坦副总理兼外长达尔