Administrator
2025-08-29 d078a0325f5ed22473d66f34da82ede493dcc649
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cc.mrbird.febs.ai.controller;
 
import cc.mrbird.febs.ai.entity.AiMember;
import cc.mrbird.febs.ai.mapper.AiMemberMapper;
import cc.mrbird.febs.ai.req.talk.AiTalkAnswerStream;
import cc.mrbird.febs.ai.res.memberTalk.ApiMemberTalkStreamVo;
import cc.mrbird.febs.ai.utils.UUID;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.entity.MallMember;
import cc.mrbird.febs.mall.mapper.MallMemberMapper;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.reactivex.Flowable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import reactor.core.publisher.Flux;
 
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author Administrator
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/ai/test")
@Api(value = "ApiProductController", tags = "AI-登录测试")
public class TestController {
 
    private final MallMemberMapper mallMemberMapper;
    private final AiMemberMapper aiMemberMapper;
    private final RedisUtils redisUtils;
    @ApiOperation(value = "登录测试", notes = "登录测试")
    @GetMapping(value = "/login")
    public FebsResponse info() {
 
        MallMember mallMember = mallMemberMapper.selectById(3634);
        if(StrUtil.isEmpty(mallMember.getMemberUuid())){
            AiMember aiMember = new AiMember();
            aiMember.setId(UUID.getSimpleUUIDString());
            aiMemberMapper.insert(aiMember);
            mallMember.setMemberUuid(aiMember.getId());
            mallMemberMapper.update(null,
                    Wrappers.lambdaUpdate(MallMember.class)
                    .set(MallMember::getMemberUuid, aiMember.getId())
                    .eq(MallMember::getId, mallMember.getId())
                    );
        }
        // 存放redis
        String redisKey = AppContants.XCX_LOGIN_PREFIX + mallMember.getId();
        String existToken = redisUtils.getString(redisKey);
        if (StrUtil.isNotBlank(existToken)) {
            Object o = redisUtils.get(existToken);
            if (ObjectUtil.isNotEmpty(o)) {
                redisUtils.del(existToken);
            }
        }
        String token = IdUtil.simpleUUID();
        redisUtils.set(token, JSONObject.toJSONString(mallMember), -1);
        redisUtils.set(redisKey, token, -1);
        Map<String, Object> authInfo = new HashMap<>();
        authInfo.put("token", token);
        authInfo.put("member", mallMember);
        authInfo.put("rasToken", generateAsaToken(token));
        return new FebsResponse().success().data(authInfo);
    }
 
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, AppContants.PUBLIC_KEY);
//        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
        //去掉时间戳
        return rsa.encryptBase64(token, KeyType.PublicKey);
    }
 
 
 
    @ApiOperation("提问AI(流式)V2")
    @ApiResponses({
            @ApiResponse(code = 200, message = "流式响应", response = ApiMemberTalkStreamVo.class),
    })
    @PostMapping("/answer-streamV2")
    public Flux<FebsResponse> answerStreamV2(@RequestBody @Validated AiTalkAnswerStream dto) {
        if (StrUtil.isEmpty(dto.getQuestion())){
            return Flux.just(new FebsResponse().fail().message("请输入问题"));
        }
        String question = dto.getQuestion();
        String prompt = dto.getPrompt() ;
 
        long startTime = System.currentTimeMillis();
 
        Flowable<GenerationResult> result;
        try {
            result = callWithMessage(question,prompt);
        } catch (NoApiKeyException | InputRequiredException e) {
            e.printStackTrace();
            return Flux.just(new FebsResponse().fail().message("调用AI服务失败: " + e.getMessage()));
        }
 
        return Flux.from(result)
                .map(message -> {
                    String content = message.getOutput().getChoices().get(0).getMessage().getContent();
                    System.out.print(content);
                    return new FebsResponse().success().data(content);
                })
                .doOnComplete(() -> {
                    long endTime = System.currentTimeMillis();
                    System.out.println("运行时间:" + (endTime - startTime) + "毫秒");
                })
                .doOnError(error -> {
                    long endTime = System.currentTimeMillis();
                    System.err.println("运行时间:" + (endTime - startTime) + "毫秒,发生错误:" + error.getMessage());
                });
    }
 
 
    public static Flowable<GenerationResult> callWithMessage(String question,String prompt) throws NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content(prompt)
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(question)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey("sk-babdcf8799144134915cee2683794b2f")
                // 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
//                .model("deepseek-r1")
//                .model("qwen-turbo-0624-ft-202508281725-c2dc")
                .messages(Arrays.asList(systemMsg, userMsg))
//                .resultFormat(GenerationParam.ResultFormat.TEXT)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();
        return gen.streamCall(param);
    }
    public static void main(String[] args) {
        //定义一个开始时间为启动这个main方法的开始时间,用于计算运行时间
        long startTime = System.currentTimeMillis();
        String question = "1";
        String prompt = "假如你是一个表扬题目生成专家,你将根据多样化结构表扬场景题目的生成需求,来解决生成多样化结构的表扬场景题目的任务。根据以下规则一步步执行:\n" +
                "1. 从角色视角/员工类型/成就类型/场景要素中各随机选择一项进行组合。\n" +
                "\t- 角色视角(上级对下级、下级对上级、平级同事、跨部门同事、项目负责人对临时团队)\n" +
                "\t- 员工类型(新入职员工、资深骨干员工、基层管理者、中层管理者、高层管理者、跨部门协作人员、远程办公团队成员、实习生 / 管培生、技术研发人员、业务 / 销售一线人员)\n" +
                "\t- 成就类型(项目攻坚、创新改进、危机处理、长期贡献、团队建设、知识分享、客户满意度、成本控制、新人培养、跨部门协作)\n" +
                "\t- 场景要素(公开会议、私下沟通、书面形式、线上场景、庆典活动、突发场景、例行场景、特殊时刻、外部场合、非正式场合)\n" +
                "2. 采用 3 种可变句式结构动态生成题目:\n" +
                "    - 结构 A:\"作为 [角色]的你,如何在 [场景] 中认可 [员工类型] 的 [成就(带有具体细节描述)]?\"\n" +
                "    - 结构 B:\"当 [员工类型] 在 [场景] 中达成 [成就(带有具体细节描述)] 时,[角色]的你 应如何表达赞赏?\"\n" +
                "    - 结构 C:\"在 [场景] 下,[角色]的你 发现 [员工类型] 取得 [成就(带有具体细节描述)],适合采取何种表扬方式?\"\n" +
                "3. 自动添加如 \"提前完成项目 deadline\"、\"创新方案节省 20% 成本\" 等具体情境细节描述。\n" +
                "4. 无论用户输入什么内容只生成 1 个独特题目,覆盖 100 + 可能组合。\n" +
                "5. 支持通过添加限定词定制题目,如 \"生成远程团队相关题目\"、\"包含成本控制成就的题目\"。\n" +
                "6. 题目设计兼顾传统与新兴职场场景(如远程办公、跨文化团队)。\n" +
                "\n" +
                "参考例子:\n" +
                "示例1:\n" +
                "输出:作为跨部门同事,如何在视频会议中认可技术研发人员提前完成创新改进项目的贡献?\n" +
                "\n" +
                "示例2:\n" +
                "输出:当实习生在团队庆功宴的危机处理中成功化解客户投诉时,作为部门经理应如何表达赞赏?\n" +
                "\n" +
                "示例3:\n" +
                "输出:在年度颁奖典礼上,基层管理者发现远程办公团队成员凭借长期稳定的工作表现达成长期贡献,适合采取何种表扬方式?\n" +
                "\n" +
                "示例4:\n" +
                "输出:作为新入职员工,如何在非正式交流中向高层管理者表达对其成功处理突发危机的认可?\n" +
                "\n" +
                "输出:\n" +
                "\n" +
                "要求:\n" +
                "1 输出一个符合上述规则生成的表扬场景题目。\n" +
                "2 题目需包含角色视角、员工类型、成就类型、场景要素等关键信息,且符合三种结构中的一种,并带有具体细节描述。\n" +
                "###";
 
        Flowable<GenerationResult> result = null;
        try {
            result = callWithMessage(question, prompt);
        } catch (NoApiKeyException e) {
            e.printStackTrace();
        } catch (InputRequiredException e) {
            e.printStackTrace();
        }
        Flux.from(result)
                .doOnNext(message -> {
                    String content = message.getOutput().getChoices().get(0).getMessage().getContent();
                    System.out.print(content);
                })
                .subscribe();
        long endTime = System.currentTimeMillis();
        System.out.println("运行时间:" + (endTime - startTime) + "毫秒");
//        result.blockingForEach(item -> {
////            log.info("item: {}", JSONUtil.toJsonStr(item));
//            // 提取文本内容
//            if (item.getOutput() != null && item.getOutput().getChoices() != null) {
//                item.getOutput().getChoices().forEach(choice -> {
//                    if (choice.getMessage() != null) {
//                        // 根据实际 API 文档确定使用哪个方法
////                        if (choice.getMessage().getContents() != null) {
////                            try {
////                                choice.getMessage().getContents().forEach(content -> {
////                                    if (content != null) {
////                                        System.out.print(content.getType());
////                                    }
////                                });
////                            } catch (NullPointerException e) {
////                                log.error("Error processing contents", e);
////                            }
////                        }
//                        if (choice.getMessage().getContent() != null) {
//                            System.out.print(choice.getMessage().getContent());
//                        }
//                    }
//                });
//            }
//        });
    }
}