diff --git a/chopperbot-common/src/main/java/org/example/constpool/PluginName.java b/chopperbot-common/src/main/java/org/example/constpool/PluginName.java index 025b7e7..2159aa8 100644 --- a/chopperbot-common/src/main/java/org/example/constpool/PluginName.java +++ b/chopperbot-common/src/main/java/org/example/constpool/PluginName.java @@ -59,4 +59,5 @@ public class PluginName { public static final String TITLE_GENERATE = "TitleGenerate"; public static final String DESC_GENERATE = "DescriptionGenerate"; + public static final String LABEL_GENERATE = "LabelGenerate"; } diff --git a/chopperbot-console/src/main/resources/application.yaml b/chopperbot-console/src/main/resources/application.yaml index d7b396e..e099ee4 100644 --- a/chopperbot-console/src/main/resources/application.yaml +++ b/chopperbot-console/src/main/resources/application.yaml @@ -13,3 +13,8 @@ chopperbot: enable: 0 address: 127.0.0.1 port: 7890 + plugin: + title-generate-plugin: + handler: ${TITLE-GENERATE-PLUGIN-HANDLER:GptTitleGenerator} + label-generate-plugin: + handler: ${LABEL-GENERATE-PLUGIN-HANDLER:GptLabelGenerator} diff --git a/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/CommonLabelGenerator.java b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/CommonLabelGenerator.java new file mode 100644 index 0000000..56e799c --- /dev/null +++ b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/CommonLabelGenerator.java @@ -0,0 +1,28 @@ +package org.example.core.auto.video.label; + +import org.example.util.MapUtil; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + +@Component +public class CommonLabelGenerator extends LabelGenerator{ + @Override + public List sqlInit() { + return null; + } + + @Override + public void preGenerate() { + + } + + @Override + public List generate(Map data) { + String liver = MapUtil.getString(data,"liver"); + String platform = MapUtil.getString(data,"platform"); + String tag = MapUtil.getString(data,"tag"); + return List.of(liver,platform,tag); + } +} diff --git a/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/GptLabelGenerator.java b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/GptLabelGenerator.java new file mode 100644 index 0000000..24b6cb2 --- /dev/null +++ b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/GptLabelGenerator.java @@ -0,0 +1,85 @@ +package org.example.core.auto.video.label; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import org.example.constpool.PluginName; +import org.example.core.gpt.ChatGPTMsgBuilder; +import org.example.core.gpt.OpenAPIPlugin; +import org.example.exception.plugin.PluginDependOnException; +import org.example.init.InitPluginRegister; +import org.example.mapper.AnalysisSchemeMapper; +import org.example.plugin.PluginCheckAndDo; +import org.example.pojo.AnalysisScheme; +import org.example.pojo.DescScheme; +import org.example.pojo.GPTKey; +import org.example.sql.annotation.SQLInit; +import org.example.util.ExceptionUtil; +import org.example.util.MapUtil; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Component +public class GptLabelGenerator extends LabelGenerator{ + + @Resource + AnalysisSchemeMapper mapper; + + private List schemes; + + @Override + @SQLInit(table = "analysis_scheme",tableSQL = "CREATE TABLE \"analysis_scheme\" (\n" + + " \"id\" integer NOT NULL,\n" + + " \"system\" text NOT NULL,\n" + + " \"comment\" TEXT,\n" + + " PRIMARY KEY (\"id\")\n" + + ")",mapper = AnalysisSchemeMapper.class) + public List sqlInit() { + return List.of(new AnalysisScheme(null, + "请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容", + "方案一")); + } + + @Override + public void preGenerate() { + if (!InitPluginRegister.isRegister(PluginName.CHAT_GPT)) { + throw PluginDependOnException.MissingFatherPlugin(PluginName.CHAT_GPT,""); + } + try { + schemes = mapper.selectList(new QueryWrapper<>()); + }catch (Exception e){ + throw new RuntimeException("读取label_scheme表失败"); + } + } + + @Override + public List generate(Map data) { + if (schemes.isEmpty())return new ArrayList<>(); + try { + return PluginCheckAndDo.CheckAndGet((plugin)->{ + Object barrages = data.get("barrages"); + String barrageStr = ""; + if(barrages instanceof List){ + barrageStr = OpenAPIPlugin.zipContent(OpenAPIPlugin.zipContent((List) barrages).toString()); + } + GPTKey gptKey = ((OpenAPIPlugin) plugin).choseKey(OpenAPIPlugin.APIFunc.CHAT_GPT); + AnalysisScheme scheme = schemes.get(0); + if(gptKey==null)return new ArrayList<>(); + ChatGPTMsgBuilder builder = new ChatGPTMsgBuilder().model(gptKey.getModel()) + .system(scheme.getSystem()) + .user("弹幕:" + barrageStr) + .stream(false); + String liver = MapUtil.getString(data,"liver"); + String platform = MapUtil.getString(data,"platform"); + String tag = MapUtil.getString(data,"tag"); + JSONObject object = ((OpenAPIPlugin) plugin).reqGPT(builder, OpenAPIPlugin.APIFunc.CHAT_GPT); + return List.of(((OpenAPIPlugin) plugin).getCommonRes(object),liver,platform,tag); + },PluginName.CHAT_GPT,List.class); + } catch (Exception e){ + throw new RuntimeException(ExceptionUtil.getCause(e)); + } + } +} diff --git a/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGeneratePlugin.java b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGeneratePlugin.java new file mode 100644 index 0000000..7fad446 --- /dev/null +++ b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGeneratePlugin.java @@ -0,0 +1,28 @@ +package org.example.core.auto.video.label; + +import org.example.core.auto.AbstractGeneratePlugin; +import org.example.core.auto.video.title.TitleGenerator; +import org.example.util.StringUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Map; + + +@Component +public class LabelGeneratePlugin extends AbstractGeneratePlugin { + + @Resource + Map labelGeneratorMap; + + @Value("${chopperbot.plugin.label-generate-plugin.handler}") + private String configType; + @Override + public boolean init() { + this.generatorMap = labelGeneratorMap; + this.type = StringUtil.lowerCaseFirstLetter(configType); + return super.init(); + } + +} diff --git a/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGenerator.java b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGenerator.java new file mode 100644 index 0000000..712aa72 --- /dev/null +++ b/chopperbot-section-work/src/main/java/org/example/core/auto/video/label/LabelGenerator.java @@ -0,0 +1,32 @@ +package org.example.core.auto.video.label; + +import org.example.bean.section.PackageSection; +import org.example.bean.section.VideoSection; +import org.example.core.auto.SectionGenerator; +import org.example.mapper.AnalysisSchemeMapper; +import org.example.pojo.AnalysisScheme; +import org.example.sql.annotation.SQLInit; +import org.example.util.ClassUtil; +import org.springframework.beans.BeanUtils; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +public abstract class LabelGenerator extends SectionGenerator> { + + + @Override + public V generator(T section) { + PackageSection packageSection = new PackageSection(); + BeanUtils.copyProperties(section,packageSection); + try { + Map map = ClassUtil.toDeepMap(packageSection); + List labels = this.generate(map); + packageSection.setLabels(labels); + }catch (Exception e){ + return (V) section; + } + return (V) packageSection; + } +} diff --git a/chopperbot-section-work/src/main/java/org/example/core/auto/video/title/TitleGeneratePlugin.java b/chopperbot-section-work/src/main/java/org/example/core/auto/video/title/TitleGeneratePlugin.java index 64fb545..d20e84c 100644 --- a/chopperbot-section-work/src/main/java/org/example/core/auto/video/title/TitleGeneratePlugin.java +++ b/chopperbot-section-work/src/main/java/org/example/core/auto/video/title/TitleGeneratePlugin.java @@ -7,6 +7,7 @@ import org.example.core.auto.SectionPipeline; import org.example.core.auto.video.description.DescGenerator; import org.example.plugin.SpringBootPlugin; import org.example.util.StringUtil; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @@ -26,10 +27,13 @@ public class TitleGeneratePlugin extends AbstractGeneratePlugin @Resource Map titleGeneratorMap; + + @Value("${chopperbot.plugin.title-generate-plugin.handler}") + private String configType; @Override public boolean init() { this.generatorMap = titleGeneratorMap; - this.type = StringUtil.lowerCaseFirstLetter(GptTitleGenerator.class.getSimpleName()); + this.type = StringUtil.lowerCaseFirstLetter(configType); return super.init(); } diff --git a/chopperbot-section-work/src/main/java/org/example/init/EmotionAnalysisPluginMachine.java b/chopperbot-section-work/src/main/java/org/example/init/EmotionAnalysisPluginMachine.java index 780f4a7..d25f828 100644 --- a/chopperbot-section-work/src/main/java/org/example/init/EmotionAnalysisPluginMachine.java +++ b/chopperbot-section-work/src/main/java/org/example/init/EmotionAnalysisPluginMachine.java @@ -17,7 +17,8 @@ import org.springframework.stereotype.Component; needPlugin = {PluginName.CHAT_GPT,PluginName.LABEL_MANAGER}, pluginClass= EmotionAnalysisPlugin.class, springBootPlugin = true, - ignore=true + ignore=true, + autoStart = false ) @Component public class EmotionAnalysisPluginMachine extends SpringPlugInitMachine{ diff --git a/chopperbot-section-work/src/main/java/org/example/init/LabelGeneratorPluginInitMachine.java b/chopperbot-section-work/src/main/java/org/example/init/LabelGeneratorPluginInitMachine.java new file mode 100644 index 0000000..7f1fc7c --- /dev/null +++ b/chopperbot-section-work/src/main/java/org/example/init/LabelGeneratorPluginInitMachine.java @@ -0,0 +1,19 @@ +package org.example.init; + +import org.example.constpool.ModuleName; +import org.example.constpool.PluginName; +import org.example.core.auto.video.label.LabelGeneratePlugin; +import org.example.plugin.annotation.Plugin; +import org.springframework.stereotype.Component; + +@Plugin(moduleName = ModuleName.SECTION_WORK, + pluginName = PluginName.LABEL_GENERATE, + pluginName_CN = "视频情感标签生成插件", + pluginDescription = "根据切片内容自动生成视频情感标签", + pluginClass= LabelGeneratePlugin.class, + springBootPlugin = true, + ignore=true +) +@Component +public class LabelGeneratorPluginInitMachine extends SpringPlugInitMachine{ +} diff --git a/chopperbot-test/src/test/java/org/example/sectionwork/LabelGenerateTest.java b/chopperbot-test/src/test/java/org/example/sectionwork/LabelGenerateTest.java new file mode 100644 index 0000000..66f123e --- /dev/null +++ b/chopperbot-test/src/test/java/org/example/sectionwork/LabelGenerateTest.java @@ -0,0 +1,48 @@ +package org.example.sectionwork; + + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.example.ConsoleApplication; +import org.example.bean.section.VideoSection; +import org.example.core.auto.video.description.DescGeneratorPlugin; +import org.example.core.auto.video.label.LabelGeneratePlugin; +import org.example.core.auto.video.title.TitleGeneratePlugin; +import org.example.util.JsonFileUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConsoleApplication.class,webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class LabelGenerateTest { + + @Resource + LabelGeneratePlugin labelGeneratePlugin; + + @Test + public void test(){ + Map map = JsonFileUtil.readJsonFile("E:\\Project\\ChopperBot\\config\\Barrage\\online\\huya\\Uzi_2023-10-18 21_22_57.json"); + Object data = map.get("data"); + if(data instanceof JSONArray){ + List content = ((JSONArray) data).stream().map(o -> { + if (o instanceof JSONObject) { + return ((JSONObject) o).get("content").toString(); + } + return ""; + }).collect(Collectors.toList()); + VideoSection videoSection = new VideoSection(); + videoSection.setBarrages(content.subList(0,100)); + videoSection.setTag("英雄联盟"); + videoSection.setLiver("UZI"); + VideoSection process = labelGeneratePlugin.process(videoSection); + System.out.println(process); + } + } +} diff --git a/config/chopperBotConfig.json b/config/chopperBotConfig.json index 3c35fff..f417937 100644 --- a/config/chopperBotConfig.json +++ b/config/chopperBotConfig.json @@ -26,11 +26,13 @@ "HotConfig":true, "TaskCenter":true, "CreeperManager":true, + "DescriptionGenerate":true, "InstantSlicing":true, "LiveDownLoadManager":true, "BarragePopularRange":true, "HotRecommendation":true, "NoticePlugin":true, + "TitleGenerate":true, "VideoPush":true, "AccountManager":true, "LabelManager":true, @@ -38,5 +40,5 @@ "OpenAPI":true } }, - "updateTime":"2023-10-25 15:01:54" + "updateTime":"2023-11-06 12:00:41" } \ No newline at end of file