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

Structured output #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ build/

### VS Code ###
.vscode/

### macOS ###
.DS_Store
2 changes: 2 additions & 0 deletions models/chat/strucured-output/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/mvnw text eol=lf
*.cmd text eol=crlf
33 changes: 33 additions & 0 deletions models/chat/strucured-output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
19 changes: 19 additions & 0 deletions models/chat/strucured-output/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
140 changes: 140 additions & 0 deletions models/chat/strucured-output/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Spring AI Structured Output Demo

Transform AI responses into type-safe Java objects effortlessly with Spring AI's structured output capabilities. This project demonstrates how to convert natural language responses from Large Language Models (LLMs) into strongly-typed Java objects without writing complex parsing logic.

## Why Structured Output Matters

Working with AI models typically means dealing with unstructured text responses. Converting these responses into usable data structures traditionally requires:

- Writing custom parsing logic
- Handling edge cases and malformed responses
- Maintaining complex regex patterns or string manipulation code
- Implementing error handling for parsing failures

Spring AI eliminates these challenges by providing built-in conversion capabilities that transform AI responses directly into Java objects. This project showcases this functionality through a practical example: converting information about technical authors into structured data.

## Project Overview

This application demonstrates structured output conversion by:
1. Taking user input for a technical author's name
2. Querying an AI model for information about the author
3. Automatically converting the response into a strongly-typed `AuthorInfo` object
4. Displaying the structured information to the user

## Requirements

- Java 17 or higher
- OpenAI API key
- Spring Boot 3.2 or higher
- Spring AI dependencies

## Project Structure

```
src/
├── main/
│ ├── java/
│ │ └── io/spring/examples/
│ │ ├── Application.java # Main application class
│ │ └── AuthorInfo.java # Data structure for author information
│ └── resources/
│ └── application.properties # Configuration properties
```

## Key Features

### Structured Data Model

The `AuthorInfo` record defines our structured output format:

```java
public record AuthorInfo(
String name,
String primaryGenre,
List<String> notableBooks,
int yearsActive,
String specialization
) {}
```

### Automatic Conversion

Spring AI handles the conversion from AI response to Java object automatically:

```java
var authorInfo = chat.prompt()
.user(u -> {
u.text("Please give me information about ${author}");
u.param("author", authorName);
})
.call()
.entity(AuthorInfo.class); // Automatic conversion to AuthorInfo
```

## Getting Started

1. Set your OpenAI API key:
```bash
export OPENAI_API_KEY=your-api-key-here
```

2. Run the application:
```bash
./mvnw spring-boot:run
```

3. Enter author names when prompted:
```
Who is your favorite technical author? (e.g., 'Rod Johnson', or type 'exit' to quit)
USER: Rod Johnson
```

## Response Format

The application displays structured information about the author:

```
=== Author Analysis ===
Name: Rod Johnson
Primary Genre: Technical Writing
Notable Books: Expert One-on-One J2EE Design and Development, Expert One-on-One J2EE Development without EJB
Years Active: 20
Specialization: Enterprise Java Development and Spring Framework
```

## Technical Details

### Configuration

The application uses Spring Boot's configuration properties:

```properties
spring.application.name=structured-output
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4
```

### Error Handling

Spring AI's structured output converter handles various edge cases:
- Malformed AI responses
- Missing fields in the response
- Type conversion errors

## Benefits of Using Spring AI's Structured Output

1. **Type Safety**: Get compile-time checking for your AI response structures
2. **Clean Code**: Eliminate boilerplate parsing code
3. **Reliability**: Robust handling of edge cases and malformed responses
4. **Maintainability**: Changes to response structure only require updating the data model
5. **Integration**: Seamless integration with Spring's ecosystem

## Learn More

For more information about Spring AI and structured output conversion, visit:
- [Spring AI Documentation](https://docs.spring.io/spring-ai/reference/)
- [Spring AI GitHub Repository](https://github.com/spring-projects/spring-ai)

---

Built with ❤️ using Spring AI
Loading