3. 实战(一):Spring AI Trae ,助力开发微信小程序

news2025/4/17 13:35:00

1、前言

前面介绍了Spring boot快速集成Spring AI实现简单的Chat聊天模式。今天立马来实战一番,通过Trae这个火爆全网的工具,来写一个微信小程序。照理说,我们只是极少量的编码应该就可以完成这项工作。开撸~

2、需求描述

微信小程序实现一个页面,页面上输入一个姓名,点击生成就可以生成对应的藏头诗,并可以根据指定的风格生成。手绘了下页面整体布局:

3、环境准备

  • IntelliJ IDEA 2024.3
  • 微信开发工具
  • 硅基流动API,这里需要提前注册申请
  • Trae AI

4、快速开始

4.1、后端服务(Spring Boot + Spring AI)

由于我这里有线程的后端框架,因此我这里就不使用Trae来帮我生成了。

4.1.1、搭建Spring Boot工程

新建一个项目,添加Spring boot相关依赖,这里我就不赘述了。直接贴出pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.shamee</groupId>
    <artifactId>chai-said-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>21</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot DevTools (Optional for auto-reloading during development) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>mybatis-spring</artifactId>
                    <groupId>org.mybatis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.36</version>
            </dependency>

            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <version>8.0.33</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.5.5</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>3.6.1</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.11</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>top.shamee.chailauncher.ChaiLauncherApplication</mainClass>     <!-- 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 -->
                    <classifier>execute</classifier>    <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

因为我这里集成了MySQL,以及做了多模块。因此我这里的pom稍微复杂了一些,大家可以按需裁剪。

4.1.2、集成Spring AI

注意这里Spring Boot版本必须选用3.2以上的版本,这里使用3.4.2,同时使用JDK21。这里添加Spring AI相关依赖:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4.1.3、添加启动类

@Slf4j
//@MapperScan("top.shamee")
@SpringBootApplication(scanBasePackages = {"top.shamee"})
public class ChaiLauncherApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ChaiLauncherApplication.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        log.info("""
                        
                        ---------------------------------------------------------- 
                        应用 '{}' 运行成功! 当前环境 '{}' !!! 端口 '[{}]' !!!
                        ----------------------------------------------------------
                 """,
                environment.getProperty("spring.application.name"),
                environment.getActiveProfiles(),
                environment.getProperty("server.port"));
    }

}

到此,项目基本搭建完毕。

4.1.4、编写Spring AI相关接口

由于这里要实现的是一个输入用户姓名,通过OpenAI接口生成藏头诗的功能。因此接口入口参数为:inputName(姓名)和inputStype(生成风格),返回生成的内容。

首先定义参数input:

@Data
public class GenPoemInput implements Serializable {

    private String inputName;
    private String inputStyle;

    public String getPrompt(){
        return "用“" + getInputName() + "”写一首藏头诗,要求风格" + getInputStyle() + ",诗词为七言诗";
    }
}

接着编写controller类:

@Slf4j
@RestController
@RequestMapping("/chai/gen-record")
public class GenRecordController {

    private final ChatClient chatClient;
    public GenRecordController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @Resource
    private GenRecordService genRecordService;

    @PostMapping("/openai")
    ResponseEntity<String> openai(@RequestBody GenPoemInput genPoemInput) {
        log.info("请求参数: {}", genPoemInput);
        String result = this.chatClient.prompt(new Prompt())
                .user(genPoemInput.getPrompt())
                .call()
                .content();
        return ResponseEntity.ok(result);
    }
}

application.properties需要配置我们的OpenAi相关API KEY:

配置属性:

  • spring.ai.openai.chat.options.model为配置的大模型
  • spring.ai.openai.chat.base-url为大模型的请求url,默认为openai.com
  • spring.ai.openai.chat.api-key为大模型对应的api key

最后启动工程类,请求下接口看下是否正常:

curl -i -X POST \
   -H "Content-Type:application/json" \
   -d \
'{
  "inputName":"秦始皇",
  "inputStype": "幽默"
}' \
 'http://localhost:8080/chai/gen-record/openai'

生成内容,成功:

4.2、前端开发(微信小程序 + Trae)

小程序代码,这里我们使用Trae来实现。我们给Trae一个提示词:

  1. 你是一个经验丰富的微信小程序UI工程师,熟悉微信的UI设计,设计风格简约明朗
  2. 你将负责设计微信小程序的UI
  3. 我会给你一个设计图,你需要解析这个图片,并设计生成一个小程序,实现这个页面功能。

并将我们手绘的prd传给他:

接着就是静静的等待了:

很快他就生成好了我们所需要的代码,点击全部接受,调整到我们的代码结构中。生成的代码结构还是符合微信小程序的代码结构的:

直接打开微信开发工具,就可以直接预览到我们的页面。剩下的就是中间不断地让Trae按照我们地要求进行细化地调整。最终的效果:

4.3、程序部署

前后端代码都就绪后,接下来就是部署了。由于小程序需要https请求,且域名需要经过严格的ICP备案,才可以正常使用。这里消耗了些时间,SSL都可以免费搞定,ICP备案比较耗时,需要走流程。
当然我们开发本地可以不校验https域名,可以在开发工具上先体验:

试下效果看看:

效果还是很不错,样式,JS代码都帮直接帮我们搞定。真的很香!!!

5、总结

到此,基本程序编码时间不到1小时就可以完全搞定,主要耗费时间的就是在不断的AI调整上。当然可能前面给的提示词比较粗糙也有关系,大家可以认真的给到一段提示信息,应该就不需要花过多时间去调整细化。
代码我还未上传到Github,大家有需要可以私聊我,或者等我有空我上传到Github:https://github.com/Shamee99
真正经验的是,我只是简单手绘了一个PRD草稿,Trae就可以代替我写出相关代码,而且还原度接近90%。大家细品~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2325665.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

UE5新材质系统效果Demo展示

1、玉质材质&#xff0c;透明玻璃材质&#xff0c;不同透射和散射。 2、浅水地面&#xff0c;地面层&#xff0c;水层&#xff0c;地面湿度&#xff0c;水面高度&#xff0c;水下扰动&#xff0c;水下浇洒&#xff0c;水下折射 Substrate-Water Substrate-Water-CodeV2

wps 怎么显示隐藏文字

wps 怎么显示隐藏文字 》文件》选项》视图》勾选“隐藏文字” wps怎么设置隐藏文字 wps怎么设置隐藏文字

CXL UIO Direct P2P学习

前言&#xff1a; 在CXL协议中&#xff0c;UIO&#xff08;Unordered Input/Output&#xff09; 是一种支持设备间直接通信&#xff08;Peer-to-Peer, P2P&#xff09;的机制&#xff0c;旨在绕过主机CPU或内存的干预&#xff0c;降低延迟并提升效率。以下是UIO的核心概念及UI…

leetcode138.随即链表的复制

思路源于 【力扣hot100】【LeetCode 138】随机链表的复制&#xff5c;哈希表 采用一个哈希表&#xff0c;键值对为<原链表的结点&#xff0c;新链表的结点>&#xff0c;第一次遍历原链表结点时只创建新链表的结点&#xff0c;第二次遍历原链表结点时&#xff0c;通过键拿…

《网络管理》实践环节01:OpenEuler22.03sp4安装zabbix6.2

兰生幽谷&#xff0c;不为莫服而不芳&#xff1b; 君子行义&#xff0c;不为莫知而止休。 1 环境 openEuler 22.03 LTSsp4PHP 8.0Apache 2Mysql 8.0zabbix6.2.4 表1-1 Zabbix网络规划&#xff08;用你们自己的特征网段规划&#xff09; 主机名 IP 功能 备注 zbx6svr 19…

Opencv计算机视觉编程攻略-第四节 图直方图统计像素

Opencv计算机视觉编程攻略-第四节 图直方图统计像素 1.计算图像直方图2.基于查找表修改图像3.直方图均衡化4.直方图反向投影进行内容查找5.用均值平移法查找目标6.比较直方图搜索相似图像7.用积分图统计图像 1.计算图像直方图 图像统计直方图的概念 图像统计直方图是一种用于描…

深度学习处理时间序列(5)

Keras中的循环层 上面的NumPy简单实现对应一个实际的Keras层—SimpleRNN层。不过&#xff0c;二者有一点小区别&#xff1a;SimpleRNN层能够像其他Keras层一样处理序列批量&#xff0c;而不是像NumPy示例中的那样只能处理单个序列。也就是说&#xff0c;它接收形状为(batch_si…

Mysql 索引性能分析

1.查看CRUD次数 show global status like Com_______&#xff08;7个下划线&#xff09; show global status like Com_______ 2.慢SQL分析 SET GLOBAL slow_query_log ON;-- 设置慢SQL日志记录开启 SET GLOBAL long_query_time 2; -- 设置执行超过 2 秒的查询为慢查询 开…

win11+ubuntu双系统安装

操作步骤&#xff1a; 官网下载ubuntu 最新镜像文件 准备U盘 准备一个容量不小于 8GB 的 U 盘&#xff0c;用于制作系统安装盘。制作过程会格式化 U 盘&#xff0c;请注意提前备份数据。 制作U盘启动盘 使用rufus工具&#xff0c;或者 balenaEtcher工具&#xff08;官网安…

linux-5.10.110内核源码分析 - 写磁盘(从VFS系统调用到I/O调度及AHCI写磁盘)

1、VFS写文件到page缓存(vfs_write) 1.1、写裸盘(dd) 使用如下命令写裸盘&#xff1a; dd if/dev/zero of/dev/sda bs4096 count1 seek1 1.2、系统调用(vfs_write) 系统调用栈如下&#xff1a; 对于调用栈的new_sync_write函数&#xff0c;buf为写磁盘的内容的内存地址&…

arinc818 fpga单色图像传输ip

arinc818协议支持的常用线速率如下图 随着图像分辨率的提高&#xff0c;单lane的速率无法满足特定需求&#xff0c;一种方式是通过多个LANE交叉的去传输图像&#xff0c;另外一种是通过降低图像的带宽&#xff0c;即通过只传单色图像达到对应的效果 程序架构如下图所示&#x…

业务流程先导及流程图回顾

一、测试流程回顾 &#xfeff; 1. 备测内容回顾 &#xfeff; 备测内容: 本次测试涵盖买家和卖家的多个业务流程&#xff0c;包括下单流程、发货流程、搜索退货退款、支付抢购、换货流程、个人中心优惠券等。 2. 先测业务强调 &#xfeff; 1&#xff09;测试业务流程 …

HCIP(RSTP+MSTP)

一、STP的重新收敛&#xff1a; 复习STP接口状态 STP初次收敛至少需要50秒的时间。STP的重新收敛情况&#xff1a; 检测到拓扑变化&#xff1a;当网络中的链路故障或新链路加入时&#xff0c;交换机会检测到拓扑变化。 选举新的根桥&#xff1a;如果原来的根桥故障或与根桥直…

《无线江湖五绝:BLE/WiFi/ZigBee的频谱大战》

点击下面图片带您领略全新的嵌入式学习路线 &#x1f525;爆款热榜 88万阅读 1.6万收藏 文章目录 **第一回武林大会&#xff0c;群雄并起****第二回WiFi的“降龙十八掌”****第三回BLE的“峨眉轻功”****第四回ZigBee的“暗器百解”****第五回LoRa的“千里传音”****第六回NB…

QT第六课------QT界面优化------QSS

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

C++ STL常用算法之常用算术生成算法

常用算术生成算法 学习目标: 掌握常用的算术生成算法 注意: 算术生成算法属于小型算法&#xff0c;使用时包含的头文件为 #include <numeric> 算法简介: accumulate // 计算容器元素累计总和 fill // 向容器中添加元素 accumulate 功能描述: 计算区间内容器元素…

Tof 深度相机原理

深度相机(TOF)的工作原理_tof相机原理-CSDN博客 深度剖析 ToF 技术&#xff1a;原理、优劣、数据纠错与工业应用全解析_tof技术-CSDN博客 飞行时间技术TOF_tof计算公式-CSDN博客 深度相机&#xff08;二&#xff09;——飞行时间&#xff08;TOF&#xff09;_飞行时间技术-C…

【Linux篇】进程入门指南:操作系统中的第一步

步入进程世界&#xff1a;初学者必懂的操作系统概念 一. 冯诺依曼体系结构1.1 背景与历史1.2 组成部分1.3 意义 二. 进程2.1 进程概念2.1.1 PCB&#xff08;进程控制块&#xff09; 2.2 查看进程2.2.1 使用系统文件查看2.2.2 使⽤top和ps这些⽤⼾级⼯具来获取2.2.3 通过系统调用…

SpringBean模块(一)定义如何创建生命周期

一、介绍 1、简介 在 Spring 框架中&#xff0c;Bean 是指由 Spring 容器 管理的 Java 对象。Spring 负责创建、配置和管理这些对象&#xff0c;并在应用程序运行时对它们进行依赖注入&#xff08;Dependency Injection&#xff0c;DI&#xff09;。 通俗地讲&#xff0c;Sp…

Redis-04.Redis常用命令-字符串常用命令

一.字符串操作命令 set name jack 点击左侧name&#xff0c;显示出值。 get name get abc&#xff1a;null setex key seconds value&#xff1a;设置过期时间&#xff0c;过期后该键值对将会被删除。 然后再get&#xff0c;在过期时间内可以get到&#xff0c;过期get不到。…