许多 LLM 和 LLM 提供商支持以结构化格式(通常是 JSON)生成输出。这些输出可以轻松映射到 Java 对象,并在应用程序的其他部分使用。
例如,假设我们有一个 Person 类:
1 | record Person(String name, int age, double height, boolean married) { |
我们的目标是像下面这样的非结构化文本中提取 Person 对象:
John is 42 years old and lives an independent life.
He stands 1.75 meters tall and carries himself with confidence.
Currently unmarried, he enjoys the freedom to focus on his personal goals and interests.
目前,根据 LLM 和 LLM 提供商的不同,有三种方式可以实现这一目标(从最可靠到最不可靠):
- JSON Schema
- Prompting + JSON Mode
- Prompting