From 37d4f6a2af28dc1994f3d17d1d54a2fd67cf3767 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Date: Sat, 14 Sep 2024 18:37:50 +0100 Subject: [PATCH] Update ChatGptService to allow for generic questions without a context --- .../features/chatgpt/ChatGptService.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java index e8b02d04bb..0a0559d569 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java @@ -12,7 +12,6 @@ import java.time.Duration; import java.util.List; -import java.util.Objects; import java.util.Optional; /** @@ -95,32 +94,32 @@ public Optional ask(String question, String context) { if (isDisabled) { return Optional.empty(); } + String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS"; + String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s" + .formatted(context, instructions, question); + return ask(questionWithContext); + } + /** + * Prompt ChatGPT with a question and receive a response. + * + * @param question The question being asked of ChatGPT. Max is {@value MAX_TOKENS} tokens. + * @return response from ChatGPT as a String. + * @see ChatGPT + * Tokens. + */ + public Optional ask(String question) { try { - String instructions = "KEEP IT CONCISE, NOT MORE THAN 280 WORDS"; - String questionWithContext = "context: Category %s on a Java Q&A discord server. %s %s" - .formatted(context, instructions, question); - ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), - Objects.requireNonNull(questionWithContext)); - ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() - .model(AI_MODEL) - .messages(List.of(chatMessage)) - .frequencyPenalty(FREQUENCY_PENALTY) - .temperature(TEMPERATURE) - .maxTokens(MAX_TOKENS) - .n(MAX_NUMBER_OF_RESPONSES) - .build(); - + ChatCompletionRequest chatCompletionRequest = + chatCompletionRequest(new ChatMessage(ChatMessageRole.USER.value(), question)); String response = openAiService.createChatCompletion(chatCompletionRequest) .getChoices() .getFirst() .getMessage() .getContent(); - if (response == null) { return Optional.empty(); } - return Optional.of(response); } catch (OpenAiHttpException openAiHttpException) { logger.warn( @@ -133,4 +132,15 @@ public Optional ask(String question, String context) { } return Optional.empty(); } + + private static ChatCompletionRequest chatCompletionRequest(ChatMessage chatMessage) { + return ChatCompletionRequest.builder() + .model(AI_MODEL) + .messages(List.of(chatMessage)) + .frequencyPenalty(FREQUENCY_PENALTY) + .temperature(TEMPERATURE) + .maxTokens(MAX_TOKENS) + .n(MAX_NUMBER_OF_RESPONSES) + .build(); + } }