当前位置:   article > 正文

ai聊天问答知识库机器人源码,基于gpt实现的本地知识库问答实现,聊天对话效果,发送回复以及流式输出...

开源的问答机器人源吗

现在基于gpt做自己项目的问答机器人,效果非常的好。可以把自己的文档上传上去,让机器人根据文档来进行回答。

想要实现智能AI问答功能,现在大部分都是基于向量数据库的形式。

整体的流程就是:上传文档===>openai向量接口 ====> 存入向量数据库

访客咨询:  咨询问题 ====> openai向量接口  ====>搜索向量数据库  ====> 组织prompt 到 openai的chat接口

下面的源码是前端逻辑,实现的界面以及问答的聊天对话效果,发送回复以及流式输出

 效果图的前端源码

  1. <template>
  2. <div class="chatpdf">
  3. <div class="pannel">
  4. <div class="fileList">
  5. <div class="fileTitle" v-bind:class="{'active': collect==item.name}" @click="selectCollect(item.name)" v-for="(item,index) in collects">
  6. <svg t="1682317088056" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1403" width="16" height="16"><path d="M512 64c259.2 0 469.333333 200.576 469.333333 448s-210.133333 448-469.333333 448a484.48 484.48 0 0 1-232.725333-58.88l-116.394667 50.645333a42.666667 42.666667 0 0 1-58.517333-49.002666l29.76-125.013334C76.629333 703.402667 42.666667 611.477333 42.666667 512 42.666667 264.576 252.8 64 512 64z m0 64C287.488 128 106.666667 300.586667 106.666667 512c0 79.573333 25.557333 155.434667 72.554666 219.285333l5.525334 7.317334 18.709333 24.192-26.965333 113.237333 105.984-46.08 27.477333 15.018667C370.858667 878.229333 439.978667 896 512 896c224.512 0 405.333333-172.586667 405.333333-384S736.512 128 512 128z m-157.696 341.333333a42.666667 42.666667 0 1 1 0 85.333334 42.666667 42.666667 0 0 1 0-85.333334z m159.018667 0a42.666667 42.666667 0 1 1 0 85.333334 42.666667 42.666667 0 0 1 0-85.333334z m158.997333 0a42.666667 42.666667 0 1 1 0 85.333334 42.666667 42.666667 0 0 1 0-85.333334z" fill="#ffffff" p-id="1404"></path></svg>
  7. {{item.name}}</div>
  8. </div>
  9. </div>
  10. <div class="chatpdfBox">
  11. <div class="chatpdfLine">
  12. <div class="chatpdfLineScroll">
  13. <h1>欢迎使用知识库AI</h1>
  14. <h2>由 AI 支持的网页版 Copilot</h2>
  15. <div class="chatpdfRow " v-bind:class="{'chatpdfAsk': item.type=='ask'}" v-for="(item,index) in msgList">
  16. <div class="chatpdfContent" v-html="html(item.content)"></div>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="chatpdfArea">
  21. <button @click="clearHistory">
  22. <svg t="1682398861245" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1371" width="20" height="20"><path d="M883.2 403.2l-147.2-44.8 57.6-224c0-6.4 0-19.2-6.4-25.6-6.4-6.4-12.8-12.8-19.2-12.8L627.2 57.6c-6.4 0-19.2 0-25.6 0C595.2 70.4 588.8 76.8 588.8 83.2L524.8 300.8 358.4 256c-6.4 0-19.2 0-25.6 0S320 275.2 320 281.6l-89.6 320C211.2 684.8 128 768 128 768c-6.4 6.4-12.8 19.2-6.4 32 0 12.8 12.8 19.2 25.6 25.6l524.8 140.8c0 0 6.4 0 6.4 0 6.4 0 19.2-6.4 25.6-12.8 6.4-6.4 83.2-89.6 115.2-179.2 32-83.2 89.6-326.4 89.6-332.8C908.8 422.4 896 409.6 883.2 403.2zM755.2 748.8c-25.6 57.6-70.4 115.2-89.6 147.2l-70.4-19.2c32-38.4 70.4-96 89.6-160 6.4-19.2-6.4-32-25.6-38.4-19.2-6.4-32 6.4-38.4 25.6-19.2 70.4-76.8 134.4-96 153.6l-57.6-12.8c32-38.4 70.4-96 83.2-153.6 6.4-19.2-6.4-32-25.6-38.4-19.2-6.4-32 6.4-38.4 25.6-19.2 64-70.4 128-89.6 153.6l-64-19.2c32-38.4 70.4-96 89.6-153.6 6.4-19.2-6.4-32-25.6-38.4C384 608 364.8 620.8 364.8 633.6c-19.2 64-70.4 128-96 153.6l-57.6-19.2c32-38.4 70.4-96 83.2-153.6l76.8-294.4 166.4 44.8c6.4 0 19.2 0 25.6 0C569.6 364.8 576 358.4 582.4 352L640 128l83.2 19.2-57.6 224c-6.4 19.2 6.4 32 19.2 38.4L832 454.4C819.2 524.8 780.8 691.2 755.2 748.8z" p-id="1372" fill="#ffffff"></path><path d="M364.8 473.6C364.8 492.8 371.2 505.6 390.4 512l339.2 96c0 0 6.4 0 6.4 0 12.8 0 25.6-6.4 32-25.6 6.4-19.2-6.4-32-19.2-38.4L409.6 448C390.4 448 371.2 454.4 364.8 473.6z" p-id="1373" fill="#ffffff"></path>
  23. </svg>
  24. </button>
  25. <textarea @keydown.prevent.enter="sendAsk" v-model="askContent"></textarea>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import MarkdownIt from 'markdown-it';
  32. import hljs from 'highlight.js';
  33. import 'highlight.js/styles/monokai-sublime.css';
  34. export default {
  35. name: 'ChatPage',
  36. data() {
  37. return {
  38. apiPost:"http://127.0.0.1:8083",
  39. collects:[],
  40. collect:"test",
  41. askContent:"",
  42. msgList:[
  43. {type:"ask",content:"自建私有数据知识库 · 与知识库AI聊天"},
  44. {type:"answer",content:"我是知识库机器人,一个专门响应人类指令的大模型"},
  45. ],
  46. }
  47. },
  48. methods: {
  49. sendAsk(){
  50. if(this.askContent=="") return;
  51. let msg={
  52. 'type':'ask',
  53. 'content':this.askContent,
  54. }
  55. this.msgList.push(msg);
  56. let data=JSON.stringify(this.msgList);
  57. localStorage.setItem("data_"+this.collect,data);
  58. this.scrollBottom();
  59. this.getReplyFromApi();
  60. },
  61. selectCollect(name){
  62. this.collect=name;
  63. this.msgList=[];
  64. this.getHistory();
  65. },
  66. getCollects(){
  67. let _this=this;
  68. fetch(this.apiPost+'/collects', {
  69. method: 'get',
  70. })
  71. .then((response) => response.json())
  72. .then((response) => {
  73. console.log(response);
  74. _this.collects=response.result.collections;
  75. })
  76. },
  77. getReplyFromApi(){
  78. let _this=this;
  79. let msg={
  80. 'type':'answer',
  81. 'content':"正在为你生成答案...",
  82. }
  83. _this.msgList.push(msg);
  84. let i=0;
  85. var xhr = new XMLHttpRequest();
  86. xhr.open("GET", this.apiPost+"/"+this.collect+"/searchStream?keywords="+_this.askContent);
  87. xhr.setRequestHeader("Content-Type", "text/html");
  88. xhr.onprogress = function(event) {
  89. console.log(i,event.currentTarget.responseText);
  90. _this.msgList[_this.msgList.length-1].content=event.currentTarget.responseText;
  91. _this.scrollBottom();
  92. };
  93. xhr.onreadystatechange = () => {
  94. if (xhr.readyState === XMLHttpRequest.DONE) {
  95. let data=JSON.stringify(this.msgList);
  96. localStorage.setItem("data_"+this.collect,data);
  97. }
  98. };
  99. xhr.send();
  100. this.askContent="";
  101. },
  102. getHistory(name){
  103. let str=localStorage.getItem("data_"+this.collect);
  104. if(!str) return;
  105. let data=JSON.parse(str);
  106. this.msgList=data;
  107. },
  108. //滚动到底部
  109. scrollBottom:function(){
  110. var _this=this;
  111. this.$nextTick(function(){
  112. var container = _this.$el.querySelector(".chatpdfLine");
  113. container.scrollTop = 999999999;
  114. });
  115. },
  116. html(sourceStr) {
  117. const md = new MarkdownIt({
  118. highlight: function (str, lang) {
  119. if (lang && hljs.getLanguage(lang)) {
  120. try {
  121. return '<pre class="hljs"><code>' +
  122. hljs.highlight(lang, str, true).value +
  123. '</code></pre>';
  124. } catch (__) {}
  125. }
  126. return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
  127. }
  128. });
  129. return md.render(sourceStr);
  130. },
  131. clearHistory(){
  132. localStorage.removeItem("data_"+this.collect);
  133. this.msgList=[];
  134. },
  135. getQuery(key) {
  136. // 获取所有参数
  137. var query = window.location.search.substring(1);
  138. var hash = window.location.hash.substring(1);
  139. // 如果锚点后面有参数,把锚点后面的参数加入到search参数中
  140. if(hash.indexOf("?") > -1){
  141. query += "&" + hash.split("?")[1];
  142. }
  143. var key_values = query.split("&");
  144. var params = {};
  145. // 遍历参数并存入params对象
  146. key_values.map(function (key_val){
  147. var key_val_arr = key_val.split("=");
  148. params[key_val_arr[0]] = key_val_arr[1];
  149. });
  150. // 如果找到了key对应的参数,返回对应值
  151. if(typeof params[key]!="undefined"){
  152. return params[key];
  153. }
  154. // 如果没找到,返回空字符串
  155. return "";
  156. }
  157. },
  158. mounted: function () {
  159. let collect=this.getQuery("collect");
  160. if(collect){
  161. this.collect=collect;
  162. }
  163. this.getCollects();
  164. this.getHistory();
  165. }
  166. }
  167. </script>
  168. <style>
  169. .chatpdf{
  170. display: flex;
  171. height: 100vh;
  172. flex-direction: row;
  173. }
  174. .chatpdf .pannel{
  175. width: 255px;
  176. background-color: rgb(0, 21, 41);
  177. display: none;
  178. }
  179. .chatpdfBox{
  180. display: flex;
  181. flex-direction: column;
  182. flex: 1;
  183. background: linear-gradient(to bottom right,#dbe6fb, #f3f4f8);
  184. background-size: cover;
  185. background-attachment: fixed;
  186. }
  187. .chatpdfHeader{
  188. font-size: 18px;
  189. padding: 10px;
  190. text-align: center;
  191. width: 100%;
  192. }
  193. .chatpdfLine{
  194. flex: 1;
  195. width: 100%;
  196. overflow-y: auto;
  197. }
  198. .chatpdfLine h1{
  199. color: #111111;
  200. text-align: center;
  201. margin-top: 80px;
  202. margin-bottom: 20px;
  203. font-size: 36px;
  204. }
  205. .chatpdfLine h2{
  206. color: #1e1e1e;
  207. text-align: center;
  208. font-size: 20px;
  209. font-weight: 400;
  210. }
  211. .chatpdfLineScroll{
  212. max-width: 1000px;
  213. margin: 0 auto;
  214. }
  215. .chatpdfRow{
  216. margin: 20px 10px;
  217. display: flex;
  218. }
  219. .chatpdfAsk{
  220. justify-content: flex-end;
  221. }
  222. .chatpdfContent{
  223. line-height: 23px;
  224. display: inline-block;
  225. border-radius: 8px;
  226. padding: 12px 15px;
  227. max-width: 700px;
  228. background: rgba(255, 255, 255, 0.6);
  229. font-size: 14px;
  230. box-shadow: 0px 0.3px 0.9px rgba(0, 0, 0, 0.12), 0px 1.6px 3.6px rgba(0, 0, 0, 0.16);
  231. }
  232. .chatpdfAsk .chatpdfContent{
  233. background: linear-gradient(90deg, #2870EA 10.79%, #1B4AEF 87.08%);;
  234. color: #fff;
  235. }
  236. .chatpdfContent pre{
  237. padding: 10px;
  238. }
  239. .chatpdfArea{
  240. display: flex;
  241. margin-bottom: 10px;
  242. max-width: 1000px;
  243. margin: 0 auto;
  244. width: 98%;
  245. margin-bottom: 15px;
  246. transition: all 0.3s,height 0s;
  247. }
  248. .chatpdfArea textarea{
  249. flex: 1;
  250. border: none;
  251. resize: none;
  252. outline: none;
  253. padding: 0px 5px;
  254. height: 40px;
  255. line-height: 35px;
  256. color: #404040;
  257. border-radius: 10px;
  258. box-shadow: 0px 0.3px 0.9px rgba(0, 0, 0, 0.12), 0px 1.6px 3.6px rgba(0, 0, 0, 0.08);
  259. }
  260. .chatpdfArea:hover{
  261. border-color: #4096ff;
  262. }
  263. .chatpdfArea button{
  264. height: 40px;
  265. color: #fff;
  266. background: linear-gradient(90deg, #1B4AEF 10.79%, #2870EA 87.08%);
  267. box-shadow: 0 2px 0 rgba(5, 145, 255, 0.1);
  268. border: none;
  269. padding: 0 20px;
  270. border-radius: 15px;
  271. cursor: pointer;
  272. box-shadow: 0px 0.3px 0.9px rgba(0, 0, 0, 0.12), 0px 1.6px 3.6px rgba(0, 0, 0, 0.08);
  273. margin-right: 10px;
  274. }
  275. .chatpdfArea button:hover{
  276. background-color: #388aff;
  277. }
  278. .chatpdf .fileTitle{
  279. background-color: #1677ff;
  280. color: #fff;
  281. border-radius: 8px;
  282. padding: 10px;
  283. margin: 10px;
  284. font-size: 14px;
  285. cursor: pointer;
  286. display: flex;
  287. }
  288. .chatpdf .fileTitle svg{
  289. margin-right: 5px;
  290. }
  291. .chatpdf .fileTitle.active{
  292. background-color: #66a6ff;
  293. }
  294. @media (max-width: 768px) {
  295. .pannel{
  296. display: none;
  297. }
  298. }
  299. </style>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/257377
推荐阅读
相关标签
  

闽ICP备14008679号