核工业西南物理研究院知识库AI客户端
xiangpei
2025-03-20 5d929ee64eed9b9e54ec84a7ce531afe444f29df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<template>
  <div class="ai-chat-dialog">
    <!-- 聊天消息列表 -->
    <div class="chat-messages">
      <div
          v-for="(message, index) in messages"
          :key="index"
          :class="['message', message.role]"
      >
        <div class="avatar">
          <img :src="getAvatar(message.role)" alt="avatar" />
        </div>
        <div class="content">
<!--          <div class="name">{{ getRoleName(message.role) }}</div>-->
          <div class="text">{{ message.content }}</div>
        </div>
      </div>
    </div>
 
    <!-- 输入框 -->
    <div class="chat-input">
        <el-input
            style="width: 50%"
            v-model="inputMessage"
            placeholder="请输入消息"
            @keyup.native.enter="sendMessage"
        >
          <div slot="append">
            <el-button type="primary" @click="sendMessage" icon="el-icon-thumb"></el-button>
          </div>
        </el-input>
<!--      <div class="send-but" @click="sendMessage">-->
<!--        <svg t="1742455190051" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1160" width="32" height="32"><path d="M417.016025 991.681197h266.763504l286.972859-930.353884z" fill="#6E6E96" opacity=".2" p-id="1161"></path><path d="M318.168022 667.620127l106.879202 307.873376L985.008068 34.974312z" fill="#E5C0A8" p-id="1162"></path><path d="M425.051265 993.681924a18.180337 18.180337 0 0 1-17.186036-12.222619l-106.87516-307.873376a18.188421 18.188421 0 0 1 4.664319-19.15847L972.498476 21.777602a18.184379 18.184379 0 0 1 28.14355 22.497056L440.67714 984.797891a18.192463 18.192463 0 0 1-15.625875 8.884033z m-85.849345-320.940746l90.210525 259.868071L902.064828 138.741273 339.20192 672.741178z" fill="#6E6E96" p-id="1163"></path><path d="M51.287304 548.991206L985.008068 34.974312 318.168022 667.620127z" fill="#FFDAC1" p-id="1164"></path><path d="M318.16398 685.808548c-2.497876 0-5.01192-0.513318-7.384499-1.568246l-266.876675-118.628922a18.188421 18.188421 0 0 1-10.783713-15.848177 18.204588 18.204588 0 0 1 9.401393-16.713137L976.237207 19.041256a18.188421 18.188421 0 1 1 21.292578 29.129766L330.689739 680.816837a18.184379 18.184379 0 0 1-12.525759 4.991711z m-226.005273-138.555347l222.302919 98.811627L869.915784 119.097779 92.158707 547.253201z" fill="#6E6E96" p-id="1165"></path><path d="M985.008068 34.974312L425.281652 711.656315l429.48924 166.755482z" fill="#FFDAC1" p-id="1166"></path><path d="M854.770892 896.60426a18.196504 18.196504 0 0 1-6.584208-1.232771l-429.48924-166.759524a18.176295 18.176295 0 0 1-7.42896-28.551779L970.998942 23.382225a18.196504 18.196504 0 0 1 31.995453 14.368853l-130.245259 843.445569a18.224798 18.224798 0 0 1-8.87595 12.978448 18.325844 18.325844 0 0 1-9.102294 2.429165z m-399.409634-192.781092l384.887191 149.44819L956.969607 97.417181 455.361258 703.823168z" fill="#6E6E96" p-id="1167"></path><path d="M425.281652 993.681924a18.192463 18.192463 0 0 1-18.188421-18.188421v-263.837188a18.188421 18.188421 0 0 1 36.376842 0v263.837188a18.188421 18.188421 0 0 1-18.188421 18.188421z" fill="#6E6E96" p-id="1168"></path><path d="M985.008068 34.974312L518.147686 728.203736l-62.786428-24.380568zM417.016025 711.656315l78.937746 109.18711 32.504729-54.593555z" fill="#6E6E96" opacity=".2" p-id="1169"></path></svg>-->
<!--      </div>-->
    </div>
  </div>
</template>
 
<script>
import {sendKbMsg} from "@/api/chat";
 
export default {
  name: 'AiChat',
  data() {
    return {
      messages: [
        {
          role: 'assistant',
          content: '你好!我是你的 AI 助手,有什么可以帮你的吗?',
        },
      ],
      inputMessage: '',
      sendMsgForm: {
        query: "",
        mode: "local_kb",
        kb_name: "samples",
        top_k: 3,
        score_threshold: 2,
        history: [
          {
            "content": "我们来玩成语接龙,我先来,生龙活虎",
            "role": "user"
          },
          {
            "content": "虎头虎脑",
            "role": "assistant"
          }
        ],
        stream: true,
        model: "qwen:7b",
        temperature: 0.7,
        max_tokens: 0,
        prompt_name: "default",
        return_direct: false
      }
    };
  },
  methods: {
    // 获取角色头像
    getAvatar(role) {
      const avatars = {
        user: 'https://picsum.photos/200',
        assistant: 'https://picsum.photos/200',
      };
      return avatars[role];
    },
 
    // 获取角色名称
    getRoleName(role) {
      const roleNames = {
        user: '用户',
        assistant: 'AI 助手',
      };
      return roleNames[role];
    },
 
    // 发送消息
    async sendMessage() {
      if (this.inputMessage.trim() === '') return;
 
      // 添加用户消息
      this.messages.push({
        role: 'user',
        content: this.inputMessage,
      });
      this.sendMsgForm.query = this.inputMessage
      // 清空输入框
      this.inputMessage = '';
 
      sendKbMsg(this.sendMsgForm).then(res => {
        console.log(res, "拿到回复了!!")
        this.messages.push({
          role: 'assistant',
          content: '这是一条 AI 回复。',
        });
      })
    },
  },
};
</script>
 
<style scoped>
.send-but {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 58px;
  height: 54px;
  background-color: #f6f6f6;
  margin-left: 5px;
}
.send-but:hover {
  cursor: pointer;
}
.ai-chat-dialog {
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  overflow: hidden;
}
 
.chat-messages {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}
 
.message {
  display: flex;
  margin-bottom: 16px;
}
 
.message.user {
  flex-direction: row-reverse;
}
 
.avatar img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
 
.content {
  max-width: 70%;
  margin: 0 12px;
  padding: 8px 12px;
  border-radius: 8px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
 
.message.user .content {
  background-color: #409eff;
  color: #fff;
}
 
.name {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}
 
.message.user .name {
  color: #fff;
}
 
.text {
  font-size: 14px;
}
 
.chat-input {
  padding: 16px;
  background-color: #fff;
  border-top: 1px solid #ddd;
  display: flex;
  width: 100%;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  position: absolute;
  bottom: 70px;
}
</style>