Powered by Blogger.

Spring AI Part-1

1 comment :

Spring AI Tutorial – Part 1

Build a local support-ticket draft API with Java and Spring Boot.

By A143MK  |  Part 1  |  Spring AI & Enterprise Example

1What Is Spring AI?

Spring AI helps Java and Spring applications integrate with AI models and related capabilities. A ChatClient sends a prompt to a model and receives its response.

This part uses Spring AI 1.1.8, Spring Boot 3.5.16 and Java 17. Spring AI 1.1.x supports Spring Boot 3.4.x and 3.5.x.

Official references: Spring AI setup and Ollama integration.

2Enterprise Use Case

A support agent sends a short, fictional issue. The API asks a local model for a polite response draft; an agent must review and edit it before sending.

Client --POST JSON--> Spring Boot API --ChatClient--> Ollama model
Client <--draft JSON-- Spring Boot API <--model response--
Demo, not production: It has no authentication, authorization, rate limiting or business-system integration. Run it only on localhost with fictional data. Do not send real customer data or secrets.

3Prerequisites and Setup

  1. Install Java 17 or later and Maven 3.6.3 or later. The commands below use Windows PowerShell.
  2. Install and start Ollama. If its server is already running, do not start a second server.
  3. Download the model with the next command. This requires internet, sufficient disk space and memory; the download can be large.
java -version
mvn -version
ollama pull llama3.2

The Ollama server address in this example is http://localhost:11434. If it is unavailable, the model call fails.

4Maven Configuration

In an empty project folder, create the following pom.xml. The Spring AI BOM manages the starter version.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.16</version>
    <relativePath/>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>support-assistant</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>17</java.version>
    <spring-ai.version>1.1.8</spring-ai.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-bom</artifactId>
        <version>${spring-ai.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-starter-model-ollama</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

5Spring Boot API

Create src/main/java/com/example/support/SupportApplication.java in the project folder. Requests must not be blank and may contain at most 1,500 characters.

package com.example.support;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@SpringBootApplication
public class SupportApplication {
    public static void main(String[] args) {
        SpringApplication.run(SupportApplication.class, args);
    }
}

@RestController
@RequestMapping("/api/support")
class SupportController {
    private final ChatClient chatClient;

    SupportController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @PostMapping("/draft")
    DraftResponse draft(@Valid @RequestBody DraftRequest request) {
        String reply = chatClient.prompt()
            .system("Draft a polite support reply using only the supplied issue. "
                + "Do not promise refunds or invent company policy. "
                + "Ask for clarification if needed. A human will review the draft.")
            .user(u -> u.text("{ticket}").param("ticket", request.ticket()))
            .call()
            .content();
        if (reply == null || reply.isBlank()) {
            throw new ResponseStatusException(HttpStatus.BAD_GATEWAY,
                "The model returned an empty response");
        }
        return new DraftResponse(reply);
    }

    record DraftRequest(@NotBlank @Size(max = 1500) String ticket) {}
    record DraftResponse(String draft) {}
}

Create src/main/resources/application.properties. The API listens only on loopback; the Ollama model must already be downloaded.

server.address=127.0.0.1
server.port=8080
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=llama3.2

6Run and Test the API

Run this command from the project folder. After startup, send the request from another PowerShell window. Keep the Ollama server running.

mvn spring-boot:run
$body = @{ ticket = "My fictional order has not arrived." } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "http://127.0.0.1:8080/api/support/draft" `
  -ContentType "application/json" -Body $body

A successful request returns JSON with a draft; the exact wording varies by model and run. A blank or oversized ticket returns HTTP 400. If the model is unavailable, the request fails; inspect server logs.

7Live Prompt Preview

This is only a local browser preview of the prompt, not an AI model. It makes no network request. For an actual model response, run the API instructions above. Use fictional text only.

8Production Considerations

  • Add authentication, authorization, rate limiting and secure network configuration before real deployment.
  • Check permissions, retention and provider policies for personal data. Do not put secrets in source code or prompts.
  • Treat user text and model output as untrusted. Defend against prompt injection and retain human review; prompt instructions alone are not a security boundary.
  • Plan for timeouts, failures, monitoring and model quality. Avoid unnecessary logging of sensitive prompts.

9Series Roadmap

Future parts can cover prompt design, structured output, conversation memory, RAG, tool calling, testing, observability and secure deployment using the same support-application context.

10Quick Quiz

Choose an answer for each question and check your score.

1. What does ChatClient do here?
2. What is the browser preview?
3. Are prompt instructions alone a security boundary?

Show answer explanations
  1. A: ChatClient sends the prompt and receives a model response.
  2. B: The preview displays text locally; it does not call a model.
  3. A: Real security requires access controls and other safeguards.

You completed Part 1 of the Spring AI series. Continue learning in the next part. — A143MK

1 comment :

Please Write a Message for Programming or something else 🙏