Skip to content

Commit 5009653

Browse files
tj-wazeisurajkumar
andauthored
Update ChatGPT model to accept ChatModel & use 4.1-mini for questions (#1376)
* Update ChatGPT model to GPT_5_MINI * feat: add a chat model parameter to allow choice between models (speed vs quality) * feat: create an enum to house the AI models * update AI response footer to include model * docs: add java docs --------- Co-authored-by: Suraj Kumar <sk96.uk@gmail.com>
1 parent 938c6c1 commit 5009653

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* which it will respond with an AI generated answer.
2626
*/
2727
public final class ChatGptCommand extends SlashCommandAdapter {
28+
private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel.HIGH_QUALITY;
2829
public static final String COMMAND_NAME = "chatgpt";
2930
private static final String QUESTION_INPUT = "question";
3031
private static final int MAX_MESSAGE_INPUT_LENGTH = 200;
@@ -82,8 +83,8 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
8283

8384
String question = event.getValue(QUESTION_INPUT).getAsString();
8485

85-
Optional<String> chatgptResponse =
86-
chatGptService.ask(question, "You may use markdown syntax for the response");
86+
Optional<String> chatgptResponse = chatGptService.ask(question,
87+
"You may use markdown syntax for the response", CHAT_GPT_MODEL);
8788
if (chatgptResponse.isPresent()) {
8889
userIdToAskedAtCache.put(event.getMember().getId(), Instant.now());
8990
}
@@ -96,7 +97,8 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
9697
String response = chatgptResponse.orElse(errorResponse);
9798
SelfUser selfUser = event.getJDA().getSelfUser();
9899

99-
MessageEmbed responseEmbed = helper.generateGptResponseEmbed(response, selfUser, question);
100+
MessageEmbed responseEmbed =
101+
helper.generateGptResponseEmbed(response, selfUser, question, CHAT_GPT_MODEL);
100102

101103
event.getHook().sendMessageEmbeds(responseEmbed).queue();
102104
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.togetherjava.tjbot.features.chatgpt;
2+
3+
import com.openai.models.ChatModel;
4+
5+
/**
6+
* Logical abstraction over OpenAI chat models.
7+
* <p>
8+
* This enum allows the application to select models based on performance/quality intent rather than
9+
* hard-coding specific OpenAI model versions throughout the codebase.
10+
*
11+
*/
12+
public enum ChatGptModel {
13+
/**
14+
* Fastest response time with the lowest computational cost.
15+
*/
16+
FASTEST(ChatModel.GPT_3_5_TURBO),
17+
18+
/**
19+
* Balanced option between speed and quality.
20+
*/
21+
FAST(ChatModel.GPT_4_1_MINI),
22+
23+
/**
24+
* Highest quality responses with increased reasoning capability.
25+
*/
26+
HIGH_QUALITY(ChatModel.GPT_5_MINI);
27+
28+
private final ChatModel chatModel;
29+
30+
ChatGptModel(ChatModel chatModel) {
31+
this.chatModel = chatModel;
32+
}
33+
34+
/**
35+
* @return the underlying OpenAI model used by this enum.
36+
*/
37+
public ChatModel toChatModel() {
38+
return chatModel;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return chatModel.toString();
44+
}
45+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.openai.client.OpenAIClient;
44
import com.openai.client.okhttp.OpenAIOkHttpClient;
5-
import com.openai.models.ChatModel;
65
import com.openai.models.responses.Response;
76
import com.openai.models.responses.ResponseCreateParams;
87
import com.openai.models.responses.ResponseOutputText;
@@ -51,11 +50,12 @@ public ChatGptService(Config config) {
5150
* @param question The question being asked of ChatGPT. Max is {@value MAX_TOKENS} tokens.
5251
* @param context The category of asked question, to set the context(eg. Java, Database, Other
5352
* etc).
53+
* @param chatModel The AI model to use for this request.
5454
* @return response from ChatGPT as a String.
5555
* @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
5656
* Tokens</a>.
5757
*/
58-
public Optional<String> ask(String question, @Nullable String context) {
58+
public Optional<String> ask(String question, @Nullable String context, ChatGptModel chatModel) {
5959
if (isDisabled) {
6060
return Optional.empty();
6161
}
@@ -76,7 +76,7 @@ public Optional<String> ask(String question, @Nullable String context) {
7676
String response = null;
7777
try {
7878
ResponseCreateParams params = ResponseCreateParams.builder()
79-
.model(ChatModel.GPT_5_NANO)
79+
.model(chatModel.toChatModel())
8080
.input(inputPrompt)
8181
.maxOutputTokens(MAX_TOKENS)
8282
.build();

application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.togetherjava.tjbot.db.generated.tables.HelpThreads;
2626
import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord;
2727
import org.togetherjava.tjbot.features.chatgpt.ChatGptCommand;
28+
import org.togetherjava.tjbot.features.chatgpt.ChatGptModel;
2829
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;
2930
import org.togetherjava.tjbot.features.componentids.ComponentIdInteractor;
3031
import org.togetherjava.tjbot.features.utils.Guilds;
@@ -55,6 +56,7 @@
5556
*/
5657
public final class HelpSystemHelper {
5758
private static final Logger logger = LoggerFactory.getLogger(HelpSystemHelper.class);
59+
private static final ChatGptModel CHAT_GPT_MODEL = ChatGptModel.FAST;
5860

5961
static final Color AMBIENT_COLOR = new Color(255, 255, 165);
6062

@@ -143,7 +145,7 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
143145
String context =
144146
"Category %s on a Java Q&A discord server. You may use markdown syntax for the response"
145147
.formatted(matchingTag.getName());
146-
chatGptAnswer = chatGptService.ask(question, context);
148+
chatGptAnswer = chatGptService.ask(question, context, CHAT_GPT_MODEL);
147149

148150
if (chatGptAnswer.isEmpty()) {
149151
return useChatGptFallbackMessage(threadChannel);
@@ -168,7 +170,8 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
168170
answer = answer.substring(0, responseCharLimit);
169171
}
170172

171-
MessageEmbed responseEmbed = generateGptResponseEmbed(answer, selfUser, originalQuestion);
173+
MessageEmbed responseEmbed =
174+
generateGptResponseEmbed(answer, selfUser, originalQuestion, CHAT_GPT_MODEL);
172175
return post.flatMap(_ -> threadChannel.sendMessageEmbeds(responseEmbed)
173176
.addActionRow(generateDismissButton(componentIdInteractor, messageId.get())));
174177
}
@@ -178,11 +181,13 @@ RestAction<Message> constructChatGptAttempt(ThreadChannel threadChannel,
178181
*
179182
* @param answer The response text generated by AI.
180183
* @param selfUser The SelfUser representing the bot.
181-
* @param title The title for the MessageEmbed.
184+
* @param title The title for the MessageEmbed
185+
* @param model The AI model that was used for the foot notes
182186
* @return A MessageEmbed that contains response generated by AI.
183187
*/
184-
public MessageEmbed generateGptResponseEmbed(String answer, SelfUser selfUser, String title) {
185-
String responseByGptFooter = "- AI generated response";
188+
public MessageEmbed generateGptResponseEmbed(String answer, SelfUser selfUser, String title,
189+
ChatGptModel model) {
190+
String responseByGptFooter = "- AI generated response using %s model".formatted(model);
186191

187192
int embedTitleLimit = MessageEmbed.TITLE_MAX_LENGTH;
188193
String capitalizedTitle = Character.toUpperCase(title.charAt(0)) + title.substring(1);

application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.togetherjava.tjbot.features.BotCommandAdapter;
3232
import org.togetherjava.tjbot.features.CommandVisibility;
3333
import org.togetherjava.tjbot.features.MessageContextCommand;
34+
import org.togetherjava.tjbot.features.chatgpt.ChatGptModel;
3435
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;
3536
import org.togetherjava.tjbot.features.utils.StringDistances;
3637

@@ -98,7 +99,8 @@ public void onMessageContext(MessageContextInteractionEvent event) {
9899
String chatGptTitleRequest =
99100
"Summarize the following question into a concise title or heading not more than 5 words, remove quotations if any: %s"
100101
.formatted(originalMessage);
101-
Optional<String> chatGptTitle = chatGptService.ask(chatGptTitleRequest, null);
102+
Optional<String> chatGptTitle =
103+
chatGptService.ask(chatGptTitleRequest, null, ChatGptModel.FASTEST);
102104
String title = chatGptTitle.orElse(createTitle(originalMessage));
103105
if (title.startsWith("\"") && title.endsWith("\"")) {
104106
title = title.substring(1, title.length() - 1);

0 commit comments

Comments
 (0)