Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Spring AI Chat Demo

A minimal Spring Boot + Spring AI application that exposes an HTTP endpoint backed by an OpenAI chat model.

Stack

  • Java 21
  • Spring Boot 4.0.1
  • Spring AI 2.0.0
  • Spring Web
  • Spring AI OpenAI ChatClient
  • Maven

Prerequisites

  1. JDK 21 or newer
  2. Maven 3.9+
  3. An OpenAI API key

Set the API key as an environment variable.

macOS / Linux

export OPENAI_API_KEY="your-api-key"

Windows PowerShell

$env:OPENAI_API_KEY="your-api-key"

Do not commit the API key to Git.

Run

mvn clean spring-boot:run

Test

Open:

http://localhost:8080/ai/ask?question=Explain%20Kafka%20consumer%20groups

Or with curl:

curl "http://localhost:8080/ai/ask?question=Explain%20Kafka%20consumer%20groups"

How it works

HTTP Request
     |
     v
AiController
     |
     v
AiService
     |
     v
Spring AI ChatClient
     |
     v
OpenAI Chat Model
     |
     v
AI Response

The important Spring AI API is:

chatClient
    .prompt()
    .system("...")
    .user(question)
    .call()
    .content();

Next improvements

After this basic example works, the project can be extended with:

  • Prompt templates
  • Structured output
  • Streaming responses
  • Embeddings
  • Vector database
  • RAG
  • Tool/function calling
  • Conversation memory
  • Observability

About

Spring Boot + Spring AI chat demo backed by an OpenAI chat model

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages