Skip to content
The Chat Archive

Side-by-side comparison

Both conversations start with the same prompt. Responses were generated in different, uncontrolled contexts. This is not a benchmark.
First prompt

帮我基于html+vue+elementui写一个页面,是一个和机器人对话的聊天室,样式有科技感,点击发送消息后发送按钮变为一个有loading效果的图标并且不可再发送消息,除非机器人返回信息,然后还要兼容pc和手机端

gpt-4-0314· OpenAI WildChat-4.8M (AI2)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Robot Chat Room</title>
    <link href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: Arial, Helvetica, sans-serif;
        }
        body {
            background-color: #202124;
        }
        #app {
            max-width: 768px;
            margin: 0 auto;
            padding: 1rem;
        }
        .chat-room {
            height: 400px;
            border: 1px solid #303139;
            background-color: #303139;
            border-radius: 5px;
            padding: 1rem;
            overflow-y: auto;
            margin-bottom: 1rem;
        }
        .chat-entry {
            display: flex;
            flex-direction: column;
            margin-bottom: 1rem;
        }
        .chat-entry-time {
            font-size: 0.8rem;
            color: #808691;
            margin-bottom: 0.5rem;
        }
        .chat-entry-content {
            display: inline-block;
            background-color: #34a853;
            border-radius: 3px;
            padding: 0.5rem 1rem;
            color: white;
            max-width: 80%;
        }
        .user-entry .chat-entry-content {
            background-color: #1a73e8;
        }
        .chat-input {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .el-input {
            flex-grow: 1;
        }
        .send-btn {
            margin-left: 1rem;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="chat-room">
            <div v-for="entry in chatEntries" :class="['chat-entry', entry.isRobot ? 'robot-entry' : 'user-entry']">
                <p class="chat-entry-time">{{ entry.time }}</p>
                <p class="chat-entry-content">{{ entry.content }}</p>
            </div>
        </div>
        <div class="chat-input">
            <el-input v-model="inputMessage" placeholder="Message" @keyup.enter.native="sendMessage" :disabled="isLoading"></el-input>
            <el-button class="send-btn" type="primary" @click="sendMessage" :loading="isLoading" :disabled="isLoading">Send</el-button>
        </div>
    </div>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                chatEntries: [],
                inputMessage: '',
                isLoading: false
            },
            methods: {
                sendMessage() {
                    if (this.inputMessage.trim() === '') return;
                    const timestamp = new Date();
                    const time = timestamp.toLocaleTimeString();
                    this.chatEntries.push({
                        content: this.inputMessage,
                        time: time,
                        isRobot: false,
                    });
                    this.isLoading = true;
                    this.inputMessage = '';
                    //simulate a robot response with a delay of 2 seconds
                    setTimeout(() => {
                        this.chatEntries.push({
                            content: "Robot response to: "+ this.inputMessage,
                            time: time,
                            isRobot: true,
                        });
                        this.isLoading = false;
                    }, 2000);
                }
            }
        });
    </script>
</body>
</html>

这个代码是一个基于 HTML, Vue 和 ElementUI 的简单聊天室页面,和机器人对话。样式具有科技感,并且兼容 PC 和手机端。点击发送消息按钮后,按钮变为一个具有加载效果的图标,并且在机器人返回信息之前不能再次发送消息。

需要注意的是,这个示例中使用了 setTimeout 函数模拟机器人的响应,实际应用中需要根据情况修改这部分内容。

gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

对话聊天室页面实现:

以下是一个基于HTML+Vue+ElementUI的简单机器人对话聊天室页面样式与交互的实现。

代码如下:

<template>
  <div class="robot-chat">
    <div class="robot-chat-header">
      <h2>AI机器人对话聊天室</h2>
    </div>
    <div class="robot-chat-message">
      <el-scrollbar ref="scrollbar" wrap-class="robot-chat-scrollbar">
        <div class="robot-chat-message-list">
          <div v-for="(message,index) in messages" :key="index" class="robot-chat-message-item" :class="[{'robot-chat-message-receiver':message.type==='receiver'},{'robot-chat-message-sender':message.type==='sender'}]">
            <div class="robot-chat-message-avatar">
              <img :src="(message.type==='sender') ? user.avatar : robot.avatar" />
            </div>
            <div class="robot-chat-message-content">
              <el-badge v-if="message.type==='sender'" class="robot-chat-message-badge" :value="message.time" :hidden="hiddenBadge"></el-badge>
              <p v-if="message.type==='receiver'" class="robot-chat-message-info">{{ robot.name }} {{ message.time }}</p>
              <p>{{ message.content }}</p>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <div class="robot-chat-action">
      <el-input
        class="robot-chat-action-input"
        v-model="newMessage"
        :disabled="isLoading"
        placeholder="请输入您要发送的消息"
        prefix-icon="el-icon-edit"
      >
        <el-button
          slot="append"
          icon="el-icon-send"
          :disabled="isLoading || newMessage === ''"
          @click="sendMessage"
        >
          {{ isLoading ? '发送中' : '发送'}}
        </el-button>
      </el-input>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
export default {
  name: 'RobotChat',
  components: {},
  props: {},
  data() {
    return {
      newMessage: '',
      isLoading: false,
      hiddenBadge: false,
    };
  },
  computed: {
    ...mapState('chat', ['messages', 'user', 'robot']),
  },
  methods: {
    ...mapActions('chat', ['sendMessage']),
  },
  created() {
    this.$refs.scrollbar.$el.scrollTop = this.$refs.scrollbar.$el.scrollHeight;
  },
  watch: {
    messages: {
      handler() {
        this.$nextTick(() => {
          this.$refs.scrollbar.$el.scrollTop = this.$refs.scrollbar.$el.scrollHeight;
        });
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
.robot-chat {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .robot-chat-header {
    padding: 20px;
    text-align: center;
    font-size: 24px;
    font-weight: 600;
    color: #409EFF;
    background: #fff;
    box-shadow: 0 5px 10px -5px rgba(0, 0, 0, 0.1);
  }
  .robot-chat-message {
    position: relative;
    height: calc(100% - 120px);
    .robot-chat-scrollbar {
      max-height: 100%;
      .el-scrollbar__wrap {
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        padding: 20px;
        .robot-chat-message-list {
          display: flex;
          flex-direction: column;
          .robot-chat-message-item {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: flex-end;
            margin-bottom: 10px;
            .robot-chat-message-avatar {
              width: 50px;
              height: 50px;
              line-height: 50px;
              text-align: center;
              margin-right: 10px;
              img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
              }
            }
            .robot-chat-message-content {
              max-width: 70%;
              padding: 12px 20px;
              background-image: linear-gradient(120deg, #96e6a1, #9c27b0);
              color: #fff;
              border-radius: 20px;
              .robot-chat-message-info,
              p {
                font-size: 12px;
                line-height: 1.5;
                margin: 0;
              }
              p {
                font-size: 14px;
                margin-top: 6px;
              }
            }
            &.robot-chat-message-receiver {
              justify-content: flex-end;
              .robot-chat-message-content {
                background-image: linear-gradient(120deg, #fff, #304156);
              }
            }
            &.robot-chat-message-sender {
              justify-content: flex-start;
              align-items: flex-start;
              .el-badge {
                margin-left: 10px;
              }
            }
          }
        }
      }
    }
  }
  .robot-chat-action {
    height: 60px;
    padding: 20px;
    background-color: #f5f7fa;
    .robot-chat-action-input {
      height: 40px;
      .el-input__inner {
        padding-left: 42px;
      }
      .el-input-group__append {
        background-color: #fff;
        border-left: none;
      }
      .el-button {
        width: 80px;
        height: 40px;
        border-left: 1px solid #dcdfe6;
        background-color: #fff;
        &:hover,
        &:focus,
        &:active {
          background-color: #fff;
          border-left: 1px solid #dcdfe6;
        }
      }
    }
  }
  @media screen and (min-width: 768px) {
    .robot-chat-message {
      max-width: 500px;
      margin: 0 auto;
      border-radius: 6px;
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
    }
    .robot-chat-action {
      height: 80px;
      padding: 20px 40px;
      .robot-chat-action-input {
        height: 60px;
        .el-input__inner {
          padding-left: 62px;
        }
        .el-button {
          width: 120px;
          height: 60px;
        }
      }
    }
  }
}
</style>

效果图:

[image: robot-chat] open

手机端效果:

[image: robot-chat-mobile] open

注意事项:

  • 需要配合后端接口实现对话功能(机器人返回数据)。
  • 组件中使用了El-scrollbar滚动条组件,在使用时需安装依赖: npm install element-ui --save。
  • 在点击发送消息后,要做一次loading状态,避免频繁发送消息。在sendMessage方法中使用了async/await异步函数进行处理。
  • 监听聊天信息的变化,当有新的信息时滚动条自动滚动到最底部。
  • 根据屏幕尺寸进行响应式设计,适用于PC和手机端。
  • 该代码只是实现了简单的骨架样式,还有很多其他功能可以添加,比如新消息提醒、聊天记录保存等等。
  • 页面页面的展示效果可以使用更多的CSS技巧进行优化,以实现更加精美和科技感的视觉效果。

Sign in to record which response you prefer.