CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework-ai--spring-ai-model

Core model interfaces and abstractions for Spring AI framework providing portable API for chat, embeddings, images, audio, and tool calling across multiple AI providers

Overview
Eval results
Files

Spring AI Model

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.

Package Information

  • Package: org.springframework.ai:spring-ai-model:1.1.2
  • Language: Java
  • Installation:
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-model</artifactId>
    <version>1.1.2</version>
</dependency>

Quick Start

@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()));

Complete Quick Start Guide →

Core Architecture

Generic Model Pattern

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.

Capabilities Quick Reference

CapabilityInterfacePurpose
ChatChatModelConversational AI
EmbeddingsEmbeddingModelVector generation
ImagesImageModelText-to-image
TranscriptionTranscriptionModelSpeech-to-text
TTSTextToSpeechModelText-to-audio
ModerationModerationModelContent safety
ToolsToolCallbackFunction calling
MemoryChatMemoryConversation history
EnrichmentDocumentTransformerAI-powered metadata
ObservabilityObservationContextMonitoring

Core Capabilities

Chat Models

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);
}

Reference: Chat Models →

Messages

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);
}

Reference: Messages →

Prompts

Template-based prompt creation with variable substitution.

public class PromptTemplate implements PromptTemplateActions {
    public PromptTemplate(String template);
    public Prompt create(Map<String, Object> model);
}

Reference: Prompts →

Chat Memory

Conversation history management.

public interface ChatMemory {
    void add(String conversationId, Message message);
    List<Message> get(String conversationId);
    void clear(String conversationId);
}

Reference: Chat Memory →

Embeddings

Vector generation for semantic search.

public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> {
    float[] embed(String text);
    List<float[]> embed(List<String> texts);
    int dimensions();
}

Reference: Embeddings →

Image Models

Text-to-image generation.

public interface ImageModel extends Model<ImagePrompt, ImageResponse> {
    ImageResponse call(ImagePrompt request);
}

Reference: Image Models →

Audio Transcription

Speech-to-text conversion.

public interface TranscriptionModel extends Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {
    String transcribe(Resource audioResource);
}

Reference: Audio Transcription →

Text-to-Speech

Text-to-audio generation.

public interface TextToSpeechModel extends Model<TextToSpeechPrompt, TextToSpeechResponse> {
    byte[] call(String text);
}

Reference: Text-to-Speech →

Content Moderation

Content safety checking.

public interface ModerationModel extends Model<ModerationPrompt, ModerationResponse> {
    ModerationResponse call(ModerationPrompt request);
}

Reference: Moderation →

Tool/Function Calling

Enable AI to invoke Java methods.

@Tool(description = "Get weather for a city")
public String getWeather(@ToolParam(description = "City") String city) {
    return "Weather data";
}

Reference: Tools →

Structured Output

Convert text to Java objects.

public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
    public BeanOutputConverter(Class<T> clazz);
    public T convert(String text);
}

Reference: Converters →

Metadata & Usage

Track tokens, rate limits, and model info.

public interface Usage {
    Integer getPromptTokens();
    Integer getCompletionTokens();
    Integer getTotalTokens();
}

Reference: Metadata →

Observability

Micrometer integration for monitoring.

Reference: Observability →

Tool Calling Options

Advanced tool configuration.

public interface ToolCallingChatOptions extends ChatOptions {
    List<ToolCallback> getToolCallbacks();
    Boolean getInternalToolExecutionEnabled();
}

Reference: Tool Calling Options →

Document Enrichment

AI-powered keyword and summary extraction.

public class KeywordMetadataEnricher implements DocumentTransformer {
    public KeywordMetadataEnricher(ChatModel chatModel, int keywordCount);
}

Reference: Document Enrichment →

Utilities

JSON parsing, schema generation, and support utilities.

Reference: Utilities →

Reference: Core Abstractions →

Essential Patterns

Multi-Provider Setup

@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); }
}

Error Handling

try {
    ChatResponse response = chatModel.call(prompt);
} catch (Exception e) {
    // Handle errors
}

Core Imports

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.*;

Documentation Navigation

Guides (Step-by-Step)

Examples (Real-World Usage)

Reference (Detailed API)

Install with Tessl CLI

npx tessl i tessl/maven-org-springframework-ai--spring-ai-model@1.1.1
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.springframework.ai/spring-ai-model@1.1.x