2121public class ChatGptService {
2222 private static final Logger logger = LoggerFactory .getLogger (ChatGptService .class );
2323 private static final Duration TIMEOUT = Duration .ofSeconds (90 );
24+
25+ /** The maximum number of tokens allowed for the generated answer. */
2426 private static final int MAX_TOKENS = 3_000 ;
27+
28+ /**
29+ * This parameter reduces the likelihood of the AI repeating itself. A higher frequency penalty
30+ * makes the model less likely to repeat the same lines verbatim. It helps in generating more
31+ * diverse and varied responses.
32+ */
33+ private static final double FREQUENCY_PENALTY = 0.5 ;
34+
35+ /**
36+ * This parameter controls the randomness of the AI's responses. A higher temperature results in
37+ * more varied, unpredictable, and creative responses. Conversely, a lower temperature makes the
38+ * model's responses more deterministic and conservative.
39+ */
40+ private static final double TEMPERATURE = 0.8 ;
41+
42+ /**
43+ * n: This parameter specifies the number of responses to generate for each prompt. If n is more
44+ * than 1, the AI will generate multiple different responses to the same prompt, each one being
45+ * a separate iteration based on the input.
46+ */
47+ private static final int MAX_NUMBER_OF_RESPONSES = 1 ;
2548 private static final String AI_MODEL = "gpt-3.5-turbo" ;
2649
2750 private boolean isDisabled = false ;
@@ -42,18 +65,16 @@ public ChatGptService(Config config) {
4265
4366 openAiService = new OpenAiService (apiKey , TIMEOUT );
4467
45- ChatMessage setupMessage = new ChatMessage (ChatMessageRole .SYSTEM .value (),
46- """
47- Please answer questions in 1500 characters or less. Remember to count spaces in the
48- character limit. For code supplied for review, refer to the old code supplied rather than
49- rewriting the code. Don't supply a corrected version of the code.\s """ );
68+ ChatMessage setupMessage = new ChatMessage (ChatMessageRole .SYSTEM .value (), """
69+ For code supplied for review, refer to the old code supplied rather than
70+ rewriting the code. DON'T supply a corrected version of the code.\s """ );
5071 ChatCompletionRequest systemSetupRequest = ChatCompletionRequest .builder ()
5172 .model (AI_MODEL )
5273 .messages (List .of (setupMessage ))
53- .frequencyPenalty (0.5 )
54- .temperature (0.3 )
74+ .frequencyPenalty (FREQUENCY_PENALTY )
75+ .temperature (TEMPERATURE )
5576 .maxTokens (50 )
56- .n (1 )
77+ .n (MAX_NUMBER_OF_RESPONSES )
5778 .build ();
5879
5980 // Sending the system setup message to ChatGPT.
@@ -68,26 +89,29 @@ public ChatGptService(Config config) {
6889 * @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
6990 * Tokens</a>.
7091 */
71- public Optional <String []> ask (String question ) {
92+ public Optional <String []> ask (String question , String context ) {
7293 if (isDisabled ) {
7394 return Optional .empty ();
7495 }
7596
7697 try {
77- ChatMessage chatMessage =
78- new ChatMessage (ChatMessageRole .USER .value (), Objects .requireNonNull (question ));
98+ String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS" ;
99+ String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s"
100+ .formatted (context , instructions , question );
101+ ChatMessage chatMessage = new ChatMessage (ChatMessageRole .USER .value (),
102+ Objects .requireNonNull (questionWithContext ));
79103 ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder ()
80104 .model (AI_MODEL )
81105 .messages (List .of (chatMessage ))
82- .frequencyPenalty (0.5 )
83- .temperature (0.3 )
106+ .frequencyPenalty (FREQUENCY_PENALTY )
107+ .temperature (TEMPERATURE )
84108 .maxTokens (MAX_TOKENS )
85- .n (1 )
109+ .n (MAX_NUMBER_OF_RESPONSES )
86110 .build ();
87111
88112 String response = openAiService .createChatCompletion (chatCompletionRequest )
89113 .getChoices ()
90- .get ( 0 )
114+ .getFirst ( )
91115 .getMessage ()
92116 .getContent ();
93117
0 commit comments