基于Springboot+微信小步伐调用文心一言大模型实现AI聊天 ...

打印 上一主题 下一主题

主题 988|帖子 988|积分 2964

一、文章前言

   此文主要实现基于Springboot+微信小步伐调用文心一言大模型实现AI聊天对话功能,使用Java作为后端语言进行支持,界面友爱,开发简单。
   
  
   
  二、开发流程及工具准备

2.1、登录百度智能云平台,获取 API Key 和 Secret Key两个凭证
2.2、注册微信公众平台账号。
2.3、下载安装IntelliJ IDEA(后端语言开发工具),Mysql数据库,微信Web开发者工具。
三、开发步调

   1.创建maven project
  先创建一个名为SpringBootDemo的项目,选择【New Project】

然后在弹出的下图窗口中,选择左侧菜单的【New Project】(注:和2022之前的idea版本差别,这里左侧没有【Maven】选项,不要选【Maven Archetype】!!!),输入Name(项目名):SpringBootDemo,language选择【java】,build system选择【maven】,然后选择jdk,我这里选的是jdk18.
然后点击【Create】

   2.在project下创建module
  点击右键选择【new】—【Module…】

左侧选择【Spring initializr】,通过idea中集成的Spring initializr工具进行spring boot项目的快速创建。窗口右侧:name可根据自己喜好设置,group和artifact和上面一样的规则,其他选项保持默认值即可,【next】

Developer Tools模块勾选【Spring Boot DevTools】,web模块勾选【Spring Web】

此时,一个Springboot项目已经搭建完成,可开发后续功能
   3.编写一个消息实体类、Mapper、service(三层架构)
  1. @Data
  2. public class Chat {
  3.     @TableId(type = IdType.AUTO)
  4.     private Long id;
  5.     private Long userId;
  6.     private Long targetUserId;
  7.     private LocalDateTime createTime;
  8.     private String userName;
  9.     private String targetUserName;
  10.     private String content;
  11. }
复制代码
由于我们使用mybatis-plus,以是简单的增删改查不用自己写,框架自带了,只必要实现或者继承他的Mapper、Service

   4.编写调用ai服务类
  1. //获取AccessKey
  2. public static String GetAccessToken()
  3. {
  4.     String url = "https://aip.baidubce.com/oauth/2.0/token";
  5.     String params="grant_type=client_credentials&client_id=xxxx&client_secret=xxxxx";
  6.     // 发送 POST 请求
  7.     String response = HttpUtil.post(url, params);
  8.     JSONObject json=JSONObject.parseObject(response);
  9.     return json.getString("access_token");
  10. }
复制代码
  5.创建控制器Controller
  先创建Controller Package

创建一个Controller

输入类名,选在【Class】

由于要编写Rest风格的Api,要在Controller上标注@RestController注解
   6.创建详细的Api接口
  1. @RestController
  2. public class DemoController {
  3.     @RequestMapping("chat-gpt")
  4.     public Map chat(String sender){
  5.     String token= GetAccessToken();
  6.     List<MessageDTO> messageDTOS=new ArrayList<>();
  7.     messageDTOS.add(new MessageDTO("user",sender));
  8.     Map<String, Object> map=new HashMap<>();
  9.     map.put("user_id","");
  10.     map.put("messages",messageDTOS);
  11.     map.put("temperature",0.95);
  12.     map.put("top_p",0.8);
  13.     map.put("penalty_score",1);
  14.     map.put("disable_search",false);
  15.     map.put("enable_citation",false);
  16.     map.put("stream",false);
  17.     String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token="+token;
  18.     String response = HttpUtil.createPost(url)
  19.             .header("Content-Type", "application/json")
  20.             .body(JSONObject.toJSONString(map))
  21.             .execute()
  22.             .body();
  23.     ReturnMap returnMap = new ReturnMap();
  24.     returnMap.setData(JSONObject.parse(response));
  25.     return returnMap.getreturnMap();
  26. }
  27.    
  28. }
复制代码
7.小步伐代码

3.20、在pages文件夹下面创建一个文件夹并新建对应的page文件,并实现聊天对话框样式。


  1. <view class="cu-chat" id="j_page">
  2.   <view class="cu-item 'self'" wx:for="{{chatData}}">
  3.     <view class="main">
  4.       <view class="content shadow bg-green">
  5.         <text>{{item.content}}</text>
  6.       </view>
  7.     </view>
  8.     <view class="cu-avatar radius" style="background-image:url(../../../images/cat.jpg)"></view>
  9.     <view class="date">{{item.createTime}}</view>
  10.   </view>
  11. </view>
  12. <view class="cu-bar foot input {{InputBottom!=0?'cur':''}}" style="bottom:{{InputBottom}}px">
  13.   <view class="action">
  14.     <text class="cuIcon-sound text-grey"></text>
  15.   </view>
  16.   <input class="solid-bottom" value="{{content}}" bindinput="formMsg"  bindfocus="InputFocus" bindblur="InputBlur" adjust-position="{{false}}" focus="{{false}}" maxlength="300" cursor-spacing="10"></input>
  17.   <view class="action">
  18.     <text class="cuIcon-emojifill text-grey"></text>
  19.   </view>
  20.   <button class="cu-btn bg-green shadow" bindtap="sendMsg">发送</button>
  21. </view>
复制代码
  1. .cu-chat {
  2.         display: flex;
  3.         flex-direction: column;
  4. }
  5. .cu-chat .cu-item {
  6.         display: flex;
  7.         padding: 30rpx 30rpx 70rpx;
  8.         position: relative;
  9. }
  10. .cu-chat .cu-item>.cu-avatar {
  11.         width: 80rpx;
  12.         height: 80rpx;
  13. }
  14. .cu-chat .cu-item>.main {
  15.         max-width: calc(100% - 260rpx);
  16.         margin: 0 40rpx;
  17.         display: flex;
  18.         align-items: center;
  19. }
  20. .cu-chat .cu-item>image {
  21.         height: 320rpx;
  22. }
  23. .cu-chat .cu-item>.main .content {
  24.         padding: 20rpx;
  25.         border-radius: 6rpx;
  26.         display: inline-flex;
  27.         max-width: 100%;
  28.         align-items: center;
  29.         font-size: 30rpx;
  30.         position: relative;
  31.         min-height: 80rpx;
  32.         line-height: 40rpx;
  33.         text-align: left;
  34. }
  35. .cu-chat .cu-item>.main .content:not([class*="bg-"]) {
  36.         background-color: var(--white);
  37.         color: var(--black);
  38. }
  39. .cu-chat .cu-item .date {
  40.         position: absolute;
  41.         font-size: 24rpx;
  42.         color: var(--grey);
  43.         width: calc(100% - 320rpx);
  44.         bottom: 20rpx;
  45.         left: 160rpx;
  46. }
  47. .cu-chat .cu-item .action {
  48.         padding: 0 30rpx;
  49.         display: flex;
  50.         align-items: center;
  51. }
  52. .cu-chat .cu-item>.main .content::after {
  53.         content: "";
  54.         top: 27rpx;
  55.         transform: rotate(45deg);
  56.         position: absolute;
  57.         z-index: 100;
  58.         display: inline-block;
  59.         overflow: hidden;
  60.         width: 24rpx;
  61.         height: 24rpx;
  62.         left: -12rpx;
  63.         right: initial;
  64.         background-color: inherit;
  65. }
  66. .cu-chat .cu-item.self>.main .content::after {
  67.         left: auto;
  68.         right: -12rpx;
  69. }
复制代码
3.22、准备两张头像,在WXML中根据对应的用户名判断聊天记录是否是自己发出,并赋对应的class样式,后续这个步调可以直接在接口返回的数据中进行判断,哀求查询列表的接口将用户token作为参数传输已往即可。
   
[img=180,180/]https://i-blog.csdnimg.cn/blog_migrate/634442219bc30b871d44275841a6e666.png[/img]
  
[img=180,180/]https://i-blog.csdnimg.cn/blog_migrate/be6c47d69fb6c692a793a558b2194d31.png[/img]
  1. <view class="cu-chat" id="j_page">
  2.   <view class="cu-item {{item.userId=='2'?'self':''}}" wx:for="{{chatData}}">
  3.     <view class="cu-avatar radius" style="background-image:url(../../../images/cat.jpg)" wx:if="{{item.userId=='1'}}"></view>
  4.     <view class="main">
  5.       <view class="content shadow {{item.userId=='1'?'bg-green':''}}">
  6.         <text>{{item.content}}</text>
  7.       </view>
  8.     </view>
  9.     <view class="cu-avatar radius" style="background-image:url(../../../images/dog.jpg)" wx:if="{{item.userId=='2'}}"></view>
  10.     <view class="date">{{item.createTime}}</view>
  11.   </view>
  12. </view>
复制代码
3.23、这里必要注意的是,我们必要在每次发送消息后将页面内容定位在底部,不停保持一个阅读最新消息的状态。
  1. wx.pageScrollTo({
  2.   scrollTop: 9999
  3. })
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表