-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR enhance ServiceOutPutParser allowing outputFormatInstructions to document nested objects in jsonStructure. Integration tests have been modified to add a nested address object. Maybe a dedicated test would be better?
- Loading branch information
1 parent
525968d
commit 4d3e622
Showing
3 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
langchain4j/src/test/java/dev/langchain4j/service/ServiceOutputParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package dev.langchain4j.service; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.Serializable; | ||
import java.time.LocalDate; | ||
import java.util.Calendar; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ServiceOutputParserTest { | ||
|
||
static class Person { | ||
private String firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_SimplePerson() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(Person.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonWithFirstNameList { | ||
private List<String> firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithFirstNameList() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonWithFirstNameList.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: array of string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonWithFirstNameArray { | ||
private String[] firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithFirstNameArray() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonWithFirstNameArray.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: array of string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonWithCalendarDate { | ||
private String firstName; | ||
private String lastName; | ||
private Calendar birthDate; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithJavaType() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonWithCalendarDate.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: java.util.Calendar),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonWithStaticField implements Serializable { | ||
private static final long serialVersionUID = 1234567L; | ||
private String firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithStaticFinalField() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonWithStaticField.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"}"); | ||
} | ||
|
||
static class Address { | ||
private Integer streetNumber; | ||
private String street; | ||
private String city; | ||
} | ||
|
||
static class PersonAndAddress { | ||
private String firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
private Address address; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithNestedObject() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonAndAddress.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"\"address\": (type: {\n" + | ||
"\"streetNumber\": (type: integer),\n" + | ||
"\"street\": (type: string),\n" + | ||
"\"city\": (type: string),\n" + | ||
"}),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonAndAddressList { | ||
private String firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
private List<Address> address; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithNestedObjectList() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonAndAddressList.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"\"address\": (type: array of {\n" + | ||
"\"streetNumber\": (type: integer),\n" + | ||
"\"street\": (type: string),\n" + | ||
"\"city\": (type: string),\n" + | ||
"}),\n" + | ||
"}"); | ||
} | ||
|
||
static class PersonAndAddressArray { | ||
private String firstName; | ||
private String lastName; | ||
private LocalDate birthDate; | ||
private List<Address> address; | ||
} | ||
|
||
@Test | ||
void outputFormatInstructions_PersonWithNestedObjectArray() { | ||
String formatInstructions = ServiceOutputParser.outputFormatInstructions(PersonAndAddressList.class); | ||
|
||
assertThat(formatInstructions).isEqualTo( | ||
"\nYou must answer strictly in the following JSON format: {\n" + | ||
"\"firstName\": (type: string),\n" + | ||
"\"lastName\": (type: string),\n" + | ||
"\"birthDate\": (type: date string (2023-12-31)),\n" + | ||
"\"address\": (type: array of {\n" + | ||
"\"streetNumber\": (type: integer),\n" + | ||
"\"street\": (type: string),\n" + | ||
"\"city\": (type: string),\n" + | ||
"}),\n" + | ||
"}"); | ||
} | ||
} |