Skip to content

Commit bd3a026

Browse files
committed
feat: use new gpt-5-nano model
1 parent 5c64afe commit bd3a026

File tree

1 file changed

+35
-69
lines changed

1 file changed

+35
-69
lines changed

application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.togetherjava.tjbot.features.chatgpt;
22

3-
import com.theokanning.openai.OpenAiHttpException;
4-
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
5-
import com.theokanning.openai.completion.chat.ChatMessage;
6-
import com.theokanning.openai.completion.chat.ChatMessageRole;
7-
import com.theokanning.openai.service.OpenAiService;
3+
import com.openai.client.OpenAIClient;
4+
import com.openai.client.okhttp.OpenAIOkHttpClient;
5+
import com.openai.models.ChatModel;
6+
import com.openai.models.responses.Response;
7+
import com.openai.models.responses.ResponseCreateParams;
8+
import com.openai.models.responses.ResponseOutputText;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011

@@ -13,8 +14,8 @@
1314
import javax.annotation.Nullable;
1415

1516
import java.time.Duration;
16-
import java.util.List;
1717
import java.util.Optional;
18+
import java.util.stream.Collectors;
1819

1920
/**
2021
* Service used to communicate to OpenAI API to generate responses.
@@ -26,30 +27,8 @@ public class ChatGptService {
2627
/** The maximum number of tokens allowed for the generated answer. */
2728
private static final int MAX_TOKENS = 3_000;
2829

29-
/**
30-
* This parameter reduces the likelihood of the AI repeating itself. A higher frequency penalty
31-
* makes the model less likely to repeat the same lines verbatim. It helps in generating more
32-
* diverse and varied responses.
33-
*/
34-
private static final double FREQUENCY_PENALTY = 0.5;
35-
36-
/**
37-
* This parameter controls the randomness of the AI's responses. A higher temperature results in
38-
* more varied, unpredictable, and creative responses. Conversely, a lower temperature makes the
39-
* model's responses more deterministic and conservative.
40-
*/
41-
private static final double TEMPERATURE = 0.8;
42-
43-
/**
44-
* n: This parameter specifies the number of responses to generate for each prompt. If n is more
45-
* than 1, the AI will generate multiple different responses to the same prompt, each one being
46-
* a separate iteration based on the input.
47-
*/
48-
private static final int MAX_NUMBER_OF_RESPONSES = 1;
49-
private static final String AI_MODEL = "gpt-3.5-turbo";
50-
5130
private boolean isDisabled = false;
52-
private OpenAiService openAiService;
31+
private OpenAIClient openAIClient;
5332

5433
/**
5534
* Creates instance of ChatGPTService
@@ -63,23 +42,7 @@ public ChatGptService(Config config) {
6342
isDisabled = true;
6443
return;
6544
}
66-
67-
openAiService = new OpenAiService(apiKey, TIMEOUT);
68-
69-
ChatMessage setupMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), """
70-
For code supplied for review, refer to the old code supplied rather than
71-
rewriting the code. DON'T supply a corrected version of the code.\s""");
72-
ChatCompletionRequest systemSetupRequest = ChatCompletionRequest.builder()
73-
.model(AI_MODEL)
74-
.messages(List.of(setupMessage))
75-
.frequencyPenalty(FREQUENCY_PENALTY)
76-
.temperature(TEMPERATURE)
77-
.maxTokens(50)
78-
.n(MAX_NUMBER_OF_RESPONSES)
79-
.build();
80-
81-
// Sending the system setup message to ChatGPT.
82-
openAiService.createChatCompletion(systemSetupRequest);
45+
openAIClient = OpenAIOkHttpClient.builder().apiKey(apiKey).timeout(TIMEOUT).build();
8346
}
8447

8548
/**
@@ -98,32 +61,35 @@ public Optional<String> ask(String question, @Nullable String context) {
9861
}
9962

10063
String contextText = context == null ? "" : ", Context: %s.".formatted(context);
101-
String fullQuestion = "(KEEP IT CONCISE, NOT MORE THAN 280 WORDS%s) - %s"
102-
.formatted(contextText, question);
103-
104-
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), fullQuestion);
105-
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
106-
.model(AI_MODEL)
107-
.messages(List.of(chatMessage))
108-
.frequencyPenalty(FREQUENCY_PENALTY)
109-
.temperature(TEMPERATURE)
110-
.maxTokens(MAX_TOKENS)
111-
.n(MAX_NUMBER_OF_RESPONSES)
112-
.build();
113-
logger.debug("ChatGpt Request: {}", fullQuestion);
64+
String inputPrompt = """
65+
For code supplied for review, refer to the old code supplied rather than
66+
rewriting the code. DON'T supply a corrected version of the code.
67+
68+
KEEP IT CONCISE, NOT MORE THAN 280 WORDS
69+
70+
%s
71+
Question: %s
72+
""".formatted(contextText, question);
73+
74+
logger.debug("ChatGpt request: {}", inputPrompt);
11475

11576
String response = null;
11677
try {
117-
response = openAiService.createChatCompletion(chatCompletionRequest)
118-
.getChoices()
119-
.getFirst()
120-
.getMessage()
121-
.getContent();
122-
} catch (OpenAiHttpException openAiHttpException) {
123-
logger.warn(
124-
"There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}",
125-
openAiHttpException.getMessage(), openAiHttpException.code,
126-
openAiHttpException.type, openAiHttpException.statusCode);
78+
ResponseCreateParams params = ResponseCreateParams.builder()
79+
.model(ChatModel.GPT_5_NANO)
80+
.input(inputPrompt)
81+
.maxOutputTokens(MAX_TOKENS)
82+
.build();
83+
84+
Response chatGptResponse = openAIClient.responses().create(params);
85+
86+
response = chatGptResponse.output()
87+
.stream()
88+
.flatMap(item -> item.message().stream())
89+
.flatMap(message -> message.content().stream())
90+
.flatMap(content -> content.outputText().stream())
91+
.map(ResponseOutputText::text)
92+
.collect(Collectors.joining("\n"));
12793
} catch (RuntimeException runtimeException) {
12894
logger.warn("There was an error using the OpenAI API: {}",
12995
runtimeException.getMessage());

0 commit comments

Comments
 (0)