Skip to content

Commit

Permalink
Update ChatGptService to allow for generic questions without a context
Browse files Browse the repository at this point in the history
  • Loading branch information
surajkumar committed Sep 14, 2024
1 parent f49d430 commit 37d4f6a
Showing 1 changed file with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -95,32 +94,32 @@ public Optional<String> 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 <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
* Tokens</a>.
*/
public Optional<String> 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(
Expand All @@ -133,4 +132,15 @@ public Optional<String> 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();
}
}

0 comments on commit 37d4f6a

Please sign in to comment.