mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-09-01 14:50:18 +08:00
情感分析插件开发完成 # 10
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package org.example.core.analysis;
|
||||
|
||||
import lombok.Data;
|
||||
import org.example.bean.Barrage;
|
||||
import org.example.pojo.AnalysisScheme;
|
||||
import org.jdom2.output.EscapeStrategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
@Data
|
||||
public class AnalysisSchemeBuilder {
|
||||
|
||||
private AnalysisScheme scheme;
|
||||
|
||||
private List<Barrage> barrages;
|
||||
|
||||
public AnalysisSchemeBuilder labels(String msg){
|
||||
scheme.setLabels(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AnalysisSchemeBuilder system(String msg){
|
||||
scheme.setSystem(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AnalysisSchemeBuilder comment(String msg){
|
||||
scheme.setComment(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AnalysisSchemeBuilder barrages(List<Barrage> barrages){
|
||||
this.barrages = barrages;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.example.core.analysis;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.Data;
|
||||
import org.example.bean.Barrage;
|
||||
import org.example.core.gpt.ChatGPTMsgBuilder;
|
||||
import org.example.core.gpt.ChatGPTPlugin;
|
||||
import org.example.mapper.AnalysisSchemeMapper;
|
||||
import org.example.plugin.SpringBootPlugin;
|
||||
import org.example.pojo.AnalysisScheme;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
public class EmotionAnalysisPlugin extends SpringBootPlugin {
|
||||
|
||||
@Resource
|
||||
AnalysisSchemeMapper mapper;
|
||||
|
||||
@Resource
|
||||
ChatGPTPlugin chatGPTPlugin;
|
||||
|
||||
String barrage;
|
||||
|
||||
AnalysisScheme scheme;
|
||||
|
||||
@Override
|
||||
public boolean init(){
|
||||
try {
|
||||
chooseScheme();
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("init analysis failed!pleas check or try again!");
|
||||
}
|
||||
return super.init();
|
||||
}
|
||||
|
||||
private void chooseScheme(){
|
||||
List<AnalysisScheme> schemes = mapper.selectList(new QueryWrapper<>());
|
||||
if(schemes == null || schemes.isEmpty()) throw new RuntimeException("invaild anlysis scheme!please set scheme!");
|
||||
scheme = schemes.get(0);
|
||||
}
|
||||
|
||||
public String analysis(AnalysisSchemeBuilder analysisSchemeBuilder){
|
||||
return analysis(analysisSchemeBuilder.getBarrages(), analysisSchemeBuilder.getScheme());
|
||||
}
|
||||
|
||||
public String analysis(List<Barrage> barrages, AnalysisScheme scheme){
|
||||
if(scheme != null) this.scheme = scheme;
|
||||
return analysis(barrages);
|
||||
}
|
||||
|
||||
public String analysis(List<Barrage> barrages){
|
||||
try {
|
||||
barrage = List.of(barrages.stream().map(Barrage::getContent).collect(Collectors.toList())).toString();
|
||||
|
||||
ChatGPTMsgBuilder builder = new ChatGPTMsgBuilder().model(chatGPTPlugin.getKey().getModel())
|
||||
.system(this.scheme.getSystem())
|
||||
.user("弹幕:" + barrage)
|
||||
.stream(false);
|
||||
|
||||
JSONObject object = chatGPTPlugin.reqGPT(builder);
|
||||
|
||||
// System.out.println(object.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"));
|
||||
|
||||
Pattern pattern = Pattern.compile("\\[(.*?)]");
|
||||
|
||||
Matcher matcher = pattern.matcher(object.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"));
|
||||
|
||||
if (matcher.find()) return matcher.group(1);
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException("analysis failed!please check or try again!");
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.constpool.ModuleName;
|
||||
import org.example.constpool.PluginName;
|
||||
import org.example.core.analysis.EmotionAnalysisPlugin;
|
||||
import org.example.core.gpt.ChatGPTPlugin;
|
||||
import org.example.plugin.annotation.Plugin;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
@Plugin(moduleName = ModuleName.ACCOUNT,
|
||||
pluginName = PluginName.EMOTION_ANALYSIS,
|
||||
pluginName_CN = "情感分析插件",
|
||||
needPlugin = {PluginName.CHAT_GPT},
|
||||
pluginClass= EmotionAnalysisPlugin.class,
|
||||
springBootPlugin = true
|
||||
)
|
||||
@Component
|
||||
public class EmotionAnalysisPluginMachine extends SpringPlugInitMachine{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.example.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.example.pojo.AnalysisScheme;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
public interface AnalysisSchemeMapper extends BaseMapper<AnalysisScheme> {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.example.pojo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@TableName("analysis_scheme")
|
||||
public class AnalysisScheme {
|
||||
@TableId(value = "id")
|
||||
private Long id;
|
||||
|
||||
private String labels;
|
||||
|
||||
private String system;
|
||||
|
||||
private String comment;
|
||||
|
||||
public String getSystem(){
|
||||
return system + " 标签:[" + labels + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.example.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import netscape.javascript.JSObject;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import org.example.core.config.GPTConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/13
|
||||
* @Author xiaochun
|
||||
*/
|
||||
public class GPTUtil {
|
||||
|
||||
// public static RequestBody getRequestBodyJSON(String[] labels){
|
||||
// StringBuilder label = new StringBuilder();
|
||||
// label.append("标签:[").append(labels[0]);
|
||||
// for (int i = 1;i < labels.length; i++){
|
||||
// label.append(",").append(labels[i]);
|
||||
// }
|
||||
// label.append("]");
|
||||
// String videoInfo = "弹幕:帅,这操作我要学一年,太6了";
|
||||
// String systemInfo = "请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容标签:[搞笑,秀操作,破防,泪目]";
|
||||
//
|
||||
// Map<String, Object> message = Map.of(
|
||||
// "messages", List.of(new Role("system", "???"), new Role("user", "hello")),
|
||||
// "ai", "gpt-3.5-turbo-16k-0613",
|
||||
// "stream", false
|
||||
// );
|
||||
// String requestBodyString = JSONObject.toJSONString(message);
|
||||
// RequestBody requestBody = RequestBody.create(requestBodyString, MediaType.parse("application/json"));
|
||||
// return requestBody;
|
||||
// }
|
||||
|
||||
// public static Headers getHeaders(String key){
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Data
|
||||
// @AllArgsConstructor
|
||||
// static class Role{
|
||||
// private String role;
|
||||
// private String content;
|
||||
// }
|
||||
}
|
||||
@@ -44,6 +44,8 @@ public class PluginName {
|
||||
|
||||
public static final String CHAT_GPT = "ChatGPT";
|
||||
|
||||
public static final String EMOTION_ANALYSIS = "EmotionAnalysis";
|
||||
|
||||
|
||||
|
||||
public static final String LIVE_CONFIG_PLUGIN= "LiveConfig";
|
||||
|
||||
Binary file not shown.
+16
-16
@@ -102,22 +102,22 @@
|
||||
<version>portable-1.7.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.squareup.okhttp3</groupId>-->
|
||||
<!-- <artifactId>okhttp</artifactId>-->
|
||||
<!-- <version>4.9.0</version>-->
|
||||
<!-- <exclusions>-->
|
||||
<!-- <exclusion>-->
|
||||
<!-- <groupId>com.squareup.okio</groupId>-->
|
||||
<!-- <artifactId>okio</artifactId>-->
|
||||
<!-- </exclusion>-->
|
||||
<!-- </exclusions>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.9.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.squareup.okio</groupId>
|
||||
<artifactId>okio</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.squareup.okio</groupId>-->
|
||||
<!-- <artifactId>okio</artifactId>-->
|
||||
<!-- <version>3.2.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okio</groupId>
|
||||
<artifactId>okio</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.example.account;
|
||||
|
||||
import org.example.ConsoleApplication;
|
||||
import org.example.bean.Barrage;
|
||||
import org.example.core.analysis.AnalysisSchemeBuilder;
|
||||
import org.example.core.analysis.EmotionAnalysisPlugin;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Date 2023/10/16
|
||||
* @Author xiaochun
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConsoleApplication.class,webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
public class AnalysisTest {
|
||||
|
||||
@Resource
|
||||
EmotionAnalysisPlugin plugin;
|
||||
@Test
|
||||
public void AnalysisTest(){
|
||||
List<Barrage> barrages = new ArrayList<>();
|
||||
barrages.add(new Barrage("1", 1L, 1L,"太帅啦"));
|
||||
barrages.add(new Barrage("2", 1L, 1L,"666"));
|
||||
barrages.add(new Barrage("3", 1L, 1L,"无敌"));
|
||||
AnalysisSchemeBuilder builder = new AnalysisSchemeBuilder()
|
||||
.barrages(barrages);
|
||||
System.out.println(plugin.analysis(builder));
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public class gptTest {
|
||||
.system("请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容标签:[搞笑,秀操作,破防,泪目]")
|
||||
.user("弹幕:帅,这操作我要学一年,太6了")
|
||||
.stream(false);
|
||||
System.out.println(builder.done());
|
||||
ChatGPTPlugin plugin1 = InitPluginRegister.getPlugin(PluginName.CHAT_GPT, ChatGPTPlugin.class);
|
||||
System.out.println(plugin.reqGPT(builder));
|
||||
System.out.println(plugin1.reqGPT(builder));
|
||||
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user