Langchain+FastApi+Vue前后端Ai对话(超详细)

打印 上一主题 下一主题

主题 1056|帖子 1056|积分 3168

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、引入

起首可以先看下作者的文章


  • FastApi相关文章:创建最简朴FastApi的项目
  • Vue相关文章:最简朴的aixos二次封装
  • Langchain相关文章:怎样利用LangSmith跟踪deepseek模型
二、后端搭建

1 项目文件结构



  • routers:存放api接口
  • service:存放实际操作函数
  • init.py: 挂载接口
  • main:运行步伐

2 创建聊天接口


3 创建chatService

一定要配置代理和模型密钥

4 挂载接口


5 运行主函数


三、前端搭建

1 创建axios实例


2 创建哀求


3 发送哀求


4 界面布局

界面布局的源代码
  1. <template>
  2.   <div class="chat-container">
  3.     <div class="timeline-container">
  4.       <el-timeline>
  5.         <el-timeline-item
  6.             v-for="(activity, index) in activities"
  7.             :key="index"
  8.             :icon="activity.icon"
  9.             :type="activity.type"
  10.             :color="activity.color"
  11.             :size="activity.size"
  12.             :hollow="activity.hollow"
  13.             :timestamp="activity.timestamp"
  14.         >
  15.           {
  16.   
  17.   { activity.content }}
  18.         </el-timeline-item>
  19.       </el-timeline>
  20.     </div>
  21.     <div id="container" class="input-container">
  22.       <div id="chat" class="chat-input">
  23.         <el-input
  24.             v-model="msg"
  25.             input-style="width: calc(100% - 80px); height: 60px; border-radius: 8px;"
  26.             :rows="2"
  27.             type="text"
  28.             placeholder="请输入消息"
  29.             @keydown.enter="sendMsg();"
  30.         />
  31.         <el-button @click="sendMsg()" class="send-button">发送</el-button>
  32.       </div>
  33.     </div>
  34.   </div>   
  35. </template>
  36. <script setup>
  37. import { MoreFilled } from '@element-plus/icons-vue'
  38. import {ref, onMounted} from "vue";
  39. import {getChat} from '@/api/chat'
  40. const activities = ref([
  41.   {
  42.     content: '请问有什么可以帮您的?',
  43.     timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
  44.     color: '#0bbd87',
  45.   },
  46. ]);
  47. const msg = ref('');
  48. const sendMsg = () => {
  49.   activities.value.push(
  50.     {
  51.       content: `你:${msg.value}`,
  52.       timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
  53.       size: 'large',
  54.       type: 'primary',
  55.       icon: MoreFilled,
  56.     },
  57.   );
  58.   activities.value.push(
  59.     {
  60.       content: 'waiting...',
  61.       timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
  62.       color: '#0bbd87',
  63.     },
  64.   );
  65.   getChat(msg.value).then(res => {
  66.     activities.value.pop();
  67.     activities.value.push(
  68.       {
  69.         content: res.data,
  70.         timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
  71.         color: '#0bbd87',
  72.       },
  73.     );
  74.   });
  75.   msg.value = '';
  76. };
  77. </script>
  78. <style scoped>
  79.   .chat-container {
  80.     background-color: #A8C9D4;
  81.     padding: 10px;
  82.     border-radius: 8px;
  83.     width: 600px;
  84.     height: 500px;
  85.     display: flex;
  86.     flex-direction: column;
  87.   }
  88.   .timeline-container {
  89.     height: 600px;
  90.     width: 100%;
  91.     overflow-y: auto;
  92.     border: 1px solid #1e7ba2;
  93.     border-radius: 8px;
  94.     padding: 10px;
  95.   }
  96.   .input-container {
  97.     margin-top: 10px;
  98.   }
  99.   .chat-input {
  100.     display: flex;
  101.     align-items: center;
  102.   }
  103.   .send-button {
  104.     margin-left: 10px;
  105.     height: 60px;
  106.     border-radius: 8px;
  107.   }
  108. </style>
复制代码
5 配置代理


四、展示结果


五、源代码

源代码地点:天才奇男子/学习条记


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

郭卫东

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表