Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring AI Demo (OpenAI) — Gradle, Spring Boot 3

A minimal Spring Boot 3.5 app showcasing Spring AI ChatClient with OpenAI, prompt templates, and structured output conversion.

Requirements

  • Java 17+ (toolchain configured in build.gradle)
  • Gradle Wrapper (use provided gradlew.bat on Windows)
  • An OpenAI-compatible API key

Quick Start (Windows PowerShell)

  1. Clone and enter the project directory.
  2. Set environment variables (temporary for current session):
$env:OPENAI_API_KEY = "your_openai_api_key"
# Optional: custom base URL (e.g., for Azure/OpenAI-compatible gateways)
$env:OPENAI_API_BASE_URL = "https://api.openai.com/v1"

To persist for future sessions (requires new PowerShell after running):

setx OPENAI_API_KEY "your_openai_api_key"
setx OPENAI_API_BASE_URL "https://api.openai.com/v1"
  1. Run the app:
./gradlew.bat bootRun

The app reads defaults from src/main/resources/application.properties.

Configuration

Key properties (override via environment variables shown in braces):

  • spring.ai.openai.api-key=${OPENAI_API_KEY}
  • spring.ai.openai.api-base-url=${OPENAI_API_BASE_URL:https://api.openai.com/v1}
  • spring.ai.openai.default-model=gpt-5-nano
  • spring.ai.openai.timeout=30s
  • spring.ai.openai.max-tokens=2048

You can also package a runnable JAR:

./gradlew.bat clean build
java -jar build/libs/test-openai-0.0.1-SNAPSHOT.jar

REST Endpoints

  • ChatController

    • GET /ai?message=... — simple text completion
    • POST /chat?message=... — simple text completion (POST)
    • GET /stream?message=... — server-side streaming text
  • PromptController

    • GET /ai/prompt?message=... — send raw prompt
    • GET /ai/books/popular?genre=... — recommended books via inline template
    • GET /ai/test/system-prompt?message=... — uses system + user messages
    • GET /ai/books/popular/prompt-template?genre=... — uses prompts/books.st
  • OutputConverterController (structured output)

    • GET /ai/movies/artist/{actor} — string content
    • GET /ai/movies/artist/{actor}/listList<String> using ListOutputConverter
    • GET /ai/movies/artist/{actor}/mapMap<String,Object> via MapOutputConverter
    • GET /ai/movies/artist/{actor}/objectMovie object via BeanOutputConverter
  • OutputFluentController (fluent API + entity mapping)

    • GET /ai/movies-string — string content (Tom Hanks)
    • GET /ai/moviesFilmography entity
    • GET /ai/movies-listList<Filmography> for multiple actors
    • GET /ai/movies-by-actor?actor=...Filmography for a given actor
  • PromptStuffController (prompt template + optional stuffing)

    • GET /ai/{city}/temprature?stuff=true|false — uses prompts/temprature.st and optionally includes example context from prompts/temprature-example.txt

Static pages for quick manual testing:

  • GET /index.html
  • GET /stream-index.html (basic streaming demo)

Example Requests (PowerShell + curl)

curl "http://localhost:8080/ai?message=Hello%20there"

curl -X POST "http://localhost:8080/chat?message=Tell%20me%20a%20joke"

curl "http://localhost:8080/stream?message=Write%20a%20short%20poem" --no-buffer

curl "http://localhost:8080/ai/books/popular?genre=fantasy"

curl "http://localhost:8080/ai/movies/artist/Tom%20Hanks/list"

curl "http://localhost:8080/ai/movies-by-actor?actor=Leonardo%20DiCaprio"

curl "http://localhost:8080/ai/London/temprature?stuff=true"

Project Structure

  • controller — REST endpoints using Spring AI ChatClient
  • dtoMovie, Filmography models used by converters/entity mapping
  • resources/prompts — prompt template files consumed by controllers

Testing

Run unit tests:

./gradlew.bat test

Notes

  • This project uses Spring AI BOM and spring-ai-starter-model-openai. To switch providers, add the relevant starter and properties.
  • Windows users should prefer ./gradlew.bat over gradlew.

About

Spring AI with LLM integration

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages