{"id":100539,"date":"2026-09-04T19:05:17","date_gmt":"2026-09-04T11:05:17","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/100539.html"},"modified":"2026-09-04T19:05:17","modified_gmt":"2026-09-04T11:05:17","slug":"103-rag-llamaindex%e5%90%8e%e7%ab%afrag%e9%97%ae%e7%ad%94-%e8%81%8a%e5%a4%a9%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/100539.html","title":{"rendered":"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3\u57fa\u4e8e LlamaIndex \u6784\u5efa RAG \u77e5\u8bc6\u5e93\u95ee\u7b54\u7cfb\u7edf\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u6587\u7ae0\u4ece\u524d\u7aef\u804a\u5929\u903b\u8f91\u5165\u624b&#xff0c;\u8be6\u7ec6\u62c6\u89e3 ChatView.vue \u4e0e ChatInput.vue \u7684\u4ea4\u4e92\u6d41\u7a0b\u3001chat.ts \u4e2d\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u53e3\u8c03\u7528\u673a\u5236&#xff0c;\u5e76\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 Vue \u4ee3\u7801&#xff1b;\u968f\u540e\u6df1\u5165\u540e\u7aef&#xff0c;\u4ecb\u7ecd RAGService \u7684\u521d\u59cb\u5316\u3001\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf\u3001\u5411\u91cf\u7d22\u5f15\u6784\u5efa\u3001\u6df7\u5408\u68c0\u7d22&#xff08;\u5411\u91cf\u68c0\u7d22\u4e0e BM25 \u878d\u5408&#xff09;\u4ee5\u53ca\u8bed\u4e49\u91cd\u6392\u7b49\u6838\u5fc3\u73af\u8282&#xff0c;\u6700\u7ec8\u5b9e\u73b0\u652f\u6301\u77e5\u8bc6\u5e93\u95ee\u7b54\u3001\u6d41\u5f0f\u8f93\u51fa\u4e0e\u591a\u4f1a\u8bdd\u8bb0\u5fc6\u7684\u5b8c\u6574\u5e94\u7528\u3002<\/p>\n<p>\u5185\u5bb9\u53c2\u8003\u4e8e&#xff1a;<span style=\"color:red\">\u56fe\u7075AI\u5927\u6a21\u578b\u5168\u6808<\/span><\/p>\n<p>\u9996\u5148\u662f\u524d\u7aef\u903b\u8f91&#xff0c;\u804a\u5929\u90fd\u662f\u5728chat.ts\u91cc\u5b9e\u73b0\u7684<\/p>\n<p>\u9996\u5148\u5728\u4e0b\u56fe\u7ea2\u6846\u8fdb\u884c\u5f15\u7528chat.ts&#xff0c;chat.ts\u6587\u4ef6\u4e2d\u8fd4\u56de\u4e86\u4e00\u4e2a\u53ebuseChatStore\u6807\u8bc6&#xff08;\u5b83\u5c31\u4ee3\u8868\u4e86\u6574\u4e2achat.ts\u6587\u4ef6\u91cc\u7684\u4ee3\u7801&#xff09;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110503-6a9aa5df42949.png\" width=\"2301\" \/><\/p>\n<p>\u4e0a\u56feChatView.vue\u6587\u4ef6\u662f\u4e0b\u56fe\u7684\u9875\u9762<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1145\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110504-6a9aa5e065a9b.png\" width=\"1836\" \/><\/p>\n<p>\u7136\u540e\u5728\u4e0b\u56fe\u7ea2\u6846\u4f4d\u7f6e\u521b\u5efachat.ts\u5b9e\u4f8b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1051\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110505-6a9aa5e143ab5.png\" width=\"1093\" \/><\/p>\n<p>\u7136\u540e\u5728\u4e0b\u56fe\u7ea2\u6846\u4f4d\u7f6e\u521b\u5efa\u4e86\u4e00\u4e2asend\u7684\u6d88\u606f\u4e8b\u4ef6&#xff0c;\u5b83\u4f1a\u76d1\u542c\u5b50\u7ec4\u4ef6\u53d1\u9001\u7684send\u4e8b\u4ef6&#xff0c;\u5b50\u7ec4\u4ef6\u5c31\u662fChatInput.vue<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"995\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110505-6a9aa5e1cf28e.png\" width=\"1179\" \/><\/p>\n<p>\u5728ChatInput.vue\u4e2d\u5b83\u4f1a\u8c03\u7528sendMessage\u51fd\u6570<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1029\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110506-6a9aa5e244a15.png\" width=\"1605\" \/><\/p>\n<p>\u4e0a\u56fe\u7ea2\u6846\u548c\u84dd\u6846\u91cc\u7684\u5185\u5bb9\u662f\u9875\u9762\u4e0a\u7684\u8f93\u5165\u6846\u548c\u53d1\u9001\u6309\u94ae&#xff0c;\u5982\u4e0b\u56fe&#xff0c;\u4e0a\u56fe\u7ea2\u6846\u7684\u5bf9\u5e94\u4e0b\u56fe\u7ea2\u6846\u7684\u8f93\u5165\u6846&#xff0c;\u4e0a\u56fe\u84dd\u6846\u7684\u5bf9\u5e94\u4e0b\u56fe\u84dd\u6846\u7684\u53d1\u9001\u6309\u94ae&#xff0c;\u5728\u8f93\u5165\u6846\u4e2d\u6309\u4e0b\u56de\u8f66\u952e&#xff08;Enter&#xff09;\u4f1a\u89e6\u53d1sendMessage\u51fd\u6570&#xff0c;\u9f20\u6807\u70b9\u51fb\u4e0b\u56fe\u84dd\u6846\u7684\u53d1\u9001\u6309\u94ae\u4f1a\u89e6\u53d1sendMessage\u51fd\u6570<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1145\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110507-6a9aa5e3033bd.png\" width=\"1836\" \/><\/p>\n<p>\u7136\u540e\u5982\u4e0b\u56fe\u7ea2\u6846&#xff0c;sendMessage\u51fd\u6570\u4e2d\u4f1a\u7ed9\u7236\u7ec4\u4ef6\u4e5f\u5c31\u662fChatView.vue\u53d1\u9001\u4e00\u4e2asend\u4e8b\u4ef6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1075\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110507-6a9aa5e3da817.png\" width=\"1243\" \/><\/p>\n<p>\u5982\u4e0b\u56fe&#xff0c;\u5b83\u4f1a\u901a\u8fc7 ChatView.vue \u89e6\u53d1chatStore.sendMessage\u8fd9\u4e2a\u51fd\u6570&#xff0c;\u4e5f\u5c31\u662f\u4f1a\u6267\u884c\u5230chat.ts\u6587\u4ef6\u4e2d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1076\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110508-6a9aa5e4516ef.png\" width=\"3014\" \/><\/p>\n<p>\u7136\u540e\u5728\u4e0b\u56fe\u7ea2\u6846\u7684\u51fd\u6570\u4e2d\u8c03\u7528\u4e86\u804a\u5929\u63a5\u53e3<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1029\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110509-6a9aa5e5d1670.png\" width=\"1327\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846\u804a\u5929\u63a5\u53e3<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1067\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110510-6a9aa5e644f66.png\" width=\"1327\" \/><\/p>\n<p>\u5b8c\u6574\u4ee3\u7801<\/p>\n<p>ChatView.vue<\/p>\n<p>&lt;!&#8211; ChatView.vue \u804a\u5929\u4e3b\u9875\u9762<br \/>\n\u4f5c\u7528&#xff1a;\u5c55\u793a\u804a\u5929\u5bf9\u8bdd\u6c14\u6ce1\u3001\u5386\u53f2\u6d88\u606f&#xff1b;\u5f15\u5165ChatInput\u8f93\u5165\u5b50\u7ec4\u4ef6&#xff1b;\u76d1\u542c\u5b50\u7ec4\u4ef6\u4e8b\u4ef6&#xff1b;\u8c03\u7528pinia\u804a\u5929\u4ed3\u5e93&#xff1b;\u6e32\u67d3markdown\u6d88\u606f&#xff1b;\u6eda\u52a8\u63a7\u5236<br \/>\n&#8211;&gt;<br \/>\n&lt;script setup lang&#061;&#034;ts&#034;&gt;<br \/>\n\/\/ \u5bfc\u5165vue\u5185\u7f6eAPI<br \/>\n\/\/ onMounted&#xff1a;\u7ec4\u4ef6\u6302\u8f7d\u5b8c\u6210\u4e4b\u540e\u6267\u884c\u7684\u94a9\u5b50\u51fd\u6570&#xff1b;nextTick&#xff1a;DOM\u66f4\u65b0\u5b8c\u6210\u540e\u6267\u884c&#xff1b;ref&#xff1a;\u521b\u5efa\u54cd\u5e94\u5f0f\u57fa\u7840\u53d8\u91cf<br \/>\nimport { onMounted, nextTick, ref } from &#039;vue&#039;;<br \/>\n\/\/ \u5bfc\u5165pinia\u4ed3\u5e93<br \/>\n\/\/ userStore \u7528\u6237\u72b6\u6001\u4ed3\u5e93&#xff0c;\u5b58\u7528\u6237id\u767b\u5f55\u4fe1\u606f<br \/>\nimport { useUserStore } from &#039;..\/stores\/user&#039;;<br \/>\n\/\/ chatStore \u804a\u5929\u4ed3\u5e93&#xff0c;\u5b58\u653e\u6d88\u606f\u6570\u7ec4\u3001\u53d1\u9001\u6d88\u606f\u3001\u6e05\u7a7a\u5386\u53f2\u3001\u52a0\u8f7d\u5386\u53f2\u6d88\u606f\u4e1a\u52a1\u51fd\u6570<br \/>\nimport { useChatStore } from &#039;..\/stores\/chat&#039;;<br \/>\n\/\/ vue-router\u8def\u7531&#xff0c;\u7528\u4e8e\u9875\u9762\u8df3\u8f6c<br \/>\nimport { useRouter } from &#039;vue-router&#039;;<br \/>\n\/\/ \u5bfc\u5165\u5b50\u7ec4\u4ef6&#xff1a;\u804a\u5929\u8f93\u5165\u63a7\u5236\u9762\u677f\u7ec4\u4ef6<br \/>\nimport Header from &#039;..\/components\/Header.vue&#039;;<br \/>\nimport ChatInput from &#039;..\/components\/ChatInput.vue&#039;;<br \/>\n\/\/ watch&#xff1a;\u76d1\u542c\u54cd\u5e94\u5f0f\u6570\u636e\u53d8\u5316<br \/>\nimport { watch } from &#039;vue&#039;;<br \/>\n\/\/ marked&#xff1a;markdown\u89e3\u6790\u5e93&#xff0c;\u628aAI\u8fd4\u56demarkdown\u6587\u672c\u8f6c\u4e3ahtml<br \/>\nimport { marked } from &#039;marked&#039;;<br \/>\n\/\/ highlight.js&#xff1a;\u4ee3\u7801\u9ad8\u4eae\u5e93&#xff0c;\u6e32\u67d3\u4ee3\u7801\u5757\u989c\u8272<br \/>\nimport hljs from &#039;highlight.js&#039;;<br \/>\n\/\/ marked-highlight&#xff1a;marked\u63d2\u4ef6&#xff0c;\u96c6\u6210hljs\u4ee3\u7801\u9ad8\u4eae<br \/>\nimport { markedHighlight } from &#039;marked-highlight&#039;;<br \/>\n\/\/ element-plus\u6d88\u606f\u63d0\u793a\u7ec4\u4ef6&#xff0c;\u5f39\u51fa\u6210\u529f\/\u5931\u8d25\u63d0\u793a\u6846<br \/>\nimport { ElMessage } from &#039;element-plus&#039;;<br \/>\n\/\/ \u83b7\u53d6pinia\u4ed3\u5e93\u5b9e\u4f8b<br \/>\nconst userStore &#061; useUserStore();<br \/>\nconst chatStore &#061; useChatStore();<br \/>\n\/\/ \u83b7\u53d6\u8def\u7531\u5b9e\u4f8b<br \/>\nconst router &#061; useRouter();<br \/>\n\/\/ \u54cd\u5e94\u5f0f\u5bf9\u8c61&#xff1a;\u8bb0\u5f55\u6bcf\u4e00\u6761\u77e5\u8bc6\u5e93\u6765\u6e90\u9762\u677f\u662f\u6298\u53e0\u8fd8\u662f\u5c55\u5f00<br \/>\n\/\/ key:\u6d88\u606f\u7684index\u7d22\u5f15&#xff1b;value:true\u6298\u53e0 false\u5c55\u5f00<br \/>\nconst sourcesCollapsed &#061; ref&lt;{ [key: number]: boolean }&gt;({});<br \/>\n\/\/ \u9875\u9762\u521a\u52a0\u8f7d&#xff0c;\u5224\u65ad\u7528\u6237\u662f\u5426\u767b\u5f55&#xff0c;\u5982\u679c\u6ca1\u6709userId&#xff0c;\u8df3\u8f6c\u5230\u9996\u9875\u767b\u5f55<br \/>\nif (!userStore.userId) {<br \/>\nrouter.push(&#039;\/&#039;);<br \/>\n}<br \/>\n\/\/ \u914d\u7f6emarked markdown\u6e32\u67d3\u63d2\u4ef6&#xff0c;\u5b9e\u73b0\u4ee3\u7801\u5757\u9ad8\u4eae<br \/>\nmarked.use(markedHighlight({<br \/>\n\/\/ css\u7c7b\u540d\u524d\u7f00&#xff0c;\u914d\u5408highlight.js\u6837\u5f0f<br \/>\nlangPrefix: &#039;hljs language-&#039;,<br \/>\n\/\/ \u5bf9\u4ee3\u7801\u5757\u8fdb\u884c\u9ad8\u4eae\u5904\u7406<br \/>\nhighlight(code, lang) {<br \/>\n\/\/ \u5982\u679c\u8bc6\u522b\u5230\u4ee3\u7801\u8bed\u8a00&#xff0c;\u5e76\u4e14hljs\u652f\u6301\u8be5\u8bed\u8a00<br \/>\nif (lang &amp;&amp; hljs.getLanguage(lang)) {<br \/>\ntry {<br \/>\n\/\/ \u4f7f\u7528\u6307\u5b9a\u8bed\u8a00\u9ad8\u4eae\u4ee3\u7801&#xff0c;\u8fd4\u56dehtml\u5b57\u7b26\u4e32<br \/>\nreturn hljs.highlight(code, { language: lang }).value;<br \/>\n} catch (err) {}<br \/>\n}<br \/>\n\/\/ \u8bc6\u522b\u4e0d\u51fa\u8bed\u8a00&#xff0c;\u81ea\u52a8\u731c\u6d4b\u8bed\u8a00\u8fdb\u884c\u9ad8\u4eae<br \/>\nreturn hljs.highlightAuto(code).value;<br \/>\n}<br \/>\n}));<br \/>\n\/**<br \/>\n&#064;description \u683c\u5f0f\u5316\u6d88\u606f\u5185\u5bb9&#xff0c;\u7528\u6237\u6d88\u606f\u8f6c\u4e49html&#xff1b;AI\u6d88\u606f\u6e32\u67d3markdown<br \/>\n&#064;param text \u539f\u59cb\u6d88\u606f\u6587\u672c<br \/>\n&#064;param role \u6d88\u606f\u89d2\u8272 user\u7528\u6237 \/ assistant AI<br \/>\n&#064;returns \u5904\u7406\u5b8c\u6210\u7684html\u5b57\u7b26\u4e32<br \/>\n*\/<br \/>\nconst formatMessage &#061; (text: string, role: string) &#061;&gt; {<br \/>\nconsole.log(&#039;22&#039;, text)<br \/>\n\/\/ \u5982\u679c\u6587\u672c\u4e3a\u7a7a&#xff0c;\u76f4\u63a5\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<br \/>\nif (!text) return &#039;&#039;;<br \/>\nif (role &#061;&#061;&#061; &#039;user&#039;) {<br \/>\n\/\/ \u7528\u6237\u6d88\u606f&#xff1a;\u4e0d\u80fd\u6e32\u67d3html&#xff0c;\u9632\u6b62XSS\u653b\u51fb&#xff1b;\u628a\u7279\u6b8a\u7b26\u53f7\u8f6c\u4e49&#xff1b;\u6362\u884c\u7b26\u66ff\u6362\u6210br\u6362\u884c\u6807\u7b7e<br \/>\nreturn text<br \/>\n.replace(\/&amp;\/g, &#039;&amp;amp;&#039;)<br \/>\n.replace(\/&lt;\/g, &#039;&amp;lt;&#039;)<br \/>\n.replace(\/&gt;\/g, &#039;&amp;gt;&#039;)<br \/>\n.replace(\/\\\\n\/g, &#039;&lt;br&gt;&#039;);<br \/>\n} else {<br \/>\n\/\/ AI\u6d88\u606f&#xff1a;\u5c06markdown\u6587\u672c\u8f6c\u4e3ahtml<br \/>\nreturn marked(text);<br \/>\n}<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u5207\u6362\u77e5\u8bc6\u5e93\u6765\u6e90\u9762\u677f\u6298\u53e0\/\u5c55\u5f00\u72b6\u6001<br \/>\n&#064;param messageIndex \u5f53\u524d\u6d88\u606f\u6570\u7ec4\u7684\u4e0b\u6807\u7d22\u5f15<br \/>\n*\/<br \/>\nconst toggleSources &#061; (messageIndex: number) &#061;&gt; {<br \/>\n\/\/ \u53d6\u53cd\u5e03\u5c14\u503c&#xff0c;true\u53d8false&#xff0c;false\u53d8true<br \/>\nsourcesCollapsed.value[messageIndex] &#061; !sourcesCollapsed.value[messageIndex];<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u521d\u59cb\u5316\u5355\u6761\u6d88\u606f\u7684\u6298\u53e0\u72b6\u6001&#xff0c;\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u8fc7&#xff0c;\u9ed8\u8ba4\u6298\u53e0 true<br \/>\n&#064;param messageIndex \u6d88\u606f\u4e0b\u6807<br \/>\n*\/<br \/>\nconst initSourcesCollapsed &#061; (messageIndex: number) &#061;&gt; {<br \/>\n\/\/ \u5224\u65ad\u8fd9\u4e2a\u4e0b\u6807\u5728\u5bf9\u8c61\u91cc\u662f\u5426\u4e0d\u5b58\u5728<br \/>\nif (sourcesCollapsed.value[messageIndex] &#061;&#061;&#061; undefined) {<br \/>\nsourcesCollapsed.value[messageIndex] &#061; true; \/\/ \u9ed8\u8ba4\u6298\u53e0<br \/>\n}<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u6587\u672c\u622a\u65ad&#xff0c;\u592a\u957f\u7684\u6765\u6e90\u5185\u5bb9\u505a\u9884\u89c8&#xff0c;\u8d85\u51fa\u90e8\u5206\u663e\u793a\u7701\u7565\u53f7<br \/>\n&#064;param text \u539f\u59cb\u6587\u672c<br \/>\n&#064;param maxLength \u6700\u5927\u5141\u8bb8\u5b57\u7b26<br \/>\n&#064;returns \u622a\u65ad\u540e\u7684\u5b57\u7b26\u4e32<br \/>\n*\/<br \/>\nconst truncateText &#061; (text: string, maxLength: number &#061; 150) &#061;&gt; {<br \/>\nif (!text) return &#039;&#039;;<br \/>\nreturn text.length &gt; maxLength ? text.substring(0, maxLength) &#043; &#039;&#8230;&#039; : text;<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u804a\u5929\u5bb9\u5668\u6eda\u52a8\u5230\u5e95\u90e8&#xff0c;\u67e5\u770b\u6700\u65b0\u6d88\u606f<br \/>\nnextTick&#xff1a;\u7b49\u5f85DOM\u9875\u9762\u6e32\u67d3\u5b8c\u6210\u4e4b\u540e\u518d\u6267\u884c\u6eda\u52a8\u64cd\u4f5c<br \/>\n*\/<br \/>\nconst scrollToBottom &#061; () &#061;&gt; {<br \/>\nnextTick(() &#061;&gt; {<br \/>\n\/\/ \u83b7\u53d6\u804a\u5929\u5bb9\u5668dom\u5143\u7d20<br \/>\nconst chatContainer &#061; document.getElementById(&#039;chat-container&#039;);<br \/>\nif (chatContainer) chatContainer.scrollTop &#061; chatContainer.scrollHeight;<br \/>\n});<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u6e05\u7a7a\u804a\u5929\u8bb0\u5f55&#xff0c;\u8c03\u7528pinia\u4ed3\u5e93\u65b9\u6cd5&#xff0c;\u540c\u65f6\u91cd\u7f6e\u672c\u5730\u6298\u53e0\u72b6\u6001<br \/>\n*\/<br \/>\nconst clearChatHistory &#061; async () &#061;&gt; {<br \/>\ntry {<br \/>\n\/\/ \u8c03\u7528pinia store\u6e05\u7a7a\u804a\u5929\u8bb0\u5f55\u5f02\u6b65\u51fd\u6570<br \/>\nawait chatStore.clearChatHistory();<br \/>\n\/\/ \u6e05\u7a7a\u6240\u6709\u6765\u6e90\u9762\u677f\u6298\u53e0\u72b6\u6001<br \/>\nsourcesCollapsed.value &#061; {};<br \/>\n\/\/ element plus\u5f39\u51fa\u6210\u529f\u63d0\u793a<br \/>\nElMessage({<br \/>\nmessage: &#039;\u804a\u5929\u8bb0\u5f55\u5df2\u6e05\u7a7a&#039;,<br \/>\ntype: &#039;success&#039;,<br \/>\nduration: 2000,<br \/>\n});<br \/>\n} catch (error) {<br \/>\nconsole.error(&#039;\u6e05\u7a7a\u804a\u5929\u8bb0\u5f55\u5931\u8d25:&#039;, error);<br \/>\nElMessage({<br \/>\nmessage: &#039;\u6e05\u7a7a\u804a\u5929\u8bb0\u5f55\u5931\u8d25&#xff0c;\u8bf7\u91cd\u8bd5&#039;,<br \/>\ntype: &#039;error&#039;,<br \/>\nduration: 3000,<br \/>\n});<br \/>\n}<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description onMounted&#xff1a;\u7ec4\u4ef6\u6302\u8f7d\u5b8c\u6bd5&#xff08;\u9875\u9762DOM\u5df2\u7ecf\u6e32\u67d3\u5b8c\u6210&#xff09;\u6267\u884c<br \/>\n\u9875\u9762\u6253\u5f00\u52a0\u8f7d\u5386\u53f2\u804a\u5929\u8bb0\u5f55<br \/>\n*\/<br \/>\nonMounted(() &#061;&gt; {<br \/>\nconsole.log(chatStore.loadChatHistory().then((aa) &#061;&gt; console.log(aa)))<br \/>\n\/\/ \u8c03\u7528pinia\u52a0\u8f7d\u5386\u53f2\u6d88\u606f&#xff0c;\u52a0\u8f7d\u5b8c\u6210\u6eda\u52a8\u5230\u5e95\u90e8<br \/>\nchatStore.loadChatHistory().then(() &#061;&gt; scrollToBottom());<br \/>\n});<br \/>\n\/**<br \/>\n&#064;description watch\u76d1\u542c\u5668<br \/>\n\u76d1\u542cchatStore.messages\u804a\u5929\u6d88\u606f\u6570\u7ec4\u53d8\u5316&#xff1b;deep:true\u6df1\u5ea6\u76d1\u542c\u6570\u7ec4\u5185\u90e8\u4fee\u6539<br \/>\n\u53ea\u8981\u6d88\u606f\u6570\u7ec4\u65b0\u589e\/\u4fee\u6539\u6d88\u606f&#xff0c;\u81ea\u52a8\u6eda\u52a8\u9875\u9762\u5230\u5e95\u90e8<br \/>\n*\/<br \/>\nwatch(<br \/>\n() &#061;&gt; chatStore.messages, \/\/ \u76d1\u542c\u76ee\u6807&#xff1a;pinia\u4ed3\u5e93\u91cc\u9762messages<br \/>\n() &#061;&gt; {<br \/>\nscrollToBottom(); \/\/ \u56de\u8c03\u51fd\u6570&#xff0c;\u6bcf\u6b21 messages \u53d8\u5316\u5c31\u6eda\u52a8\u5230\u5e95<br \/>\n},<br \/>\n{ deep: true }<br \/>\n);<br \/>\n&lt;\/script&gt;<br \/>\n&lt;template&gt;<br \/>\n&lt;!&#8211; \u9875\u9762\u6700\u5916\u5c42\u5bb9\u5668&#xff0c;\u6df1\u8272\u80cc\u666f&#xff0c;\u5168\u5c4f\u9ad8\u5ea6 &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;min-h-screen bg-gray-900 text-white&#034;&gt;<br \/>\n&lt;!&#8211; \u9876\u90e8\u5934\u90e8\u7ec4\u4ef6 &#8211;&gt;<br \/>\n&lt;Header \/&gt;<br \/>\n&amp;lt;!&#8211; \u9875\u9762\u4e3b\u4f53\u5bb9\u5668&#xff0c;\u6c34\u5e73\u5c45\u4e2d &#8211;&amp;gt;<br \/>\n&amp;lt;div class&#061;&#034;flex justify-center px-4 py-6&#034;&amp;gt;<br \/>\n  &amp;lt;!&#8211; \u6700\u5927\u5bbd\u5ea6\u5bb9\u5668&#xff0c;\u5f39\u6027\u5e03\u5c40&#xff0c;\u9ad8\u5ea6\u6263\u9664\u9876\u90e8\u9ad8\u5ea6 &#8211;&amp;gt;<br \/>\n  &amp;lt;div class&#061;&#034;w-full max-w-4xl flex flex-col h-[calc(100vh-120px)]&#034;&amp;gt;<\/p>\n<p>    &amp;lt;!&#8211; \u804a\u5929\u6d88\u606f\u5bb9\u5668&#xff0c;id\u7ed9js\u83b7\u53d6dom&#xff1b;overflow-y-auto\u5185\u5bb9\u8d85\u51fa\u51fa\u73b0\u6eda\u52a8\u6761 &#8211;&amp;gt;<br \/>\n    &amp;lt;div id&#061;&#034;chat-container&#034; class&#061;&#034;flex-1 overflow-y-auto space-y-4 mb-4 px-4 py-4 rounded-lg shadow-lg&#034;&amp;gt;<br \/>\n      &amp;lt;!&#8211; v-if&#xff1a;\u6d88\u606f\u6570\u7ec4\u4e3a\u7a7a&#xff0c;\u663e\u793a\u6b22\u8fce\u5f00\u59cb\u5bf9\u8bdd\u63d0\u793a &#8211;&amp;gt;<br \/>\n      &amp;lt;div v-if&#061;&#034;chatStore.messages.length &#061;&#061;&#061; 0&#034; class&#061;&#034;flex flex-col items-center justify-center h-full text-gray-400&#034;&amp;gt;<br \/>\n        &amp;lt;div class&#061;&#034;text-6xl mb-4&#034;&amp;gt;&#x1f4ac;&amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;h2 class&#061;&#034;text-2xl font-semibold mb-2&#034;&amp;gt;\u5f00\u59cb\u5bf9\u8bdd&amp;lt;\/h2&amp;gt;<br \/>\n        &amp;lt;p class&#061;&#034;text-center&#034;&amp;gt;\u5411\u6211\u63d0\u95ee\u4efb\u4f55\u95ee\u9898&#xff0c;\u6211\u4f1a\u5c3d\u529b\u4e3a\u4f60\u89e3\u7b54&amp;lt;\/p&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<\/p>\n<p>      &amp;lt;!&#8211; v-for\u5faa\u73af\u6e32\u67d3\u6bcf\u4e00\u6761\u804a\u5929\u6d88\u606f&#xff1b;key\u4f5c\u4e3a\u5217\u8868\u552f\u4e00\u6807\u8bc6 &#8211;&amp;gt;<br \/>\n      &amp;lt;div<br \/>\n        v-for&#061;&#034;(msg, index) in chatStore.messages&#034;<br \/>\n        :key&#061;&#034;index&#034;<br \/>\n        class&#061;&#034;flex flex-col&#034;<br \/>\n        :class&#061;&#034;msg.role &#061;&#061;&#061; &#039;user&#039; ? &#039;items-end&#039; : &#039;items-start&#039;&#034;<br \/>\n      &amp;gt;<br \/>\n        &amp;lt;!&#8211; \u6d88\u606f\u6c14\u6ce1\u5916\u5c42\u5bb9\u5668&#xff0c;\u7528\u6237\u5c45\u53f3&#xff0c;AI\u5c45\u5de6 &#8211;&amp;gt;<br \/>\n        &amp;lt;div class&#061;&#034;flex items-start&#034; :class&#061;&#034;msg.role &#061;&#061;&#061; &#039;user&#039; ? &#039;justify-end&#039; : &#039;justify-start&#039;&#034;&amp;gt;<br \/>\n          &amp;lt;!&#8211; AI\u5934\u50cf&#xff1a;AI\u6d88\u606f\u624d\u663e\u793a &#8211;&amp;gt;<br \/>\n          &amp;lt;div v-if&#061;&#034;msg.role !&#061;&#061; &#039;user&#039;&#034; class&#061;&#034;flex-shrink-0 mr-3&#034;&amp;gt;<br \/>\n            &amp;lt;div class&#061;&#034;w-8 h-8 bg-blue-600 rounded-full flex items-center justify-center text-sm font-semibold&#034;&amp;gt;<br \/>\n              AI<br \/>\n            &amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;\/div&amp;gt;<\/p>\n<p>          &amp;lt;!&#8211; v-html&#xff1a;\u6e32\u67d3html\u5b57\u7b26\u4e32&#xff0c;\u5c55\u793a\u683c\u5f0f\u5316\u540e\u7684\u6d88\u606f\u5185\u5bb9 &#8211;&amp;gt;<br \/>\n          &amp;lt;div<br \/>\n            v-html&#061;&#034;formatMessage(msg.content, msg.role)&#034;<br \/>\n            class&#061;&#034;max-w-2xl px-4 py-3 rounded-lg shadow-sm prose prose-invert max-w-none&#034;<br \/>\n            :class&#061;&#034;<br \/>\n              msg.role &#061;&#061;&#061; &#039;user&#039;<br \/>\n                ? &#039;bg-blue-600 text-white rounded-br-sm prose-headings:text-white prose-p:text-white prose-strong:text-white prose-em:text-white&#039;<br \/>\n                : &#039;bg-gray-700 text-white rounded-bl-sm prose-headings:text-gray-100 prose-p:text-gray-100 prose-strong:text-gray-100 prose-em:text-gray-100 prose-code:text-blue-300 prose-code:bg-gray-800 prose-pre:bg-gray-800 prose-blockquote:border-blue-500&#039;<br \/>\n            &#034;<br \/>\n          &amp;gt;&amp;lt;\/div&amp;gt;<\/p>\n<p>          &amp;lt;!&#8211; \u7528\u6237\u5934\u50cf&#xff1a;\u7528\u6237\u6d88\u606f\u624d\u663e\u793a &#8211;&amp;gt;<br \/>\n          &amp;lt;div v-if&#061;&#034;msg.role &#061;&#061;&#061; &#039;user&#039;&#034; class&#061;&#034;flex-shrink-0 ml-3&#034;&amp;gt;<br \/>\n            &amp;lt;div class&#061;&#034;w-8 h-8 bg-gray-600 rounded-full flex items-center justify-center text-sm font-semibold&#034;&amp;gt;<br \/>\n              \u6211<br \/>\n            &amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;\/div&amp;gt;<\/p>\n<p>        &amp;lt;!&#8211; AI\u56de\u590d\u5e76\u4e14\u5b58\u5728\u77e5\u8bc6\u5e93\u6765\u6e90\u4fe1\u606f&#xff0c;\u6e32\u67d3\u6765\u6e90\u6298\u53e0\u9762\u677f &#8211;&amp;gt;<br \/>\n        &amp;lt;div<br \/>\n          v-if&#061;&#034;msg.role &#061;&#061;&#061; &#039;assistant&#039; &amp;amp;&amp;amp; msg.sources_info_list &amp;amp;&amp;amp; msg.sources_info_list.length &amp;gt; 0&#034;<br \/>\n          class&#061;&#034;w-full max-w-2xl mt-3 ml-11&#034;<br \/>\n        &amp;gt;<br \/>\n          {{ initSourcesCollapsed(index) }}<br \/>\n          &amp;lt;!&#8211; \u6765\u6e90\u4fe1\u606f\u6298\u53e0\u9762\u677f\u5916\u5c42\u76d2\u5b50 &#8211;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;bg-gray-800 rounded-lg border border-gray-600 overflow-hidden&#034;&amp;gt;<br \/>\n            &amp;lt;!&#8211; \u6298\u53e0\u9762\u677f\u5934\u90e8\u6309\u94ae&#xff0c;\u70b9\u51fb\u5207\u6362\u5c55\u5f00\u6298\u53e0 &#8211;&amp;gt;<br \/>\n            &amp;lt;button<br \/>\n              &#064;click&#061;&#034;toggleSources(index)&#034;<br \/>\n              class&#061;&#034;w-full px-4 py-3 bg-gray-800 hover:bg-gray-750 transition-colors duration-200 flex items-center justify-between text-left&#034;<br \/>\n            &amp;gt;<br \/>\n              &amp;lt;div class&#061;&#034;flex items-center space-x-2&#034;&amp;gt;<br \/>\n                &amp;lt;svg class&#061;&#034;w-5 h-5 text-blue-400&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n                  &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034; d&#061;&#034;M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z&#034;&amp;gt;&amp;lt;\/path&amp;gt;<br \/>\n                &amp;lt;\/svg&amp;gt;<br \/>\n                &amp;lt;span class&#061;&#034;font-medium text-gray-200&#034;&amp;gt;\u76f8\u5173\u6765\u6e90&amp;lt;\/span&amp;gt;<br \/>\n                &amp;lt;span class&#061;&#034;text-sm text-gray-400&#034;&amp;gt;({{ msg.sources_info_list.length }} \u4e2a)&amp;lt;\/span&amp;gt;<br \/>\n              &amp;lt;\/div&amp;gt;<br \/>\n              &amp;lt;!&#8211; \u7bad\u5934\u56fe\u6807&#xff0c;\u5c55\u5f00\u65cb\u8f6c180\u5ea6 &#8211;&amp;gt;<br \/>\n              &amp;lt;svg<br \/>\n                class&#061;&#034;w-5 h-5 text-gray-400 transform transition-transform duration-200&#034;<br \/>\n                :class&#061;&#034;{ &#039;rotate-180&#039;: !sourcesCollapsed[index] }&#034;<br \/>\n                fill&#061;&#034;none&#034;<br \/>\n                stroke&#061;&#034;currentColor&#034;<br \/>\n                viewBox&#061;&#034;0 0 24 24&#034;<br \/>\n              &amp;gt;<br \/>\n                &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034; d&#061;&#034;M19 9l-7 7-7-7&#034;&amp;gt;&amp;lt;\/path&amp;gt;<br \/>\n              &amp;lt;\/svg&amp;gt;<br \/>\n            &amp;lt;\/button&amp;gt;<\/p>\n<p>            &amp;lt;!&#8211; v-show\u63a7\u5236\u9762\u677f\u5185\u5bb9\u663e\u793a\u9690\u85cf&#xff0c;true\u663e\u793a false\u9690\u85cf &#8211;&amp;gt;<br \/>\n            &amp;lt;div<br \/>\n              v-show&#061;&#034;!sourcesCollapsed[index]&#034;<br \/>\n              class&#061;&#034;border-t border-gray-600 bg-gray-750&#034;<br \/>\n            &amp;gt;<br \/>\n              &amp;lt;div class&#061;&#034;p-4 space-y-3 max-h-96 overflow-y-auto&#034;&amp;gt;<br \/>\n                &amp;lt;!&#8211; \u5faa\u73af\u6e32\u67d3\u6bcf\u4e00\u6761\u77e5\u8bc6\u5e93\u6765\u6e90\u7247\u6bb5 &#8211;&amp;gt;<br \/>\n                &amp;lt;div<br \/>\n                  v-for&#061;&#034;(source, sourceIndex) in msg.sources_info_list&#034;<br \/>\n                  :key&#061;&#034;sourceIndex&#034;<br \/>\n                  class&#061;&#034;bg-gray-700 rounded-lg p-3 border-l-4 border-blue-500&#034;<br \/>\n                &amp;gt;<br \/>\n                  &amp;lt;div class&#061;&#034;flex items-start justify-between mb-2&#034;&amp;gt;<br \/>\n                    &amp;lt;h4 class&#061;&#034;font-medium text-gray-100 text-sm line-clamp-2&#034;&amp;gt;<br \/>\n                      {{ &#096;\u6765\u6e90 ${sourceIndex &#043; 1}&#096; }}<br \/>\n                    &amp;lt;\/h4&amp;gt;<br \/>\n                    &amp;lt;!&#8211; \u76f8\u4f3c\u5ea6\u5206\u6570\u767e\u5206\u6bd4 &#8211;&amp;gt;<br \/>\n                    &amp;lt;span<br \/>\n                      v-if&#061;&#034;source&#034;<br \/>\n                      class&#061;&#034;ml-2 px-2 py-1 bg-blue-600 text-white text-xs rounded-full flex-shrink-0&#034;<br \/>\n                    &amp;gt;<br \/>\n                      {{ Math.round(source.score * 100) }}%<br \/>\n                    &amp;lt;\/span&amp;gt;<br \/>\n                  &amp;lt;\/div&amp;gt;<\/p>\n<p>                  &amp;lt;!&#8211; \u6765\u6e90\u7247\u6bb5\u9884\u89c8\u6587\u672c &#8211;&amp;gt;<br \/>\n                  &amp;lt;p class&#061;&#034;text-gray-300 text-sm leading-relaxed mb-2&#034;&amp;gt;<br \/>\n                    {{ truncateText(source.content) }}<br \/>\n                  &amp;lt;\/p&amp;gt;<\/p>\n<p>                  &amp;lt;!&#8211; \u5982\u679c\u5b58\u5728url&#xff0c;\u6e32\u67d3\u539f\u6587\u94fe\u63a5&#xff0c;\u65b0\u6807\u7b7e\u9875\u6253\u5f00 &#8211;&amp;gt;<br \/>\n                  &amp;lt;div v-if&#061;&#034;source.url&#034; class&#061;&#034;flex items-center justify-between&#034;&amp;gt;<br \/>\n                    &amp;lt;a<br \/>\n                      :href&#061;&#034;source.url&#034;<br \/>\n                      target&#061;&#034;_blank&#034;<br \/>\n                      rel&#061;&#034;noopener noreferrer&#034;<br \/>\n                      class&#061;&#034;text-blue-400 hover:text-blue-300 text-xs flex items-center space-x-1 transition-colors&#034;<br \/>\n                    &amp;gt;<br \/>\n                      &amp;lt;svg class&#061;&#034;w-3 h-3&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n                        &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034; d&#061;&#034;M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14&#034;&amp;gt;&amp;lt;\/path&amp;gt;<br \/>\n                      &amp;lt;\/svg&amp;gt;<br \/>\n                      &amp;lt;span&amp;gt;\u67e5\u770b\u539f\u6587&amp;lt;\/span&amp;gt;<br \/>\n                    &amp;lt;\/a&amp;gt;<br \/>\n                  &amp;lt;\/div&amp;gt;<br \/>\n                &amp;lt;\/div&amp;gt;<br \/>\n              &amp;lt;\/div&amp;gt;<br \/>\n            &amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;\/div&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<\/p>\n<p>      &amp;lt;!&#8211; isLoading\u4e3atrue&#xff0c;\u663e\u793aAI\u52a0\u8f7d\u4e2d\u52a8\u753b &#8211;&amp;gt;<br \/>\n      &amp;lt;div v-if&#061;&#034;chatStore.isLoading&#034; class&#061;&#034;flex justify-start&#034;&amp;gt;<br \/>\n        &amp;lt;div class&#061;&#034;flex-shrink-0 mr-3&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-8 h-8 bg-blue-600 rounded-full flex items-center justify-center text-sm font-semibold&#034;&amp;gt;<br \/>\n            AI<br \/>\n          &amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;div class&#061;&#034;bg-gray-700 text-white px-4 py-3 rounded-lg rounded-bl-sm shadow-sm&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;flex items-center space-x-2&#034;&amp;gt;<br \/>\n            &amp;lt;!&#8211; \u4e09\u4e2a\u8df3\u52a8\u5706\u70b9\u52a8\u753b &#8211;&amp;gt;<br \/>\n            &amp;lt;div class&#061;&#034;flex space-x-1&#034;&amp;gt;<br \/>\n              &amp;lt;div class&#061;&#034;w-2 h-2 bg-gray-400 rounded-full animate-bounce&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n              &amp;lt;div class&#061;&#034;w-2 h-2 bg-gray-400 rounded-full animate-bounce&#034; style&#061;&#034;animation-delay: 0.1s&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n              &amp;lt;div class&#061;&#034;w-2 h-2 bg-gray-400 rounded-full animate-bounce&#034; style&#061;&#034;animation-delay: 0.2s&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n            &amp;lt;\/div&amp;gt;<br \/>\n            &amp;lt;span class&#061;&#034;text-sm text-gray-400&#034;&amp;gt;\u6b63\u5728\u601d\u8003\u4e2d&#8230;&amp;lt;\/span&amp;gt;<br \/>\n          &amp;lt;\/div&amp;gt;<br \/>\n        &amp;lt;\/div&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<\/p>\n<p>    &amp;lt;!&#8211; ChatInput\u5b50\u7ec4\u4ef6&#xff1a;\u8f93\u5165\u3001\u53c2\u6570\u63a7\u5236\u9762\u677f &#8211;&amp;gt;<br \/>\n    &amp;lt;!&#8211; &#064;send&#xff1a;\u76d1\u542c\u5b50\u7ec4\u4ef6\u629b\u51fasend\u81ea\u5b9a\u4e49\u4e8b\u4ef6&#xff0c;\u89e6\u53d1chatStore.sendMessage&#xff0c;\u5b50\u7ec4\u4ef6emit\u53c2\u6570\u81ea\u52a8\u4f20\u5165\u51fd\u6570 &#8211;&amp;gt;<br \/>\n    &amp;lt;!&#8211; &#064;clear-chat&#xff1a;\u76d1\u542c\u5b50\u7ec4\u4ef6\u6e05\u7a7a\u804a\u5929\u4e8b\u4ef6&#xff0c;\u89e6\u53d1\u7236\u7ec4\u4ef6clearChatHistory\u65b9\u6cd5 &#8211;&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;flex-shrink-0&#034;&amp;gt;<br \/>\n      &amp;lt;ChatInput &#064;send&#061;&#034;chatStore.sendMessage&#034; &#064;clear-chat&#061;&#034;clearChatHistory&#034; \/&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;\/div&amp;gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;style scoped&gt;<br \/>\n\/* \u6837\u5f0f scoped\u4ee3\u8868\u53ea\u4f5c\u7528\u4e8e\u5f53\u524d\u7ec4\u4ef6&#xff0c;\u4e0d\u6c61\u67d3\u5176\u4ed6\u7ec4\u4ef6 \/<br \/>\n\/ \u4e4b\u524d\u7684\u6837\u5f0f\u4ee3\u7801\u4fdd\u6301\u4e0d\u53d8 *\/<br \/>\n&lt;\/style&gt;<\/p>\n<p>ChatInput.vue<\/p>\n<p>&lt;!&#8211; ChatInput.vue<br \/>\n\u5b50\u7ec4\u4ef6&#xff1a;\u804a\u5929\u8f93\u5165\u6846\u3001\u6a21\u578b\u9009\u62e9\u3001\u77e5\u8bc6\u5e93\u5f00\u5173\u3001\u6e29\u5ea6\u6ed1\u5757\u3001\u6587\u4ef6\u4e0a\u4f20\u6309\u94ae<br \/>\n\u804c\u8d23&#xff1a;\u6536\u96c6\u7528\u6237\u8f93\u5165\u3001\u53c2\u6570&#xff1b;\u5411\u5916\u629b\u51fa\u81ea\u5b9a\u4e49\u4e8b\u4ef6 send \/ clear\u2011chat&#xff1b;\u4e0d\u7ef4\u62a4\u804a\u5929\u6d88\u606f\u72b6\u6001<br \/>\n&#8211;&gt;<br \/>\n&lt;script setup lang&#061;&#034;ts&#034;&gt;<br \/>\n\/\/ ref \u521b\u5efa\u54cd\u5e94\u5f0f\u53d8\u91cf<br \/>\nimport { ref } from &#039;vue&#039;;<br \/>\n\/\/ axios http\u8bf7\u6c42\u5e93&#xff0c;\u7528\u4e8e\u4e0a\u4f20\u6587\u4ef6<br \/>\nimport axios from &#039;axios&#039;;<br \/>\n\/\/ element-plus\u6d88\u606f\u63d0\u793a\u6846<br \/>\nimport { ElMessage } from &#039;element-plus&#039;<br \/>\n\/\/ pinia \u7528\u6237\u4ed3\u5e93<br \/>\nimport { useUserStore } from &#039;..\/stores\/user&#039;;<br \/>\n\/\/ \u7528\u6237\u8f93\u5165\u6846\u5185\u5bb9<br \/>\nconst message &#061; ref(&#039;&#039;);<br \/>\n\/\/ \u5f53\u524d\u9009\u4e2d\u5927\u6a21\u578b<br \/>\nconst model &#061; ref(&#034;qwen3.7-plus&#034;);<br \/>\n\/\/ temperature \u6e29\u5ea6\u53c2\u6570&#xff0c;\u63a7\u5236AI\u521b\u9020\u6027<br \/>\nconst temperature &#061; ref(0.7);<br \/>\n\/\/ maxTokens AI\u6700\u5927\u8f93\u51fatoken<br \/>\nconst maxTokens &#061; ref(2000);<br \/>\n\/\/ knowledge_bool \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93RAG\u68c0\u7d22<br \/>\nconst knowledge_bool &#061; ref(false)<br \/>\n\/\/ defineEmits&#xff1a;\u5b9a\u4e49\u5f53\u524d\u7ec4\u4ef6\u5411\u5916\u629b\u51fa\u7684\u81ea\u5b9a\u4e49\u4e8b\u4ef6<br \/>\n\/\/ send&#xff1a;\u53d1\u9001\u6d88\u606f\u4e8b\u4ef6&#xff1b;clear\u2011chat&#xff1a;\u6e05\u7a7a\u804a\u5929\u4e8b\u4ef6<br \/>\nconst emit &#061; defineEmits([&#039;send&#039;, &#039;clear-chat&#039;]);<br \/>\n\/\/ \u6a21\u578b\u4e0b\u62c9\u9009\u62e9\u6846\u9009\u9879\u6570\u7ec4<br \/>\nconst models &#061; [<br \/>\n{ value: &#034;qwen3.7-plus&#034;, label: &#034;qwen3.7-plus&#034; },<br \/>\n];<br \/>\n\/\/ \u6587\u4ef6\u662f\u5426\u6b63\u5728\u4e0a\u4f20\u4e2d<br \/>\nconst isUploading &#061; ref(false);<br \/>\n\/\/ \u83b7\u53d6\u9690\u85cf\u7684file\u6587\u4ef6\u9009\u62e9dom\u6807\u7b7e<br \/>\nconst fileInput &#061; ref&lt;HTMLInputElement | null&gt;(null);<br \/>\n\/**<br \/>\n&#064;description \u53d1\u9001\u6d88\u606f\u6309\u94ae \/ \u56de\u8f66\u6309\u4e0b\u6267\u884c\u51fd\u6570<br \/>\n*\/<br \/>\nconst sendMessage &#061; () &#061;&gt; {<br \/>\n\/\/ \u5982\u679c\u8f93\u5165\u6846\u5168\u662f\u7a7a\u683c&#xff0c;\u76f4\u63a5return&#xff0c;\u4e0d\u53d1\u9001<br \/>\nif (!message.value.trim()) return;<br \/>\n\/\/ emit\u5411\u5916\u89e6\u53d1 send\u4e8b\u4ef6&#xff0c;\u628a\u6240\u6709\u6536\u96c6\u5230\u7684\u53c2\u6570\u5168\u90e8\u4f20\u7ed9\u7236\u7ec4\u4ef6 ChatView<br \/>\nemit(&#039;send&#039;, message.value, model.value, knowledge_bool.value, temperature.value, maxTokens.value);<br \/>\n\/\/ \u53d1\u9001\u5b8c\u6210\u6e05\u7a7a\u8f93\u5165\u6846\u5185\u5bb9<br \/>\nmessage.value &#061; &#039;&#039;;<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u70b9\u51fb\u6e05\u7a7a\u8bb0\u5f55\u6309\u94ae&#xff0c;\u629b\u51faclear\u2011chat\u4e8b\u4ef6&#xff0c;\u901a\u77e5\u7236\u7ec4\u4ef6\u6e05\u7a7a\u804a\u5929<br \/>\n*\/<br \/>\nconst clearChat &#061; async () &#061;&gt; {<br \/>\nemit(&#039;clear-chat&#039;);<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u6587\u4ef6\u9009\u62e9\u6846\u9009\u62e9\u6587\u4ef6\u4e4b\u540e\u89e6\u53d1\u7684\u56de\u8c03&#xff0c;\u4e0a\u4f20\u6587\u6863\u5230\u540e\u7aef\u63a5\u53e3<br \/>\n&#064;param event change\u4e8b\u4ef6\u5bf9\u8c61<br \/>\n*\/<br \/>\nconst handleFileUpload &#061; async (event: Event) &#061;&gt; {<br \/>\n\/\/ \u83b7\u53d6\u4e8b\u4ef6\u76ee\u6807&#xff0c;\u5c31\u662ffile input dom<br \/>\nconst target &#061; event.target as HTMLInputElement;<br \/>\n\/\/ \u83b7\u53d6\u9009\u4e2d\u7b2c\u4e00\u4e2a\u6587\u4ef6<br \/>\nconst file &#061; target.files?.[0];<br \/>\nif (!file) return;<br \/>\n\/\/ \u4fee\u6539\u72b6\u6001&#xff1a;\u6b63\u5728\u4e0a\u4f20<br \/>\nisUploading.value &#061; true;<br \/>\n\/\/ FormData\u8868\u5355\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u4f20\u9012\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff0c;\u6587\u4ef6\u4e0a\u4f20\u5fc5\u987b\u4f7f\u7528FormData<br \/>\nconst formData &#061; new FormData();<br \/>\n\/\/ \u6dfb\u52a0\u6587\u4ef6&#xff0c;\u5b57\u6bb5\u540dfiles&#xff0c;\u540e\u7aef\u63a5\u53e3\u7ea6\u5b9a\u63a5\u6536\u5b57\u6bb5\u540d<br \/>\nformData.append(&#039;files&#039;, file);<br \/>\ntry {<br \/>\n\/\/ http post\u4e0a\u4f20\u8bf7\u6c42<br \/>\nconst response &#061; await axios.post(<br \/>\n${import.meta.env.VITE_API_URL}\/api\/docs\/upload,<br \/>\nformData,<br \/>\n{<br \/>\nheaders: {<br \/>\n\/\/ \u8bf7\u6c42\u5934\u643a\u5e26\u767b\u5f55\u8eab\u4efd\u51ed\u8bc1<br \/>\n&#039;Authorization&#039;: Bearer ${useUserStore().userId},<br \/>\n\/\/ \u26a0\ufe0f\u4e0d\u8981\u624b\u52a8\u5199Content-Type:multipart\/form-data&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u5e26\u4e0a\u5e76\u4e14\u586b\u5145boundary\u5206\u9694\u7b26&#xff0c;\u624b\u52a8\u5199\u4f1a\u4e0a\u4f20\u5931\u8d25<br \/>\n}<br \/>\n}<br \/>\n);<br \/>\n\/\/ \u5224\u65ad\u540e\u7aef\u8fd4\u56de\u72b6\u6001\u6210\u529f<br \/>\nif (response.data.status &#061;&#061;&#061; &#034;success&#034;) {<br \/>\n  const result &#061; response.data.processed_files;<br \/>\n  console.log(&#039;\u6587\u4ef6\u4e0a\u4f20\u6210\u529f:&#039;, result);<br \/>\n  ElMessage({<br \/>\n    message: &#096;\u6587\u4ef6\u4e0a\u4f20\u6210\u529f&#xff01;\u5171\u5904\u7406\u4e86 ${result.length} \u4e2a\u6587\u4ef6&#096;,<br \/>\n    type: &#039;success&#039;,<br \/>\n    duration: 3000<br \/>\n  });<\/p>\n<p>} else {<br \/>\n  console.error(&#039;\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25&#039;);<br \/>\n  ElMessage({<br \/>\n    message: &#039;\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25&#xff0c;\u8bf7\u91cd\u8bd5&#039;,<br \/>\n    type: &#039;error&#039;,<br \/>\n    duration: 3000<br \/>\n  });<br \/>\n}<br \/>\n} catch (error) {<br \/>\n\/\/ \u8bf7\u6c42\u5f02\u5e38\u6355\u83b7&#xff0c;\u7f51\u7edc\u9519\u8bef\u3001\u540e\u7aef\u62a5\u9519\u4f1a\u8d70\u5230\u8fd9\u91cc<br \/>\nconsole.error(&#039;\u4e0a\u4f20\u9519\u8bef:&#039;, error);<br \/>\n} finally {<br \/>\n\/\/ \u65e0\u8bba\u6210\u529f\u5931\u8d25&#xff0c;\u90fd\u4f1a\u6267\u884c&#xff1b;\u5173\u95ed\u4e0a\u4f20loading\u72b6\u6001&#xff1b;\u6e05\u7a7afile input\u7684\u503c&#xff0c;\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u540c\u4e00\u4e2a\u6587\u4ef6<br \/>\nisUploading.value &#061; false;<br \/>\nif (target) target.value &#061; &#039;&#039;;<br \/>\n}<br \/>\n};<br \/>\n\/**<br \/>\n&#064;description \u70b9\u51fb\u4e0a\u4f20\u6587\u6863\u6309\u94ae&#xff0c;\u8c03\u7528\u9690\u85cf\u539f\u751f\u6587\u4ef6\u9009\u62e9\u6846\u70b9\u51fb&#xff0c;\u5524\u8d77\u9009\u62e9\u6587\u4ef6\u5f39\u7a97<br \/>\n*\/<br \/>\nconst triggerFileUpload &#061; () &#061;&gt; {<br \/>\nfileInput.value?.click();<br \/>\n};<br \/>\n&lt;\/script&gt;<br \/>\n&lt;template&gt;<br \/>\n&lt;!&#8211; \u5f53\u524dChatInput\u7ec4\u4ef6\u6574\u4f53UI\u5bb9\u5668 &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;p-6 bg-gray-900\/50 backdrop-blur-sm rounded-xl border border-gray-700\/50&#034;&gt;<br \/>\n&lt;!&#8211; \u7b2c\u4e00\u884c&#xff1a;\u5404\u79cd\u63a7\u5236\u6309\u94ae \u77e5\u8bc6\u5e93\u5f00\u5173\u3001\u6a21\u578b\u4e0b\u62c9\u3001\u4e0a\u4f20\u6587\u4ef6\u3001\u6e05\u7a7a\u8bb0\u5f55 &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;flex flex-wrap items-center gap-6 mb-6&#034;&gt;<br \/>\n&lt;!&#8211; \u77e5\u8bc6\u5e93\u5f00\u5173\u6309\u94ae &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;flex items-center space-x-3&#034;&gt;<br \/>\n&lt;span class&#061;&#034;text-sm font-medium text-gray-300&#034;&gt;\u77e5\u8bc6\u5e93&lt;\/span&gt;<br \/>\n&lt;!&#8211; &#064;click\u70b9\u51fb\u5207\u6362\u5e03\u5c14\u503ctrue\/false &#8211;&gt;<br \/>\n&lt;button &#064;click&#061;&#034;knowledge_bool &#061; !knowledge_bool&#034; :class&#061;&#034;[<br \/>\n&#039;relative inline-flex h-6 w-11 items-center rounded-full transition-all duration-300 ease-in-out focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:ring-offset-2 focus:ring-offset-gray-900&#039;,<br \/>\nknowledge_bool ? &#039;bg-gradient-to-r from-emerald-500 to-teal-600&#039; : &#039;bg-gray-600&#039;<br \/>\n]&#034;&gt;<br \/>\n&lt;span :class&#061;&#034;[<br \/>\n&#039;inline-flex h-4 w-4 items-center justify-center transform rounded-full bg-white transition-all duration-300 ease-in-out shadow-lg&#039;,<br \/>\nknowledge_bool ? &#039;translate-x-6&#039; : &#039;translate-x-1&#039;<br \/>\n]&#034;&gt;<br \/>\n&lt;svg :class&#061;&#034;[<br \/>\n&#039;h-2.5 w-2.5 transition-colors duration-200&#039;,<br \/>\nknowledge_bool ? &#039;text-emerald-600&#039; : &#039;text-gray-400&#039;<br \/>\n]&#034; fill&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 20 20&#034;&gt;<br \/>\n&lt;path d&#061;&#034;M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z&#034; \/&gt;<br \/>\n&lt;\/svg&gt;<br \/>\n&lt;\/span&gt;<br \/>\n&lt;\/button&gt;<br \/>\n&lt;\/div&gt;<br \/>\n  &amp;lt;!&#8211; AI\u6a21\u578b\u4e0b\u62c9\u9009\u62e9\u6846 &#8211;&amp;gt;<br \/>\n  &amp;lt;div class&#061;&#034;flex items-center space-x-3&#034;&amp;gt;<br \/>\n    &amp;lt;label class&#061;&#034;text-sm font-medium text-gray-300&#034;&amp;gt;AI\u6a21\u578b:&amp;lt;\/label&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;relative&#034;&amp;gt;<br \/>\n      &amp;lt;!&#8211; v-model\u53cc\u5411\u7ed1\u5b9amodel\u54cd\u5e94\u5f0f\u53d8\u91cf&#xff0c;\u4e0b\u62c9\u9009\u62e9\u81ea\u52a8\u4fee\u6539\u53d8\u91cf &#8211;&amp;gt;<br \/>\n      &amp;lt;select v-model&#061;&#034;model&#034;<br \/>\n        class&#061;&#034;appearance-none bg-gray-800 text-white text-sm rounded-lg border border-gray-600 px-4 py-2 pr-8 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 min-w-[160px] transition-all duration-200 hover:bg-gray-700&#034;&amp;gt;<br \/>\n        &amp;lt;!&#8211; v-for\u5faa\u73af\u6e32\u67d3\u4e0b\u62c9\u9009\u9879 &#8211;&amp;gt;<br \/>\n        &amp;lt;option v-for&#061;&#034;modelOption in models&#034; :key&#061;&#034;modelOption.value&#034; :value&#061;&#034;modelOption.value&#034;<br \/>\n          class&#061;&#034;bg-gray-800&#034;&amp;gt;<br \/>\n          {{ modelOption.label }}<br \/>\n        &amp;lt;\/option&amp;gt;<br \/>\n      &amp;lt;\/select&amp;gt;<br \/>\n      &amp;lt;!&#8211; \u4e0b\u62c9\u7bad\u5934\u88c5\u9970\u56fe\u6807 &#8211;&amp;gt;<br \/>\n      &amp;lt;div class&#061;&#034;absolute inset-y-0 right-0 flex items-center px-2 pointer-events-none&#034;&amp;gt;<br \/>\n        &amp;lt;svg class&#061;&#034;w-4 h-4 text-gray-400&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n          &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034; d&#061;&#034;M19 9l-7 7-7-7&#034; \/&amp;gt;<br \/>\n        &amp;lt;\/svg&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<\/p>\n<p>  &amp;lt;!&#8211; \u4e0a\u4f20\u6587\u6863\u6309\u94ae&#xff0c;\u70b9\u51fb\u5524\u8d77\u6587\u4ef6\u9009\u62e9 &#8211;&amp;gt;<br \/>\n  &amp;lt;button &#064;click&#061;&#034;triggerFileUpload&#034; :disabled&#061;&#034;isUploading&#034; :class&#061;&#034;[<br \/>\n    &#039;flex items-center gap-2 px-4 py-2 text-sm font-medium rounded-lg transition-all duration-200 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-900&#039;,<br \/>\n    isUploading<br \/>\n      ? &#039;bg-gray-600 text-gray-400 cursor-not-allowed&#039;<br \/>\n      : &#039;bg-gradient-to-r from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 text-white shadow-lg hover:shadow-blue-500\/25&#039;<br \/>\n  ]&#034;&amp;gt;<br \/>\n    &amp;lt;svg :class&#061;&#034;[<br \/>\n      &#039;w-4 h-4&#039;,<br \/>\n      isUploading ? &#039;animate-spin&#039; : &#039;&#039;<br \/>\n    ]&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n      &amp;lt;path v-if&#061;&#034;!isUploading&#034; stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n        d&#061;&#034;M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12&#034; \/&amp;gt;<br \/>\n      &amp;lt;path v-else stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n        d&#061;&#034;M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15&#034; \/&amp;gt;<br \/>\n    &amp;lt;\/svg&amp;gt;<br \/>\n    &amp;lt;span&amp;gt;{{ isUploading ? &#039;\u4e0a\u4f20\u4e2d&#8230;&#039; : &#039;\u4e0a\u4f20\u6587\u6863&#039; }}&amp;lt;\/span&amp;gt;<br \/>\n  &amp;lt;\/button&amp;gt;<\/p>\n<p>  &amp;lt;!&#8211; \u6e05\u7a7a\u804a\u5929\u8bb0\u5f55\u6309\u94ae&#xff0c;\u70b9\u51fb\u629b\u51faclear\u2011chat\u4e8b\u4ef6 &#8211;&amp;gt;<br \/>\n  &amp;lt;button<br \/>\n    &#064;click&#061;&#034;clearChat&#034;<br \/>\n    class&#061;&#034;flex items-center gap-2 px-4 py-2 text-sm font-medium rounded-lg border border-yellow-500\/40 bg-yellow-500\/10 text-yellow-200 hover:bg-yellow-500\/20 hover:text-yellow-100 transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-yellow-500 focus:ring-offset-2 focus:ring-offset-gray-900&#034;<br \/>\n  &amp;gt;<br \/>\n    &amp;lt;svg class&#061;&#034;w-4 h-4&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n      &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034; d&#061;&#034;M3 6h18M8 6V4h8v2m-9 4v8m5-8v8m5-8v8M5 6l1 14h12l1-14&#034; \/&amp;gt;<br \/>\n    &amp;lt;\/svg&amp;gt;<br \/>\n    &amp;lt;span&amp;gt;\u6e05\u7a7a\u8bb0\u5f55&amp;lt;\/span&amp;gt;<br \/>\n  &amp;lt;\/button&amp;gt;<\/p>\n<p>  &amp;lt;!&#8211; \u539f\u751f\u9690\u85cf\u6587\u4ef6\u9009\u62e9\u6846&#xff0c;accept\u9650\u5b9a\u5141\u8bb8\u9009\u62e9\u6587\u4ef6\u540e\u7f00&#xff1b;&#064;change\u9009\u4e2d\u6587\u4ef6\u540e\u89e6\u53d1\u4e0a\u4f20\u51fd\u6570 &#8211;&amp;gt;<br \/>\n  &amp;lt;input ref&#061;&#034;fileInput&#034; type&#061;&#034;file&#034; &#064;change&#061;&#034;handleFileUpload&#034; class&#061;&#034;hidden&#034; accept&#061;&#034;.txt,.pdf,.doc,.docx,.md&#034; \/&amp;gt;<br \/>\n&amp;lt;\/div&amp;gt;<\/p>\n<p>&amp;lt;!&#8211; \u7b2c\u4e8c\u884c&#xff0c;\u4e24\u4e2a\u6ed1\u5757&#xff1a;\u521b\u9020\u6027\u6e29\u5ea6\u3001\u6700\u5927\u54cd\u5e94\u957f\u5ea6 &#8211;&amp;gt;<br \/>\n&amp;lt;div class&#061;&#034;grid grid-cols-1 lg:grid-cols-2 gap-8 mb-6&#034;&amp;gt;<br \/>\n  &amp;lt;!&#8211; temperature\u6e29\u5ea6\u6ed1\u5757 &#8211;&amp;gt;<br \/>\n  &amp;lt;div class&#061;&#034;space-y-3&#034;&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;flex items-center justify-between&#034;&amp;gt;<br \/>\n      &amp;lt;label class&#061;&#034;text-sm font-medium text-gray-300 flex items-center space-x-2&#034;&amp;gt;<br \/>\n        &amp;lt;svg class&#061;&#034;w-4 h-4 text-orange-400&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n          &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n            d&#061;&#034;M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z&#034; \/&amp;gt;<br \/>\n        &amp;lt;\/svg&amp;gt;<br \/>\n        &amp;lt;span&amp;gt;\u521b\u9020\u6027\u6e29\u5ea6&amp;lt;\/span&amp;gt;<br \/>\n      &amp;lt;\/label&amp;gt;<br \/>\n      &amp;lt;!&#8211; toFixed(1)\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u5c55\u793a &#8211;&amp;gt;<br \/>\n      &amp;lt;div class&#061;&#034;bg-gradient-to-r from-orange-500 to-red-500 text-white text-xs font-bold px-3 py-1 rounded-full&#034;&amp;gt;<br \/>\n        {{ temperature.toFixed(1) }}<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;relative&#034;&amp;gt;<br \/>\n      &amp;lt;!&#8211; v-model.number \u6570\u5b57\u53cc\u5411\u7ed1\u5b9a&#xff1b;range\u6ed1\u5757 min\u6700\u5c0f\u503c max\u6700\u5927\u503c step\u6b65\u957f &#8211;&amp;gt;<br \/>\n      &amp;lt;input v-model.number&#061;&#034;temperature&#034; type&#061;&#034;range&#034; min&#061;&#034;0&#034; max&#061;&#034;2&#034; step&#061;&#034;0.1&#034;<br \/>\n        class&#061;&#034;w-full h-3 bg-gray-700 rounded-lg appearance-none cursor-pointer temperature-slider&#034; \/&amp;gt;<br \/>\n      &amp;lt;div class&#061;&#034;flex justify-between text-xs text-gray-500 mt-2&#034;&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-blue-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u4fdd\u5b88&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-yellow-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u5e73\u8861&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-red-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u521b\u65b0&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<\/p>\n<p>  &amp;lt;!&#8211; maxTokens\u6700\u5927\u8f93\u51fa\u957f\u5ea6\u6ed1\u5757 &#8211;&amp;gt;<br \/>\n  &amp;lt;div class&#061;&#034;space-y-3&#034;&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;flex items-center justify-between&#034;&amp;gt;<br \/>\n      &amp;lt;label class&#061;&#034;text-sm font-medium text-gray-300 flex items-center space-x-2&#034;&amp;gt;<br \/>\n        &amp;lt;svg class&#061;&#034;w-4 h-4 text-green-400&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n          &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n            d&#061;&#034;M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z&#034; \/&amp;gt;<br \/>\n        &amp;lt;\/svg&amp;gt;<br \/>\n        &amp;lt;span&amp;gt;\u54cd\u5e94\u957f\u5ea6&amp;lt;\/span&amp;gt;<br \/>\n      &amp;lt;\/label&amp;gt;<br \/>\n      &amp;lt;div<br \/>\n        class&#061;&#034;bg-gradient-to-r from-green-500 to-emerald-500 text-white text-xs font-bold px-3 py-1 rounded-full&#034;&amp;gt;<br \/>\n        {{ maxTokens.toLocaleString() }}<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;relative&#034;&amp;gt;<br \/>\n      &amp;lt;input v-model.number&#061;&#034;maxTokens&#034; type&#061;&#034;range&#034; min&#061;&#034;100&#034; max&#061;&#034;8000&#034; step&#061;&#034;100&#034;<br \/>\n        class&#061;&#034;w-full h-3 bg-gray-700 rounded-lg appearance-none cursor-pointer tokens-slider&#034; \/&amp;gt;<br \/>\n      &amp;lt;div class&#061;&#034;flex justify-between text-xs text-gray-500 mt-2&#034;&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-green-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u7b80\u77ed&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-yellow-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u9002\u4e2d&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;span class&#061;&#034;flex items-center space-x-1&#034;&amp;gt;<br \/>\n          &amp;lt;div class&#061;&#034;w-2 h-2 bg-blue-400 rounded-full&#034;&amp;gt;&amp;lt;\/div&amp;gt;<br \/>\n          &amp;lt;span&amp;gt;\u8be6\u7ec6&amp;lt;\/span&amp;gt;<br \/>\n        &amp;lt;\/span&amp;gt;<br \/>\n      &amp;lt;\/div&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;\/div&amp;gt;<\/p>\n<p>&amp;lt;!&#8211; \u8f93\u5165\u6846\u548c\u53d1\u9001\u6309\u94ae &#8211;&amp;gt;<br \/>\n&amp;lt;div class&#061;&#034;flex space-x-3&#034;&amp;gt;<br \/>\n  &amp;lt;div class&#061;&#034;flex-1 relative&#034;&amp;gt;<br \/>\n    &amp;lt;!&#8211; v-model\u53cc\u5411\u7ed1\u5b9amessage&#xff1b;&#064;keyup.enter \u6309\u4e0b\u56de\u8f66\u952e\u89e6\u53d1sendMessage\u53d1\u9001\u6d88\u606f &#8211;&amp;gt;<br \/>\n    &amp;lt;input v-model&#061;&#034;message&#034; &#064;keyup.enter&#061;&#034;sendMessage&#034; placeholder&#061;&#034;\u8f93\u5165\u4f60\u7684\u95ee\u9898&#xff0c;\u5f00\u59cbAI\u5bf9\u8bdd&#8230;&#034; type&#061;&#034;text&#034;<br \/>\n      class&#061;&#034;w-full p-4 pr-12 rounded-xl bg-gray-800\/80 text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 border border-gray-600\/50 transition-all duration-200 hover:bg-gray-800 focus:bg-gray-800&#034; \/&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;absolute right-4 top-1\/2 transform -translate-y-1\/2&#034;&amp;gt;<br \/>\n      &amp;lt;svg class&#061;&#034;w-5 h-5 text-gray-400&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n        &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n          d&#061;&#034;M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z&#034; \/&amp;gt;<br \/>\n      &amp;lt;\/svg&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/div&amp;gt;<\/p>\n<p>  &amp;lt;!&#8211; \u53d1\u9001\u6309\u94ae &#064;click\u70b9\u51fb\u89e6\u53d1sendMessage&#xff1b;:disabled\u8f93\u5165\u4e3a\u7a7a\u7981\u6b62\u70b9\u51fb &#8211;&amp;gt;<br \/>\n  &amp;lt;button &#064;click&#061;&#034;sendMessage&#034; :disabled&#061;&#034;!message.trim()&#034; :class&#061;&#034;[<br \/>\n    &#039;px-8 py-4 rounded-xl font-medium transition-all duration-200 transform focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-900&#039;,<br \/>\n    message.trim()<br \/>\n      ? &#039;bg-gradient-to-r from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 text-white cursor-pointer hover:scale-105 shadow-lg hover:shadow-blue-500\/25&#039;<br \/>\n      : &#039;bg-gray-700 text-gray-400 cursor-not-allowed&#039;<br \/>\n  ]&#034;&amp;gt;<br \/>\n    &amp;lt;div class&#061;&#034;flex items-center space-x-2&#034;&amp;gt;<br \/>\n      &amp;lt;span&amp;gt;\u53d1\u9001&amp;lt;\/span&amp;gt;<br \/>\n      &amp;lt;svg class&#061;&#034;w-5 h-5&#034; fill&#061;&#034;none&#034; stroke&#061;&#034;currentColor&#034; viewBox&#061;&#034;0 0 24 24&#034;&amp;gt;<br \/>\n        &amp;lt;path stroke-linecap&#061;&#034;round&#034; stroke-linejoin&#061;&#034;round&#034; stroke-width&#061;&#034;2&#034;<br \/>\n          d&#061;&#034;M12 19l9 2-9-18-9 18 9-2zm0 0v-8&#034; \/&amp;gt;<br \/>\n      &amp;lt;\/svg&amp;gt;<br \/>\n    &amp;lt;\/div&amp;gt;<br \/>\n  &amp;lt;\/button&amp;gt;<br \/>\n&amp;lt;\/div&amp;gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;style scoped&gt;<br \/>\n\/* \u6ed1\u5757\u6837\u5f0f&#xff0c;\u53ea\u4f5c\u7528\u5f53\u524d\u7ec4\u4ef6 \/<br \/>\n\/ \u6e29\u5ea6\u6ed1\u5757\u6837\u5f0f *\/<br \/>\n.temperature-slider::-webkit-slider-thumb {<br \/>\nappearance: none;<br \/>\nheight: 24px;<br \/>\nwidth: 24px;<br \/>\nborder-radius: 50%;<br \/>\nbackground: linear-gradient(135deg, #f97316, #dc2626);<br \/>\ncursor: pointer;<br \/>\nborder: 3px solid #1f2937;<br \/>\nbox-shadow: 0 4px 12px rgba(249, 115, 22, 0.4);<br \/>\ntransition: all 0.3s ease;<br \/>\n}<br \/>\n.temperature-slider::-webkit-slider-thumb:hover {<br \/>\ntransform: scale(1.2);<br \/>\nbox-shadow: 0 6px 20px rgba(249, 115, 22, 0.6);<br \/>\n}<br \/>\n.temperature-slider::-webkit-slider-runnable-track {<br \/>\nheight: 12px;<br \/>\nbackground: linear-gradient(to right, #3b82f6 0%, #f59e0b 50%, #dc2626 100%);<br \/>\nborder-radius: 6px;<br \/>\n}<br \/>\n\/* Token\u6ed1\u5757\u6837\u5f0f *\/<br \/>\n.tokens-slider::-webkit-slider-thumb {<br \/>\nappearance: none;<br \/>\nheight: 24px;<br \/>\nwidth: 24px;<br \/>\nborder-radius: 50%;<br \/>\nbackground: linear-gradient(135deg, #10b981, #059669);<br \/>\ncursor: pointer;<br \/>\nborder: 3px solid #1f2937;<br \/>\nbox-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);<br \/>\ntransition: all 0.3s ease;<br \/>\n}<br \/>\n.tokens-slider::-webkit-slider-thumb:hover {<br \/>\ntransform: scale(1.2);<br \/>\nbox-shadow: 0 6px 20px rgba(16, 185, 129, 0.6);<br \/>\n}<br \/>\n.tokens-slider::-webkit-slider-runnable-track {<br \/>\nheight: 12px;<br \/>\nbackground: linear-gradient(to right, #10b981 0%, #f59e0b 50%, #3b82f6 100%);<br \/>\nborder-radius: 6px;<br \/>\n}<br \/>\n\/* Firefox \u6ed1\u5757\u6837\u5f0f *\/<br \/>\n.temperature-slider::-moz-range-thumb,<br \/>\n.tokens-slider::-moz-range-thumb {<br \/>\nheight: 24px;<br \/>\nwidth: 24px;<br \/>\nborder-radius: 50%;<br \/>\ncursor: pointer;<br \/>\nborder: 3px solid #1f2937;<br \/>\ntransition: all 0.3s ease;<br \/>\n}<br \/>\n.temperature-slider::-moz-range-thumb {<br \/>\nbackground: linear-gradient(135deg, #f97316, #dc2626);<br \/>\nbox-shadow: 0 4px 12px rgba(249, 115, 22, 0.4);<br \/>\n}<br \/>\n.tokens-slider::-moz-range-thumb {<br \/>\nbackground: linear-gradient(135deg, #10b981, #059669);<br \/>\nbox-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);<br \/>\n}<br \/>\n\/* \u52a8\u753b\u6548\u679c *\/<br \/>\n&#064;keyframes pulse {<br \/>\n0%,<br \/>\n100% {<br \/>\nopacity: 1;<br \/>\n}<br \/>\n50% {<br \/>\nopacity: 0.5;<br \/>\n}<br \/>\n}<br \/>\n.animate-pulse {<br \/>\nanimation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;<br \/>\n}<br \/>\n&lt;\/style&gt;<\/p>\n<p>chat.ts<\/p>\n<p>\/\/ \u4ece Pinia \u4e2d\u5bfc\u5165\u300c\u521b\u5efa\u72b6\u6001\u4ed3\u5e93\u300d\u7684\u6838\u5fc3\u65b9\u6cd5 defineStore<br \/>\nimport { defineStore } from &#039;pinia&#039;;<br \/>\n\/\/ \u4ece Vue \u4e2d\u5bfc\u5165 ref&#xff0c;\u7528\u4e8e\u521b\u5efa\u54cd\u5e94\u5f0f\u6570\u636e&#xff08;\u57fa\u7840\u7c7b\u578b\/\u6570\u7ec4\u63a8\u8350\u7528 ref&#xff09;<br \/>\nimport { ref } from &#039;vue&#039;;<br \/>\n\/\/ \u5bfc\u5165 axios&#xff0c;\u7528\u4e8e\u53d1\u9001\u666e\u901a\u7684 HTTP \u8bf7\u6c42&#xff08;\u5386\u53f2\u8bb0\u5f55\u3001\u6e05\u7a7a\u8bb0\u5f55\u63a5\u53e3\u7528&#xff09;<br \/>\nimport axios from &#039;axios&#039;;<br \/>\n\/\/ \u5bfc\u5165\u7528\u6237\u72b6\u6001\u4ed3\u5e93&#xff0c;\u7528\u6765\u83b7\u53d6\u7528\u6237ID\u3001\u505a\u63a5\u53e3\u8eab\u4efd\u8ba4\u8bc1<br \/>\nimport { useUserStore } from &#039;.\/user&#039;;<br \/>\n\/\/ TypeScript \u63a5\u53e3&#xff1a;\u7ea6\u5b9a\u300c\u5355\u6761\u804a\u5929\u6d88\u606f\u300d\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u8ba9\u4ee3\u7801\u6709\u7c7b\u578b\u63d0\u793a\u3001\u66f4\u5b89\u5168<br \/>\ninterface ChatMessage {<br \/>\nrole: string;    \/\/ \u6d88\u606f\u89d2\u8272&#xff0c;\u6bd4\u5982 user&#xff08;\u7528\u6237\u53d1\u7684&#xff09;\u3001assistant&#xff08;AI\u56de\u590d\u7684&#xff09;<br \/>\ncontent: string; \/\/ \u6d88\u606f\u7684\u6587\u672c\u5185\u5bb9<br \/>\nsources: any[];  \/\/ \u8fd9\u6761\u6d88\u606f\u5173\u8054\u7684\u53c2\u8003\u6765\u6e90\u6570\u636e\u6570\u7ec4&#xff08;\u6bd4\u5982\u77e5\u8bc6\u5e93\u5f15\u7528\u7684\u6587\u6863&#xff09;<br \/>\n}<br \/>\n\/\/ TypeScript \u63a5\u53e3&#xff1a;\u7ea6\u5b9a\u300c\u540e\u7aef\u8fd4\u56de\u7684\u5386\u53f2\u6d88\u606f\u300d\u7684\u6570\u636e\u7ed3\u6784<br \/>\ninterface HistoryMessageResponse {<br \/>\nmessages: ChatMessage; \/\/ \u540e\u7aef\u8fd4\u56de\u7684\u6d88\u606f\u5b57\u6bb5&#xff0c;\u91cc\u9762\u662f\u6807\u51c6\u7684\u804a\u5929\u6d88\u606f\u7ed3\u6784<br \/>\n}<br \/>\n\/\/ \u521b\u5efa\u5e76\u5bfc\u51fa\u540d\u4e3a chat \u7684\u72b6\u6001\u4ed3\u5e93&#xff08;Pinia \u7ec4\u5408\u5f0f\u5199\u6cd5&#xff09;<br \/>\nexport const useChatStore &#061; defineStore(&#039;chat&#039;, () &#061;&gt; {<br \/>\n\/\/ \u54cd\u5e94\u5f0f\u6570\u7ec4&#xff1a;\u5b58\u653e\u6240\u6709\u804a\u5929\u6d88\u606f<br \/>\n\/\/ \u6bcf\u6761\u6d88\u606f\u5305\u542b 3 \u4e2a\u5b57\u6bb5&#xff1a;\u89d2\u8272\u3001\u6587\u672c\u5185\u5bb9\u3001\u53c2\u8003\u6765\u6e90\u5217\u8868<br \/>\nconst messages &#061; ref&lt;{ role: string; content: string, sources_info_list: any }[]&gt;([]);<br \/>\n\/\/ \u54cd\u5e94\u5f0f\u5e03\u5c14\u503c&#xff1a;\u6807\u8bb0\u5f53\u524d\u662f\u5426\u6b63\u5728\u52a0\u8f7d\/\u8bf7\u6c42\u4e2d&#xff0c;\u7528\u4e8e\u9875\u9762\u663e\u793a\u52a0\u8f7d\u52a8\u753b<br \/>\nconst isLoading &#061; ref(false);<br \/>\n\/\/ \u83b7\u53d6\u7528\u6237\u4ed3\u5e93\u5b9e\u4f8b&#xff0c;\u540e\u7eed\u7528\u5b83\u62ff\u7528\u6237ID\u505a\u63a5\u53e3\u8ba4\u8bc1<br \/>\nconst userStore &#061; useUserStore();<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\/\/ \u65b9\u6cd51&#xff1a;\u52a0\u8f7d\u804a\u5929\u5386\u53f2\u8bb0\u5f55<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nconst loadChatHistory &#061; async () &#061;&gt; {<br \/>\n\/\/ \u5b88\u536b\u5224\u65ad&#xff1a;\u5982\u679c\u6ca1\u6709\u7528\u6237ID&#xff08;\u672a\u767b\u5f55&#xff09;&#xff0c;\u76f4\u63a5\u4e0d\u6267\u884c\u540e\u7eed\u903b\u8f91<br \/>\nif (!userStore.userId) return;<br \/>\ntry {<br \/>\n  \/\/ \u53d1\u9001 GET \u8bf7\u6c42&#xff0c;\u4ece\u540e\u7aef\u83b7\u53d6\u5386\u53f2\u804a\u5929\u8bb0\u5f55<br \/>\n  \/\/ import.meta.env.VITE_API_URL \u662f\u73af\u5883\u53d8\u91cf\u91cc\u914d\u7f6e\u7684\u63a5\u53e3\u57fa\u7840\u5730\u5740<br \/>\n  const { data } &#061; await axios.get(<br \/>\n    &#096;${import.meta.env.VITE_API_URL}\/api\/chat\/history&#096;,<br \/>\n    {<br \/>\n      \/\/ \u8bf7\u6c42\u5934\u914d\u7f6e<br \/>\n      headers: {<br \/>\n        &#039;Authorization&#039;: &#096;Bearer ${userStore.userId}&#096;, \/\/ \u8eab\u4efd\u8ba4\u8bc1&#xff1a;\u628a\u7528\u6237ID\u653e\u5728 Bearer \u4ee4\u724c\u91cc\u4f20\u7ed9\u540e\u7aef<br \/>\n        &#039;Content-Type&#039;: &#039;application\/json&#039;             \/\/ \u544a\u8bc9\u540e\u7aef\u8bf7\u6c42\u4f53\u662f JSON \u683c\u5f0f<br \/>\n      }<br \/>\n    }<br \/>\n  );<br \/>\n  \/\/ \u5904\u7406\u540e\u7aef\u8fd4\u56de\u7684\u6570\u636e&#xff1a;<br \/>\n  \/\/ 1. filter&#xff1a;\u8fc7\u6ee4\u6389\u6ca1\u6709\u5185\u5bb9\u7684\u65e0\u6548\u6d88\u606f<br \/>\n  \/\/ 2. map&#xff1a;\u628a\u540e\u7aef\u683c\u5f0f\u8f6c\u6362\u6210\u524d\u7aef\u9875\u9762\u9700\u8981\u7684\u683c\u5f0f&#xff0c;\u8d4b\u503c\u7ed9\u672c\u5730\u6d88\u606f\u5217\u8868<br \/>\n  messages.value &#061; data<br \/>\n    .filter((msg: HistoryMessageResponse) &#061;&amp;gt; msg.messages.content)<br \/>\n    .map((msg: HistoryMessageResponse) &#061;&amp;gt; ({<br \/>\n      role: msg.messages.role,<br \/>\n      content: msg.messages.content,<br \/>\n      sources_info_list: msg.messages.sources || [], \/\/ \u6ca1\u6709\u6765\u6e90\u6570\u636e\u5c31\u7ed9\u7a7a\u6570\u7ec4\u515c\u5e95<br \/>\n    }));<br \/>\n} catch (error) {<br \/>\n  \/\/ \u8bf7\u6c42\u51fa\u9519\u65f6&#xff0c;\u5728\u63a7\u5236\u53f0\u6253\u5370\u9519\u8bef\u4fe1\u606f&#xff0c;\u65b9\u4fbf\u8c03\u8bd5<br \/>\n  console.error(&#039;Error loading chat history: &#039;, error);<br \/>\n}<br \/>\n};<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\/\/ \u65b9\u6cd52&#xff1a;\u6e05\u7a7a\u804a\u5929\u5386\u53f2\u8bb0\u5f55<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nconst clearChatHistory &#061; async () &#061;&gt; {<br \/>\n\/\/ \u5b88\u536b\u5224\u65ad&#xff1a;\u6ca1\u6709\u7528\u6237ID\u5c31\u4e0d\u6267\u884c<br \/>\nif (!userStore.userId) return;<br \/>\n\/\/ \u53d1\u9001 POST \u8bf7\u6c42&#xff0c;\u901a\u77e5\u540e\u7aef\u628a\u5f53\u524d\u7528\u6237\u7684\u804a\u5929\u5386\u53f2\u6e05\u7a7a<br \/>\nawait axios.post(<br \/>\n  &#096;${import.meta.env.VITE_API_URL}\/api\/chat\/clear&#096;,<br \/>\n  {}, \/\/ POST \u8bf7\u6c42\u4f53\u4e3a\u7a7a&#xff08;\u4e0d\u9700\u8981\u4f20\u989d\u5916\u53c2\u6570&#xff09;<br \/>\n  {<br \/>\n    headers: {<br \/>\n      &#039;Authorization&#039;: &#096;Bearer ${userStore.userId}&#096;, \/\/ \u5e26\u4e0a\u7528\u6237\u8eab\u4efd\u8ba4\u8bc1<br \/>\n      &#039;Content-Type&#039;: &#039;application\/json&#039;<br \/>\n    }<br \/>\n  }<br \/>\n);<br \/>\n\/\/ \u540e\u7aef\u6e05\u7a7a\u6210\u529f\u540e&#xff0c;\u628a\u672c\u5730\u524d\u7aef\u7684\u6d88\u606f\u5217\u8868\u4e5f\u6e05\u7a7a&#xff0c;\u4fdd\u6301\u9875\u9762\u540c\u6b65<br \/>\nmessages.value &#061; [];<br \/>\n};<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\/\/ \u65b9\u6cd53&#xff1a;\u53d1\u9001\u7528\u6237\u6d88\u606f &#043; \u63a5\u6536AI\u6d41\u5f0f\u56de\u590d<br \/>\n\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\/\/ \u53c2\u6570\u8bf4\u660e&#xff1a;<br \/>\n\/\/ message        \u7528\u6237\u8f93\u5165\u7684\u95ee\u9898\u5185\u5bb9<br \/>\n\/\/ model          \u9009\u7528\u7684AI\u6a21\u578b\u540d\u79f0<br \/>\n\/\/ knowledge_bool \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93\u95ee\u7b54<br \/>\n\/\/ temperature    \u6a21\u578b\u6e29\u5ea6&#xff08;\u63a7\u5236\u56de\u590d\u968f\u673a\u6027&#xff0c;\u503c\u8d8a\u9ad8\u8d8a\u53d1\u6563&#xff09;<br \/>\n\/\/ max_tokens     \u6700\u5927token\u6570\u91cf&#xff08;\u9650\u5236AI\u56de\u590d\u7684\u957f\u5ea6&#xff09;<br \/>\nconst sendMessage &#061; async (message: string, model: string, knowledge_bool: boolean, temperature: number, max_tokens: number) &#061;&gt; {<br \/>\n\/\/ \u5b88\u536b\u5224\u65ad&#xff1a;\u6d88\u606f\u662f\u7a7a\u7684 \/ \u6ca1\u6709\u7528\u6237ID&#xff0c;\u76f4\u63a5\u4e0d\u6267\u884c<br \/>\nif (!message.trim() || !userStore.userId) return;<br \/>\n\/\/ \u5148\u628a\u7528\u6237\u53d1\u7684\u6d88\u606f\u52a0\u5230\u804a\u5929\u5217\u8868\u91cc&#xff0c;\u8ba9\u9875\u9762\u7acb\u523b\u663e\u793a\u7528\u6237\u7684\u63d0\u95ee<br \/>\nmessages.value.push({ role: &#039;user&#039;, content: message, sources_info_list: [] });<br \/>\n\/\/ \u5f00\u542f\u52a0\u8f7d\u72b6\u6001&#xff0c;\u9875\u9762\u53ef\u4ee5\u663e\u793a\u201cAI\u6b63\u5728\u601d\u8003\/\u56de\u590d\u4e2d\u201d\u7684\u6548\u679c<br \/>\nisLoading.value &#061; true;<\/p>\n<p>try {<br \/>\n  \/\/ \u8c03\u7528\u6d41\u5f0f\u54cd\u5e94\u5904\u7406\u65b9\u6cd5&#xff0c;\u628a\u6240\u6709\u53c2\u6570\u4f20\u8fdb\u53bb&#xff0c;\u63a5\u6536AI\u7684\u9010\u5b57\u56de\u590d<br \/>\n  await handleStreamResponse(message, model, knowledge_bool, temperature, max_tokens);<br \/>\n} catch (error) {<br \/>\n  \/\/ \u8bf7\u6c42\u51fa\u9519\u65f6&#xff0c;\u5728\u804a\u5929\u5217\u8868\u91cc\u52a0\u4e00\u6761\u9519\u8bef\u63d0\u793a&#xff0c;\u8ba9\u7528\u6237\u77e5\u9053\u5931\u8d25\u4e86<br \/>\n  console.error(&#039;Error sending message: &#039;, error);<br \/>\n  messages.value.push({<br \/>\n    role: &#039;assistant&#039;,<br \/>\n    content: &#039;Error: unable to process request&#039;,<br \/>\n    sources_info_list: []<br \/>\n  });<br \/>\n} finally {<br \/>\n  \/\/ \u65e0\u8bba\u6210\u529f\u8fd8\u662f\u5931\u8d25&#xff0c;\u6700\u540e\u90fd\u5173\u95ed\u52a0\u8f7d\u72b6\u6001<br \/>\n  isLoading.value &#061; false;<br \/>\n}<br \/>\n};<br \/>\n\/**<br \/>\n\u5185\u90e8\u65b9\u6cd5&#xff1a;\u5904\u7406\u6d41\u5f0f\u54cd\u5e94&#xff08;SSE \u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff0c;\u5b9e\u73b0\u6253\u5b57\u673a\u6548\u679c&#xff09;<br \/>\n&#064;param message &#8211; \u7528\u6237\u8f93\u5165\u7684\u95ee\u9898\u5185\u5bb9<br \/>\n&#064;param model &#8211; AI\u6a21\u578b\u540d\u79f0<br \/>\n&#064;param knowledge_bool &#8211; \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93\u95ee\u7b54<br \/>\n&#064;param temperature &#8211; \u6e29\u5ea6\u53c2\u6570&#xff0c;\u63a7\u5236\u56de\u590d\u7684\u968f\u673a\u6027<br \/>\n&#064;param max_tokens &#8211; \u6700\u5927token\u6570\u91cf&#xff0c;\u9650\u5236AI\u56de\u590d\u7684\u957f\u5ea6<br \/>\n*\/<br \/>\nconst handleStreamResponse &#061; async (message: string, model: string, knowledge_bool: boolean, temperature: number, max_tokens: number) &#061;&gt; {<br \/>\n\/\/ \u8bb0\u5f55\u8fd9\u6761AI\u56de\u590d\u5728\u6d88\u606f\u6570\u7ec4\u91cc\u7684\u4e0b\u6807\u4f4d\u7f6e&#xff0c;\u540e\u9762\u7528\u6765\u5b9e\u65f6\u66f4\u65b0\u5185\u5bb9<br \/>\nconst aiMessageIndex &#061; messages.value.length;<br \/>\n\/\/ \u5148\u5728\u6d88\u606f\u5217\u8868\u91cc\u5360\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u653e\u4e00\u6761\u7a7a\u7684AI\u6d88\u606f&#xff0c;\u540e\u7eed\u9010\u5b57\u5f80\u91cc\u9762\u586b\u5185\u5bb9<br \/>\nmessages.value.push({ role: &#039;assistant&#039;, content: &#039;&#039;, sources_info_list: [] });<br \/>\ntry {<br \/>\n\/\/ \u7528 fetch \u53d1\u8d77\u6d41\u5f0f\u8bf7\u6c42&#xff08;axios \u4e0d\u652f\u6301\u539f\u751f\u6d41\u5f0f\u8bfb\u53d6&#xff0c;\u6240\u4ee5\u7528\u6d4f\u89c8\u5668\u539f\u751f fetch&#xff09;<br \/>\nconst response &#061; await fetch(${import.meta.env.VITE_API_URL}\/api\/chat\/stream, {<br \/>\nmethod: &#039;POST&#039;, \/\/ \u8bf7\u6c42\u65b9\u5f0f\u662f POST<br \/>\nheaders: {<br \/>\n&#039;Content-Type&#039;: &#039;application\/json&#039;,  \/\/ \u544a\u8bc9\u540e\u7aef\u8bf7\u6c42\u4f53\u662f JSON \u683c\u5f0f<br \/>\n&#039;Authorization&#039;: Bearer ${userStore.userId}, \/\/ \u5e26\u4e0a\u7528\u6237\u8eab\u4efd\u8ba4\u8bc1<br \/>\n},<br \/>\n\/\/ \u8bf7\u6c42\u4f53&#xff1a;\u628a\u6240\u6709\u53c2\u6570\u8f6c\u6210 JSON \u5b57\u7b26\u4e32\u53d1\u7ed9\u540e\u7aef<br \/>\nbody: JSON.stringify({<br \/>\nquery: message,        \/\/ \u7528\u6237\u7684\u95ee\u9898<br \/>\nmodel: model,          \/\/ \u9009\u62e9\u7684AI\u6a21\u578b<br \/>\ntemperature: temperature, \/\/ \u6e29\u5ea6\u53c2\u6570<br \/>\nmax_tokens: max_tokens,   \/\/ \u6700\u5927\u751f\u6210\u957f\u5ea6<br \/>\nknowledge_bool: knowledge_bool, \/\/ \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93<br \/>\n}<br \/>\n)<br \/>\n});<br \/>\n\/\/ \u68c0\u67e5HTTP\u8bf7\u6c42\u662f\u5426\u6210\u529f&#xff0c;\u72b6\u6001\u7801\u4e0d\u662f 2xx \u5c31\u629b\u51fa\u9519\u8bef<br \/>\nif (!response.ok) {<br \/>\nthrow new Error(HTTP error! status: ${response.status});<br \/>\n}<br \/>\n\/\/ \u68c0\u67e5\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301\u8bfb\u53d6\u54cd\u5e94\u6d41&#xff0c;\u4e0d\u652f\u6301\u5c31\u62a5\u9519<br \/>\nif (!response.body) {<br \/>\nthrow new Error(&#039;ReadableStream not supported&#039;);<br \/>\n}<br \/>\n\/\/ \u83b7\u53d6\u6d41\u7684\u8bfb\u53d6\u5668&#xff0c;\u7528\u6765\u4e00\u70b9\u70b9\u8bfb\u53d6\u540e\u7aef\u6301\u7eed\u8fd4\u56de\u7684\u6570\u636e<br \/>\nconst reader &#061; response.body.getReader();<br \/>\n\/\/ \u521b\u5efa\u6587\u672c\u89e3\u7801\u5668&#xff0c;\u628a\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6570\u636e\u8f6c\u6362\u6210\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32<br \/>\nconst decoder &#061; new TextDecoder();<br \/>\n\/\/ \u7f13\u51b2\u533a&#xff1a;\u7528\u6765\u62fc\u63a5\u88ab\u62c6\u5206\u7684\u6570\u636e\u5757<br \/>\n\/\/ \u56e0\u4e3a\u6d41\u5f0f\u4f20\u8f93\u53ef\u80fd\u628a\u4e00\u884c\u5b8c\u6574\u6570\u636e\u62c6\u6210\u4e24\u6b21\u53d1&#xff0c;\u9700\u8981\u5148\u62fc\u8d77\u6765\u518d\u5904\u7406<br \/>\nlet buffer &#061; &#039;&#039;;<br \/>\n\/\/ \u5faa\u73af&#xff1a;\u6301\u7eed\u4e0d\u65ad\u5730\u8bfb\u53d6\u6d41\u91cc\u7684\u6570\u636e&#xff0c;\u76f4\u5230\u6d41\u7ed3\u675f<br \/>\nwhile (true) {<br \/>\n\/\/ \u8bfb\u53d6\u4e00\u5757\u6570\u636e&#xff1a;done \u8868\u793a\u662f\u5426\u5168\u90e8\u8bfb\u5b8c\u4e86&#xff0c;value \u662f\u8fd9\u4e00\u5757\u7684\u4e8c\u8fdb\u5236\u6570\u636e<br \/>\nconst { done, value } &#061; await reader.read();<br \/>\n\/\/ \u5982\u679c\u6d41\u7ed3\u675f\u4e86&#xff0c;\u8df3\u51fa\u5faa\u73af<br \/>\nif (done) break;<br \/>\n\/\/ \u628a\u8fd9\u5757\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u6210\u5b57\u7b26\u4e32&#xff0c;\u8ffd\u52a0\u5230\u7f13\u51b2\u533a\u672b\u5c3e<br \/>\nbuffer &#043;&#061; decoder.decode(value, { stream: true });<br \/>\n\/\/ \u6309\u6362\u884c\u7b26\u628a\u7f13\u51b2\u533a\u62c6\u6210\u4e00\u884c\u4e00\u884c\u7684&#xff08;SSE \u534f\u8bae\u89c4\u5b9a\u6309\u884c\u4f20\u8f93\u6570\u636e&#xff09;<br \/>\nconst lines &#061; buffer.split(&#039;\\\\n&#039;);<br \/>\n\/\/ \u628a\u6700\u540e\u4e00\u884c\u62ff\u51fa\u6765\u653e\u56de\u7f13\u51b2\u533a<br \/>\n\/\/ \u56e0\u4e3a\u6700\u540e\u4e00\u884c\u5927\u6982\u7387\u662f\u4e0d\u5b8c\u6574\u7684&#xff0c;\u7b49\u4e0b\u4e00\u5757\u6570\u636e\u8fc7\u6765\u62fc\u8d77\u6765\u518d\u5904\u7406<br \/>\nbuffer &#061; lines.pop() || &#039;&#039;;<br \/>\n\/\/ \u904d\u5386\u6bcf\u4e00\u884c\u5b8c\u6574\u7684\u6570\u636e&#xff0c;\u9010\u4e2a\u89e3\u6790\u5904\u7406<br \/>\nfor (const line of lines) {<br \/>\nconst trimmedLine &#061; line.trim();<br \/>\n\/\/ SSE \u534f\u8bae\u89c4\u5b9a\u6570\u636e\u884c\u4ee5 &#034;data: &#034; \u5f00\u5934&#xff0c;\u53ea\u5904\u7406\u8fd9\u79cd\u6709\u6548\u6570\u636e\u884c<br \/>\nif (trimmedLine.startsWith(&#039;data: &#039;)) {<br \/>\n\/\/ \u53bb\u6389\u5f00\u5934\u7684 &#034;data: &#034; \u524d\u7f00&#xff0c;\u62ff\u5230\u91cc\u9762\u7684 JSON \u5b57\u7b26\u4e32<br \/>\nconst jsonStr &#061; trimmedLine.slice(6);<br \/>\n   \/\/ \u8df3\u8fc7\u7a7a\u884c&#xff0c;\u6216\u8005 SSE \u6807\u51c6\u7684\u7ed3\u675f\u6807\u8bb0 [DONE]<br \/>\n   if (jsonStr.trim() &#061;&#061;&#061; &#039;&#039; || jsonStr.trim() &#061;&#061;&#061; &#039;[DONE]&#039;) continue;<\/p>\n<p>   try {<br \/>\n     \/\/ \u628a JSON \u5b57\u7b26\u4e32\u89e3\u6790\u6210 JS \u5bf9\u8c61&#xff0c;\u65b9\u4fbf\u8bfb\u53d6\u5b57\u6bb5<br \/>\n     const data &#061; JSON.parse(jsonStr);<br \/>\n     \/\/ \u5982\u679c\u540e\u7aef\u8fd4\u56de\u7ed3\u675f\u4fe1\u53f7&#xff08;finished \u4e3a true&#xff09;&#xff0c;\u5c31\u8df3\u51fa\u6570\u636e\u5904\u7406\u5faa\u73af<br \/>\n     if (data.finished) {<br \/>\n       break;<br \/>\n     }<br \/>\n     \/\/ \u6839\u636e\u540e\u7aef\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b&#xff0c;\u505a\u4e0d\u540c\u7684\u5904\u7406<br \/>\n     if (data.type &#061;&#061;&#061; &#034;sources&#034;) {<br \/>\n       \/\/ \u7c7b\u578b\u662f sources&#xff1a;\u5904\u7406\u53c2\u8003\u6765\u6e90\u6570\u636e&#xff0c;\u66f4\u65b0\u5230\u8fd9\u6761AI\u6d88\u606f\u7684\u6765\u6e90\u5217\u8868\u91cc<br \/>\n       if (data.content &amp;amp;&amp;amp; Array.isArray(data.content)) {<br \/>\n           messages.value[aiMessageIndex].sources_info_list &#061; data.content;<br \/>\n       }<br \/>\n     } else if (data.type &#061;&#061;&#061; &#034;content&#034; || data.type &#061;&#061;&#061; &#034;text&#034;) {<br \/>\n       \/\/ \u7c7b\u578b\u662f content\/text&#xff1a;\u5904\u7406AI\u56de\u590d\u7684\u6587\u672c<br \/>\n       \/\/ \u628a\u65b0\u8fd4\u56de\u7684\u6587\u672c\u8ffd\u52a0\u5230\u6d88\u606f\u5185\u5bb9\u540e\u9762&#xff0c;\u5b9e\u73b0\u9010\u5b57\u663e\u793a\u7684\u6253\u5b57\u673a\u6548\u679c<br \/>\n       if (data.content) {<br \/>\n         messages.value[aiMessageIndex].content &#043;&#061; data.content;<br \/>\n       }<br \/>\n     }<br \/>\n   } catch (e) {<br \/>\n     \/\/ \u67d0\u4e00\u884c\u89e3\u6790\u5931\u8d25\u4e0d\u5f71\u54cd\u6574\u4f53\u6d41\u7a0b&#xff0c;\u5728\u63a7\u5236\u53f0\u6253\u5370\u8b66\u544a\u65b9\u4fbf\u8c03\u8bd5<br \/>\n     console.warn(&#039;Failed to parse JSON:&#039;, jsonStr, e);<br \/>\n   }<br \/>\n }<br \/>\n}<br \/>\n}<br \/>\n\/\/ \u8bfb\u53d6\u5b8c\u6210\u540e&#xff0c;\u91ca\u653e\u8bfb\u53d6\u5668\u7684\u9501&#xff0c;\u91ca\u653e\u6d4f\u89c8\u5668\u8d44\u6e90<br \/>\nreader.releaseLock();<br \/>\n} catch (error) {<br \/>\n  \/\/ \u6d41\u5f0f\u8bf7\u6c42\u51fa\u9519\u65f6&#xff0c;\u628a\u4e4b\u524d\u5360\u4f4d\u7684\u90a3\u6761\u7a7aAI\u6d88\u606f\u5220\u6389&#xff08;\u907f\u514d\u9875\u9762\u7559\u4e00\u6761\u7a7a\u6d88\u606f&#xff09;<br \/>\n  console.error(&#039;Stream error:&#039;, error);<br \/>\n  messages.value.splice(aiMessageIndex, 1);<br \/>\n  \/\/ \u628a\u9519\u8bef\u629b\u51fa\u53bb&#xff0c;\u8ba9\u5916\u5c42\u7684 sendMessage \u6355\u83b7\u5e76\u505a\u540e\u7eed\u5904\u7406<br \/>\n  throw error;<br \/>\n}<br \/>\n};<br \/>\n\/\/ \u628a\u72b6\u6001\u548c\u65b9\u6cd5\u66b4\u9732\u51fa\u53bb&#xff0c;\u7ec4\u4ef6\u91cc\u5bfc\u5165\u4ed3\u5e93\u540e\u5c31\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u6570\u636e\u548c\u65b9\u6cd5<br \/>\nreturn { messages, isLoading, loadChatHistory, sendMessage, clearChatHistory };<br \/>\n});<\/p>\n<\/p>\n<p>\u804a\u5929\u63a5\u53e3<\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846&#xff0c;\u5b83\u662f\u804a\u5929\u7684\u63a5\u53e3<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110510-6a9aa5e6b2266.png\" width=\"1795\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846\u5904\u7406\u804a\u5929\u7684\u63a5\u53e3<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110511-6a9aa5e774670.png\" width=\"1891\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846&#xff0c;\u5b83\u8c03\u7528\u4e86svc\u91cc\u7684\u51fd\u6570&#xff0c;\u7136\u540esvc\u662f\u901a\u8fc7\u4e0b\u56fe\u84dd\u6846\u521b\u5efa\u7684<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110512-6a9aa5e83cba2.png\" width=\"1105\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846 get_rag_service \u7684\u903b\u8f91&#xff0c;\u5b83\u662f\u521b\u5efa\u4e00\u4e2aRAGService&#xff0c;\u8fd9\u4e2aRAGService\u7684\u903b\u8f91\u4e0a\u4e00\u8282\u4e2d\u5199\u8fc7\u4e86&#xff0c;\u5c31\u662f\u521d\u59cb\u5316llamaindex\u6784\u5efarag\u7684\u6d41\u7a0b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110512-6a9aa5e8bb172.png\" width=\"1105\" \/><\/p>\n<p>\u4e3b\u8981\u5c31\u662f\u4e0b\u56fe\u7ea2\u6846\u91cc\u9762\u7684\u903b\u8f91<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110513-6a9aa5e977dfc.png\" width=\"1105\" \/><\/p>\n<p>\u9996\u5148\u4ed6\u66f4\u65b0\u4e86\u4e00\u4e0b\u6a21\u578b\u4fe1\u606f<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110514-6a9aa5ea6a67e.png\" width=\"1105\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846update_model_config\u91cc\u8c03\u7528\u7684ingestion_pipeline\u7684update_model_config<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110514-6a9aa5eaef022.png\" width=\"1105\" \/><\/p>\n<p>\u7136\u540e\u5982\u4e0b\u56fe\u7ea2\u6846 ingestion_pipeline\u91cc\u7684update_model_config\u5c31\u662f\u8bbe\u7f6e\u4e00\u4e0b\u5168\u5c40\u7684\u5927\u8bed\u8a00\u6a21\u578b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110515-6a9aa5eb7b3db.png\" width=\"1105\" \/><\/p>\n<p>\u7136\u540eupdate_model_config\u770b\u5b8c\u4e86&#xff0c;\u63a5\u4e0b\u6765\u662f\u4e0b\u56fe\u7ea2\u6846\u7684 query_documents_stream<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110515-6a9aa5ebee626.png\" width=\"1105\" \/><\/p>\n<p>\u5982\u4e0b\u56fe\u7ea2\u6846\u5b83\u9996\u5148\u4f1a\u83b7\u53d6\u804a\u5929\u5f15\u64ce<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110516-6a9aa5ec6ba5f.png\" width=\"1105\" \/><\/p>\n<p>\u8fd9\u91cc\u901a\u8fc7\u524d\u7aef\u4f20\u6765\u7684\u6807\u8bb0\u6765\u5224\u65ad\u521b\u5efa\u5e26\u77e5\u8bc6\u5e93\u7684\u804a\u5929\u5f15\u64ce&#xff0c;\u8fd8\u662f\u4e0d\u5e26\u77e5\u8bc6\u5e93\u7684\u804a\u5929\u5f15\u64ce<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-044oye4rgv5bp.png\" width=\"1105\" \/><\/p>\n<p>\u7d22\u5f15\u7684\u521b\u5efa&#xff0c;\u4e0a\u4e00\u8282\u4e2d\u5199\u7684\u7d22\u5f15\u9ed8\u8ba4\u662f\u7a7a\u7684&#xff0c;\u8fd9\u91cc\u5c31\u6765\u770b\u770b\u5982\u4f55\u521b\u5efa\u7684\u7d22\u5f15<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04dc1k3o5kaqn.png\" width=\"1105\" \/><\/p>\n<p>\u83b7\u53d6\u7d22\u5f15<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04iixoy12pyg0.png\" width=\"1105\" \/><\/p>\n<p>\u83b7\u53d6\u7d22\u5f15<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04nh2dek0uoe0.png\" width=\"1105\" \/><\/p>\n<p>\u8fd9\u91cc\u901a\u8fc7\u5b58\u50a8\u7ba1\u7406\u5668\u521b\u5efa\u7d22\u5f15<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04zcg4ejovqu0.png\" width=\"1105\" \/><\/p>\n<p>\u4e0a\u56fe\u8fd9\u4e2a\u5b58\u50a8\u7ba1\u7406\u5668\u662f\u5728 RAGService \u6784\u9020\u65b9\u6cd5\u4e2d\u521b\u5efa\u7684\u5982\u4e0b\u56fe<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04utq2q2fslkq.png\" width=\"3418\" \/><\/p>\n<p>\u5982\u679c\u4e4b\u524d\u521b\u5efa\u8fc7\u77e5\u8bc6\u5e93&#xff0c;\u5e76\u4e14\u672c\u5730\u5df2\u7ecf\u6709\u8fc7\u77e5\u8bc6\u7684\u5185\u5bb9&#xff08;\u7d22\u5f15&#xff09;\u4e86&#xff0c;\u5b83\u624d\u4f1a\u521b\u5efa&#xff0c;\u5426\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1077\" src=\"2026-09-04vdw2ot1fj1z.png\" width=\"2728\" \/><\/p>\n<p>\u5230\u8fd9llamaindex\u7d22\u5f15\u7684\u521b\u5efa\u5c31\u5b8c\u6210\u4e86&#xff0c;\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u68c0\u7d22\u5668&#xff0c;\u8fd9\u91cc\u4f7f\u7528\u878d\u5408\u68c0\u7d22\u5668&#xff0c;BM25\u548c\u5411\u91cf\u68c0\u7d22<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04xrlezdlvj1a.png\" width=\"1000\" \/><\/p>\n<p>\u5230\u8fd9\u804a\u5929\u5f15\u64ce\u5c31\u5b8c\u6210\u4e86&#xff0c;\u7136\u540e\u5c31\u662f\u4e0b\u56fe\u7ea2\u6846\u7684 \u67e5\u8be2\u3001\u4fdd\u5b58\u804a\u5929\u8bb0\u5f55\u3001RAG\u68c0\u7d22\u7684\u4fe1\u606f\u8fd4\u56de\u7ed9\u524d\u7aef\u4e86&#xff0c;\u7136\u540e\u5c31\u7ed3\u675f\u4e86<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1136\" src=\"2026-09-04wdszqt5wlzs.png\" width=\"1000\" \/><\/p>\n<p>\u5b8c\u6574\u4ee3\u7801<\/p>\n<p>\u4ee3\u7801\u4e2d\u7528\u4e86\u5f88\u591ayield&#xff0c;\u5b83\u662f\u4f7f\u7528\u5b9e\u4f8b<\/p>\n<p># \u7b2c\u4e00\u79cd\u7528\u6cd5<br \/>\ndef count():<br \/>\n    yield 1<br \/>\n    yield 2<br \/>\n    # \u51fd\u6570\u6267\u884c\u5230\u8fd9\u91cc&#xff0c;\u6ca1\u6709\u66f4\u591a\u4ee3\u7801\u4e86<br \/>\ngen &#061; count()<br \/>\nnext(gen) # 1<br \/>\nnext(gen) # 2<br \/>\nnext(gen) # \u629b\u51fa StopIteration&#xff0c;\u4ee3\u8868\u751f\u6210\u5b8c\u4e86<br \/>\n\u7b2c\u4e8c\u79cd\u7528\u6cd5<br \/>\ndef count():<br \/>\nyield 1<br \/>\nyield 2<br \/>\nyield 3<br \/>\nfor num in count():<br \/>\nprint(num)<br \/>\n#\u7b2c\u4e09\u79cd\u7528\u6cd5<br \/>\ngen &#061; count()<br \/>\nwhile True:<br \/>\ntry:<br \/>\nnum &#061; next(gen)  # \u624b\u52a8\u8c03\u7528 next \u53d6\u4e0b\u4e00\u4e2a\u503c<br \/>\nprint(num)<br \/>\nexcept StopIteration:  # \u6355\u83b7\u5230\u7ed3\u675f\u5f02\u5e38<br \/>\nbreak  # \u9000\u51fa\u5faa\u73af<\/p>\n<p>rag.py&#xff0c;\u4e0b\u65b9\u6211\u4eec\u628a event_generator \u51fd\u6570\u7ed9\u5230\u4e86 StreamingResponse\u91cc&#xff0c;StreamingResponse\u91cc\u9762\u4f1a\u6709\u4e0a\u65b9\u7b2c\u4e8c\u79cdfor\u5faa\u73af\u5199\u6cd5\u7684\u4ee3\u7801<\/p>\n<p># \u5bfc\u5165json\u6807\u51c6\u5e93&#xff0c;\u7528\u4e8e\u5c06Python\u5b57\u5178\u8f6c\u6210JSON\u5b57\u7b26\u4e32&#xff0c;\u6ee1\u8db3SSE\u6d41\u5f0f\u4f20\u8f93\u7684\u6570\u636e\u683c\u5f0f\u8981\u6c42<br \/>\nimport json<br \/>\n# \u5bfc\u5165\u7c7b\u578b\u63d0\u793aList&#xff0c;\u7528\u6765\u6807\u6ce8\u5217\u8868\u7c7b\u578b\u7684\u8fd4\u56de\u503c&#xff0c;\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7f16\u8f91\u5668\u7684\u8865\u5168\u6821\u9a8c\u80fd\u529b<br \/>\nfrom typing import List<br \/>\n\u4eceFastAPI\u5bfc\u5165\u8def\u7531\u7ba1\u7406\u5668APIRouter\u548c\u4f9d\u8d56\u6ce8\u5165\u5de5\u5177Depends<br \/>\nAPIRouter&#xff1a;\u7528\u6765\u5206\u7ec4\u7ba1\u7406\u4e00\u7ec4\u76f8\u5173\u7684\u63a5\u53e3&#xff0c;\u65b9\u4fbf\u6a21\u5757\u5316\u62c6\u5206<br \/>\nDepends&#xff1a;\u4f9d\u8d56\u6ce8\u5165\u5de5\u5177&#xff0c;\u81ea\u52a8\u5e2e\u6211\u4eec\u83b7\u53d6\/\u521b\u5efa\u5bf9\u8c61&#xff0c;\u4e0d\u7528\u624b\u52a8\u5b9e\u4f8b\u5316<br \/>\nfrom fastapi import APIRouter, Depends<br \/>\n\u5bfc\u5165\u6d41\u5f0f\u54cd\u5e94\u7c7b&#xff0c;\u4e13\u95e8\u7528\u4e8e\u8fd4\u56deSSE\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\u7684\u6d41\u5f0f\u6570\u636e&#xff0c;\u5b9e\u73b0\u524d\u7aef\u6253\u5b57\u673a\u6548\u679c<br \/>\nfrom fastapi.responses import StreamingResponse<br \/>\n\u5bfc\u5165RAG\u670d\u52a1\u7c7b\u548c\u83b7\u53d6\u670d\u52a1\u5355\u4f8b\u7684\u51fd\u6570&#xff0c;\u8d1f\u8d23\u5904\u7406\u804a\u5929\u3001\u4e0a\u4f20\u7b49\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91<br \/>\nfrom app.rag_service import RAGService, get_rag_service<br \/>\n\u5bfc\u5165\u7528\u6237\u6a21\u578b\u548c\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u51fd\u6570&#xff0c;\u7528\u4e8e\u63a5\u53e3\u7684\u8eab\u4efd\u6821\u9a8c<br \/>\nfrom app.routers.users import User, get_current_active_user<br \/>\n\u5bfc\u5165\u63a5\u53e3\u7684\u6570\u636e\u6a21\u578b&#xff08;schema&#xff09;&#xff0c;\u7528\u6765\u89c4\u8303\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u6570\u636e\u683c\u5f0f<br \/>\nfrom app.schemas import ChatMessage, ChatRequest, CommonResponse, HistoryMessageResponse<br \/>\n\u5bfc\u5165\u65e5\u5fd7\u5de5\u5177\u51fd\u6570&#xff0c;\u7528\u4e8e\u521b\u5efa\u5f53\u524d\u6a21\u5757\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668<br \/>\nfrom utils.logger import setup_logger<br \/>\n\u521b\u5efa\u5f53\u524d\u6a21\u5757\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668&#xff0c;\u7528\u6765\u6253\u5370\u63a5\u53e3\u8c03\u7528\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\u3001\u9519\u8bef\u7b49\u65e5\u5fd7<br \/>\nlogger &#061; setup_logger(name)<br \/>\n\u521b\u5efa\u804a\u5929\u6a21\u5757\u7684\u8def\u7531\u5b9e\u4f8b&#xff0c;\u6240\u6709\u804a\u5929\u76f8\u5173\u7684\u63a5\u53e3\u90fd\u6302\u8f7d\u5728\u8fd9\u4e2a\u8def\u7531\u4e0a<br \/>\nchat_router &#061; APIRouter()<br \/>\n\u88c5\u9970\u5668&#xff1a;\u6ce8\u518c\u4e00\u4e2aPOST\u7c7b\u578b\u7684\u63a5\u53e3&#xff0c;\u8def\u5f84\u4e3a\/stream&#xff0c;\u6302\u8f7d\u5728chat_router\u8def\u7531\u4e0b<br \/>\n&#064;chat_router.post(&#034;\/stream&#034;)<br \/>\n\u5f02\u6b65\u63a5\u53e3\u51fd\u6570&#xff1a;\u6d41\u5f0f\u804a\u5929\u63a5\u53e3&#xff0c;\u63a5\u6536\u7528\u6237\u7684\u804a\u5929\u8bf7\u6c42&#xff0c;\u8fd4\u56deSSE\u6d41\u5f0f\u54cd\u5e94<br \/>\nasync def chat_query_stream(<br \/>\n# \u8bf7\u6c42\u4f53\u53c2\u6570&#xff1a;\u524d\u7aef\u4f20\u8fc7\u6765\u7684\u804a\u5929\u8bf7\u6c42&#xff0c;\u5305\u542b\u95ee\u9898\u3001\u6a21\u578b\u540d\u79f0\u3001\u662f\u5426\u5f00\u77e5\u8bc6\u5e93\u3001\u6e29\u5ea6\u7b49\u5b57\u6bb5<br \/>\nreq: ChatRequest,<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u81ea\u52a8\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7684\u6d3b\u8dc3\u7528\u6237<br \/>\n# \u4f1a\u81ea\u52a8\u6267\u884cget_current_active_user\u51fd\u6570\u6821\u9a8c\u7528\u6237token&#xff0c;\u6821\u9a8c\u5931\u8d25\u76f4\u63a5\u8fd4\u56de\u9519\u8bef&#xff0c;\u4e0d\u7528\u81ea\u5df1\u5199\u6821\u9a8c\u903b\u8f91<br \/>\ncurrent_user: User &#061; Depends(get_current_active_user),<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u81ea\u52a8\u83b7\u53d6RAG\u670d\u52a1\u7684\u5355\u4f8b\u5b9e\u4f8b&#xff0c;\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5bf9\u8c61<br \/>\nsvc: RAGService &#061; Depends(get_rag_service),<br \/>\n\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u662f\u6d41\u5f0f\u54cd\u5e94<br \/>\n) -&gt; StreamingResponse:<br \/>\n&#034;&#034;&#034;\u6d41\u5f0f\u804a\u5929&#xff0c;\u76f4\u63a5\u8f6c\u53d1 LlamaIndex streaming delta\u3002&#034;&#034;&#034;<br \/>\n# \u5b9a\u4e49\u5185\u90e8\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570&#xff0c;\u7528\u6765\u751f\u6210\u7b26\u5408SSE\u534f\u8bae\u683c\u5f0f\u7684\u6d41\u5f0f\u6570\u636e<br \/>\nasync def event_generator():<br \/>\n    # \u5f02\u5e38\u6355\u83b7&#xff1a;\u5904\u7406\u6d41\u5f0f\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u6240\u6709\u9519\u8bef<br \/>\n    try:<br \/>\n        # \u5f02\u6b65\u904d\u5386RAG\u670d\u52a1\u8fd4\u56de\u7684\u6bcf\u4e00\u5757\u6d41\u5f0f\u6570\u636e&#xff08;\u6587\u672c\u7247\u6bb5\u3001\u6765\u6e90\u3001\u5b8c\u6210\u4fe1\u53f7\u7b49&#xff09;<br \/>\n        async for chunk in svc.query_stream(<br \/>\n            # \u7528\u7528\u6237\u540d\u4f5c\u4e3a\u4f1a\u8bddID&#xff0c;\u6bcf\u4e2a\u7528\u6237\u72ec\u7acb\u4fdd\u5b58\u81ea\u5df1\u7684\u804a\u5929\u8bb0\u5f55<br \/>\n            session_id&#061;current_user.username,<br \/>\n            # \u7528\u6237\u7684\u95ee\u9898\u6587\u672c<br \/>\n            query&#061;req.query,<br \/>\n            # \u9009\u62e9\u7684AI\u6a21\u578b\u540d\u79f0<br \/>\n            model&#061;req.model,<br \/>\n            # \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93\u95ee\u7b54<br \/>\n            knowledge_bool&#061;req.knowledge_bool,<br \/>\n            # \u6a21\u578b\u6e29\u5ea6\u53c2\u6570<br \/>\n            temperature&#061;req.temperature,<br \/>\n            # \u6700\u5927\u8f93\u51fatoken\u6570<br \/>\n            max_tokens&#061;req.max_tokens,<br \/>\n        ):<br \/>\n            # \u5c06\u6570\u636e\u5757\u8f6c\u6210SSE\u534f\u8bae\u89c4\u5b9a\u7684\u683c\u5f0f<br \/>\n            # \u683c\u5f0f\u89c4\u5219&#xff1a;\u4ee5 data: \u5f00\u5934&#xff0c;\u540e\u9762\u8ddfJSON\u5b57\u7b26\u4e32&#xff0c;\u6700\u540e\u52a0\u4e24\u4e2a\u6362\u884c&#xff08;\u7a7a\u884c\u5206\u9694\u4e0d\u540c\u4e8b\u4ef6&#xff0c;\u524d\u7aef\u624d\u80fd\u6b63\u786e\u89e3\u6790&#xff09;<br \/>\n            # ensure_ascii&#061;False \u4fdd\u8bc1\u4e2d\u6587\u6b63\u5e38\u663e\u793a&#xff0c;\u4e0d\u4f1a\u8f6c\u6210Unicode\u7f16\u7801<br \/>\n            yield f&#034;data: {json.dumps(chunk, ensure_ascii&#061;False)}\\\\n\\\\n&#034;<br \/>\n    # \u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38<br \/>\n    except Exception as exc:<br \/>\n        # \u6784\u9020\u7edf\u4e00\u683c\u5f0f\u7684\u9519\u8bef\u6570\u636e<br \/>\n        error_data &#061; {<br \/>\n            &#034;type&#034;: &#034;error&#034;,<br \/>\n            &#034;content&#034;: f&#034;\u6d41\u5f0f\u4f20\u8f93\u9519\u8bef: {exc}&#034;,<br \/>\n            &#034;finished&#034;: True,<br \/>\n        }<br \/>\n        # \u540c\u6837\u4ee5SSE\u683c\u5f0f\u8fd4\u56de\u9519\u8bef\u4fe1\u606f&#xff0c;\u6807\u8bb0\u5bf9\u8bdd\u7ed3\u675f<br \/>\n        yield f&#034;data: {json.dumps(error_data, ensure_ascii&#061;False)}\\\\n\\\\n&#034;<\/p>\n<p># \u8fd4\u56de\u6d41\u5f0f\u54cd\u5e94\u5bf9\u8c61&#xff0c;\u628a\u751f\u6210\u5668\u4f5c\u4e3a\u5185\u5bb9\u6e90&#xff0c;\u4e00\u8fb9\u751f\u6210\u4e00\u8fb9\u4f20\u8f93\u7ed9\u524d\u7aef<br \/>\nreturn StreamingResponse(<br \/>\n    # \u6d41\u5f0f\u5185\u5bb9\u6e90&#xff1a;\u5c31\u662f\u4e0a\u9762\u5b9a\u4e49\u7684\u751f\u6210\u5668\u51fd\u6570<br \/>\n    content&#061;event_generator(),<br \/>\n    # \u6307\u5b9a\u54cd\u5e94\u7684\u5a92\u4f53\u7c7b\u578b\u4e3aSSE\u4e8b\u4ef6\u6d41&#xff0c;\u8fd9\u662fSSE\u534f\u8bae\u7684\u6807\u51c6\u7c7b\u578b<br \/>\n    media_type&#061;&#034;text\/event-stream&#034;,<br \/>\n    # \u8bbe\u7f6e\u54cd\u5e94\u5934&#xff0c;\u4fdd\u8bc1\u957f\u8fde\u63a5\u548c\u5b9e\u65f6\u6027<br \/>\n    headers&#061;{&#034;Cache-Control&#034;: &#034;no-cache&#034;, &#034;Connection&#034;: &#034;keep-alive&#034;},<br \/>\n)<br \/>\n\u88c5\u9970\u5668&#xff1a;\u6ce8\u518c\u4e00\u4e2aGET\u7c7b\u578b\u7684\u63a5\u53e3&#xff0c;\u8def\u5f84\u4e3a\/history&#xff0c;\u83b7\u53d6\u7528\u6237\u804a\u5929\u5386\u53f2<br \/>\n&#064;chat_router.get(&#034;\/history&#034;)<br \/>\n\u5f02\u6b65\u63a5\u53e3\u51fd\u6570&#xff1a;\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\u7684\u6240\u6709\u804a\u5929\u5386\u53f2\u8bb0\u5f55<br \/>\nasync def get_user_history(<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u6821\u9a8c\u5e76\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237<br \/>\ncurrent_user: User &#061; Depends(get_current_active_user),<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u83b7\u53d6RAG\u670d\u52a1\u5355\u4f8b<br \/>\nsvc: RAGService &#061; Depends(get_rag_service),<br \/>\n\u8fd4\u56de\u503c\u662f\u5386\u53f2\u6d88\u606f\u54cd\u5e94\u5bf9\u8c61\u7684\u5217\u8868<br \/>\n) -&gt; List[HistoryMessageResponse]:<br \/>\n&#034;&#034;&#034;\u8bfb\u53d6\u5f53\u524d\u7528\u6237\u7684 LlamaIndex Memory \u5386\u53f2\u3002&#034;&#034;&#034;<br \/>\n# \u5217\u8868\u63a8\u5bfc\u5f0f&#xff1a;\u904d\u5386\u7528\u6237\u7684\u4f1a\u8bdd\u5386\u53f2&#xff0c;\u8f6c\u6362\u6210\u524d\u7aef\u9700\u8981\u7684\u6807\u51c6\u54cd\u5e94\u683c\u5f0f<br \/>\nreturn [<br \/>\n# \u7528schema\u5b9a\u4e49\u7684\u5386\u53f2\u6d88\u606f\u6a21\u578b\u5305\u88c5\u6bcf\u6761\u6d88\u606f&#xff0c;\u4fdd\u8bc1\u6570\u636e\u683c\u5f0f\u7edf\u4e00<br \/>\nHistoryMessageResponse(<br \/>\n# \u5185\u5c42\u7528\u804a\u5929\u6d88\u606f\u6a21\u578b\u5305\u88c5\u5177\u4f53\u6d88\u606f\u5185\u5bb9<br \/>\nmessages&#061;ChatMessage(<br \/>\n# \u6d88\u606f\u89d2\u8272&#xff1a;user&#xff08;\u7528\u6237&#xff09;\u6216assistant&#xff08;AI\u52a9\u624b&#xff09;<br \/>\nrole&#061;msg[&#034;role&#034;],<br \/>\n# \u6d88\u606f\u7684\u6587\u672c\u5185\u5bb9<br \/>\ncontent&#061;msg[&#034;content&#034;],<br \/>\n# \u6d88\u606f\u5173\u8054\u7684\u53c2\u8003\u6765\u6e90\u5217\u8868&#xff0c;\u6ca1\u6709\u5c31\u8fd4\u56de\u7a7a\u6570\u7ec4\u515c\u5e95<br \/>\nsources&#061;msg.get(&#034;sources&#034;) or [],<br \/>\n)<br \/>\n)<br \/>\n# \u4ece\u670d\u52a1\u5c42\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u6240\u6709\u4f1a\u8bdd\u6d88\u606f&#xff0c;\u7528\u7528\u6237\u540d\u4f5c\u4e3a\u4f1a\u8bddID<br \/>\nfor msg in svc.get_session(current_user.username)<br \/>\n# \u8fc7\u6ee4\u6761\u4ef6&#xff1a;\u53ea\u4fdd\u7559\u6709\u5185\u5bb9\u7684\u6d88\u606f&#xff0c;\u8df3\u8fc7\u7a7a\u6d88\u606f<br \/>\nif msg.get(&#034;content&#034;)<br \/>\n]<br \/>\n\u88c5\u9970\u5668&#xff1a;\u6ce8\u518c\u4e00\u4e2aPOST\u7c7b\u578b\u7684\u63a5\u53e3&#xff0c;\u8def\u5f84\u4e3a\/clear&#xff0c;\u540c\u65f6\u6307\u5b9a\u54cd\u5e94\u6570\u636e\u6a21\u578b\u4e3a\u901a\u7528\u54cd\u5e94\u683c\u5f0f<br \/>\n&#064;chat_router.post(&#034;\/clear&#034;, response_model&#061;CommonResponse)<br \/>\n\u5f02\u6b65\u63a5\u53e3\u51fd\u6570&#xff1a;\u6e05\u7a7a\u5f53\u524d\u7528\u6237\u7684\u804a\u5929\u8bb0\u5f55\u548c\u8bb0\u5fc6<br \/>\nasync def chat_clear(<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u6821\u9a8c\u5e76\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237<br \/>\ncurrent_user: User &#061; Depends(get_current_active_user),<br \/>\n# \u4f9d\u8d56\u6ce8\u5165&#xff1a;\u83b7\u53d6RAG\u670d\u52a1\u5355\u4f8b<br \/>\nsvc: RAGService &#061; Depends(get_rag_service),<br \/>\n\u8fd4\u56de\u503c\u662f\u901a\u7528\u54cd\u5e94\u5bf9\u8c61<br \/>\n) -&gt; CommonResponse:<br \/>\n&#034;&#034;&#034;\u6e05\u7a7a\u5f53\u524d\u7528\u6237\u7684\u804a\u5929\u8bb0\u5fc6\u3002&#034;&#034;&#034;<br \/>\n# \u8c03\u7528\u670d\u52a1\u5c42\u7684\u6e05\u7a7a\u65b9\u6cd5&#xff0c;\u4f20\u5165\u5f53\u524d\u7528\u6237\u540d&#xff08;\u4f1a\u8bddID&#xff09;&#xff0c;\u6e05\u7a7a\u5bf9\u5e94\u7684\u804a\u5929\u8bb0\u5f55\u548c\u8bb0\u5fc6<br \/>\nsvc.clear_session(current_user.username)<br \/>\n# \u8fd4\u56de\u7edf\u4e00\u683c\u5f0f\u7684\u6210\u529f\u54cd\u5e94<br \/>\nreturn CommonResponse(status&#061;&#034;success&#034;, message&#061;&#034;\u4f1a\u8bdd\u5df2\u6e05\u7a7a&#034;)<\/p>\n<p>rag_service.py<\/p>\n<p># \u5bfc\u5165os\u6807\u51c6\u5e93&#xff0c;\u63d0\u4f9b\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u6587\u4ef6\u8def\u5f84\u62fc\u63a5\u3001\u6587\u4ef6\u64cd\u4f5c\u7b49\u57fa\u7840\u529f\u80fd<br \/>\nimport os<br \/>\n# \u5bfc\u5165shutil\u6807\u51c6\u5e93&#xff0c;\u63d0\u4f9b\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c&#xff0c;\u6bd4\u5982\u9012\u5f52\u5220\u9664\u6574\u4e2a\u76ee\u5f55<br \/>\nimport shutil<br \/>\n# \u5bfc\u5165tempfile\u6807\u51c6\u5e93&#xff0c;\u7528\u4e8e\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u548c\u4e34\u65f6\u76ee\u5f55&#xff0c;\u7528\u5b8c\u53ef\u81ea\u52a8\u6e05\u7406<br \/>\nimport tempfile<br \/>\n# \u5bfc\u5165\u7c7b\u578b\u63d0\u793a\u5de5\u5177&#xff0c;\u7528\u6765\u6807\u6ce8\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b&#xff0c;\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7f16\u8f91\u5668\u6821\u9a8c\u80fd\u529b<br \/>\nfrom typing import Dict, List, Optional, Tuple<br \/>\n\u5bfc\u5165\u6838\u5fc3\u4e1a\u52a1\u5c42\u7684RAG\u5e94\u7528\u7c7b&#xff0c;\u6240\u6709\u77e5\u8bc6\u5e93\u95ee\u7b54\u7684\u6838\u5fc3\u903b\u8f91\u90fd\u5728\u8fd9\u4e2a\u7c7b\u91cc<br \/>\nfrom core.application import RAGApplication<br \/>\n\u5bfc\u5165\u9879\u76ee\u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u5de5\u5177\u51fd\u6570&#xff0c;\u7528\u4e8e\u521b\u5efa\u5f53\u524d\u6a21\u5757\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668<br \/>\nfrom utils.logger import setup_logger<br \/>\n\u521b\u5efa\u5f53\u524d\u6a21\u5757\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668&#xff0c;\u7528\u6765\u6253\u5370\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u4fe1\u606f\u3001\u9519\u8bef\u7b49\u65e5\u5fd7<br \/>\nlogger &#061; setup_logger(name)<br \/>\n\u5b9a\u4e49RAG\u670d\u52a1\u7c7b&#xff1a;\u4f5c\u4e3aHTTP\u63a5\u53e3\u5c42\u7684\u9002\u914d\u5668&#xff0c;\u5bf9\u63a5\u4e0a\u5c42\u7684HTTP\u8bf7\u6c42&#xff0c;\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u7684\u4e34\u65f6\u5b58\u50a8\u903b\u8f91&#xff0c;\u518d\u8c03\u7528\u6838\u5fc3RAG\u529f\u80fd<br \/>\nclass RAGService:<br \/>\n&#034;&#034;&#034;HTTP \u5c42\u9002\u914d\u5668&#xff1a;\u5904\u7406\u4e0a\u4f20\u4e34\u65f6\u6587\u4ef6&#xff0c;\u5e76\u8c03\u7528\u6838\u5fc3 RAG \u5e94\u7528\u3002&#034;&#034;&#034;<br \/>\n# \u7c7b\u7684\u6784\u9020\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u521b\u5efaRAGService\u5b9e\u4f8b\u65f6\u81ea\u52a8\u6267\u884c<br \/>\ndef __init__(self) -&amp;gt; None:<br \/>\n    # \u5b9e\u4f8b\u5316\u6838\u5fc3RAG\u5e94\u7528\u5bf9\u8c61&#xff0c;\u6240\u6709\u4e1a\u52a1\u903b\u8f91\u90fd\u59d4\u6258\u7ed9\u8fd9\u4e2aapp\u5bf9\u8c61\u5904\u7406<br \/>\n    self.app &#061; RAGApplication()<\/p>\n<p># \u5904\u7406\u4e0a\u4f20\u6587\u4ef6\u7684\u65b9\u6cd5&#xff1a;\u63a5\u6536\u6587\u4ef6\u4e8c\u8fdb\u5236\u5185\u5bb9\u548c\u6587\u4ef6\u540d&#xff0c;\u4fdd\u5b58\u4e3a\u4e34\u65f6\u6587\u4ef6\u540e\u4ea4\u7ed9\u6838\u5fc3\u5e94\u7528\u5904\u7406<br \/>\n# \u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5143\u7ec4&#xff1a;(\u5904\u7406\u7ed3\u679c\u5b57\u7b26\u4e32, \u6587\u4ef6\u540d\u5217\u8868)<br \/>\ndef upload_and_process_files(self, files: List[bytes], filenames: List[str]) -&amp;gt; Tuple[str, List[str]]:<br \/>\n    # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u76ee\u5f55&#xff0c;\u76ee\u5f55\u540d\u524d\u7f00\u4e3arag_upload_&#xff0c;\u7528\u6765\u4e34\u65f6\u5b58\u653e\u4e0a\u4f20\u7684\u6587\u4ef6<br \/>\n    tmpdir &#061; tempfile.mkdtemp(prefix&#061;&#034;rag_upload_&#034;)<br \/>\n    # \u5b9a\u4e49\u7a7a\u5217\u8868&#xff0c;\u7528\u6765\u4fdd\u5b58\u6bcf\u4e2a\u4e34\u65f6\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84<br \/>\n    paths &#061; []<br \/>\n    # try\u4ee3\u7801\u5757&#xff1a;\u5305\u88f9\u53ef\u80fd\u51fa\u9519\u7684\u4e1a\u52a1\u903b\u8f91<br \/>\n    try:<br \/>\n        # \u540c\u65f6\u904d\u5386\u6587\u4ef6\u540d\u5217\u8868\u548c\u6587\u4ef6\u4e8c\u8fdb\u5236\u5185\u5bb9\u5217\u8868&#xff0c;\u4e00\u4e00\u5bf9\u5e94\u5904\u7406<br \/>\n        for name, content in zip(filenames, files):<br \/>\n            # \u62fc\u63a5\u4e34\u65f6\u76ee\u5f55\u548c\u6587\u4ef6\u540d&#xff0c;\u5f97\u5230\u4e34\u65f6\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84<br \/>\n            path &#061; os.path.join(tmpdir, name)<br \/>\n            # \u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u8fd9\u4e2a\u4e34\u65f6\u6587\u4ef6&#xff0c;with\u8bed\u6cd5\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<br \/>\n            with open(path, &#034;wb&#034;) as file:<br \/>\n                # \u628a\u4e0a\u4f20\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u5185\u5bb9\u5199\u5165\u5230\u4e34\u65f6\u6587\u4ef6\u4e2d<br \/>\n                file.write(content)<br \/>\n            # \u5c06\u8fd9\u4e2a\u4e34\u65f6\u6587\u4ef6\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u8def\u5f84\u5217\u8868\u4e2d<br \/>\n            paths.append(path)<\/p>\n<p>        # \u6253\u5370\u4fe1\u606f\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u672c\u6b21\u4e0a\u4f20\u7684\u6240\u6709\u6587\u4ef6\u8def\u5f84<br \/>\n        logger.info(&#034;\u4e0a\u4f20\u7684\u6587\u6863: %s&#034;, paths)<br \/>\n        # \u8c03\u7528\u6838\u5fc3RAG\u5e94\u7528\u7684\u4e0a\u4f20\u5904\u7406\u65b9\u6cd5&#xff0c;\u4f20\u5165\u6587\u4ef6\u8def\u5f84\u5217\u8868&#xff0c;\u540c\u65f6\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u548c\u539f\u6587\u4ef6\u540d<br \/>\n        return self.app.upload_and_process_files(paths), filenames<br \/>\n    # finally\u4ee3\u7801\u5757&#xff1a;\u65e0\u8bba\u4e0a\u9762\u7684\u4ee3\u7801\u6210\u529f\u6267\u884c\u8fd8\u662f\u62a5\u9519&#xff0c;\u90fd\u4f1a\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801<br \/>\n    finally:<br \/>\n        # \u9012\u5f52\u5220\u9664\u6574\u4e2a\u4e34\u65f6\u76ee\u5f55\u548c\u91cc\u9762\u7684\u6240\u6709\u6587\u4ef6&#xff0c;ignore_errors&#061;True\u8868\u793a\u5220\u9664\u5931\u8d25\u4e5f\u4e0d\u629b\u51fa\u9519\u8bef<br \/>\n        shutil.rmtree(tmpdir, ignore_errors&#061;True)<\/p>\n<p># \u5f02\u6b65\u65b9\u6cd5&#xff1a;\u6d41\u5f0f\u95ee\u7b54\u63a5\u53e3&#xff0c;\u63a5\u6536\u67e5\u8be2\u53c2\u6570&#xff0c;\u8f6c\u53d1\u7ed9\u6838\u5fc3\u5e94\u7528&#xff0c;\u518d\u6d41\u5f0f\u8fd4\u56de\u7ed3\u679c\u7ed9\u4e0a\u5c42<br \/>\nasync def query_stream(<br \/>\n    self,<br \/>\n    session_id: str,          # \u4f1a\u8bddID&#xff0c;\u7528\u6765\u533a\u5206\u4e0d\u540c\u7684\u7528\u6237\u6216\u8005\u4e0d\u540c\u7684\u804a\u5929\u7a97\u53e3<br \/>\n    query: str,               # \u7528\u6237\u8f93\u5165\u7684\u95ee\u9898\u6587\u672c<br \/>\n    model: str,               # \u9009\u62e9\u4f7f\u7528\u7684AI\u6a21\u578b\u540d\u79f0<br \/>\n    knowledge_bool: bool,     # \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93\u95ee\u7b54\u7684\u5f00\u5173<br \/>\n    temperature: float,       # \u6a21\u578b\u6e29\u5ea6\u53c2\u6570&#xff0c;\u63a7\u5236\u56de\u7b54\u7684\u968f\u673a\u6027<br \/>\n    max_tokens: int,          # \u6a21\u578b\u6700\u5927\u8f93\u51fatoken\u6570&#xff0c;\u9650\u5236\u56de\u7b54\u957f\u5ea6<br \/>\n):<br \/>\n    # \u5148\u8c03\u7528\u6838\u5fc3\u5e94\u7528\u7684\u65b9\u6cd5&#xff0c;\u66f4\u65b0\u6a21\u578b\u7684\u914d\u7f6e\u53c2\u6570<br \/>\n    self.app.update_model_config(model, temperature, max_tokens)<br \/>\n    # \u5f02\u6b65\u904d\u5386\u6838\u5fc3\u5e94\u7528\u8fd4\u56de\u7684\u6d41\u5f0f\u6570\u636e\u5757<br \/>\n    async for chunk in self.app.query_documents_stream(<br \/>\n        session_id&#061;session_id,<br \/>\n        query&#061;query,<br \/>\n        knowledge_bool&#061;knowledge_bool,<br \/>\n    ):<br \/>\n        # \u5c06\u6bcf\u4e00\u4e2a\u6570\u636e\u5757\u539f\u6837yield\u8fd4\u56de\u7ed9\u4e0a\u5c42\u8c03\u7528\u8005&#xff08;\u4e5f\u5c31\u662fHTTP\u63a5\u53e3\u5c42&#xff09;&#xff0c;\u5b9e\u73b0\u6d41\u5f0f\u4f20\u8f93<br \/>\n        yield chunk<\/p>\n<p># \u83b7\u53d6\u6307\u5b9a\u4f1a\u8bdd\u7684\u5386\u53f2\u804a\u5929\u8bb0\u5f55<br \/>\ndef get_session(self, session_id: str):<br \/>\n    # \u76f4\u63a5\u8c03\u7528\u6838\u5fc3\u5e94\u7528\u7684\u83b7\u53d6\u5386\u53f2\u65b9\u6cd5&#xff0c;\u8fd4\u56de\u7ed3\u679c<br \/>\n    return self.app.get_session_history(session_id)<\/p>\n<p># \u6e05\u7a7a\u6307\u5b9a\u4f1a\u8bdd\u7684\u804a\u5929\u8bb0\u5f55\u548c\u8bb0\u5fc6<br \/>\ndef clear_session(self, session_id: str) -&amp;gt; None:<br \/>\n    # \u76f4\u63a5\u8c03\u7528\u6838\u5fc3\u5e94\u7528\u7684\u6e05\u7a7a\u65b9\u6cd5<br \/>\n    self.app.clear_session(session_id)<\/p>\n<p># \u91cd\u7f6e\u6574\u4e2aRAG\u7cfb\u7edf<br \/>\ndef reset_system(self) -&amp;gt; None:<br \/>\n    # \u8c03\u7528\u6838\u5fc3\u5e94\u7528\u7684\u91cd\u7f6e\u65b9\u6cd5<br \/>\n    self.app.reset()<br \/>\n\u5168\u5c40\u53d8\u91cf&#xff1a;\u5b58\u50a8RAGService\u7684\u5355\u4f8b\u5b9e\u4f8b&#xff0c;\u521d\u59cb\u503c\u4e3aNone&#xff0c;\u7c7b\u578b\u662f\u53ef\u9009\u7684RAGService<br \/>\n_rag_service: Optional[RAGService] &#061; None<br \/>\n\u5168\u5c40\u51fd\u6570&#xff1a;\u83b7\u53d6RAGService\u7684\u5355\u4f8b\u5b9e\u4f8b&#xff0c;\u4fdd\u8bc1\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2aRAGService\u5b9e\u4f8b<br \/>\ndef get_rag_service() -&gt; RAGService:<br \/>\n# \u58f0\u660e\u8981\u4f7f\u7528\u5168\u5c40\u4f5c\u7528\u57df\u7684_rag_service\u53d8\u91cf<br \/>\nglobal _rag_service<br \/>\n# \u5224\u65ad\u5168\u5c40\u5b9e\u4f8b\u662f\u5426\u8fd8\u6ca1\u6709\u88ab\u521b\u5efa<br \/>\nif _rag_service is None:<br \/>\n# \u6ca1\u6709\u521b\u5efa\u7684\u8bdd&#xff0c;\u5c31\u65b0\u5efa\u4e00\u4e2aRAGService\u5b9e\u4f8b&#xff0c;\u8d4b\u503c\u7ed9\u5168\u5c40\u53d8\u91cf<br \/>\n_rag_service &#061; RAGService()<br \/>\n# \u8fd4\u56de\u5168\u5c40\u7684\u5355\u4f8b\u5b9e\u4f8b<br \/>\nreturn _rag_service<\/p>\n<p>application.py<\/p>\n<p># \u5bfc\u5165Python\u6807\u51c6\u5e93\u7684Path\u7c7b&#xff0c;\u7528\u4e8e\u5b89\u5168\u3001\u8de8\u5e73\u53f0\u5730\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84&#xff0c;\u907f\u514d\u5b57\u7b26\u4e32\u62fc\u63a5\u8def\u5f84\u7684\u517c\u5bb9\u6027\u95ee\u9898<br \/>\nfrom pathlib import Path<br \/>\n# \u5bfc\u5165Python\u7c7b\u578b\u63d0\u793a\u5de5\u5177\u96c6&#xff0c;\u7528\u4e8e\u6807\u6ce8\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b&#xff0c;\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7f16\u8f91\u5668\u7684\u8865\u5168\u6821\u9a8c\u80fd\u529b<br \/>\nfrom typing import Any, AsyncGenerator, Dict, List, Optional<br \/>\n\u5bfc\u5165ChromaDB\u5411\u91cf\u6570\u636e\u5e93&#xff0c;\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8\u6587\u672c\u7684\u8bed\u4e49\u5411\u91cf&#xff0c;\u652f\u6301\u5feb\u901f\u76f8\u4f3c\u5ea6\u68c0\u7d22&#xff0c;\u662fRAG\u7cfb\u7edf\u7684\u6838\u5fc3\u5b58\u50a8\u7ec4\u4ef6<br \/>\nimport chromadb<br \/>\n\u4eceLlamaIndex\u6838\u5fc3\u5e93\u5bfc\u5165\u591a\u4e2a\u6838\u5fc3\u7ec4\u4ef6&#xff0c;LlamaIndex\u662f\u6784\u5efa\u77e5\u8bc6\u5e93\u95ee\u7b54&#xff08;RAG&#xff09;\u7cfb\u7edf\u7684\u4e3b\u6d41\u5f00\u53d1\u6846\u67b6<br \/>\nfrom llama_index.core import (<br \/>\nSettings,                 # Settings&#xff1a;LlamaIndex\u5168\u5c40\u914d\u7f6e\u5bf9\u8c61&#xff0c;\u7edf\u4e00\u7ba1\u7406\u6574\u4e2a\u9879\u76ee\u7684\u5927\u6a21\u578b\u3001\u5d4c\u5165\u6a21\u578b\u7b49\u5168\u5c40\u914d\u7f6e<br \/>\nSimpleDirectoryReader,    # SimpleDirectoryReader&#xff1a;\u6587\u6863\u8bfb\u53d6\u5de5\u5177&#xff0c;\u81ea\u52a8\u8bc6\u522b\u5e76\u8bfb\u53d6\u591a\u79cd\u683c\u5f0f\u7684\u6587\u6863\u6587\u4ef6&#xff0c;\u8f6c\u6362\u4e3a\u7a0b\u5e8f\u53ef\u5904\u7406\u7684\u6587\u6863\u5bf9\u8c61<br \/>\nStorageContext,           # StorageContext&#xff1a;\u5b58\u50a8\u4e0a\u4e0b\u6587&#xff0c;\u7edf\u4e00\u5c01\u88c5\u7ba1\u7406\u5411\u91cf\u5b58\u50a8\u3001\u6587\u6863\u5b58\u50a8\u3001\u7d22\u5f15\u5b58\u50a8\u4e09\u7c7b\u6570\u636e\u5b58\u50a8<br \/>\nVectorStoreIndex,         # VectorStoreIndex&#xff1a;\u5411\u91cf\u7d22\u5f15&#xff0c;\u57fa\u4e8e\u6587\u672c\u5411\u91cf\u6784\u5efa\u7684\u68c0\u7d22\u7d22\u5f15&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u8bed\u4e49\u76f8\u4f3c\u5ea6\u641c\u7d22<br \/>\nload_index_from_storage,  # load_index_from_storage&#xff1a;\u5de5\u5177\u51fd\u6570&#xff0c;\u7528\u4e8e\u4ece\u672c\u5730\u6301\u4e45\u5316\u6587\u4ef6\u4e2d\u52a0\u8f7d\u5df2\u6784\u5efa\u597d\u7684\u5411\u91cf\u7d22\u5f15<br \/>\n)<br \/>\n\u5bfc\u5165\u4e0a\u4e0b\u6587\u804a\u5929\u5f15\u64ce&#xff0c;\u5f00\u542fRAG\u77e5\u8bc6\u5e93\u65f6\u4f7f\u7528&#xff0c;\u56de\u7b54\u95ee\u9898\u524d\u4f1a\u5148\u68c0\u7d22\u77e5\u8bc6\u5e93&#xff0c;\u518d\u7ed3\u5408\u68c0\u7d22\u5185\u5bb9\u751f\u6210\u7b54\u6848<br \/>\nfrom llama_index.core.chat_engine.context import ContextChatEngine<br \/>\n\u5bfc\u5165\u7b80\u5355\u804a\u5929\u5f15\u64ce&#xff0c;\u7eaf\u5bf9\u8bdd\u6a21\u5f0f\u4f7f\u7528&#xff0c;\u4e0d\u68c0\u7d22\u77e5\u8bc6\u5e93&#xff0c;\u76f4\u63a5\u548c\u5927\u6a21\u578b\u8fdb\u884c\u5bf9\u8bdd<br \/>\nfrom llama_index.core.chat_engine.simple import SimpleChatEngine<br \/>\n\u5bfc\u5165\u6807\u9898\u63d0\u53d6\u5668&#xff0c;\u53ef\u81ea\u52a8\u4e3a\u6bcf\u4e2a\u6587\u672c\u5757\u63d0\u53d6\u6807\u9898&#xff0c;\u672c\u4ee3\u7801\u4e2d\u6682\u65f6\u6ce8\u91ca\u7981\u7528<br \/>\nfrom llama_index.core.extractors import TitleExtractor<br \/>\n\u5bfc\u5165\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf&#xff0c;\u53ef\u5c06\u591a\u4e2a\u6587\u6863\u5904\u7406\u6b65\u9aa4\u6309\u987a\u5e8f\u4e32\u8054\u6210\u81ea\u52a8\u5316\u6d41\u6c34\u7ebf<br \/>\nfrom llama_index.core.ingestion import IngestionPipeline<br \/>\n\u5bfc\u5165\u804a\u5929\u8bb0\u5fc6\u7f13\u51b2\u533a&#xff0c;\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2a\u4f1a\u8bdd\u7684\u804a\u5929\u5386\u53f2&#xff0c;\u8ba9AI\u5177\u5907\u4e0a\u4e0b\u6587\u8bb0\u5fc6\u80fd\u529b<br \/>\nfrom llama_index.core.memory import ChatMemoryBuffer<br \/>\n\u5bfc\u5165\u53e5\u5b50\u5206\u5272\u5668&#xff0c;\u7528\u4e8e\u5c06\u957f\u6587\u6863\u5207\u5206\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u6587\u672c\u5757&#xff08;chunk&#xff09;&#xff0c;\u9002\u914d\u68c0\u7d22\u548c\u5411\u91cf\u5316\u7684\u8981\u6c42<br \/>\nfrom llama_index.core.node_parser import SentenceSplitter<br \/>\n\u5bfc\u5165\u8bed\u4e49\u91cd\u6392\u5668&#xff0c;\u5bf9\u521d\u6b65\u68c0\u7d22\u7684\u7ed3\u679c\u6309\u8bed\u4e49\u76f8\u5173\u6027\u91cd\u65b0\u6392\u5e8f&#xff0c;\u63d0\u5347\u68c0\u7d22\u51c6\u786e\u7387<br \/>\nfrom llama_index.core.postprocessor import SentenceTransformerRerank<br \/>\n\u5bfc\u5165\u67e5\u8be2\u878d\u5408\u68c0\u7d22\u5668&#xff0c;\u53ef\u5c06\u591a\u79cd\u4e0d\u540c\u68c0\u7d22\u65b9\u5f0f\u7684\u7ed3\u679c\u878d\u5408\u5728\u4e00\u8d77&#xff0c;\u63d0\u5347\u68c0\u7d22\u6548\u679c<br \/>\nfrom llama_index.core.retrievers import QueryFusionRetriever<br \/>\n\u5bfc\u5165\u5411\u91cf\u7d22\u5f15\u68c0\u7d22\u5668&#xff0c;\u4e13\u95e8\u8d1f\u8d23\u4ece\u5411\u91cf\u7d22\u5f15\u4e2d\u6267\u884c\u8bed\u4e49\u76f8\u4f3c\u5ea6\u68c0\u7d22<br \/>\nfrom llama_index.core.retrievers import VectorIndexRetriever<br \/>\n\u5bfc\u5165\u7b80\u5355\u6587\u6863\u5b58\u50a8&#xff0c;\u7528\u4e8e\u6301\u4e45\u5316\u4fdd\u5b58\u539f\u59cb\u7684\u6587\u6863\u8282\u70b9&#xff08;\u6587\u672c\u5757&#xff09;\u6570\u636e<br \/>\nfrom llama_index.core.storage.docstore import SimpleDocumentStore<br \/>\n\u5bfc\u5165\u7b80\u5355\u804a\u5929\u5b58\u50a8&#xff0c;\u7528\u4e8e\u6301\u4e45\u5316\u4fdd\u5b58\u6240\u6709\u4f1a\u8bdd\u7684\u804a\u5929\u8bb0\u5f55\u6570\u636e<br \/>\nfrom llama_index.core.storage.chat_store import SimpleChatStore<br \/>\n\u5bfc\u5165\u7b80\u5355\u7d22\u5f15\u5b58\u50a8&#xff0c;\u7528\u4e8e\u6301\u4e45\u5316\u4fdd\u5b58\u5411\u91cf\u7d22\u5f15\u7684\u5143\u6570\u636e\u4fe1\u606f<br \/>\nfrom llama_index.core.storage.index_store import SimpleIndexStore<br \/>\n\u5bfc\u5165HuggingFace\u5d4c\u5165\u6a21\u578b&#xff0c;\u8d1f\u8d23\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u8bed\u4e49\u5411\u91cf&#xff0c;\u662f\u5b9e\u73b0\u8bed\u4e49\u641c\u7d22\u7684\u6838\u5fc3\u6a21\u578b<br \/>\nfrom llama_index.embeddings.huggingface import HuggingFaceEmbedding<br \/>\n\u5bfc\u5165DashScope&#xff08;\u963f\u91cc\u4e91\u901a\u4e49\u5343\u95ee&#xff09;\u5927\u6a21\u578b\u9002\u914d\u5668&#xff0c;\u672c\u4ee3\u7801\u5b9e\u9645\u4f7f\u7528OpenAI\u517c\u5bb9\u6a21\u5f0f&#xff0c;\u6b64\u5bfc\u5165\u6682\u672a\u76f4\u63a5\u4f7f\u7528<br \/>\nfrom llama_index.llms.dashscope import DashScope<br \/>\n\u5bfc\u5165BM25\u68c0\u7d22\u5668&#xff0c;\u57fa\u4e8e\u4f20\u7edf\u5173\u952e\u8bcd\u5339\u914d\u7684\u68c0\u7d22\u65b9\u5f0f&#xff0c;\u548c\u5411\u91cf\u68c0\u7d22\u5f62\u6210\u4e92\u8865<br \/>\nfrom llama_index.retrievers.bm25 import BM25Retriever<br \/>\n\u5bfc\u5165Chroma\u5411\u91cf\u5b58\u50a8\u9002\u914d\u5668&#xff0c;\u8ba9LlamaIndex\u53ef\u4ee5\u5bf9\u63a5ChromaDB\u5411\u91cf\u6570\u636e\u5e93<br \/>\nfrom llama_index.vector_stores.chroma import ChromaVectorStore<br \/>\n\u5bfc\u5165OpenAI\u517c\u5bb9\u5927\u6a21\u578b\u9002\u914d\u5668&#xff0c;\u53ea\u8981\u63a5\u53e3\u7b26\u5408OpenAI\u89c4\u8303\u7684\u5927\u6a21\u578b\u90fd\u53ef\u4ee5\u7528\u5b83\u5bf9\u63a5<br \/>\nfrom llama_index.llms.openai_like import OpenAILike<br \/>\n\u5bfc\u5165\u9879\u76ee\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u7c7b&#xff0c;\u5e76\u91cd\u547d\u540d\u4e3aAppSettings&#xff0c;\u907f\u514d\u548cLlamaIndex\u7684Settings\u91cd\u540d<br \/>\nfrom config.settings import Settings as AppSettings<br \/>\n\u5bfc\u5165\u9879\u76ee\u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u5de5\u5177\u51fd\u6570&#xff0c;\u7528\u4e8e\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<br \/>\nfrom utils.logger import setup_logger<br \/>\n\u521b\u5efa\u5f53\u524d\u6a21\u5757\u7684\u65e5\u5fd7\u8bb0\u5f55\u5668&#xff0c;\u7528\u4e8e\u6253\u5370\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\u548c\u9519\u8bef\u65e5\u5fd7<br \/>\nlogger &#061; setup_logger(name)<br \/>\n\u5b9a\u4e49\u6587\u6863\u6444\u53d6\u7ba1\u9053\u7c7b&#xff0c;\u8d1f\u8d23\u5b8c\u6574\u7684\u6587\u6863\u5165\u5e93\u5168\u6d41\u7a0b&#xff1a;\u8bfb\u53d6\u6587\u6863\u2192\u5207\u5206\u6587\u672c\u2192\u5411\u91cf\u5316\u2192\u5b58\u5165\u6570\u636e\u5e93\u2192\u6301\u4e45\u5316\u4fdd\u5b58<br \/>\nclass DocumentIngestionPipeline:<br \/>\n&#034;&#034;&#034;\u6587\u6863\u6444\u53d6&#xff1a;\u628a\u8bfb\u53d6\u3001\u5207\u5206\u3001\u5411\u91cf\u5316\u548c\u6301\u4e45\u5316\u4ea4\u7ed9 LlamaIndex\u3002&#034;&#034;&#034;<br \/>\n# \u7c7b\u7684\u6784\u9020\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u521b\u5efa\u7c7b\u5b9e\u4f8b\u65f6\u81ea\u52a8\u6267\u884c&#xff0c;\u8fd4\u56de\u503c\u7c7b\u578b\u4e3aNone<br \/>\ndef __init__(self) -&amp;gt; None:<br \/>\n    # \u5b9e\u4f8b\u5c5e\u6027&#xff1a;\u5411\u91cf\u7d22\u5f15\u5bf9\u8c61&#xff0c;\u7c7b\u578b\u4e3a\u53ef\u9009\u7684VectorStoreIndex&#xff0c;\u521d\u59cb\u503c\u8bbe\u4e3aNone&#xff0c;\u540e\u7eed\u52a0\u8f7d\u6216\u521b\u5efa\u540e\u624d\u4f1a\u8d4b\u503c<br \/>\n    self.index: Optional[VectorStoreIndex] &#061; None<br \/>\n    # \u8c03\u7528\u79c1\u6709\u65b9\u6cd5&#xff0c;\u521d\u59cb\u5316\u5927\u8bed\u8a00\u6a21\u578b\u548c\u5d4c\u5165\u6a21\u578b\u7684\u5168\u5c40\u914d\u7f6e<br \/>\n    self._setup_models()<br \/>\n    # \u8c03\u7528\u79c1\u6709\u65b9\u6cd5&#xff0c;\u521b\u5efa\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf&#xff0c;\u4e32\u8054\u6587\u6863\u5207\u5206\u3001\u5411\u91cf\u5316\u7b49\u5904\u7406\u6b65\u9aa4<br \/>\n    self._create_pipeline()<br \/>\n    # \u8c03\u7528\u79c1\u6709\u65b9\u6cd5&#xff0c;\u521d\u59cb\u5316\u5404\u7c7b\u5b58\u50a8\u7ec4\u4ef6&#xff08;\u5411\u91cf\u6570\u636e\u5e93\u3001\u6587\u6863\u5b58\u50a8\u3001\u7d22\u5f15\u5b58\u50a8&#xff09;<br \/>\n    self._initialize_storage()<\/p>\n<p># \u5b9a\u4e49\u66f4\u65b0\u6a21\u578b\u914d\u7f6e\u7684\u65b9\u6cd5&#xff0c;\u652f\u6301\u52a8\u6001\u5207\u6362\u6a21\u578b\u3001\u8c03\u6574\u6e29\u5ea6\u53c2\u6570\u548c\u6700\u5927\u8f93\u51fa\u957f\u5ea6<br \/>\ndef update_model_config(self, model_name: str, temperature: float, max_tokens: int) -&amp;gt; None:<br \/>\n    # \u66f4\u65b0LlamaIndex\u5168\u5c40\u914d\u7f6e\u4e2d\u7684\u5927\u8bed\u8a00\u6a21\u578b&#xff0c;\u4f7f\u7528OpenAI\u517c\u5bb9\u683c\u5f0f\u5bf9\u63a5<br \/>\n    Settings.llm &#061; OpenAILike(<br \/>\n        model&#061;model_name,                     # \u6ce8\u610f\u53c2\u6570\u540d\u662f model \u800c\u4e0d\u662f model_name<br \/>\n        api_key&#061;AppSettings.API_KEY,          # \u5927\u6a21\u578b\u63a5\u53e3\u7684\u8ba4\u8bc1\u5bc6\u94a5&#xff0c;\u4ece\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6<br \/>\n        api_base&#061;AppSettings.API_BASE_URL,    # \u5927\u6a21\u578b\u63a5\u53e3\u7684\u57fa\u7840\u8bbf\u95ee\u5730\u5740&#xff0c;\u4ece\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6<br \/>\n        temperature&#061;temperature,              # \u6e29\u5ea6\u53c2\u6570&#xff0c;\u63a7\u5236\u56de\u7b54\u7684\u968f\u673a\u6027&#xff1a;\u503c\u8d8a\u9ad8\u8d8a\u53d1\u6563\u6709\u521b\u610f&#xff0c;\u503c\u8d8a\u4f4e\u8d8a\u7a33\u5b9a\u4e25\u8c28<br \/>\n        max_tokens&#061;max_tokens,                # \u6700\u5927\u8f93\u51fatoken\u6570&#xff0c;\u9650\u5236AI\u56de\u7b54\u7684\u5185\u5bb9\u957f\u5ea6<br \/>\n        context_window&#061;32768,                 # \u6a21\u578b\u652f\u6301\u7684\u6700\u5927\u4e0a\u4e0b\u6587\u7a97\u53e3\u5927\u5c0f&#xff0c;\u51b3\u5b9a\u80fd\u5904\u7406\u7684\u6700\u957f\u4e0a\u4e0b\u6587<br \/>\n        is_chat_model&#061;True,                   # \u6807\u8bb0\u8be5\u6a21\u578b\u662f\u5bf9\u8bdd\u578b\u6a21\u578b&#xff0c;\u9002\u914d\u804a\u5929\u573a\u666f<br \/>\n    )<br \/>\n    # \u6253\u5370\u4fe1\u606f\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u6a21\u578b\u66f4\u65b0\u7684\u53c2\u6570<br \/>\n    logger.info(&#034;\u6a21\u578b\u5df2\u66f4\u65b0: model&#061;%s, temperature&#061;%s&#034;, model_name, temperature)<\/p>\n<p># \u5b9a\u4e49\u6587\u6863\u6444\u53d6\u7684\u6838\u5fc3\u65b9\u6cd5&#xff0c;\u63a5\u6536\u6587\u4ef6\u8def\u5f84\u5217\u8868&#xff0c;\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u5b57\u7b26\u4e32<br \/>\ndef ingest_documents(self, file_paths: List[str]) -&amp;gt; str:<br \/>\n    # \u6821\u9a8c\u6240\u6709\u4f20\u5165\u7684\u6587\u4ef6\u8def\u5f84&#xff0c;\u53ea\u4fdd\u7559\u771f\u5b9e\u5b58\u5728\u7684\u6587\u4ef6&#xff0c;\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u6709\u6548\u8def\u5f84\u5217\u8868<br \/>\n    valid_paths &#061; [str(Path(path)) for path in file_paths if Path(path).exists()]<br \/>\n    # \u5224\u65ad\u6709\u6548\u8def\u5f84\u5217\u8868\u662f\u5426\u4e3a\u7a7a<br \/>\n    if not valid_paths:<br \/>\n        # \u6ca1\u6709\u6709\u6548\u6587\u4ef6\u65f6&#xff0c;\u76f4\u63a5\u8fd4\u56de\u63d0\u793a\u4fe1\u606f<br \/>\n        return &#034;\u6ca1\u6709\u627e\u5230\u6709\u6548\u7684\u6587\u6863&#034;<\/p>\n<p>    # \u5f00\u59cb\u5f02\u5e38\u6355\u83b7&#xff0c;\u5904\u7406\u6587\u6863\u6444\u53d6\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u6240\u6709\u9519\u8bef<br \/>\n    try:<br \/>\n        # \u4f7f\u7528\u6587\u6863\u8bfb\u53d6\u5668\u52a0\u8f7d\u6240\u6709\u6709\u6548\u6587\u4ef6&#xff0c;\u8f6c\u6362\u4e3aLlamaIndex\u53ef\u5904\u7406\u7684Document\u5bf9\u8c61\u5217\u8868<br \/>\n        documents &#061; SimpleDirectoryReader(input_files&#061;valid_paths).load_data()<br \/>\n        # \u5c06\u6587\u6863\u9001\u5165\u6444\u53d6\u6d41\u6c34\u7ebf&#xff0c;\u81ea\u52a8\u5b8c\u6210\u6587\u672c\u5207\u5206\u3001\u5411\u91cf\u5316&#xff0c;\u5f97\u5230\u5904\u7406\u540e\u7684\u6587\u6863\u8282\u70b9\u5217\u8868<br \/>\n        nodes &#061; self.pipeline.run(documents&#061;documents)<br \/>\n        # \u5224\u65ad\u751f\u6210\u7684\u6587\u6863\u8282\u70b9\u662f\u5426\u4e3a\u7a7a<br \/>\n        if not nodes:<br \/>\n            # \u6ca1\u6709\u751f\u6210\u6709\u6548\u8282\u70b9\u65f6\u8fd4\u56de\u63d0\u793a\u4fe1\u606f<br \/>\n            return &#034;\u6ca1\u6709\u751f\u6210\u6709\u6548\u7684\u6587\u6863\u8282\u70b9&#034;<\/p>\n<p>        # \u5224\u65ad\u5f53\u524d\u5411\u91cf\u7d22\u5f15\u662f\u5426\u4e3a\u7a7a&#xff08;\u8fd8\u6ca1\u521b\u5efa\u6216\u52a0\u8f7d&#xff09;<br \/>\n        if self.index is None:<br \/>\n            # \u5c1d\u8bd5\u52a0\u8f7d\u672c\u5730\u5df2\u6709\u7684\u7d22\u5f15<br \/>\n            try:<br \/>\n                # \u8c03\u7528\u52a0\u8f7d\u7d22\u5f15\u7684\u65b9\u6cd5&#xff0c;\u4ece\u672c\u5730\u6301\u4e45\u5316\u6587\u4ef6\u52a0\u8f7d\u7d22\u5f15<br \/>\n                self.load_index()<br \/>\n            # \u6355\u83b7\u52a0\u8f7d\u5931\u8d25\u7684\u8fd0\u884c\u65f6\u9519\u8bef<br \/>\n            except RuntimeError:<br \/>\n                # \u52a0\u8f7d\u5931\u8d25\u5219\u8df3\u8fc7&#xff0c;\u540e\u7eed\u4f1a\u65b0\u5efa\u7d22\u5f15<br \/>\n                pass<\/p>\n<p>        # \u518d\u6b21\u5224\u65ad\u7d22\u5f15\u662f\u5426\u4ecd\u4e3a\u7a7a&#xff08;\u52a0\u8f7d\u5931\u8d25\u6216\u672c\u6765\u5c31\u6ca1\u6709\u7d22\u5f15&#xff09;<br \/>\n        if self.index is None:<br \/>\n            # \u8c03\u7528\u79c1\u6709\u65b9\u6cd5&#xff0c;\u6e05\u7a7a\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u7684\u65e7\u6570\u636e&#xff0c;\u907f\u514d\u6570\u636e\u51b2\u7a81<br \/>\n            self._clear_vector_store()<br \/>\n            # \u4f7f\u7528\u5904\u7406\u597d\u7684\u6587\u6863\u8282\u70b9\u521b\u5efa\u5168\u65b0\u7684\u5411\u91cf\u7d22\u5f15<br \/>\n            self.index &#061; VectorStoreIndex(<br \/>\n                nodes,<br \/>\n                # \u4f7f\u7528\u521d\u59cb\u5316\u597d\u7684\u5b58\u50a8\u4e0a\u4e0b\u6587\u6765\u7ba1\u7406\u6570\u636e\u5b58\u50a8<br \/>\n                storage_context&#061;self.storage_context,<br \/>\n                store_nodes_override&#061;True,  # \u5f00\u542f\u8282\u70b9\u5b58\u50a8\u8986\u76d6&#xff0c;\u786e\u4fdd\u8282\u70b9\u6570\u636e\u5b8c\u6574\u5b58\u5165\u6587\u6863\u5b58\u50a8<br \/>\n            )<br \/>\n        # \u7d22\u5f15\u5df2\u7ecf\u5b58\u5728\u7684\u60c5\u51b5<br \/>\n        else:<br \/>\n            # \u5c06\u65b0\u7684\u6587\u6863\u8282\u70b9\u8ffd\u52a0\u5230\u5df2\u6709\u7684\u5411\u91cf\u7d22\u5f15\u4e2d<br \/>\n            self.index.insert_nodes(nodes)<br \/>\n        # \u8c03\u7528\u79c1\u6709\u65b9\u6cd5&#xff0c;\u5c06\u6240\u6709\u5b58\u50a8\u6570\u636e\u6301\u4e45\u5316\u4fdd\u5b58\u5230\u672c\u5730\u786c\u76d8<br \/>\n        self._persist_storage()<\/p>\n<p>        # \u62fc\u63a5\u5904\u7406\u6210\u529f\u7684\u7ed3\u679c\u4fe1\u606f&#xff0c;\u5305\u542b\u6587\u6863\u6570\u91cf\u548c\u8282\u70b9\u6570\u91cf<br \/>\n        result &#061; f&#034;\u6210\u529f\u6444\u53d6\u4e86 {len(valid_paths)} \u4e2a\u6587\u6863&#xff0c;\u751f\u6210\u4e86 {len(nodes)} \u4e2a\u8282\u70b9&#034;<br \/>\n        # \u6253\u5370\u4fe1\u606f\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u6587\u6863\u6444\u53d6\u6210\u529f<br \/>\n        logger.info(result)<br \/>\n        # \u8fd4\u56de\u6210\u529f\u7ed3\u679c\u4fe1\u606f<br \/>\n        return result<br \/>\n    # \u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38<br \/>\n    except Exception as exc:<br \/>\n        # \u62fc\u63a5\u9519\u8bef\u4fe1\u606f&#xff0c;\u5305\u542b\u5f02\u5e38\u8be6\u60c5<br \/>\n        error_msg &#061; f&#034;\u6587\u6863\u6444\u53d6\u5931\u8d25: {exc}&#034;<br \/>\n        # \u6253\u5370\u9519\u8bef\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u5931\u8d25\u539f\u56e0<br \/>\n        logger.error(error_msg)<br \/>\n        # \u8fd4\u56de\u9519\u8bef\u4fe1\u606f<br \/>\n        return error_msg<\/p>\n<p># \u5b9a\u4e49\u52a0\u8f7d\u7d22\u5f15\u7684\u65b9\u6cd5&#xff0c;\u4ece\u672c\u5730\u6301\u4e45\u5316\u5b58\u50a8\u4e2d\u52a0\u8f7d\u5df2\u6784\u5efa\u7684\u5411\u91cf\u7d22\u5f15<br \/>\ndef load_index(self) -&amp;gt; None:<br \/>\n    # \u6253\u5370\u65e5\u5fd7&#xff0c;\u63d0\u793a\u5f00\u59cb\u52a0\u8f7d\u77e5\u8bc6\u5e93\u7d22\u5f15<br \/>\n    logger.info(&#034;\u4ece\u6301\u4e45\u5316\u5b58\u50a8\u52a0\u8f7d\u77e5\u8bc6\u5e93\u7d22\u5f15&#034;)<br \/>\n    # \u5f02\u5e38\u6355\u83b7&#xff0c;\u5904\u7406\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef<br \/>\n    try:<br \/>\n        # \u8c03\u7528LlamaIndex\u63d0\u4f9b\u7684\u51fd\u6570&#xff0c;\u4ece\u5b58\u50a8\u4e0a\u4e0b\u6587\u4e2d\u52a0\u8f7d\u5411\u91cf\u7d22\u5f15<br \/>\n        self.index &#061; load_index_from_storage(<br \/>\n            self.storage_context,<br \/>\n            store_nodes_override&#061;True,  # \u5f00\u542f\u8282\u70b9\u5b58\u50a8\u8986\u76d6&#xff0c;\u4fdd\u8bc1\u52a0\u8f7d\u7684\u8282\u70b9\u6570\u636e\u5b8c\u6574<br \/>\n        )<br \/>\n    # \u6355\u83b7\u503c\u9519\u8bef\u5f02\u5e38<br \/>\n    except ValueError as exc:<br \/>\n        # \u5224\u65ad\u9519\u8bef\u662f\u5426\u662f\u201c\u5b58\u50a8\u4e2d\u6ca1\u6709\u7d22\u5f15\u201d\u5bfc\u81f4\u7684<br \/>\n        if &#034;No index in storage context&#034; in str(exc):<br \/>\n            # \u8f6c\u6362\u4e3a\u66f4\u6613\u61c2\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u629b\u51fa&#xff0c;\u540c\u65f6\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u94fe<br \/>\n            raise RuntimeError(&#034;\u77e5\u8bc6\u5e93\u7d22\u5f15\u672a\u521d\u59cb\u5316&#xff0c;\u8bf7\u5148\u4e0a\u4f20\u6587\u6863&#034;) from exc<br \/>\n        # \u5176\u4ed6\u7c7b\u578b\u7684\u503c\u9519\u8bef\u76f4\u63a5\u629b\u51fa<br \/>\n        raise<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u521d\u59cb\u5316\u5168\u5c40\u6a21\u578b\u914d\u7f6e<br \/>\ndef _setup_models(self) -&amp;gt; None:<br \/>\n    # \u914d\u7f6e\u5168\u5c40\u4f7f\u7528\u7684\u5927\u8bed\u8a00\u6a21\u578b&#xff0c;\u91c7\u7528OpenAI\u517c\u5bb9\u534f\u8bae\u5bf9\u63a5<br \/>\n    Settings.llm &#061; OpenAILike(<br \/>\n        model&#061;AppSettings.MODEL,  # \u6ce8\u610f\u53c2\u6570\u540d\u662f model \u800c\u4e0d\u662f model_name<br \/>\n        api_key&#061;AppSettings.API_KEY,       # \u63a5\u53e3\u8ba4\u8bc1\u5bc6\u94a5&#xff0c;\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6<br \/>\n        api_base&#061;AppSettings.API_BASE_URL, # \u63a5\u53e3\u57fa\u7840\u5730\u5740&#xff0c;\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6<br \/>\n        temperature&#061;AppSettings.TEMPERATURE # \u9ed8\u8ba4\u6e29\u5ea6\u53c2\u6570&#xff0c;\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6<br \/>\n    )<\/p>\n<p>    # \u914d\u7f6e\u5168\u5c40\u4f7f\u7528\u7684\u5d4c\u5165\u6a21\u578b&#xff0c;\u57fa\u4e8eHuggingFace\u7684\u6a21\u578b&#xff0c;\u8d1f\u8d23\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u8bed\u4e49\u5411\u91cf<br \/>\n    Settings.embed_model &#061; HuggingFaceEmbedding(model_name&#061;AppSettings.EMBEDDING_MODEL_PATH)<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u521b\u5efa\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf<br \/>\ndef _create_pipeline(self) -&amp;gt; None:<br \/>\n    # \u521b\u5efa\u6444\u53d6\u6d41\u6c34\u7ebf\u5b9e\u4f8b&#xff0c;\u5c06\u591a\u4e2a\u5904\u7406\u6b65\u9aa4\u6309\u987a\u5e8f\u4e32\u8054<br \/>\n    self.pipeline &#061; IngestionPipeline(<br \/>\n        # \u6d41\u6c34\u7ebf\u7684\u8f6c\u6362\u6b65\u9aa4\u5217\u8868&#xff0c;\u6587\u6863\u4f1a\u6309\u987a\u5e8f\u7ecf\u8fc7\u6bcf\u4e2a\u6b65\u9aa4\u5904\u7406<br \/>\n        transformations&#061;[<br \/>\n            # \u7b2c\u4e00\u6b65&#xff1a;\u53e5\u5b50\u5206\u5272\u5668&#xff0c;\u5c06\u957f\u6587\u6863\u5207\u5206\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u6587\u672c\u5757&#xff08;chunk&#xff09;<br \/>\n            SentenceSplitter(<br \/>\n                chunk_size&#061;AppSettings.CHUNK_SIZE,       # \u6bcf\u4e2a\u6587\u672c\u5757\u7684\u5927\u5c0f&#xff0c;\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6<br \/>\n                chunk_overlap&#061;AppSettings.CHUNK_OVERLAP, # \u76f8\u90bb\u6587\u672c\u5757\u7684\u91cd\u53e0\u5185\u5bb9\u5927\u5c0f&#xff0c;\u907f\u514d\u8bed\u4e49\u88ab\u622a\u65ad<br \/>\n            ),<br \/>\n            # TitleExtractor(nodes&#061;AppSettings.TITLE_EXTRACTOR_NODES), # \u6807\u9898\u63d0\u53d6\u5668&#xff0c;\u6682\u65f6\u6ce8\u91ca\u7981\u7528<br \/>\n            # \u7b2c\u4e8c\u6b65&#xff1a;\u5d4c\u5165\u6a21\u578b&#xff0c;\u5c06\u6bcf\u4e2a\u6587\u672c\u5757\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u8bed\u4e49\u5411\u91cf<br \/>\n            Settings.embed_model,<br \/>\n        ]<br \/>\n    )<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u521d\u59cb\u5316\u6240\u6709\u5b58\u50a8\u7ec4\u4ef6<br \/>\ndef _initialize_storage(self) -&amp;gt; None:<br \/>\n    # \u521b\u5efa\u6301\u4e45\u5316\u6839\u76ee\u5f55&#xff0c;parents&#061;True\u8868\u793a\u81ea\u52a8\u521b\u5efa\u6240\u6709\u7236\u76ee\u5f55&#xff0c;exist_ok&#061;True\u8868\u793a\u76ee\u5f55\u5df2\u5b58\u5728\u4e5f\u4e0d\u62a5\u9519<br \/>\n    Path(AppSettings.DEFAULT_PERSIST_DIR).mkdir(parents&#061;True, exist_ok&#061;True)<\/p>\n<p>    # \u521b\u5efaChromaDB\u6301\u4e45\u5316\u5ba2\u6237\u7aef&#xff0c;\u6570\u636e\u4f1a\u6c38\u4e45\u4fdd\u5b58\u5728\u6307\u5b9a\u76ee\u5f55\u7684\u672c\u5730\u786c\u76d8<br \/>\n    chroma_client &#061; chromadb.PersistentClient(AppSettings.CHROMA_PERSIST_DIR)<br \/>\n    # \u83b7\u53d6\u6216\u521b\u5efaChromaDB\u7684\u96c6\u5408&#xff08;\u76f8\u5f53\u4e8e\u6570\u636e\u5e93\u4e2d\u7684\u8868&#xff09;&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5411\u91cf\u6570\u636e<br \/>\n    self.chroma_collection &#061; chroma_client.get_or_create_collection(AppSettings.CHROMA_COLLECTION)<br \/>\n    # \u5c06ChromaDB\u96c6\u5408\u5305\u88c5\u4e3aLlamaIndex\u517c\u5bb9\u7684\u5411\u91cf\u5b58\u50a8\u5bf9\u8c61<br \/>\n    vector_store &#061; ChromaVectorStore(chroma_collection&#061;self.chroma_collection)<\/p>\n<p>    # \u521b\u5efa\u5b58\u50a8\u4e0a\u4e0b\u6587&#xff0c;\u7edf\u4e00\u7ba1\u7406\u4e09\u7c7b\u5b58\u50a8<br \/>\n    self.storage_context &#061; StorageContext.from_defaults(<br \/>\n        docstore&#061;self._load_docstore(),      # \u4f20\u5165\u6587\u6863\u5b58\u50a8\u5b9e\u4f8b&#xff0c;\u4fdd\u5b58\u539f\u59cb\u6587\u672c\u8282\u70b9<br \/>\n        index_store&#061;self._load_index_store(),# \u4f20\u5165\u7d22\u5f15\u5b58\u50a8\u5b9e\u4f8b&#xff0c;\u4fdd\u5b58\u7d22\u5f15\u7684\u5143\u6570\u636e<br \/>\n        vector_store&#061;vector_store,           # \u4f20\u5165\u5411\u91cf\u5b58\u50a8\u5b9e\u4f8b&#xff0c;\u4fdd\u5b58\u8bed\u4e49\u5411\u91cf<br \/>\n    )<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u52a0\u8f7d\u6587\u6863\u5b58\u50a8&#xff0c;\u8fd4\u56deSimpleDocumentStore\u5b9e\u4f8b<br \/>\ndef _load_docstore(self) -&amp;gt; SimpleDocumentStore:<br \/>\n    # \u83b7\u53d6\u6587\u6863\u5b58\u50a8\u7684\u672c\u5730\u6587\u4ef6\u8def\u5f84<br \/>\n    docstore_path &#061; Path(AppSettings.DOCSTORE_PATH)<br \/>\n    # \u5224\u65ad\u672c\u5730\u662f\u5426\u5df2\u6709\u4fdd\u5b58\u7684\u6587\u6863\u5b58\u50a8\u6587\u4ef6<br \/>\n    if docstore_path.exists():<br \/>\n        # \u4ece\u672c\u5730\u8def\u5f84\u52a0\u8f7d\u5df2\u6709\u7684\u6587\u6863\u5b58\u50a8<br \/>\n        return SimpleDocumentStore.from_persist_path(str(docstore_path))<br \/>\n    # \u6ca1\u6709\u672c\u5730\u6587\u4ef6\u5219\u8fd4\u56de\u4e00\u4e2a\u5168\u65b0\u7684\u7a7a\u6587\u6863\u5b58\u50a8<br \/>\n    return SimpleDocumentStore()<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u52a0\u8f7d\u7d22\u5f15\u5b58\u50a8&#xff0c;\u8fd4\u56deSimpleIndexStore\u5b9e\u4f8b<br \/>\ndef _load_index_store(self) -&amp;gt; SimpleIndexStore:<br \/>\n    # \u83b7\u53d6\u7d22\u5f15\u5b58\u50a8\u7684\u672c\u5730\u6587\u4ef6\u8def\u5f84<br \/>\n    index_store_path &#061; Path(AppSettings.INDEX_STORE_PATH)<br \/>\n    # \u5224\u65ad\u672c\u5730\u662f\u5426\u5df2\u6709\u4fdd\u5b58\u7684\u7d22\u5f15\u5b58\u50a8\u6587\u4ef6<br \/>\n    if index_store_path.exists():<br \/>\n        # \u4ece\u672c\u5730\u8def\u5f84\u52a0\u8f7d\u5df2\u6709\u7684\u7d22\u5f15\u5b58\u50a8<br \/>\n        return SimpleIndexStore.from_persist_path(str(index_store_path))<br \/>\n    # \u6ca1\u6709\u672c\u5730\u6587\u4ef6\u5219\u8fd4\u56de\u4e00\u4e2a\u5168\u65b0\u7684\u7a7a\u7d22\u5f15\u5b58\u50a8<br \/>\n    return SimpleIndexStore()<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u5c06\u6240\u6709\u5b58\u50a8\u6570\u636e\u6301\u4e45\u5316\u5230\u672c\u5730\u786c\u76d8<br \/>\ndef _persist_storage(self) -&amp;gt; None:<br \/>\n    # \u8c03\u7528\u5b58\u50a8\u4e0a\u4e0b\u6587\u7684\u6301\u4e45\u5316\u65b9\u6cd5&#xff0c;\u5c06\u6240\u6709\u6570\u636e\u4fdd\u5b58\u5230\u6307\u5b9a\u76ee\u5f55<br \/>\n    self.storage_context.persist(persist_dir&#061;AppSettings.DEFAULT_PERSIST_DIR)<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u6e05\u7a7a\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u6570\u636e<br \/>\ndef _clear_vector_store(self) -&amp;gt; None:<br \/>\n    # \u83b7\u53d6\u96c6\u5408\u4e2d\u6240\u6709\u5411\u91cf\u6570\u636e\u7684ID\u5217\u8868&#xff0c;\u6ca1\u6709\u6570\u636e\u5219\u8fd4\u56de\u7a7a\u5217\u8868<br \/>\n    ids &#061; self.chroma_collection.get().get(&#034;ids&#034;) or []<br \/>\n    # \u5224\u65adID\u5217\u8868\u662f\u5426\u4e0d\u4e3a\u7a7a<br \/>\n    if ids:<br \/>\n        # \u6839\u636eID\u5217\u8868\u5220\u9664\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5411\u91cf\u6570\u636e<br \/>\n        self.chroma_collection.delete(ids&#061;ids)<br \/>\n\u5b9a\u4e49RAG\u5e94\u7528\u4e3b\u7c7b&#xff0c;\u662f\u6574\u4e2a\u77e5\u8bc6\u5e93\u95ee\u7b54\u7cfb\u7edf\u7684\u5bf9\u5916\u5165\u53e3&#xff0c;\u6574\u5408\u6587\u6863\u7ba1\u7406\u3001\u68c0\u7d22\u3001\u804a\u5929\u3001\u8bb0\u5fc6\u7b49\u6240\u6709\u529f\u80fd<br \/>\nclass RAGApplication:<br \/>\n&#034;&#034;&#034;RAG \u5e94\u7528\u5165\u53e3&#xff1a;\u7528 LlamaIndex \u7ba1\u7406\u6444\u53d6\u3001\u804a\u5929\u3001\u8bb0\u5fc6\u548c\u77e5\u8bc6\u5e93\u95ee\u7b54\u3002&#034;&#034;&#034;<br \/>\n# \u6784\u9020\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u521b\u5efa\u7c7b\u5b9e\u4f8b\u65f6\u81ea\u52a8\u6267\u884c<br \/>\ndef __init__(self) -&amp;gt; None:<br \/>\n    # \u5b9e\u4f8b\u5316\u6587\u6863\u6444\u53d6\u7ba1\u9053&#xff0c;\u8d1f\u8d23\u6587\u6863\u5165\u5e93\u7684\u5168\u6d41\u7a0b\u5904\u7406<br \/>\n    self.ingestion_pipeline &#061; DocumentIngestionPipeline()<br \/>\n    # \u83b7\u53d6\u804a\u5929\u8bb0\u5f55\u6301\u4e45\u5316\u5b58\u50a8\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n    self.chat_store_path &#061; Path(AppSettings.CHAT_STORE_PATH)<br \/>\n    # \u786e\u4fdd\u804a\u5929\u8bb0\u5f55\u6587\u4ef6\u7684\u7236\u76ee\u5f55\u5b58\u5728&#xff0c;\u4e0d\u5b58\u5728\u5219\u81ea\u52a8\u521b\u5efa<br \/>\n    self.chat_store_path.parent.mkdir(parents&#061;True, exist_ok&#061;True)<br \/>\n    # \u52a0\u8f7d\u804a\u5929\u5b58\u50a8\u5b9e\u4f8b&#xff0c;\u7528\u4e8e\u6301\u4e45\u5316\u4fdd\u5b58\u6240\u6709\u4f1a\u8bdd\u7684\u804a\u5929\u8bb0\u5f55<br \/>\n    self.chat_store &#061; self._load_chat_store()<br \/>\n    # \u5b57\u5178&#xff0c;\u5b58\u50a8\u6bcf\u4e2a\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u7f13\u51b2\u533a&#xff0c;key\u662f\u4f1a\u8bddID&#xff0c;value\u662f\u5bf9\u5e94\u7684\u8bb0\u5fc6\u5bf9\u8c61<br \/>\n    self.memories: Dict[str, ChatMemoryBuffer] &#061; {}<br \/>\n    # RAG\u6df7\u5408\u68c0\u7d22\u5668&#xff0c;\u521d\u59cb\u4e3aNone&#xff0c;\u91c7\u7528\u61d2\u52a0\u8f7d\u6a21\u5f0f&#xff0c;\u7528\u5230\u65f6\u518d\u521b\u5efa<br \/>\n    self.rag_retriever: Optional[QueryFusionRetriever] &#061; None<br \/>\n    # \u8bed\u4e49\u91cd\u6392\u5668&#xff0c;\u521d\u59cb\u4e3aNone&#xff0c;\u61d2\u52a0\u8f7d\u6a21\u5f0f&#xff0c;\u7528\u4e8e\u5bf9\u68c0\u7d22\u7ed3\u679c\u91cd\u65b0\u6392\u5e8f\u63d0\u5347\u51c6\u786e\u7387<br \/>\n    self.reranker: Optional[SentenceTransformerRerank] &#061; None<\/p>\n<p># \u66f4\u65b0\u6a21\u578b\u914d\u7f6e\u7684\u5bf9\u5916\u65b9\u6cd5&#xff0c;\u540c\u6b65\u66f4\u65b0\u6587\u6863\u7ba1\u9053\u7684\u6a21\u578b\u914d\u7f6e<br \/>\ndef update_model_config(self, model_name: str, temperature: float, max_tokens: int) -&amp;gt; None:<br \/>\n    # \u8c03\u7528\u6587\u6863\u6444\u53d6\u7ba1\u9053\u7684\u66f4\u65b0\u65b9\u6cd5&#xff0c;\u540c\u6b65\u66f4\u65b0\u6a21\u578b\u914d\u7f6e<br \/>\n    self.ingestion_pipeline.update_model_config(model_name, temperature, max_tokens)<\/p>\n<p># \u5904\u7406\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\u7684\u5bf9\u5916\u65b9\u6cd5&#xff0c;\u63a5\u6536\u6587\u4ef6\u8def\u5f84\u5217\u8868&#xff0c;\u8fd4\u56de\u5904\u7406\u7ed3\u679c<br \/>\ndef upload_and_process_files(self, file_paths: List[str]) -&amp;gt; str:<br \/>\n    # \u5224\u65ad\u6587\u4ef6\u8def\u5f84\u5217\u8868\u662f\u5426\u4e3a\u7a7a<br \/>\n    if not file_paths:<br \/>\n        # \u4e3a\u7a7a\u5219\u8fd4\u56de\u63d0\u793a\u4fe1\u606f<br \/>\n        return &#034;\u8bf7\u4e0a\u4f20\u81f3\u5c11\u4e00\u4e2a\u6587\u4ef6&#034;<\/p>\n<p>    # \u8c03\u7528\u6444\u53d6\u7ba1\u9053\u5904\u7406\u6587\u6863&#xff0c;\u5b8c\u6210\u77e5\u8bc6\u5e93\u5165\u5e93<br \/>\n    result &#061; self.ingestion_pipeline.ingest_documents(file_paths)<br \/>\n    # \u6587\u6863\u66f4\u65b0\u540e&#xff0c;\u65e7\u7684\u68c0\u7d22\u5668\u4e0d\u518d\u9002\u7528&#xff0c;\u7f6e\u7a7a\u540e\u4e0b\u6b21\u4f7f\u7528\u4f1a\u81ea\u52a8\u91cd\u65b0\u521b\u5efa<br \/>\n    self.rag_retriever &#061; None<br \/>\n    # \u8fd4\u56de\u5904\u7406\u7ed3\u679c<br \/>\n    return result<\/p>\n<p># \u5f02\u6b65\u65b9\u6cd5&#xff1a;\u6d41\u5f0f\u95ee\u7b54&#xff0c;\u4ee5\u5f02\u6b65\u751f\u6210\u5668\u5f62\u5f0f\u9010\u5757\u8fd4\u56de\u6570\u636e&#xff0c;\u5b9e\u73b0\u524d\u7aef\u6253\u5b57\u673a\u6548\u679c<br \/>\nasync def query_documents_stream(<br \/>\n    self,<br \/>\n    session_id: str,          # \u4f1a\u8bddID&#xff0c;\u7528\u4e8e\u533a\u5206\u4e0d\u540c\u7684\u7528\u6237\u6216\u4e0d\u540c\u7684\u804a\u5929\u7a97\u53e3<br \/>\n    query: str,               # \u7528\u6237\u8f93\u5165\u7684\u95ee\u9898\u6587\u672c<br \/>\n    knowledge_bool: bool,     # \u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93\u95ee\u7b54\u7684\u5f00\u5173<br \/>\n) -&amp;gt; AsyncGenerator[Dict[str, Any], None]:<br \/>\n    # \u5f02\u5e38\u6355\u83b7&#xff0c;\u5904\u7406\u67e5\u8be2\u8fc7\u7a0b\u4e2d\u7684\u6240\u6709\u9519\u8bef<br \/>\n    try:<br \/>\n        # \u6839\u636e\u662f\u5426\u5f00\u542f\u77e5\u8bc6\u5e93&#xff0c;\u521b\u5efa\u5bf9\u5e94\u7684\u804a\u5929\u5f15\u64ce<br \/>\n        chat_engine &#061; self._get_chat_engine(session_id, knowledge_bool)<br \/>\n        # \u5f02\u6b65\u53d1\u8d77\u6d41\u5f0f\u804a\u5929\u8bf7\u6c42&#xff0c;\u5f97\u5230\u6d41\u5f0f\u54cd\u5e94\u5bf9\u8c61<br \/>\n        stream_response &#061; await chat_engine.astream_chat(query)<\/p>\n<p>        # \u5f02\u6b65\u904d\u5386\u6d41\u5f0f\u54cd\u5e94\u7684\u6bcf\u4e2atoken&#xff08;\u6587\u672c\u7247\u6bb5&#xff09;<br \/>\n        async for token in stream_response.async_response_gen():<br \/>\n            # \u5224\u65ad\u5f53\u524dtoken\u4e0d\u4e3a\u7a7a<br \/>\n            if token:<br \/>\n                # \u4ea7\u51fa\u6587\u672c\u7c7b\u578b\u7684\u6570\u636e\u5757&#xff0c;\u6807\u8bb0\u672a\u5b8c\u6210&#xff0c;\u5185\u5bb9\u662f\u5f53\u524dtoken<br \/>\n                yield {&#034;type&#034;: &#034;text&#034;, &#034;finished&#034;: False, &#034;content&#034;: token}<\/p>\n<p>        # \u4ece\u54cd\u5e94\u4e2d\u83b7\u53d6\u68c0\u7d22\u5230\u7684\u6765\u6e90\u8282\u70b9&#xff0c;\u8f6c\u6362\u4e3a\u5b57\u5178\u5217\u8868&#xff0c;\u6ca1\u6709\u5219\u8fd4\u56de\u7a7a\u5217\u8868<br \/>\n        sources &#061; self._source_nodes_to_dicts(getattr(stream_response, &#034;source_nodes&#034;, []))<br \/>\n        # \u5224\u65ad\u662f\u5426\u6709\u6765\u6e90\u6570\u636e<br \/>\n        if sources:<br \/>\n            # \u4ea7\u51fa\u6765\u6e90\u7c7b\u578b\u7684\u6570\u636e\u5757&#xff0c;\u5305\u542b\u68c0\u7d22\u5230\u7684\u53c2\u8003\u6587\u6863\u4fe1\u606f<br \/>\n            yield {<br \/>\n                &#034;type&#034;: &#034;sources&#034;,<br \/>\n                &#034;finished&#034;: False,<br \/>\n                &#034;content&#034;: sources,<br \/>\n                &#034;sources_data&#034;: sources,<br \/>\n            }<\/p>\n<p>        # \u5c06\u672c\u6b21\u804a\u5929\u8bb0\u5f55\u6301\u4e45\u5316\u4fdd\u5b58\u5230\u672c\u5730<br \/>\n        self._persist_chat_store()<br \/>\n        # \u4ea7\u51fa\u5b8c\u6210\u4fe1\u53f7&#xff0c;\u6807\u8bb0\u5bf9\u8bdd\u7ed3\u675f&#xff0c;\u9644\u5e26\u5b8c\u6574\u7684\u56de\u7b54\u5185\u5bb9<br \/>\n        yield {&#034;type&#034;: &#034;complete&#034;, &#034;finished&#034;: True, &#034;content&#034;: stream_response.response}<br \/>\n    # \u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38<br \/>\n    except Exception as exc:<br \/>\n        # \u62fc\u63a5\u9519\u8bef\u4fe1\u606f&#xff0c;\u5305\u542b\u5f02\u5e38\u8be6\u60c5<br \/>\n        error_msg &#061; f&#034;\u67e5\u8be2\u5931\u8d25: {exc}&#034;<br \/>\n        # \u6253\u5370\u9519\u8bef\u65e5\u5fd7&#xff0c;\u8bb0\u5f55\u5931\u8d25\u539f\u56e0<br \/>\n        logger.error(error_msg)<br \/>\n        # \u4ea7\u51fa\u9519\u8bef\u7c7b\u578b\u7684\u6570\u636e\u5757&#xff0c;\u6807\u8bb0\u5bf9\u8bdd\u7ed3\u675f<br \/>\n        yield {&#034;type&#034;: &#034;error&#034;, &#034;content&#034;: error_msg, &#034;finished&#034;: True}<\/p>\n<p># \u83b7\u53d6\u6307\u5b9a\u4f1a\u8bdd\u7684\u5386\u53f2\u804a\u5929\u8bb0\u5f55&#xff0c;\u8fd4\u56de\u5b57\u5178\u5217\u8868\u683c\u5f0f<br \/>\ndef get_session_history(self, session_id: str) -&amp;gt; List[Dict[str, Any]]:<br \/>\n    # \u83b7\u53d6\u8be5\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u5bf9\u8c61&#xff0c;\u518d\u53d6\u51fa\u6240\u6709\u5386\u53f2\u6d88\u606f<br \/>\n    messages &#061; self._get_memory(session_id).get()<br \/>\n    # \u904d\u5386\u6240\u6709\u6d88\u606f&#xff0c;\u8f6c\u6362\u4e3a\u524d\u7aef\u9700\u8981\u7684\u683c\u5f0f\u8fd4\u56de<br \/>\n    return [<br \/>\n        {<br \/>\n            # \u6d88\u606f\u89d2\u8272&#xff0c;\u517c\u5bb9\u679a\u4e3e\u7c7b\u578b\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u4e24\u79cd\u60c5\u51b5<br \/>\n            &#034;role&#034;: message.role.value if hasattr(message.role, &#034;value&#034;) else str(message.role),<br \/>\n            # \u6d88\u606f\u5185\u5bb9&#xff0c;\u4e3a\u7a7a\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<br \/>\n            &#034;content&#034;: message.content or &#034;&#034;,<br \/>\n            # \u6765\u6e90\u5217\u8868&#xff0c;\u5386\u53f2\u6d88\u606f\u9ed8\u8ba4\u8fd4\u56de\u7a7a<br \/>\n            &#034;sources&#034;: [],<br \/>\n        }<br \/>\n        for message in messages<br \/>\n    ]<\/p>\n<p># \u6e05\u7a7a\u6307\u5b9a\u4f1a\u8bdd\u7684\u804a\u5929\u8bb0\u5f55\u548c\u8bb0\u5fc6<br \/>\ndef clear_session(self, session_id: str) -&amp;gt; None:<br \/>\n    # \u5224\u65ad\u5185\u5b58\u4e2d\u662f\u5426\u5b58\u5728\u8be5\u4f1a\u8bdd\u7684\u8bb0\u5fc6<br \/>\n    if session_id in self.memories:<br \/>\n        # \u91cd\u7f6e\u8be5\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u7f13\u51b2\u533a&#xff0c;\u6e05\u7a7a\u5185\u5b58\u4e2d\u7684\u5386\u53f2<br \/>\n        self.memories[session_id].reset()<br \/>\n    # \u4ece\u6301\u4e45\u5316\u7684\u804a\u5929\u5b58\u50a8\u4e2d\u5220\u9664\u8be5\u4f1a\u8bdd\u7684\u6240\u6709\u6d88\u606f<br \/>\n    self.chat_store.delete_messages(session_id)<br \/>\n    # \u4fdd\u5b58\u4fee\u6539\u540e\u7684\u804a\u5929\u5b58\u50a8\u5230\u672c\u5730<br \/>\n    self._persist_chat_store()<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u83b7\u53d6\u6307\u5b9a\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u5bf9\u8c61&#xff0c;\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u65b0\u7684<br \/>\ndef _get_memory(self, session_id: str) -&amp;gt; ChatMemoryBuffer:<br \/>\n    # \u5224\u65ad\u8be5\u4f1a\u8bddID\u4e0d\u5728\u8bb0\u5fc6\u5b57\u5178\u4e2d<br \/>\n    if session_id not in self.memories:<br \/>\n        # \u4e3a\u8be5\u4f1a\u8bdd\u521b\u5efa\u65b0\u7684\u8bb0\u5fc6\u7f13\u51b2\u533a<br \/>\n        self.memories[session_id] &#061; ChatMemoryBuffer.from_defaults(<br \/>\n            llm&#061;Settings.llm,             # \u4f7f\u7528\u5168\u5c40\u914d\u7f6e\u7684\u5927\u6a21\u578b<br \/>\n            chat_store&#061;self.chat_store,   # \u4f7f\u7528\u5168\u5c40\u7684\u804a\u5929\u5b58\u50a8\u505a\u6301\u4e45\u5316<br \/>\n            chat_store_key&#061;session_id,    # \u7528\u4f1a\u8bddID\u4f5c\u4e3a\u5b58\u50a8\u7684\u952e&#xff0c;\u533a\u5206\u4e0d\u540c\u4f1a\u8bdd<br \/>\n        )<br \/>\n    # \u8fd4\u56de\u8be5\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u5bf9\u8c61<br \/>\n    return self.memories[session_id]<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u52a0\u8f7d\u804a\u5929\u5b58\u50a8\u5b9e\u4f8b<br \/>\ndef _load_chat_store(self) -&amp;gt; SimpleChatStore:<br \/>\n    # \u5224\u65ad\u672c\u5730\u662f\u5426\u6709\u4fdd\u5b58\u7684\u804a\u5929\u8bb0\u5f55\u6587\u4ef6<br \/>\n    if self.chat_store_path.exists():<br \/>\n        # \u4ece\u672c\u5730\u8def\u5f84\u52a0\u8f7d\u5df2\u6709\u7684\u804a\u5929\u5b58\u50a8<br \/>\n        return SimpleChatStore.from_persist_path(str(self.chat_store_path))<br \/>\n    # \u6ca1\u6709\u672c\u5730\u6587\u4ef6\u5219\u8fd4\u56de\u5168\u65b0\u7684\u7a7a\u804a\u5929\u5b58\u50a8<br \/>\n    return SimpleChatStore()<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u5c06\u804a\u5929\u5b58\u50a8\u6301\u4e45\u5316\u5230\u672c\u5730\u786c\u76d8<br \/>\ndef _persist_chat_store(self) -&amp;gt; None:<br \/>\n    # \u8c03\u7528\u804a\u5929\u5b58\u50a8\u7684\u6301\u4e45\u5316\u65b9\u6cd5&#xff0c;\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n    self.chat_store.persist(str(self.chat_store_path))<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u6839\u636e\u77e5\u8bc6\u5e93\u5f00\u5173\u83b7\u53d6\u5bf9\u5e94\u7684\u804a\u5929\u5f15\u64ce<br \/>\ndef _get_chat_engine(self, session_id: str, knowledge_bool: bool):<br \/>\n    # \u5148\u83b7\u53d6\u4f1a\u8bdd\u7684\u8bb0\u5fc6\u5bf9\u8c61<br \/>\n    memory &#061; self._get_memory(session_id)<br \/>\n    # \u5f00\u542f\u77e5\u8bc6\u5e93\u7684\u60c5\u51b5<br \/>\n    if knowledge_bool:<br \/>\n        # \u521b\u5efa\u4e0a\u4e0b\u6587\u804a\u5929\u5f15\u64ce&#xff0c;\u652f\u6301RAG\u77e5\u8bc6\u5e93\u95ee\u7b54<br \/>\n        return ContextChatEngine.from_defaults(<br \/>\n            retriever&#061;self._get_rag_retriever(),  # \u4f20\u5165RAG\u68c0\u7d22\u5668&#xff0c;\u7528\u4e8e\u68c0\u7d22\u77e5\u8bc6\u5e93<br \/>\n            memory&#061;memory,                         # \u4f20\u5165\u4f1a\u8bdd\u8bb0\u5fc6&#xff0c;\u652f\u6301\u4e0a\u4e0b\u6587\u5bf9\u8bdd<br \/>\n            llm&#061;Settings.llm,                      # \u4f7f\u7528\u5168\u5c40\u5927\u6a21\u578b\u751f\u6210\u7b54\u6848<br \/>\n            node_postprocessors&#061;[self._get_reranker()], # \u4f20\u5165\u91cd\u6392\u5668&#xff0c;\u5bf9\u68c0\u7d22\u7ed3\u679c\u505a\u91cd\u6392\u5e8f\u4f18\u5316<br \/>\n        )<\/p>\n<p>    # \u4e0d\u5f00\u542f\u77e5\u8bc6\u5e93&#xff0c;\u8fd4\u56de\u7b80\u5355\u804a\u5929\u5f15\u64ce&#xff0c;\u7eaf\u5bf9\u8bdd\u4e0d\u68c0\u7d22\u77e5\u8bc6\u5e93<br \/>\n    return SimpleChatEngine.from_defaults(memory&#061;memory, llm&#061;Settings.llm)<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u83b7\u53d6RAG\u6df7\u5408\u68c0\u7d22\u5668&#xff0c;\u91c7\u7528\u61d2\u52a0\u8f7d\u6a21\u5f0f<br \/>\ndef _get_rag_retriever(self) -&amp;gt; QueryFusionRetriever:<br \/>\n    # \u5148\u786e\u4fdd\u5411\u91cf\u7d22\u5f15\u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6210<br \/>\n    self._ensure_index_loaded()<br \/>\n    # \u83b7\u53d6\u7d22\u5f15\u5bf9\u8c61<br \/>\n    index &#061; self.ingestion_pipeline.index<br \/>\n    # \u7d22\u5f15\u4e3a\u7a7a\u5219\u629b\u51fa\u8fd0\u884c\u65f6\u9519\u8bef<br \/>\n    if index is None:<br \/>\n        raise RuntimeError(&#034;\u77e5\u8bc6\u5e93\u7d22\u5f15\u672a\u521d\u59cb\u5316&#xff0c;\u8bf7\u5148\u4e0a\u4f20\u6587\u6863&#034;)<\/p>\n<p>    # \u68c0\u7d22\u5668\u8fd8\u6ca1\u521b\u5efa\u5219\u8fdb\u884c\u521b\u5efa<br \/>\n    if self.rag_retriever is None:<br \/>\n        # \u521b\u5efa\u5411\u91cf\u68c0\u7d22\u5668&#xff0c;\u57fa\u4e8e\u8bed\u4e49\u76f8\u4f3c\u5ea6\u505a\u68c0\u7d22<br \/>\n        vector_retriever &#061; VectorIndexRetriever(<br \/>\n            index&#061;index,<br \/>\n            similarity_top_k&#061;AppSettings.SIMILARITY_TOP_K, # \u8fd4\u56de\u6700\u76f8\u5173\u7684\u524dK\u6761\u7ed3\u679c<br \/>\n        )<br \/>\n        # \u6536\u96c6\u6240\u6709\u6709\u5185\u5bb9\u7684\u6587\u6863\u8282\u70b9&#xff0c;\u7528\u4e8eBM25\u5173\u952e\u8bcd\u68c0\u7d22<br \/>\n        bm25_nodes &#061; [<br \/>\n            node for node in index.docstore.docs.values() if node.get_content()<br \/>\n        ]<br \/>\n        # \u6ca1\u6709\u53ef\u7528\u7684\u6587\u6863\u8282\u70b9\u5219\u629b\u51fa\u9519\u8bef<br \/>\n        if not bm25_nodes:<br \/>\n            raise RuntimeError(&#034;\u672c\u5730\u6587\u6863\u5b58\u50a8\u4e3a\u7a7a&#xff0c;\u8bf7\u91cd\u65b0\u4e0a\u4f20\u6587\u6863&#034;)<\/p>\n<p>        # \u521b\u5efaBM25\u68c0\u7d22\u5668&#xff0c;\u57fa\u4e8e\u5173\u952e\u8bcd\u5339\u914d\u505a\u68c0\u7d22<br \/>\n        bm25_retriever &#061; BM25Retriever.from_defaults(<br \/>\n            nodes&#061;bm25_nodes,<br \/>\n            similarity_top_k&#061;AppSettings.SIMILARITY_TOP_K, # \u8fd4\u56de\u6700\u76f8\u5173\u7684\u524dK\u6761\u7ed3\u679c<br \/>\n        )<br \/>\n        # \u521b\u5efa\u67e5\u8be2\u878d\u5408\u68c0\u7d22\u5668&#xff0c;\u878d\u5408\u5411\u91cf\u68c0\u7d22\u548cBM25\u68c0\u7d22\u7684\u7ed3\u679c<br \/>\n        self.rag_retriever &#061; QueryFusionRetriever(<br \/>\n            [vector_retriever, bm25_retriever], # \u8981\u878d\u5408\u7684\u4e24\u4e2a\u68c0\u7d22\u5668&#xff1a;\u5411\u91cf\u68c0\u7d22&#043;BM25\u68c0\u7d22<br \/>\n            num_queries&#061;1,                      # \u751f\u6210\u7684\u67e5\u8be2\u6570\u91cf&#xff0c;1\u8868\u793a\u7528\u539f\u67e5\u8be2\u76f4\u63a5\u878d\u5408<br \/>\n            use_async&#061;True,                     # \u542f\u7528\u5f02\u6b65\u6267\u884c&#xff0c;\u63d0\u5347\u68c0\u7d22\u901f\u5ea6<br \/>\n        )<br \/>\n    # \u8fd4\u56de\u68c0\u7d22\u5668\u5b9e\u4f8b<br \/>\n    return self.rag_retriever<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u83b7\u53d6\u8bed\u4e49\u91cd\u6392\u5668&#xff0c;\u61d2\u52a0\u8f7d\u6a21\u5f0f<br \/>\ndef _get_reranker(self) -&amp;gt; SentenceTransformerRerank:<br \/>\n    # \u91cd\u6392\u5668\u8fd8\u6ca1\u521b\u5efa\u5219\u8fdb\u884c\u521b\u5efa<br \/>\n    if self.reranker is None:<br \/>\n        # \u521b\u5efa\u91cd\u6392\u5668\u5b9e\u4f8b<br \/>\n        self.reranker &#061; SentenceTransformerRerank(<br \/>\n            model&#061;AppSettings.RERANK_MODEL_PATH, # \u91cd\u6392\u6a21\u578b\u7684\u672c\u5730\u8def\u5f84&#xff0c;\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6<br \/>\n            top_n&#061;AppSettings.RERANK_TOP_K,      # \u91cd\u6392\u540e\u4fdd\u7559\u7684\u7ed3\u679c\u6570\u91cf<br \/>\n        )<br \/>\n    # \u8fd4\u56de\u91cd\u6392\u5668\u5b9e\u4f8b<br \/>\n    return self.reranker<\/p>\n<p># \u79c1\u6709\u65b9\u6cd5&#xff1a;\u786e\u4fdd\u7d22\u5f15\u5df2\u7ecf\u52a0\u8f7d&#xff0c;\u6ca1\u52a0\u8f7d\u5219\u5c1d\u8bd5\u52a0\u8f7d<br \/>\ndef _ensure_index_loaded(self) -&amp;gt; None:<br \/>\n    # \u7d22\u5f15\u5df2\u5b58\u5728\u5219\u76f4\u63a5\u8fd4\u56de&#xff0c;\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c<br \/>\n    if self.ingestion_pipeline.index is not None:<br \/>\n        return<\/p>\n<p>    # \u8c03\u7528\u6444\u53d6\u7ba1\u9053\u7684\u52a0\u8f7d\u65b9\u6cd5&#xff0c;\u4ece\u672c\u5730\u52a0\u8f7d\u7d22\u5f15<br \/>\n    self.ingestion_pipeline.load_index()<br \/>\n    # \u52a0\u8f7d\u540e\u7d22\u5f15\u4ecd\u4e3a\u7a7a\u5219\u629b\u51fa\u8fd0\u884c\u65f6\u9519\u8bef<br \/>\n    if self.ingestion_pipeline.index is None:<br \/>\n        raise RuntimeError(&#034;\u77e5\u8bc6\u5e93\u7d22\u5f15\u672a\u521d\u59cb\u5316&#xff0c;\u8bf7\u5148\u4e0a\u4f20\u6587\u6863&#034;)<\/p>\n<p># \u9759\u6001\u65b9\u6cd5&#xff0c;\u4e0d\u9700\u8981\u5b9e\u4f8b\u4e5f\u80fd\u8c03\u7528&#xff0c;\u4e0d\u4f9d\u8d56\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027<br \/>\n&#064;staticmethod<br \/>\n# \u9759\u6001\u65b9\u6cd5&#xff1a;\u5c06\u6765\u6e90\u8282\u70b9\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u5178\u5217\u8868&#xff0c;\u65b9\u4fbf\u8fd4\u56de\u7ed9\u524d\u7aef\u4f7f\u7528<br \/>\ndef _source_nodes_to_dicts(source_nodes) -&amp;gt; List[Dict[str, Any]]:<br \/>\n    # \u904d\u5386\u6240\u6709\u6765\u6e90\u8282\u70b9&#xff0c;\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u5178<br \/>\n    return [<br \/>\n        {<br \/>\n            &#034;content&#034;: node.node.get_content(), # \u6765\u6e90\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9<br \/>\n            &#034;score&#034;: float(node.score) if node.score is not None else None, # \u76f8\u5173\u6027\u5f97\u5206&#xff0c;\u8f6c\u4e3a\u6d6e\u70b9\u6570&#xff0c;\u6ca1\u6709\u5219\u4e3aNone<br \/>\n            &#034;metadata&#034;: node.node.metadata or {}, # \u6765\u6e90\u8282\u70b9\u7684\u5143\u6570\u636e&#xff08;\u5982\u6587\u4ef6\u540d\u3001\u9875\u7801\u7b49&#xff09;&#xff0c;\u6ca1\u6709\u5219\u4e3a\u7a7a\u5b57\u5178<br \/>\n        }<br \/>\n        for node in source_nodes or []<br \/>\n    ]<\/p>\n<hr \/>\n<p class=\"img-center\"><img decoding=\"async\" alt=\"img\" src=\"2026-09-04sit4fjw04ix.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3\u57fa\u4e8e LlamaIndex \u6784\u5efa RAG \u77e5\u8bc6\u5e93\u95ee\u7b54\u7cfb\u7edf\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u6587\u7ae0\u4ece\u524d\u7aef\u804a\u5929\u903b\u8f91\u5165\u624b&#xff0c;\u8be6\u7ec6\u62c6\u89e3 ChatView.vue \u4e0e ChatInput.vue \u7684\u4ea4\u4e92\u6d41\u7a0b\u3001chat.ts \u4e2d\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u53e3\u8c03\u7528\u673a\u5236&#xff0c;\u5e76\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 Vue \u4ee3\u7801&#xff1b;\u968f\u540e\u6df1\u5165\u540e\u7aef&#xff0c;\u4ecb\u7ecd RAGService \u7684\u521d\u59cb\u5316\u3001\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf\u3001\u5411\u91cf\u7d22\u5f15\u6784\u5efa\u3001\u6df7\u5408\u68c0\u7d22&#xff08;\u5411\u91cf\u68c0\u7d22\u4e0e BM25 \u878d\u5408&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":100519,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[187,625,50,87],"topic":[],"class_list":["post-100539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-javascript","tag-vue-js","tag-50","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/100539.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3\u57fa\u4e8e LlamaIndex \u6784\u5efa RAG \u77e5\u8bc6\u5e93\u95ee\u7b54\u7cfb\u7edf\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u6587\u7ae0\u4ece\u524d\u7aef\u804a\u5929\u903b\u8f91\u5165\u624b&#xff0c;\u8be6\u7ec6\u62c6\u89e3 ChatView.vue \u4e0e ChatInput.vue \u7684\u4ea4\u4e92\u6d41\u7a0b\u3001chat.ts \u4e2d\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u53e3\u8c03\u7528\u673a\u5236&#xff0c;\u5e76\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 Vue \u4ee3\u7801&#xff1b;\u968f\u540e\u6df1\u5165\u540e\u7aef&#xff0c;\u4ecb\u7ecd RAGService \u7684\u521d\u59cb\u5316\u3001\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf\u3001\u5411\u91cf\u7d22\u5f15\u6784\u5efa\u3001\u6df7\u5408\u68c0\u7d22&#xff08;\u5411\u91cf\u68c0\u7d22\u4e0e BM25 \u878d\u5408&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/100539.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-04T11:05:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110503-6a9aa5df42949.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"37 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100539.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/100539.html\",\"name\":\"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-04T11:05:17+00:00\",\"dateModified\":\"2026-09-04T11:05:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/100539.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/100539.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100539.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/100539.html","og_locale":"zh_CN","og_type":"article","og_title":"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3\u57fa\u4e8e LlamaIndex \u6784\u5efa RAG \u77e5\u8bc6\u5e93\u95ee\u7b54\u7cfb\u7edf\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u6587\u7ae0\u4ece\u524d\u7aef\u804a\u5929\u903b\u8f91\u5165\u624b&#xff0c;\u8be6\u7ec6\u62c6\u89e3 ChatView.vue \u4e0e ChatInput.vue \u7684\u4ea4\u4e92\u6d41\u7a0b\u3001chat.ts \u4e2d\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u53e3\u8c03\u7528\u673a\u5236&#xff0c;\u5e76\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684 Vue \u4ee3\u7801&#xff1b;\u968f\u540e\u6df1\u5165\u540e\u7aef&#xff0c;\u4ecb\u7ecd RAGService \u7684\u521d\u59cb\u5316\u3001\u6587\u6863\u6444\u53d6\u6d41\u6c34\u7ebf\u3001\u5411\u91cf\u7d22\u5f15\u6784\u5efa\u3001\u6df7\u5408\u68c0\u7d22&#xff08;\u5411\u91cf\u68c0\u7d22\u4e0e BM25 \u878d\u5408&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/100539.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-04T11:05:17+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260904110503-6a9aa5df42949.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"37 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/100539.html","url":"https:\/\/www.wsisp.com\/helps\/100539.html","name":"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-04T11:05:17+00:00","dateModified":"2026-09-04T11:05:17+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/100539.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/100539.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/100539.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"103.RAG-LLamaIndex\u540e\u7aefrag\u95ee\u7b54-\u804a\u5929\u63a5\u53e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=100539"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100539\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/100519"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=100539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=100539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=100539"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=100539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}