Core model interfaces and abstractions for Spring AI framework providing portable API for chat, embeddings, images, audio, and tool calling across multiple AI providers
Spring AI Model provides fundamental abstractions and interfaces for building AI-powered applications with Spring Boot. Offers a portable API across multiple AI providers (OpenAI, Azure, Anthropic, Google, AWS Bedrock, etc.) with synchronous and streaming support.
org.springframework.ai:spring-ai-model:1.1.2<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model</artifactId>
<version>1.1.2</version>
</dependency>@Autowired
private ChatModel chatModel;
// Simple call
String response = chatModel.call("What is AI?");
// Streaming
Flux<ChatResponse> stream = chatModel.stream(new Prompt("Explain ML"));
stream.subscribe(chunk -> System.out.print(chunk.getResult().getOutput().getText()));public interface Model<TReq extends ModelRequest<?>, TRes extends ModelResponse<?>> {
TRes call(TReq request);
}
public interface StreamingModel<TReq extends ModelRequest<?>, TResChunk extends ModelResponse<?>> {
Flux<TResChunk> stream(TReq request);
}All AI modalities (chat, embeddings, images, audio) follow this consistent pattern.
| Capability | Interface | Purpose |
|---|---|---|
| Chat | ChatModel | Conversational AI |
| Embeddings | EmbeddingModel | Vector generation |
| Images | ImageModel | Text-to-image |
| Transcription | TranscriptionModel | Speech-to-text |
| TTS | TextToSpeechModel | Text-to-audio |
| Moderation | ModerationModel | Content safety |
| Tools | ToolCallback | Function calling |
| Memory | ChatMemory | Conversation history |
| Enrichment | DocumentTransformer | AI-powered metadata |
| Observability | ObservationContext | Monitoring |
Conversational AI with messages, prompts, and streaming.
public interface ChatModel extends Model<Prompt, ChatResponse>, StreamingChatModel {
String call(String message);
ChatResponse call(Prompt prompt);
Flux<ChatResponse> stream(Prompt prompt);
}Message types for conversations: UserMessage, SystemMessage, AssistantMessage, ToolResponseMessage.
public class UserMessage extends AbstractMessage implements MediaContent {
public UserMessage(String content);
public UserMessage(String content, List<Media> media);
}Template-based prompt creation with variable substitution.
public class PromptTemplate implements PromptTemplateActions {
public PromptTemplate(String template);
public Prompt create(Map<String, Object> model);
}Conversation history management.
public interface ChatMemory {
void add(String conversationId, Message message);
List<Message> get(String conversationId);
void clear(String conversationId);
}Vector generation for semantic search.
public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> {
float[] embed(String text);
List<float[]> embed(List<String> texts);
int dimensions();
}Text-to-image generation.
public interface ImageModel extends Model<ImagePrompt, ImageResponse> {
ImageResponse call(ImagePrompt request);
}Speech-to-text conversion.
public interface TranscriptionModel extends Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {
String transcribe(Resource audioResource);
}Reference: Audio Transcription →
Text-to-audio generation.
public interface TextToSpeechModel extends Model<TextToSpeechPrompt, TextToSpeechResponse> {
byte[] call(String text);
}Content safety checking.
public interface ModerationModel extends Model<ModerationPrompt, ModerationResponse> {
ModerationResponse call(ModerationPrompt request);
}Enable AI to invoke Java methods.
@Tool(description = "Get weather for a city")
public String getWeather(@ToolParam(description = "City") String city) {
return "Weather data";
}Convert text to Java objects.
public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
public BeanOutputConverter(Class<T> clazz);
public T convert(String text);
}Track tokens, rate limits, and model info.
public interface Usage {
Integer getPromptTokens();
Integer getCompletionTokens();
Integer getTotalTokens();
}Micrometer integration for monitoring.
Advanced tool configuration.
public interface ToolCallingChatOptions extends ChatOptions {
List<ToolCallback> getToolCallbacks();
Boolean getInternalToolExecutionEnabled();
}Reference: Tool Calling Options →
AI-powered keyword and summary extraction.
public class KeywordMetadataEnricher implements DocumentTransformer {
public KeywordMetadataEnricher(ChatModel chatModel, int keywordCount);
}Reference: Document Enrichment →
JSON parsing, schema generation, and support utilities.
Reference: Core Abstractions →
@Configuration
public class AiConfig {
@Bean @Qualifier("openai")
public ChatModel openAiModel(OpenAiApi api) { return new OpenAiChatModel(api); }
@Bean @Qualifier("anthropic")
public ChatModel anthropicModel(AnthropicApi api) { return new AnthropicChatModel(api); }
}try {
ChatResponse response = chatModel.call(prompt);
} catch (Exception e) {
// Handle errors
}import org.springframework.ai.model.*;
import org.springframework.ai.chat.model.*;
import org.springframework.ai.chat.prompt.*;
import org.springframework.ai.chat.messages.*;
import org.springframework.ai.embedding.*;
import org.springframework.ai.image.*;
import org.springframework.ai.audio.transcription.*;
import org.springframework.ai.audio.tts.*;
import org.springframework.ai.tool.*;
import org.springframework.ai.tool.annotation.*;
import org.springframework.ai.model.tool.*;