Quick Start
This article describes a simple agent created using Spring AI. This agent uses a system text to set its persona.
The complete source is available on GitHub JavaAIDev/simple-ai-agent.
The quickest way to get started is using Spring Initializr. Go to Spring Initializr to create a new Spring Boot project. It is a Maven project with Java 21 and Spring Boot 3.4.2
. The added dependencies are OpenAI and Web.
- OpenAI Spring Boot starter adds support for interacting with OpenAI models.
- Web Spring Boot starter adds support for building REST APIs.
Download the created project and open it using IntelliJ IDEA. Now we can start building AI applications.
We can also manually add dependencies for those Spring Boot starters.
REST Controller
Here I'll create a simple agent to provide cooking suggestions. In the prompt sent to LLM, a system text is included to set the persona of this agent.
ChatClient
is the entry point of interacting with LLMs. A ChatClient
sends prompts to LLMs and receives responses. ChatClient
s are built using ChatClient.Builder
. After adding the Spring Boot starter for an AI service, we'll have ChatClient.Builder
and other beans auto-configured.
We can use the prompt
method of ChatClient
to build the prompt, including user text and system text. The call
method sends the prompt to an LLM and receives its response. The content
method simply returns the response content.
package com.javaaidev.agent;
import org.springframework.ai.chat.client.ChatClient;
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;
@RestController
@RequestMapping("/chat")
public class ChatAgentController {
public record ChatRequest(String input) {
}
public record ChatResponse(String output) {
}
private static final String SYSTEM_TEXT = "You are a chef who is proficient in various cuisines. Please answer users' questions about cooking.";
private final ChatClient chatClient;
public ChatAgentController(ChatClient.Builder builder) {
chatClient = builder.build();
}
@PostMapping
public ChatResponse chat(@RequestBody ChatRequest request) {
var output = chatClient.prompt().system(SYSTEM_TEXT)
.user(request.input())
.call()
.content();
return new ChatResponse(output);
}
}
An OpenAI API key is required to run this agent. In the configuration below, OpenAI API key is read from environment variable OPENAI_API_KEY
. The model is set to gpt-3.5-turbo
.
spring:
application:
name: simple-ai-agent
threads.virtual.enabled: true
ai:
openai:
api-key: ${OPENAI_API_KEY:}
chat:
options:
model: gpt-3.5-turbo
temperature: 0.2
Web UI
After starting the application, we can use any REST client to test the API. Or we can use a simple UI. After adding its dependency, we can access the web UI at http://localhost:8080/webjars/chat-agent-ui/index.html
.
<dependency>
<groupId>com.javaaidev</groupId>
<artifactId>chat-agent-ui</artifactId>
<version>0.5.1</version>
</dependency>
Test
The screenshot below shows the UI of this simple agent.