Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for structured outputs #57

Merged
merged 17 commits into from
Aug 14, 2024

Conversation

bertilmuth
Copy link
Contributor

@bertilmuth bertilmuth commented Aug 13, 2024

This is a resolution to #54.

I tried to stick as close to the OpenAI API as possible.
Note that for the JSON schema generation to work, all properties of the serialized classes must be marked with @NotNull or @JsonProperty(required = true)

Here's a test case that replicates the math tutor example from the OpenAI website:

@Test
  void createChatCompletionWithJsonSchema() throws JsonProcessingException {
      final List<ChatMessage> messages = new ArrayList<>();
      final ChatMessage systemMessage = new SystemMessage("You are a helpful math tutor. Guide the user through the solution step by step.");
      final ChatMessage userMessage = new UserMessage("how can I solve 8x + 7 = -23");
      messages.add(systemMessage);
      messages.add(userMessage);

      Class<MathReasoning> rootClass = MathReasoning.class;
      ChatResponseFormat responseFormat = ChatResponseFormat.jsonSchema(rootClass);
      
      ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
              .builder()
              .model("gpt-4o-2024-08-06")
              .messages(messages)
              .responseFormat(responseFormat)
              .maxTokens(1000)
              .build();

      ChatCompletionChoice choice = service.createChatCompletion(chatCompletionRequest).getChoices().get(0);
      MathReasoning mathReasoning = choice.getMessage().parsed(rootClass);
      
      String finalAnswer = mathReasoning.getFinal_answer();
      assertTrue(finalAnswer.contains("x"));
      assertTrue(finalAnswer.contains("="));
  }
  
  @Data
  @NoArgsConstructor
  private static class MathReasoning {
      @NotNull private List<Step> steps;
      @NotNull private String final_answer;
  }
  
  @Data
  @NoArgsConstructor
  private static class Step {
      @NotNull private String explanation;
      @NotNull private String output;
  }

@Lambdua Lambdua merged commit f4e84eb into Lambdua:main Aug 14, 2024
@bertilmuth bertilmuth deleted the Basic-support-for-structured-outputs branch August 14, 2024 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants