CommonController.java
7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.glsk.controller;
import com.glsk.conf.ConfigurationManager;
import com.glsk.connection.MysqlConn;
import com.glsk.llm.impl.LLMToolFactory;
import com.glsk.llm.impl.LLMTools;
import com.glsk.llm.model.LLMRequest;
import com.glsk.llm.model.LLMResponse;
import com.glsk.util.DocxToMarkdownConverter;
import com.glsk.util.JsonToExcelConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/api")
public class CommonController {
@PostMapping("/ai/common/doc_to_markdown")
public Map docToMarkdown(@RequestParam("fileName") String fileName) {
Map<String, Object> result = new HashMap<>();
Map<String, Object> text = DocxToMarkdownConverter.textToExcel(fileName);
result.put("text", text);
result.put("code", 200);
result.put("success", true);
return result;
}
@PostMapping("/ai/common/text_to_excel")
public Map textToExcel(@RequestParam("fileName") String fileName,
@RequestParam("author") String author,
@RequestBody String text) {
log.info("开始根据文本生成示例,text: {},fileName:{},author:{}", text,fileName,author);
Map<String, Object> result = new HashMap<>();
try {
JsonToExcelConverter.convertJsonToExcel(text, fileName, author);
result.put("code", 200);
result.put("success", true);
} catch (Exception ex) {
result.put("code", 800);
result.put("success", false);
result.put("error", ex.getMessage());
ex.printStackTrace();
}
log.info("完成测试用例的生成,fileName:{}",fileName);
return result;
}
@PostMapping("/ai/common/post_merge_summary")
public Map queryData(@RequestParam("post_date") String postDate,
@RequestParam("themes") String themes) throws UnsupportedEncodingException {
log.info("开始根据日期和主题查询数据,postDate:{},themes:{}", postDate,themes);
String password = "v#@JrWCEH9g3";
String encodedPassword = URLEncoder.encode(password, "UTF-8");
String url = "jdbc:mysql://rm-wz9uqix4n8s738dq18o.mysql.rds.aliyuncs.com:3306/xlyq_agi"
+ "?user=bool"
+ "&password=" + encodedPassword
+ "&useSSL=false"
+ "&serverTimezone=Asia/Shanghai"
+ "&allowPublicKeyRetrieval=true"
+ "&autoReconnect=true"
+ "&autoReconnectForPools=true";
Connection connection = MysqlConn.getConnection(url);
String[] themeSplit = themes.trim().replaceAll(",", ",").split(",");
String whereTheme = Arrays.stream(themeSplit).map(s -> "'" + s + "'").collect(Collectors.joining(", "));
Map<String, Object> result = new HashMap<>();
try {
StringBuffer sqlBuff = new StringBuffer();
StringBuffer idBuff = new StringBuffer();
for (int index = 0; index < themeSplit.length; index++) {
if (index > 0) sqlBuff.append("UNION ALL \n");
sqlBuff.append("select id,theme,content_unit,post_content from ");
sqlBuff.append("(").append(String.format("select id,theme,content_unit,post_content from xlyq_agi.fund_post\n" +
"where post_date = '%s' and theme = '%s' limit 10 ) as t%d\n", postDate, themeSplit[index], index));
}
System.out.println(sqlBuff.toString());
StringBuffer sb = new StringBuffer();
ArrayList<ArrayList<String>> arrayLists = MysqlConn.executeQuery(connection, sqlBuff.toString(), 4);
for (ArrayList<String> item : arrayLists) {
sb.append("主题:").append(item.get(1)).append("\n");
sb.append("内容单元:").append(item.get(2)).append("\n");
sb.append("帖子:").append(item.get(3)).append("\n");
sb.append("-----------------------------------------");
idBuff.append(item.get(0)).append(",");
}
String sql = "select system_prompt,user_prompt from llm_prompt where type_id = 1 and status = 1";
ArrayList<ArrayList<String>> executeQuery = MysqlConn.executeQuery(connection, sql, 2);
String systemPrompt = executeQuery.get(0).get(0);
String userPrompt = executeQuery.get(0).get(1);
LLMRequest req = new LLMRequest();
req.setModeName("qwen-plus");
req.setSystem(systemPrompt);
req.setUser(userPrompt);
req.setContent(sb.toString());
LLMTools llmTool = LLMToolFactory.createLLMTool(2);
LLMResponse response = llmTool.invoke(req);
String insertSql = String.format("insert into xlyq_agi.post_merge_summary(post_date,select_themes,data_source_ids,post_merge_content,content_length,status,input_token_count,output_token_count,token_count,error_message) " +
"values('%s','%s','%s','%s',%d,%d,%d,%d,%d,'%s')", postDate, themes, idBuff.toString(), response.getMessage(), response.getMessage().length(), 1,
response.getPromptTokens(),response.getCompletionTokens(), response.getTotalTokens(), response.getError());
System.out.println(insertSql);
MysqlConn.executeUpdate(connection, insertSql);
result.put("text",response.getMessage());
result.put("code", 200);
result.put("success", true);
} catch (Exception ex) {
result.put("code", 800);
result.put("success", false);
result.put("error", ex.getMessage());
ex.printStackTrace();
} finally {
MysqlConn.close(connection);
}
log.info("完成数据查询,postDate:{},themes:{}",postDate,themes);
return result;
}
public static void main(String[] args) {
String themes = "市场涨跌,基金净值,安利卖点";
String postDate = "2025-11-15";
String[] themeSplit = themes.trim().replaceAll(",", ",").split(",");
StringBuffer sqlBuff = new StringBuffer();
for (int index = 0; index < themeSplit.length; index++) {
if (index > 0) sqlBuff.append("UNION ALL \n");
sqlBuff.append("select id,theme,content_unit,post_content from ");
sqlBuff.append("(").append(String.format("select id,theme,content_unit,post_content from xlyq_agi.fund_post\n" +
"where post_date = '%s' and theme = '%s' limit 10 ) as t%d\n", postDate, themeSplit[index], index));
}
System.out.println(sqlBuff.toString());
LLMRequest req = new LLMRequest();
req.setModeName("qwen-plus");
req.setSystem("你是谁");
req.setUser("你是一个专家");
req.setContent("写一首诗");
LLMTools llmTool = LLMToolFactory.createLLMTool(2);
LLMResponse response = llmTool.invoke(req);
StringBuffer idBuff = new StringBuffer();
idBuff.append("1,2,3");
String insertSql = String.format("insert into xlyq_agi.post_merge_summary(post_date,select_themes,data_source_ids,post_merge_content,content_length,status,input_token_count,output_token_count,token_count,error_message) " +
"values('%s','%s','%s','%s',%d,%d,%d,%d,%d,'%s')", postDate, themes, idBuff.toString(), response.getMessage(), response.getMessage().length(), 1,
response.getPromptTokens(),response.getCompletionTokens(), response.getTotalTokens(), response.getError());
System.out.println(insertSql);
}
}