11package fr .lifesteal .respectguard .business ;
22
3+ import com .google .gson .Gson ;
34import fr .lifesteal .respectguard .business .Interface .IChatGptService ;
45import fr .lifesteal .respectguard .business .config .Interface .IConfigurationService ;
56import fr .lifesteal .respectguard .business .Interface .IHttpRequestService ;
67import fr .lifesteal .respectguard .business .Interface .ILoggerService ;
8+ import fr .lifesteal .respectguard .business .object .MessageAnalysesResult ;
79
810import java .util .HashMap ;
11+ import java .util .HashSet ;
912import 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}
0 commit comments