♻️Refactor: 使用 SpringAI 框架生成结构化 JSON

This commit is contained in:
2025-11-06 15:05:42 +08:00
parent cf5fbc15a3
commit 2df11e2f37
5 changed files with 77 additions and 0 deletions
+6
View File
@@ -71,6 +71,12 @@
<artifactId>mybatis-plus-jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.0.3</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -32,4 +32,13 @@ public interface CommonConstant {
" \"{前端 Echarts V5 的 option 配置对象的json代码,合理地将数据进行可视化,不要生成任何多余的内容,比如注释}\\n\" +\n" +
" \"【【【【【\\n\" +\n" +
" \"{明确的数据分析结论、越详细越好,不要生成多余的注释}\"";
String CHART_AGENT_PROMPT = """
你是一个数据分析师和前端开发专家,我会按照以下固定格式给你提供内容:\
分析需求:
{数据分析的需求或者目标}
原始数据
{csv格式的原始数据,用,作为分隔符}
请根据这两部分内容,按照以下指定格式生成内容
{前端 Echarts V5 的 option 配置对象的json代码,合理地将数据进行可视化,不要生成任何多余的内容,比如注释}""";
}
@@ -0,0 +1,28 @@
package com.wonder.bi.manager;
import com.wonder.bi.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ChartAgentManager {
private ChatClient client;
public ChartAgentManager(ChatClient.Builder chatClientBuilder) {
this.client = chatClientBuilder
.defaultSystem(CommonConstant.CHART_AGENT_PROMPT)
.build();
}
public String getChartJson(String message) {
String content = client
.prompt(message)
.call()
.content();
log.info(content);
return content;
}
}
@@ -19,6 +19,13 @@ knife4j:
spring:
application:
name: bi-backend
ai:
openai:
base-url: https://api.siliconflow.cn
api-key: sk-idvvtxhxwaulcmvoshpvouuenezzttqgaeqwbdcucqmvfncm
chat:
options:
model: Qwen/Qwen3-Coder-30B-A3B-Instruct
# 支持 swagger3
mvc:
pathmatch:
@@ -0,0 +1,27 @@
package com.wonder.bi.manager;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ChartAgentManagerTest {
@Resource
private ChartAgentManager chartAgent;
@Test
void getChartJson() {
String chartJson = chartAgent.getChartJson("分析需求:\n" +
"分析网站用户的增长情况\n" +
"原始数据:\n" +
"日期,用户数\n" +
"1号,10\n" +
"2号,20\n" +
"3号,30\n");
Assertions.assertNotNull(chartJson);
}
}