How to use Spring to extract complex structural data #1122
Replies: 3 comments 1 reply
-
Do you have a Java object corresponding to that JSON schema, or do you need to pass the schema explicitly? I have an example here for the "structured data extraction" use case, in case it might help: https://github.com/ThomasVitale/llm-apps-java-spring-ai/blob/main/00-use-cases/structured-data-extraction/src/main/java/com/thomasvitale/ai/spring/StructuredDataExtractionService.java Incidentally, it's also healthcare related. You can find info about how to run the app locally here. Once the app is running, you can call it as follows:
The model will extract the data into a structured patient journal object. This is the prompt I used, which is then enhanced by Spring AI with instructions and info about the JSON schema for the object used as the return type.
A couple of things to consider for this use case:
|
Beta Was this translation helpful? Give feedback.
-
@ThomasVitale |
Beta Was this translation helpful? Give feedback.
-
The model is unable to return correct results when dealing with time-related queries. According to my research, the model lacks a reference date. I've tried providing today's date as a reference in the prompt, but it still returns incorrect values. Do you have any good solutions for this? private static String getFormat(String jsonSchema) {
String template =
"Extract structured data from the provided text.\n"
+ "Your response should be in JSON format.\n"
+ "If you do not know the value of a field asked to extract,\n"
+ "do not include any value for the field in the result.\n"
+ "If you cannot extract the value of any field, return an empty JSON directly.\n"
+ "Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.\n"
+ "Do not include markdown code blocks in your response.\n"
+ "Remove the ```json markdown from the output.\n"
+ "Here is the JSON Schema instance your output must adhere to:\n"
+ "```%s```\n";
return String.format(template, jsonSchema);
}
@PostMapping("/chat")
public String chat(String content){
SystemMessage systemMessage = new SystemMessage(getFormat("{\"$schema\":\"https://json-schema.org/draft/2020-12/schema \",\"type\":\"object\",\"properties\":{\"bck03\":{\"type\":\"string\",\"description\":\"Ward Name\"},\"bcq04b\":{\"type\":\"number\",\"description\":\"Bed number\"},\"diastolicPressValue\":{\"type\":\"number\",\"description\":\"Diastolic blood pressure\"},\"pulseValue\":{\"type\":\"number\",\"description\":\"Pulse\"},\"recordDate\":{\"type\":\"string\",\"description\":\"Date\",\"format\":\"yyyy-MM-dd\"},\"recordTime\":{\"type\":\"string\",\"description\":\"Time\",\"format\":\"hh:mm:ss\"},\"systolicPressValue\":{\"type\":\"number\",\"description\":\"Systolic pressure\"},\"tempValue\":{\"type\":\"number\",\"description\":\"Body temperature\"}}}\n"));
return ChatClient.create(chatModel).prompt()
.messages(systemMessage)
.user(u -> u.text("""
---------------------
TEXT:
{text}
---------------------
""").param("text", content))
.call()
.content();
}
``` |
Beta Was this translation helpful? Give feedback.
-
Seeking advice from experts, my requirement is to have the large model extract data according to the given JSON schema. I have tried many solutions and consulted a lot of information, but have not been able to achieve the desired results. I hope that experienced experts can share their experience with me
Here is my example
Prompt:
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
Beta Was this translation helpful? Give feedback.
All reactions