秒变智能化 — 使用MateChat为你的项目添加一个智能化助手

avatar
前端解决方案集 @华为

DevUI团队重磅推出~MateChat V1.0 版本正式发布

源码:gitcode.com/DevCloudFE/…(欢迎star~)

官网:matechat.gitcode.com

使用MateChat项目AI助手搭建步骤

《MateChat正式开源 – 前端智能化场景解决方案集,轻松构建你的AI应用》发布后得到社区积极的响应,由于上次篇幅有限,今天小编就手把手教教学如何用简单的20行代码为你快速搭建一个简易版的智能化助手;

❗❗ 还没有项目的小伙伴可以自己创建一个新项目并引入此次教程需要的依赖项

$ npm create vite@latest
$ npm i vue-devui @matechat/core @devui-design/icons

在Vue项目中引入MateChat开源依赖

在新建项目的main.ts文件中引入依赖:

import { createApp } from 'vue';
import App from './App.vue';

// matechat基于vue-devui组件
import DevUI from 'vue-devui';
import MateChat from '@matechat/core';

import 'vue-devui/style.css';
import '@devui-design/icons/icomoon/devui-icon.css';

createApp(App).use(DevUI).use(MateChat).mount('#app');

在App.vue文件中快速加载MateChat组件:

<script setup lang="ts">
const description = [
  'MateChat 可以辅助研发人员编码、查询知识和相关作业信息、编写文档等。',
  '作为AI模型,MateChat 提供的答案可能不总是确定或准确的,但您的反馈可以帮助 MateChat 做的更好。',
];

const list = [
    {
      label: '帮我写一个快速排序',
      iconConfig: { name: 'icon-info-o', color: '#5e7ce0' },
    },
    {
      label: '你可以帮我做些什么?',
      iconConfig: { name: 'icon-star', color: 'rgb(255, 215, 0)'},
    }
  ];
</script>

<template>
  <McLayout style="height: 100vh; padding: 20px; gap: 4px">
    <McHeader :title="'MateChat'" :logoImg="'https://matechat.gitcode.com/logo.svg'">  </McHeader>
    <McLayoutContent>
      <McIntroduction :logoImg="'https://matechat.gitcode.com/logo2x.svg'"
        :title="'MateChat'" :subTitle="'Hi,欢迎使用 MateChat'" :description="description">
      </McIntroduction>
    </McLayoutContent>
     <div style="display: flex;align-items: center; gap: 8px">
        <McPrompt :list="list" :direction="'horizontal'" style="flex: 1"></McPrompt>
     </div>
    <McLayoutSender>
      <McInput></McInput>
    </McLayoutSender>
  </McLayout>
</template>

此时npm run dev启动你的项目得到页面如下,恭喜你就成功获搭建了一个简易的智能化助手了~

image.png

完整demo示例

这时候有的小伙伴多半就要问了,现在搭建的助手虽然已经拥有智能化交互的外壳,提供了对话交互视觉规范,大家可以基于MateChat官网开放API自定义去调整,我们目前开放了七大核心原子组件给大家做调整,大家可以参照如下demo搭建一个更完善的示例。 首先我们先MateChat的McLayout【布局组件】、McIntroduction【介绍组件】、McPrompt【快捷提示】、McInput【输入组件】搭建一下页面内容。

<template>
  <McLayout class="container">
    <McHeader :title="'MateChat'" :logoImg="'https://matechat.gitcode.com/logo.svg'">
      <template #operationArea>
        <div class="operations">
          <i class="icon-helping"></i>
        </div>
      </template>
    </McHeader>
    <McLayoutContent
      v-if="startPage"
      style="display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px"
    >
      <McIntroduction
        :logoImg="'https://matechat.gitcode.com/logo2x.svg'"
        :title="'MateChat'"
        :subTitle="'Hi,欢迎使用 MateChat'"
        :description="description"
      ></McIntroduction>
      <McPrompt
        :list="introPrompt.list"
        :direction="introPrompt.direction"
        class="intro-prompt"
        @itemClick="onSubmit($event.label)"
      ></McPrompt>
    </McLayoutContent>
    <McLayoutContent class="content-container" v-else>
      <template v-for="(msg, idx) in messages" :key="idx">
        <McBubble
          v-if="msg.from === 'user'"
          :content="msg.content"
          :align="'right'"
          :avatarConfig="{ imgSrc: 'https://matechat.gitcode.com/png/demo/userAvatar.svg' }"
        >
        </McBubble>
        <McBubble v-else :content="msg.content" :avatarConfig="{ imgSrc: 'https://matechat.gitcode.com/logo.svg' }"> </McBubble>
      </template>
    </McLayoutContent>
    <div class="shortcut" style="display: flex; align-items: center; gap: 8px">
      <McPrompt
        v-if="!startPage"
        :list="simplePrompt"
        :direction="'horizontal'"
        style="flex: 1"
        @itemClick="onSubmit($event.label)"
      ></McPrompt>
      <d-button
        style="margin-left: auto"
        icon="add"
        shape="circle"
        title="新建对话"
        size="sm"
        @click="newConversation"
      />
    </div>
    <McLayoutSender>
      <McInput :value="inputValue" :maxLength="2000" @change="(e) => (inputValue = e)" @submit="onSubmit">
        <template #extra>
          <div class="input-foot-wrapper">
            <div class="input-foot-left">
              <span v-for="(item, index) in inputFootIcons" :key="index">
                <i :class="item.icon"></i>
                {{ item.text }}
              </span>
              <span class="input-foot-dividing-line"></span>
              <span class="input-foot-maxlength">{{ inputValue.length }}/2000</span>
            </div>
            <div class="input-foot-right">
              <d-button icon="op-clearup" shape="round" :disabled="!inputValue" @click="inputValue = ''">清空输入</d-button>
            </div>
          </div>
        </template>
      </McInput>
    </McLayoutSender>
  </McLayout>
</template>

我们再给Demo补充对应的交互逻辑,这里定义了初始介绍页信息,信息提示页列表数据,输入框交互事件等等。

<script setup lang="ts">
import { ref } from 'vue';

const description = [
  'MateChat 可以辅助研发人员编码、查询知识和相关作业信息、编写文档等。',
  '作为AI模型,MateChat 提供的答案可能不总是确定或准确的,但您的反馈可以帮助 MateChat 做的更好。',
];
const introPrompt = {
  direction: 'horizontal',
  list: [
    {
      value: 'quickSort',
      label: '帮我写一个快速排序',
      iconConfig: { name: 'icon-info-o', color: '#5e7ce0' },
      desc: '使用 js 实现一个快速排序',
    },
    {
      value: 'helpMd',
      label: '你可以帮我做些什么?',
      iconConfig: { name: 'icon-star', color: 'rgb(255, 215, 0)' },
      desc: '了解当前大模型可以帮你做的事',
    },
    {
      value: 'bindProjectSpace',
      label: '怎么绑定项目空间',
      iconConfig: { name: 'icon-priority', color: '#3ac295' },
      desc: '如何绑定云空间中的项目',
    },
  ],
};
const simplePrompt = [
  {
    value: 'quickSort',
    iconConfig: { name: 'icon-info-o', color: '#5e7ce0' },
    label: '帮我写一个快速排序',
  },
  {
    value: 'helpMd',
    iconConfig: { name: 'icon-star', color: 'rgb(255, 215, 0)' },
    label: '你可以帮我做些什么?',
  },
];
const startPage = ref(true);
const inputValue = ref('');
const inputFootIcons = [
  { icon: 'icon-at', text: '智能体' },
  { icon: 'icon-standard', text: '词库' },
  { icon: 'icon-add', text: '附件' },
];

const messages = ref<any[]>([
  {
    from: 'user',
    content: '你好',
  },
  {
    from: 'model',
    content: '你好,我是 MateChat',
    id: 'init-msg',
  },
]);

const newConversation = () => {
  startPage.value = true;
  messages.value = [];
}

const onSubmit = (evt) => {
  inputValue.value='';
  startPage.value = false;
  // 用户发送消息
  messages.value.push({
    from: 'user',
    content: evt,
  });
  setTimeout(() => {
    // 模型返回消息
    messages.value.push({
      from: 'model',
      content: evt,
    });
  }, 200);
};
</script>

最后再Demo中vue文件里补充了一些简单的样式进行视觉丰富。

<style>
.container {
  width: 1000px;
  margin: 20px auto;
  height: calc(100vh - 40px);
  padding: 20px;
  gap: 8px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 16px;
}

.content-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}

.input-foot-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 100%;
  margin-right: 8px;

  .input-foot-left {
    display: flex;
    align-items: center;
    gap: 8px;

    span {
      font-size: 12px;
      color: #252b3a;
      cursor: pointer;
    }

    .input-foot-dividing-line {
      width: 1px;
      height: 14px;
      background-color: #d7d8da;
    }

    .input-foot-maxlength {
      font-size: 12px;
      color: #71757f;
    }
  }

  .input-foot-right {
    & > *:not(:first-child) {
      margin-left: 8px;
    }
  }
}
</style>

效果是不是比基础版本更丰富了呢?当然大家也可以MateChat抛出来的API自定义去调整大家想要的效果;

image.png

适配主题切换

我想大家根据上面的步骤应该已经搭建起了一个智能化助手的页面了吧,现在再带着大家给助手增加主题适配的能力~MateChat是基于vue-devui物料基座构建,这里我们直接继承它的能力来快速搭建一个。

$ npm i devui-theme

完成动态主题切换

引入主题后,样式设置可参照devui.design/components/… 直接引入DevUI变量来适配主题,如container的颜色原来用的#fff可直接替换为$devui-base-bg。

import { ThemeServiceInit, infinityTheme, galaxyTheme } from 'devui-theme';
 
// 初始是 infinityTheme 无限主题
const themeService = ThemeServiceInit({ infinityTheme }, 'infinityTheme');
 
// 可以动态切换成 galaxyTheme 追光主题
themeService.applyTheme(galaxyTheme);

image.png

对接大模型

在搭建完成页面后,现在咱们就可以开始对接模型服务,如 盘古大模型、ChatGPT 等优秀大模型,在注册并生成对应模型的调用API_Key后,可以参考官网快速开始-对接模型服务进行对接。

考虑到篇幅对接大模型场景我们将在下一篇文章给大家详细展开教程,当然也可以按照官网步骤先行~

🔥项目使用&技术答疑

欢迎添加DevUI小助手官方微信号:devui-official,并备注matechat交流;我们会把大家拉进官方交流群并第一时间给大家解决使用上的问题~

大家也可以直接微信搜索DevUI,关注公众号给我们留言~