Skip to content

Commit 8323ba7

Browse files
authored
OpenAI moderation tool implementation instead of ChatGPT models (#5)
1 parent 3611ac8 commit 8323ba7

9 files changed

Lines changed: 78 additions & 39 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<version>1.20.4-R0.1-SNAPSHOT</version>
4747
<scope>provided</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>com.google.code.gson</groupId>
51+
<artifactId>gson</artifactId>
52+
<version>2.10.1</version>
53+
</dependency>
4954
</dependencies>
5055

5156
</project>

src/main/java/fr/lifesteal/respectguard/RespectGuard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private void initServices() {
3434
this.configurationService = new ConfigurationService(this, this.getConfig(), configurationCacheService);
3535

3636
var httpRequestService = new HttpRequestService(loggerService);
37-
var chatGptService = new ChatGptService(loggerService, this.configurationService, httpRequestService);
37+
var chatGptService = new ModerationService(loggerService, this.configurationService, httpRequestService);
3838

3939
var commandDispatcher = new CommandDispatcherWrapper(this);
4040
var eventCaller = new EventCallerWrapper(this);

src/main/java/fr/lifesteal/respectguard/business/ChatGuardService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public ChatGuardService(IChatGptService chatGptService, IConfigurationService co
2424

2525
@Override
2626
public boolean analyzePlayerMessage(Player player, String message) {
27-
boolean isBadMessage = this.chatGptService.IsBadMessage(message);
27+
var analysesResult = this.chatGptService.analyzeMessage(message);
2828

29-
if (!isBadMessage) return false;
29+
if (!analysesResult.isHarmful()) return false;
3030

31-
this.eventCaller.callEvent(new BadMessageEvent(player, message, true));
31+
this.eventCaller.callEvent(new BadMessageEvent(player, message, true, analysesResult.getHarmfulCategories()));
3232

3333
for (String command : this.configurationService.getCommandsToExecute()) {
3434
this.commandDispatcher.dispatchConsoleCommand(command);

src/main/java/fr/lifesteal/respectguard/business/Interface/IChatGptService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fr.lifesteal.respectguard.business.Interface;
22

3+
import fr.lifesteal.respectguard.business.object.MessageAnalysesResult;
4+
35
/**
46
* Service d'intéraction avec l'API ChatGPT.
57
*/
@@ -10,5 +12,5 @@ public interface IChatGptService {
1012
* @param message Message à tester.
1113
* @return Retourne vrai si le message contient une insulte, faux sinon.
1214
*/
13-
boolean IsBadMessage(String message);
15+
MessageAnalysesResult analyzeMessage(String message);
1416
}

src/main/java/fr/lifesteal/respectguard/business/ChatGptService.java renamed to src/main/java/fr/lifesteal/respectguard/business/ModerationService.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package fr.lifesteal.respectguard.business;
22

3+
import com.google.gson.Gson;
34
import fr.lifesteal.respectguard.business.Interface.IChatGptService;
45
import fr.lifesteal.respectguard.business.config.Interface.IConfigurationService;
56
import fr.lifesteal.respectguard.business.Interface.IHttpRequestService;
67
import fr.lifesteal.respectguard.business.Interface.ILoggerService;
8+
import fr.lifesteal.respectguard.business.object.MessageAnalysesResult;
79

810
import java.util.HashMap;
11+
import java.util.HashSet;
912
import java.util.Map;
1013

1114
/**
1215
* {@inheritDoc}
1316
*/
14-
public class ChatGptService implements IChatGptService {
15-
private static final String CHATGPT_URL = "https://api.openai.com/v1/chat/completions";
16-
private static final String IS_BAD_MESSAGE_REQUEST = "Uniquement par oui ou par non, est-ce que cette phrase contient une insulte : %testedMessage";
17+
public class ModerationService implements IChatGptService {
18+
private static final String OPENAI_MODERATION_URL = "https://api.openai.com/v1/moderations";
1719
private final IConfigurationService configurationService;
1820
private final IHttpRequestService httpRequestService;
1921
private final ILoggerService loggerService;
@@ -23,7 +25,7 @@ public class ChatGptService implements IChatGptService {
2325
* @param loggerService service de gestion des logs.
2426
* @param configurationService service de gestion de la configuration.
2527
*/
26-
public ChatGptService(ILoggerService loggerService, IConfigurationService configurationService, IHttpRequestService httpRequestService) {
28+
public ModerationService(ILoggerService loggerService, IConfigurationService configurationService, IHttpRequestService httpRequestService) {
2729
this.loggerService = loggerService;
2830
this.configurationService = configurationService;
2931
this.httpRequestService = httpRequestService;
@@ -32,18 +34,18 @@ public ChatGptService(ILoggerService loggerService, IConfigurationService config
3234
/**
3335
* {@inheritDoc}
3436
*/
35-
public boolean IsBadMessage(String message) {
37+
public MessageAnalysesResult analyzeMessage(String message) {
3638
this.loggerService.Debug("RequestMessage = %message".replace("%message", message));
3739

3840
String response = this.httpRequestService.getRequestResponse(
39-
CHATGPT_URL,
41+
OPENAI_MODERATION_URL,
4042
"POST",
41-
getRequestPrompt(message),
43+
getRequestMessage(message),
4244
getRequestProperties());
4345

4446
this.loggerService.Debug("RequestReponse = %message".replace("%message", response));
4547

46-
return extractContentFromResponse(response).contains("Oui");
48+
return extractContentFromResponse(response);
4749
}
4850

4951
private Map<String, String> getRequestProperties() {
@@ -52,22 +54,39 @@ private Map<String, String> getRequestProperties() {
5254
put("Authorization", "Bearer " + configurationService.getChatGptApiKey());
5355
}};
5456
}
55-
private String getRequestPrompt(String message) {
56-
String requestMessage = IS_BAD_MESSAGE_REQUEST
57-
.replace("%testedMessage", message);
5857

59-
String prompt = "[{\"role\": \"user\", \"content\": \"" + requestMessage + "\"}]";
60-
return "{\"model\": \"" + this.configurationService.getChatGptModel() + "\", \"messages\": " + prompt + "}";
58+
private String getRequestMessage(String message) {
59+
return "{\"input\": \"" + message + "\"}";
6160
}
6261

6362
/**
6463
* Méthode permettant d'éxtraire la réponse de chatGpt depuis les réponses de la requete.
6564
* @param response Reponse de la requete à ChatGpt.
6665
* @return Retourne la phrase de reponse de ChatGpt.
6766
*/
68-
private String extractContentFromResponse(String response) {
69-
int startMarker = response.indexOf("content")+11; // Marker for where the content starts.
70-
int endMarker = response.indexOf("\"", startMarker); // Marker for where the content ends.
71-
return response.substring(startMarker, endMarker); // Returns the substring containing only the response.
67+
private MessageAnalysesResult extractContentFromResponse(String response) {
68+
Gson gson = new Gson();
69+
ResultWrapper resultWrapper = gson.fromJson(response, ResultWrapper.class);
70+
Result result = resultWrapper.results[0];
71+
72+
boolean flagged = result.flagged;
73+
74+
var categories = new HashSet<String>();
75+
for (var category : result.categories.entrySet()) {
76+
if (category.getValue()) {
77+
categories.add(category.getKey());
78+
}
79+
}
80+
81+
return new MessageAnalysesResult(flagged, categories);
82+
}
83+
84+
private static class ResultWrapper {
85+
Result[] results;
86+
}
87+
88+
private static class Result {
89+
boolean flagged;
90+
Map<String, Boolean> categories;
7291
}
7392
}

src/main/java/fr/lifesteal/respectguard/business/config/ConfigurationService.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public ConfigurationService(JavaPlugin plugin, FileConfiguration fileConfigurati
3434
*/
3535
public void initDefaultConfiguration() {
3636
this.fileConfiguration.addDefault(ConfigurationConstante.CHATGPT_API_KEY, ConfigurationConstante.CHATGPT_API_KEY_DEFAULT_VALUE);
37-
this.fileConfiguration.addDefault(ConfigurationConstante.CHATGPT_MODEL_KEY, ConfigurationConstante.CHATGPT_MODEL_KEY_DEFAULT_VALUE);
3837
this.fileConfiguration.addDefault(ConfigurationConstante.EVENT_CANCEL_KEY, ConfigurationConstante.EVENT_CANCEL_KEY_DEFAULT_VALUE);
3938
this.fileConfiguration.addDefault(ConfigurationConstante.EVENT_COMMANDS_KEY, ConfigurationConstante.EVENT_COMMANDS_KEY_DEFAULT_VALUE);
4039

@@ -47,7 +46,6 @@ public void initDefaultConfiguration() {
4746
*/
4847
public void loadConfiguration() {
4948
this.loadStringValue(ConfigurationConstante.CHATGPT_API_KEY);
50-
this.loadStringValue(ConfigurationConstante.CHATGPT_MODEL_KEY);
5149
this.loadBooleanValue(ConfigurationConstante.EVENT_CANCEL_KEY);
5250
this.loadStringListValue(ConfigurationConstante.EVENT_COMMANDS_KEY);
5351
}
@@ -60,14 +58,6 @@ public String getChatGptApiKey() {
6058
return (String) this.cacheService.getValue(ConfigurationConstante.CHATGPT_API_KEY);
6159
}
6260

63-
/**
64-
* {@inheritDoc}
65-
*/
66-
@Override
67-
public String getChatGptModel() {
68-
return (String) this.cacheService.getValue(ConfigurationConstante.CHATGPT_MODEL_KEY);
69-
}
70-
7161
/**
7262
* {@inheritDoc}
7363
*/

src/main/java/fr/lifesteal/respectguard/business/config/Interface/IConfigurationService.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public interface IConfigurationService {
2323
*/
2424
String getChatGptApiKey();
2525

26-
/**
27-
* Méthode permettant de récupérer le model ChatGPT à utiliser.
28-
* @return Retourne le nom du model ChatGPT à utiliser.
29-
*/
30-
String getChatGptModel();
31-
3226
/**
3327
* Méthode permettant de récupérer une valeur indiquant si l'event de chat doit être annulé en cas d'insulte dans le message.
3428
* @return Retourne une valeur indiquant si l'event de chat doit être annulé en cas d'insulte dans le message.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package fr.lifesteal.respectguard.business.object;
2+
3+
import java.util.Set;
4+
5+
public class MessageAnalysesResult {
6+
private final boolean isHarmful;
7+
private final Set<String> harmfulCategories;
8+
9+
public MessageAnalysesResult(boolean isHarmful, Set<String> harmfulCategories) {
10+
this.isHarmful = isHarmful;
11+
this.harmfulCategories = harmfulCategories;
12+
}
13+
14+
public boolean isHarmful() {
15+
return isHarmful;
16+
}
17+
18+
public Set<String> getHarmfulCategories() {
19+
return harmfulCategories;
20+
}
21+
}

src/main/java/fr/lifesteal/respectguard/event/BadMessageEvent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
import org.bukkit.event.Event;
55
import org.bukkit.event.HandlerList;
66

7+
import java.util.Set;
8+
79
public class BadMessageEvent extends Event {
810
private static final HandlerList HANDLERS = new HandlerList();
911
private final Player player;
1012
private final String message;
13+
private final Set<String> harmfulCategories;
1114

12-
public BadMessageEvent(Player player, String message, boolean isAsync) {
15+
public BadMessageEvent(Player player, String message, boolean isAsync, Set<String> harmfulCategories) {
1316
super(isAsync);
1417
this.player = player;
1518
this.message = message;
19+
this.harmfulCategories = harmfulCategories;
1620
}
1721

1822
public static HandlerList getHandlerList() {
@@ -31,4 +35,8 @@ public Player getPlayer() {
3135
public String getMessage() {
3236
return message;
3337
}
38+
39+
public Set<String> getHarmfulCategories() {
40+
return harmfulCategories;
41+
}
3442
}

0 commit comments

Comments
 (0)