Skip to content

Spring AI ​

Spring AI's OpenAI chat module.

Connect ​

application.properties. PLUGBOARD_BASE_URL is http://127.0.0.1:4010/v1 and PLUGBOARD_KEY is your plugboard key:

properties
spring.ai.openai.api-key=${PLUGBOARD_KEY}
spring.ai.openai.base-url=${PLUGBOARD_BASE_URL}
spring.ai.openai.chat.options.model=fast

base-url must end in /v1 (Spring AI 2.x uses the official OpenAI Java SDK, which appends /chat/completions). Without /v1 every call is a 404.

Chat ​

Set the model per call with .options(OpenAiChatOptions.builder().model("fast")). Model names with / or # need no escaping.

java
ChatResponse response = chat.prompt().user(PROMPT).options(model(model)).call().chatResponse();

Tool calls ​

Spring AI runs the tool and sends the result back before it answers.

java
@Tool(name = "get_weather", description = "Current weather for a city.")
String getWeather(@ToolParam(description = "The city") String city) {
  cities.add(city);
  return "{\"temp_c\": 18, \"sky\": \"cloudy\", \"city\": \"" + city + "\"}";
}
java
ChatResponse response = chat.prompt().user(TOOL_PROMPT).tools(tools).options(model(model)).call().chatResponse();

Streaming ​

java
List<ChatResponse> chunks = chat.prompt().user(PROMPT).options(model(model)).stream().chatResponse().collectList().block();

The runnable app is in examples/spring-ai/.

What works ​

Chat, streaming (with usage), and @Tool methods, which Spring AI runs itself before it answers.

Limits ​

  • When the model reasons, set a token limit high enough for it to answer (see Spring AI's Token Limit Parameters for which property applies).