Skip to content

Commit

Permalink
Removing incorrect end comma in jsonStructure for POJO extracting AI …
Browse files Browse the repository at this point in the history
…Services (#894)

## Context
I noted that for more complex POJO object, the JSON returned by the LLM
often contained a last comma before closing bracket, making the JSON
invalid and resulting in an error while parsing it. I traced it back to
the proposed jsonStructure that is part of the SystemMessage, sending a
wrong format to the LLM.

## Change
Removed the last comma that shouldn't be there in the jsonStructure +
updated the unit tests

---------

Co-authored-by: Lize
  • Loading branch information
LizeRaes committed Apr 7, 2024
1 parent b6f3df8 commit a4e82af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private static String jsonStructure(Class<?> structured, Set<Class<?>> visited)
}
jsonSchema.append(format("\"%s\": (%s),\n", name, descriptionFor(field, visited)));
}
jsonSchema.delete(jsonSchema.lastIndexOf(","), jsonSchema.lastIndexOf(",")+1);
jsonSchema.append("}");
return jsonSchema.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void outputFormatInstructions_SimplePerson() {
"\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" +
"\"birthDate\": (type: date string (2023-12-31))\n" +
"}");
}

Expand All @@ -43,7 +43,7 @@ void outputFormatInstructions_PersonWithFirstNameList() {
"\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" +
"\"birthDate\": (type: date string (2023-12-31))\n" +
"}");
}

Expand All @@ -61,7 +61,7 @@ void outputFormatInstructions_PersonWithFirstNameArray() {
"\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" +
"\"birthDate\": (type: date string (2023-12-31))\n" +
"}");
}

Expand All @@ -79,7 +79,7 @@ void outputFormatInstructions_PersonWithJavaType() {
"\nYou must answer strictly in the following JSON format: {\n" +
"\"firstName\": (type: string),\n" +
"\"lastName\": (type: string),\n" +
"\"birthDate\": (type: java.util.Calendar),\n" +
"\"birthDate\": (type: java.util.Calendar)\n" +
"}");
}

Expand All @@ -98,7 +98,7 @@ void outputFormatInstructions_PersonWithStaticFinalField() {
"\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" +
"\"birthDate\": (type: date string (2023-12-31))\n" +
"}");
}

Expand Down Expand Up @@ -127,8 +127,8 @@ void outputFormatInstructions_PersonWithNestedObject() {
"\"address\": (type: dev.langchain4j.service.ServiceOutputParserTest$Address: {\n" +
"\"streetNumber\": (type: integer),\n" +
"\"street\": (type: string),\n" +
"\"city\": (type: string),\n" +
"}),\n" +
"\"city\": (type: string)\n" +
"})\n" +
"}");
}

Expand All @@ -151,8 +151,8 @@ void outputFormatInstructions_PersonWithNestedObjectList() {
"\"address\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$Address: {\n" +
"\"streetNumber\": (type: integer),\n" +
"\"street\": (type: string),\n" +
"\"city\": (type: string),\n" +
"}),\n" +
"\"city\": (type: string)\n" +
"})\n" +
"}");
}

Expand All @@ -175,8 +175,8 @@ void outputFormatInstructions_PersonWithNestedObjectArray() {
"\"address\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$Address: {\n" +
"\"streetNumber\": (type: integer),\n" +
"\"street\": (type: string),\n" +
"\"city\": (type: string),\n" +
"}),\n" +
"\"city\": (type: string)\n" +
"})\n" +
"}");
}

Expand All @@ -200,7 +200,7 @@ void outputFormatInstructions_PersonWithFinalFields() {
"\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" +
"\"birthDate\": (type: date string (2023-12-31))\n" +
"}");
}

Expand All @@ -221,8 +221,8 @@ void outputFormatInstructions_PersonWithParents() {
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParents: {\n" +
"\"firstName\": (type: string),\n" +
"\"lastName\": (type: string),\n" +
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParents),\n" +
"}),\n" +
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParents)\n" +
"})\n" +
"}");
}

Expand All @@ -243,8 +243,8 @@ void outputFormatInstructions_PersonWithParentArray() {
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParentArray: {\n" +
"\"firstName\": (type: string),\n" +
"\"lastName\": (type: string),\n" +
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParentArray),\n" +
"}),\n" +
"\"parents\": (type: array of dev.langchain4j.service.ServiceOutputParserTest$PersonWithParentArray)\n" +
"})\n" +
"}");
}

Expand All @@ -267,9 +267,9 @@ void outputFormatInstructions_PersonWithMotherAndFather() {
"\"firstName\": (type: string),\n" +
"\"lastName\": (type: string),\n" +
"\"mother\": (type: dev.langchain4j.service.ServiceOutputParserTest$PersonWithMotherAndFather),\n" +
"\"father\": (type: dev.langchain4j.service.ServiceOutputParserTest$PersonWithMotherAndFather),\n" +
"\"father\": (type: dev.langchain4j.service.ServiceOutputParserTest$PersonWithMotherAndFather)\n" +
"}),\n" +
"\"father\": (type: dev.langchain4j.service.ServiceOutputParserTest$PersonWithMotherAndFather),\n" +
"\"father\": (type: dev.langchain4j.service.ServiceOutputParserTest$PersonWithMotherAndFather)\n" +
"}");
}

Expand Down

0 comments on commit a4e82af

Please sign in to comment.