TheoKanning / openai-java

OpenAI Api Client in Java
MIT License
4.75k stars 1.19k forks source link

getContent() returns a string with JSON content even when not asked to format it to JSON. #476

Closed breisamoralde closed 7 months ago

breisamoralde commented 7 months ago

I have this code:

  private String buildQuery(){
    OpenAiService service = new OpenAiService(openAIApiKey, Duration.ofSeconds(60));
    List<ChatMessage> messages = new ArrayList<>();
    ChatMessage systemMessage = new ChatMessage(ChatMessageRole.USER.value(), "test message in 10 words");
    messages.add(systemMessage);
    ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
        .builder()
        .model(openAIModel)
        .messages(messages)
        .n(1)
        .build();
    ChatCompletionResult result = service.createChatCompletion(chatCompletionRequest);
    service.shutdownExecutor();
    return result.getChoices().get(0).getMessage().getContent();
  }

The value this returns is:

"{\n\t\"message\": \"This is a test message with 10 words in it.\"\n}"

Why is it in JSON format? I try to change the prompt to something else, and it gave me better answers, but this one in particular gave JSON results even when not asked to. I also notice, surrounding a message with double quotes gave JSON results.