### Example Format Instructions for LLM (Text) Source: https://docs.spring.io/spring-ai/reference/api/structured-output-converter Illustrates the type of formatting instructions that can be provided to an LLM via the FormatProvider. These instructions guide the LLM to produce output in a specific format, such as JSON, adhering to a given Java class structure. ```text Your response should be in JSON format. The data structure for the JSON should match this Java class: java.util.HashMap Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation. ``` -------------------------------- ### Run Postgres & PGVector DB Locally (Docker) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This command starts a PostgreSQL container with the PGVector extension enabled. It maps the default PostgreSQL port (5432) to the host and sets the user and password. Ensure Docker is installed and running before executing this command. ```bash docker run -it --rm --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres pgvector/pgvector ``` -------------------------------- ### Spring Boot Starter for Azure OpenAI Source: https://docs.spring.io/spring-ai/reference/upgrade-notes This Maven dependency configures the Spring Boot starter for integrating with Azure OpenAI services. It allows for easy setup of AI functionalities powered by Azure's offerings. Ensure you have the correct Spring AI version and Azure credentials configured. ```xml org.springframework.experimental.ai spring-ai-azure-openai-spring-boot-starter 0.7.1-SNAPSHOT ``` -------------------------------- ### Manual PGVector Configuration Dependencies Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector Maven dependencies required for manual configuration of the PgVectorStore. Includes starter JDBC, PostgreSQL driver, and the Spring AI PGVector store artifact. ```xml org.springframework.boot spring-boot-starter-jdbc org.postgresql postgresql runtime org.springframework.ai spring-ai-pgvector-store Copied! ``` -------------------------------- ### Java One-Shot & Few-Shot Prompting for JSON Parsing Source: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns This Java method demonstrates one-shot and few-shot prompting using Spring AI's ChatClient. It shows how to provide examples within a prompt to guide the language model in parsing a pizza order into a specific JSON format. The method includes example inputs and expected JSON outputs, along with configuration for the AI model, temperature, and maximum tokens. ```java public void pt_one_shot_few_shots(ChatClient chatClient) { String pizzaOrder = chatClient.prompt(""" Parse a customer's pizza order into valid JSON EXAMPLE 1: I want a small pizza with cheese, tomato sauce, and pepperoni. JSON Response: ``` { "size": "small", "type": "normal", "ingredients": ["cheese", "tomato sauce", "pepperoni"] } ``` EXAMPLE 2: Can I get a large pizza with tomato sauce, basil and mozzarella. JSON Response: ``` { "size": "large", "type": "normal", "ingredients": ["tomato sauce", "basil", "mozzarella"] } ``` Now, I would like a large pizza, with the first half cheese and mozzarella. And the other tomato sauce, ham and pineapple. """) .options(ChatOptions.builder() .model("claude-3-7-sonnet-latest") .temperature(0.1) .maxTokens(250) .build()) .call() .content(); } ``` -------------------------------- ### Example EmbeddingModel Bean Configuration - Java Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/redis Provides an example of configuring an `EmbeddingModel` bean, specifically using `OpenAiEmbeddingModel`. This is a necessary component for initializing the `RedisVectorStore`. Ensure the `OPENAI_API_KEY` environment variable is set. ```java // This can be any EmbeddingModel implementation @Bean public EmbeddingModel embeddingModel() { return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY"))); } ``` -------------------------------- ### Spring AI Vector Store Autoconfiguration Dependencies Source: https://docs.spring.io/spring-ai/reference/upgrade-notes This snippet provides example Maven dependencies for integrating various vector stores with Spring AI. It showcases configurations for Redis, Pgvector, and Chroma vector databases. These are often managed transitively by starter dependencies. ```xml org.springframework.ai spring-ai-autoconfigure-vector-store-redis org.springframework.ai spring-ai-autoconfigure-vector-store-pgvector org.springframework.ai spring-ai-autoconfigure-vector-store-chroma ``` -------------------------------- ### Spring AI Prompt Template Example Source: https://docs.spring.io/spring-ai/reference/concepts Demonstrates a basic prompt template structure used in Spring AI. This template is processed by the StringTemplate library, allowing dynamic insertion of values into a predefined text structure to guide AI model responses. ```text Tell me a {adjective} joke about {content}. ``` -------------------------------- ### Inject MCP Sync or Async Clients in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs Examples demonstrating how to inject MCP client beans into your Spring components using dependency injection. This allows you to interact with the MCP clients for synchronous or asynchronous operations. ```java @Autowired private List mcpSyncClients; // For sync client ``` ```java // OR @Autowired private List mcpAsyncClients; // For async client ``` -------------------------------- ### Add Pinecone Starter Dependency (Maven/Gradle) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pinecone This snippet shows how to add the Pinecone vector store starter dependency to your project's build file, enabling auto-configuration. It includes examples for both Maven and Gradle. ```xml org.springframework.ai spring-ai-starter-vector-store-pinecone ``` ```gradle dependencies { implementation 'org.springframework.ai:spring-ai-starter-vector-store-pinecone' } ``` -------------------------------- ### SQL: Setup PGvector Database Extensions and Table Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector SQL commands to create the necessary PGvector extensions (vector, hstore, uuid-ossp) and the 'vector_store' table with an HNSW index. This is required before using the PgVectorStore. The embedding dimension can be adjusted as needed. ```sql CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS hstore; CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE IF NOT EXISTS vector_store ( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, content text, metadata json, embedding vector(1536) // 1536 is the default embedding dimension ); CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops); ``` -------------------------------- ### Manual MiniMax Chat Model Configuration Source: https://docs.spring.io/spring-ai/reference/api/chat/minimax-chat Demonstrates manual instantiation and configuration of `MiniMaxChatModel` using the low-level `MiniMaxApi`. This approach offers fine-grained control over model parameters and API client setup. ```java var miniMaxApi = new MiniMaxApi(System.getenv("MINIMAX_API_KEY")); var chatModel = new MiniMaxChatModel(this.miniMaxApi, MiniMaxChatOptions.builder() .model(MiniMaxApi.ChatModel.ABAB_6_5_S_Chat.getValue()) .temperature(0.4) .maxTokens(200) .build()); ChatResponse response = this.chatModel.call( new Prompt("Generate the names of 5 famous pirates.")); // Or with streaming responses Flux streamResponse = this.chatModel.stream( new Prompt("Generate the names of 5 famous pirates.")); ``` -------------------------------- ### Add Weaviate Starter Dependency (Maven) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/weaviate This Maven dependency adds the Spring AI starter module for Weaviate, enabling auto-configuration for the Weaviate Vector Store. It simplifies the setup process. ```xml org.springframework.ai spring-ai-starter-vector-store-weaviate ``` -------------------------------- ### Connect to Local Postgres Instance (psql) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This command demonstrates how to connect to the locally running PostgreSQL instance using the psql command-line client. It specifies the username, host, and port. This is useful for direct database interaction and verification. ```bash psql -U postgres -h localhost -p 5432 ``` -------------------------------- ### Basic RetrievalAugmentationAdvisor Configuration (Java) Source: https://docs.spring.io/spring-ai/reference/api/retrieval-augmented-generation Shows the basic setup for RetrievalAugmentationAdvisor, integrating it with a VectorStoreDocumentRetriever. This configures a standard RAG flow where documents are retrieved based on a similarity threshold. ```java Advisor retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder() .documentRetriever(VectorStoreDocumentRetriever.builder() .similarityThreshold(0.50) .vectorStore(vectorStore) .build()) .build(); String answer = chatClient.prompt() .advisors(retrievalAugmentationAdvisor) .user(question) .call() .content(); ``` -------------------------------- ### Configure STDIO Transport for MCP Client Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This example shows how to configure the STDIO transport for the Spring AI MCP client using application properties. It includes settings for root change notifications and defines named connections to MCP servers, specifying the command, arguments, and environment variables for each server process. ```properties spring: ai: mcp: client: stdio: root-change-notification: true connections: server1: command: /path/to/server args: - --port=8080 - --mode=production env: API_KEY: your-api-key DEBUG: "true" ``` -------------------------------- ### Java Few-Shot Chain of Thought (CoT) with Spring AI Source: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns Illustrates the few-shot approach to Chain of Thought prompting. This method provides an example of a question and its step-by-step solution before presenting the actual question to guide the model's reasoning process. It requires a configured ChatClient. ```java public void pt_chain_of_thought_singleshot_fewshots(ChatClient chatClient) { String output = chatClient .prompt(""" Q: When my brother was 2 years old, I was double his age. Now I am 40 years old. How old is my brother? Let's think step by step. A: When my brother was 2 years, I was 2 * 2 = 4 years old. That's an age difference of 2 years and I am older. Now I am 40 years old, so my brother is 40 - 2 = 38 years old. The answer is 38. Q: When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Let's think step by step. A: """) .call() .content(); } ``` -------------------------------- ### Run Weaviate in Docker Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/weaviate Start a local Weaviate instance using Docker for quick development and testing. This command configures anonymous access, data persistence, and port mapping. ```bash docker run -it --rm --name weaviate \ -e AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true \ -e PERSISTENCE_DATA_PATH=/var/lib/weaviate \ -e QUERY_DEFAULTS_LIMIT=25 \ -e DEFAULT_VECTORIZER_MODULE=none \ -e CLUSTER_HOSTNAME=node1 \ -p 8080:8080 \ semitechnologies/weaviate:1.22.4 ``` -------------------------------- ### Add PgVectorStore Dependency (Gradle) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This snippet shows how to add the PgVectorStore starter dependency to your Gradle build file. Ensure you have the correct Spring AI version. ```gradle dependencies { implementation 'org.springframework.ai:spring-ai-starter-vector-store-pgvector' } ``` -------------------------------- ### Manual Configuration of BedrockCohereEmbeddingModel Source: https://docs.spring.io/spring-ai/reference/api/embeddings/bedrock-cohere-embedding This example demonstrates how to manually create and use the `BedrockCohereEmbeddingModel`. It initializes the low-level CohereEmbeddingBedrockApi client with necessary configurations such as the model ID, credentials provider, AWS region, and an ObjectMapper. The `embedForResponse` method is then used to get embeddings for a list of strings. ```java var cohereEmbeddingApi =new CohereEmbeddingBedrockApi( CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(), EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper()); var embeddingModel = new BedrockCohereEmbeddingModel(this.cohereEmbeddingApi); EmbeddingResponse embeddingResponse = this.embeddingModel .embedForResponse(List.of("Hello World", "World is big and salvation is near")); ``` -------------------------------- ### Spring Boot Starter for OpenAI Source: https://docs.spring.io/spring-ai/reference/upgrade-notes This Maven dependency sets up the Spring Boot starter for direct integration with OpenAI services. It simplifies the process of adding OpenAI capabilities to your Spring applications. Verify the Spring AI version and your OpenAI API key. ```xml org.springframework.experimental.ai spring-ai-openai-spring-boot-starter 0.7.1-SNAPSHOT ``` -------------------------------- ### Configure STDIO Transport using External JSON Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This configuration example illustrates how to specify an external JSON file for STDIO server configurations in the Spring AI MCP client. The `servers-configuration` property points to a classpath resource containing the MCP server definitions in a format compatible with Claude Desktop. ```properties spring: ai: mcp: client: stdio: servers-configuration: classpath:mcp-servers.json ``` -------------------------------- ### Add PgVectorStore Dependency (Maven) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This snippet shows how to add the PgVectorStore starter dependency to your Maven project. Ensure you have the correct Spring AI version. ```xml org.springframework.ai spring-ai-starter-vector-store-pgvector ``` -------------------------------- ### Spring AI Model Autoconfiguration Dependencies Source: https://docs.spring.io/spring-ai/reference/upgrade-notes This snippet shows example Maven dependencies for configuring different AI models within Spring AI. These artifacts enable specific model integrations like OpenAI, Anthropic, and Vertex AI. They are typically included transitively via starter dependencies. ```xml org.springframework.ai spring-ai-autoconfigure-model-openai org.springframework.ai spring-ai-autoconfigure-model-anthropic org.springframework.ai spring-ai-autoconfigure-model-vertex-ai ``` -------------------------------- ### Add OpenAI EmbeddingModel Dependency (Maven) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This snippet demonstrates adding the OpenAI EmbeddingModel starter dependency to your Maven project. This is required for embedding documents. ```xml org.springframework.ai spring-ai-starter-model-openai ``` -------------------------------- ### Manual OllamaChatModel Configuration (Java) Source: https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat This Java snippet demonstrates the manual configuration of an `OllamaChatModel` without relying on Spring Boot auto-configuration. It shows how to build the `OllamaApi` client and then the `OllamaChatModel` with specific options, including the model and temperature. It also includes examples for calling the model for single and streaming responses. ```java var ollamaApi = OllamaApi.builder().build(); var chatModel = OllamaChatModel.builder() .ollamaApi(ollamaApi) .defaultOptions( OllamaOptions.builder() .model(OllamaModel.MISTRAL) .temperature(0.9) .build()) .build(); ChatResponse response = this.chatModel.call( new Prompt("Generate the names of 5 famous pirates.")); // Or with streaming responses Flux response = this.chatModel.stream( new Prompt("Generate the names of 5 famous pirates.")); ``` -------------------------------- ### Configure SSE Transport for MCP Client Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This example demonstrates how to configure the SSE (Server-Sent Events) transport for the Spring AI MCP client. It shows how to define named connections, specifying the base URL for each MCP server and optionally a custom SSE endpoint. ```properties spring: ai: mcp: client: sse: connections: server1: url: http://localhost:8080 server2: url: http://otherserver:8081 sse-endpoint: /custom-sse ``` -------------------------------- ### Spring Boot Typesense Configuration Properties Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/typesense Example of configuring the TypesenseVectorStore using Spring Boot's application.yml. This covers connection details like host, port, API key, and schema initialization. ```yaml spring: ai: vectorstore: typesense: initialize-schema: true collection-name: vector_store embedding-dimension: 1536 client: protocol: http host: localhost port: 8108 api-key: xyz ``` -------------------------------- ### Use VectorStore for Document Operations (Java) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This Java code demonstrates how to auto-wire the VectorStore, create a list of Document objects with optional metadata, add them to the store using `vectorStore.add()`, and perform a similarity search using `vectorStore.similaritySearch()` with a query and top-K value. ```java @Autowired VectorStore vectorStore; // ... List documents = List.of( new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")), new Document("The World is Big and Salvation Lurks Around the Corner"), new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2"))); // Add the documents to PGVector vectorStore.add(documents); // Retrieve documents similar to a query List results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build()); ``` -------------------------------- ### Configure OpenAI Chat Model in Spring Boot Application Properties Source: https://docs.spring.io/spring-ai/reference/api/chat/openai-chat Provides example application properties to enable and configure the OpenAI chat model in a Spring Boot application, including API key and model selection. Assumes `spring-ai-starter-model-openai` dependency. ```properties spring.ai.openai.api-key=YOUR_API_KEY spring.ai.openai.chat.options.model=gpt-4o spring.ai.openai.chat.options.temperature=0.7 ``` -------------------------------- ### Expose Tools with ToolCallbackProvider in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs Demonstrates how to expose tools to clients using the ToolCallbackProvider bean. This method allows for easy registration of multiple tools. ```java @Bean public ToolCallbackProvider myTools(...) { List tools = ... return ToolCallbackProvider.from(tools); } ``` -------------------------------- ### Manual PGVectorStore Bean Configuration Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector Java configuration for manually creating a PgVectorStore bean. This method allows explicit setting of dimensions, distance type, index type, schema, table name, and batch size. ```java @Bean public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) { return PgVectorStore.builder(jdbcTemplate, embeddingModel) .dimensions(1536) // Optional: defaults to model dimensions or 1536 .distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE .indexType(HNSW) // Optional: defaults to HNSW .initializeSchema(true) // Optional: defaults to false .schemaName("public") // Optional: defaults to "public" .vectorTableName("vector_store") // Optional: defaults to "vector_store" .maxDocumentBatchSize(10000) // Optional: defaults to 10000 .build(); } Copied! ``` -------------------------------- ### Expose Prompts with SyncPromptSpecification in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs Demonstrates exposing prompt templates using SyncPromptSpecification beans. This allows servers to manage and share prompt definitions with clients, including arguments and versioning. ```java @Bean public List myPrompts() { var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt", List.of(new McpSchema.PromptArgument("name", "The name to greet", true))); var promptSpecification = new McpServerFeatures.SyncPromptSpecification(prompt, (exchange, getPromptRequest) -> { String nameArgument = (String) getPromptRequest.arguments().get("name"); if (nameArgument == null) { nameArgument = "friend"; } var userMessage = new PromptMessage(Role.USER, new TextContent("Hello " + nameArgument + "! How can I assist you today?")); return new GetPromptResult("A personalized greeting message", List.of(userMessage)); }); return List.of(promptSpecification); } ``` -------------------------------- ### PGVector Metadata Filtering with Text Expression Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector Example of using a text expression language for metadata filtering in PGVector's similarity search. It allows filtering by author and article type. ```java vectorStore.similaritySearch( SearchRequest.builder() .query("The World") .topK(TOP_K) .similarityThreshold(SIMILARITY_THRESHOLD) .filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build()); Copied! ``` -------------------------------- ### Expose Tools with Low-Level API in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs Shows how to expose tools using the low-level API by returning a list of SyncToolSpecification beans. This provides finer control over tool exposure. ```java @Bean public List myTools(...) { List tools = ... return tools; } ``` -------------------------------- ### PGVector Metadata Filtering with Filter.Expression DSL Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector Example of using the Filter.Expression DSL for programmatic metadata filtering in PGVector's similarity search. This approach offers a structured way to define filters. ```java FilterExpressionBuilder b = new FilterExpressionBuilder(); vectorStore.similaritySearch(SearchRequest.builder() .query("The World") .topK(TOP_K) .similarityThreshold(SIMILARITY_THRESHOLD) .filterExpression(b.and( b.in("author","john", "jill"), b.eq("article_type", "blog")).build()).build()); Copied! ``` -------------------------------- ### Spring Boot Controller for Ollama Chat Generation (Java) Source: https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat An example of a Spring Boot `@RestController` that injects and utilizes an `OllamaChatModel` for text generation. It provides endpoints for both single response generation and streaming responses. This requires the `spring-ai-starter-model-ollama` dependency. ```java @RestController public class ChatController { private final OllamaChatModel chatModel; @Autowired public ChatController(OllamaChatModel chatModel) { this.chatModel = chatModel; } @GetMapping("/ai/generate") public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { return Map.of("generation", this.chatModel.call(message)); } @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return this.chatModel.stream(prompt); } } ``` -------------------------------- ### Initialize Additional Ollama Models (YAML) Source: https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat Configure Spring AI to initialize additional Ollama models at startup, useful for models used dynamically at runtime. This example shows how to specify chat models like 'llama3.2' and 'qwen2.5' to be pulled. ```yaml spring: ai: ollama: init: pull-model-strategy: always chat: additional-models: - llama3.2 - qwen2.5 ``` -------------------------------- ### STDIO Server Configuration (Properties) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs This configuration snippet shows how to set up the Spring AI MCP Server for STDIO transport using application properties. It specifies the server's name, version, and type as SYNC. ```properties spring: ai: mcp: server: name: stdio-mcp-server version: 1.0.0 type: SYNC ``` -------------------------------- ### Update Maven Dependencies (XML) Source: https://docs.spring.io/spring-ai/reference/upgrade-notes Provides an example of how to update Maven artifact IDs for Spring AI starter dependencies. The pattern has shifted from `spring-ai-{type}-spring-boot-starter` to `spring-ai-starter-{type}-{model_or_store}`. ```xml org.springframework.ai spring-ai-openai-spring-boot-starter org.springframework.ai spring-ai-starter-model-openai ``` -------------------------------- ### Role Prompting with Style Instructions in Spring AI Source: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns Enhances role prompting by adding style instructions, specifically requesting a humorous tone from the travel guide persona. This shows how to further refine the model's output characteristics. ```java public void pt_role_prompting_2(ChatClient chatClient) { String humorousTravelSuggestions = chatClient .prompt() .system(""" I want you to act as a travel guide. I will write to you about my location and you will suggest 3 places to visit near me in a humorous style. """) .user(""" My suggestion: \"I am in Amsterdam and I want to visit only museums.\" Travel Suggestions: """) .call() .content(); } ``` -------------------------------- ### Expose Completions with SyncCompletionSpecification in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs Shows how to expose completion capabilities using SyncCompletionSpecification beans. This enables servers to provide code completion suggestions or other completion services to clients. ```java @Bean public List myCompletions() { var completion = new McpServerFeatures.SyncCompletionSpecification( "code-completion", "Provides code completion suggestions", (exchange, request) -> { // Implementation that returns completion suggestions return new McpSchema.CompletionResult(List.of( new McpSchema.Completion("suggestion1", "First suggestion"), new McpSchema.Completion("suggestion2", "Second suggestion") )); } ); return List.of(completion); } ``` -------------------------------- ### Role Prompting with Spring AI ChatClient Source: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns Demonstrates basic role prompting where the model acts as a travel guide. It takes location and preferences as input and suggests places to visit. This method influences the model's persona and response style. ```java public void pt_role_prompting_1(ChatClient chatClient) { String travelSuggestions = chatClient .prompt() .system(""" I want you to act as a travel guide. I will write to you about my location and you will suggest 3 places to visit near me. In some cases, I will also give you the type of places I will visit. """) .user(""" My suggestion: \"I am in Amsterdam and I want to visit only museums.\" Travel Suggestions: """) .call() .content(); } ``` -------------------------------- ### Configure PgVectorStore Connection and Properties (YAML) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This YAML configuration provides the necessary details to connect to your PgVectorStore instance, including database URL, username, password, and specific index/distance settings. It also shows how to optionally set document batch size. ```yaml spring: datasource: url: jdbc:postgresql://localhost:5432/postgres username: postgres password: postgres ai: vectorstore: pgvector: index-type: HNSW distance-type: COSINE_DISTANCE dimensions: 1536 max-document-batch-size: 10000 # Optional: Maximum number of documents per batch ``` -------------------------------- ### Spring AI MCP Autoconfiguration Dependencies Source: https://docs.spring.io/spring-ai/reference/upgrade-notes This snippet illustrates example Maven dependencies for Message Queue (MQ) communication (MCP) in Spring AI, specifically for client and server configurations. These artifacts are usually brought in transitively through other dependencies. ```xml org.springframework.ai spring-ai-autoconfigure-mcp-client org.springframework.ai spring-ai-autoconfigure-mcp-server ``` -------------------------------- ### WebFlux Server Configuration (Properties) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs This configuration snippet outlines the properties for setting up the Spring AI MCP Server with WebFlux transport, suitable for reactive applications. It defines server properties, instructions, the SSE endpoint, and capabilities, recommending ASYNC as the server type. ```properties spring: ai: mcp: server: name: webflux-mcp-server version: 1.0.0 type: ASYNC # Recommended for reactive applications instructions: "This reactive server provides weather information tools and resources" sse-message-endpoint: /mcp/messages capabilities: tool: true resource: true prompt: true completion: true ``` -------------------------------- ### Add Standard MCP Client Dependency (Maven) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This snippet shows how to add the standard Spring AI MCP client starter dependency to your Maven project. This starter enables simultaneous connections to MCP servers over STDIO and SSE transports, supporting both synchronous and asynchronous client types. ```xml org.springframework.ai spring-ai-starter-mcp-client ``` -------------------------------- ### Java Zero-Shot Chain of Thought (CoT) with Spring AI Source: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns Demonstrates the zero-shot approach to Chain of Thought prompting. The model is prompted with a complex question and the phrase 'Let's think step by step.' to encourage step-by-step reasoning without prior examples. It requires a configured ChatClient. ```java public void pt_chain_of_thought_zero_shot(ChatClient chatClient) { String output = chatClient .prompt(""" When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Let's think step by step. """) .call() .content(); } ``` -------------------------------- ### Manual OpenAI Moderation API Setup in Java Source: https://docs.spring.io/spring-ai/reference/api/moderation/openai-moderation Shows how to manually set up the OpenAI Moderation API in Java. It involves creating an OpenAiModerationApi instance using an API key, then creating an OpenAiModerationModel. Finally, it configures moderation options and makes a moderation call with a prompt. ```java OpenAiModerationApi openAiModerationApi = new OpenAiModerationApi(System.getenv("OPENAI_API_KEY")); OpenAiModerationModel openAiModerationModel = new OpenAiModerationModel(this.openAiModerationApi); OpenAiModerationOptions moderationOptions = OpenAiModerationOptions.builder() .model("text-moderation-latest") .build(); ModerationPrompt moderationPrompt = new ModerationPrompt("Text to be moderated", this.moderationOptions); ModerationResponse response = this.openAiModerationModel.call(this.moderationPrompt); ``` -------------------------------- ### Customize Synchronous MCP Client with Callbacks Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This Java code demonstrates how to implement `McpSyncClientCustomizer` to customize a synchronous MCP client. It shows how to set request timeouts, define accessible file system roots, configure custom sampling handlers, and register consumers for tools, resources, prompts, and logging events. This allows fine-grained control over client behavior and interaction with the server. ```java @Component public class CustomMcpSyncClientCustomizer implements McpSyncClientCustomizer { @Override public void customize(String serverConfigurationName, McpClient.SyncSpec spec) { // Customize the request timeout configuration spec.requestTimeout(Duration.ofSeconds(30)); // Sets the root URIs that this client can access. spec.roots(roots); // Sets a custom sampling handler for processing message creation requests. spec.sampling((CreateMessageRequest messageRequest) -> { // Handle sampling CreateMessageResult result = ... return result; }); // Adds a consumer to be notified when the available tools change, such as tools // being added or removed. spec.toolsChangeConsumer((List tools) -> { // Handle tools change }); // Adds a consumer to be notified when the available resources change, such as resources // being added or removed. spec.resourcesChangeConsumer((List resources) -> { // Handle resources change }); // Adds a consumer to be notified when the available prompts change, such as prompts // being added or removed. spec.promptsChangeConsumer((List prompts) -> { // Handle prompts change }); // Adds a consumer to be notified when logging messages are received from the server. spec.loggingConsumer((McpSchema.LoggingMessageNotification log) -> { // Handle log messages }); } } ``` -------------------------------- ### Expose Resources with SyncResourceSpecification in Java Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs Illustrates exposing resources via Spring beans, specifically using SyncResourceSpecification. This enables servers to share static and dynamic resources with clients. ```java @Bean public List myResources(...) { var systemInfoResource = new McpSchema.Resource(...); var resourceSpecification = new McpServerFeatures.SyncResourceSpecification(systemInfoResource, (exchange, request) -> { try { var systemInfo = Map.of(...); String jsonContent = new ObjectMapper().writeValueAsString(systemInfo); return new McpSchema.ReadResourceResult( List.of(new McpSchema.TextResourceContents(request.uri(), "application/json", jsonContent))); } catch (Exception e) { throw new RuntimeException("Failed to generate system info", e); } }); return List.of(resourceSpecification); } ``` -------------------------------- ### Claude Desktop JSON Format for STDIO Servers Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This JSON structure defines MCP servers for STDIO transport using the Claude Desktop format. It specifies the command and arguments required to launch the server process, such as the modelcontextprotocol server-filesystem. ```json { "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/Users/username/Desktop", "/Users/username/Downloads" ] } } } ``` -------------------------------- ### Add WebFlux MCP Client Dependency (Maven) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This snippet demonstrates how to include the Spring AI MCP client WebFlux starter dependency in your Maven project. This starter provides similar functionality to the standard starter but utilizes a WebFlux-based SSE transport implementation for improved performance in reactive applications. ```xml org.springframework.ai spring-ai-starter-mcp-client-webflux ``` -------------------------------- ### Add Standard MCP Server Starter Dependency Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs This dependency includes the Standard MCP Server starter, which provides full MCP Server features with STDIO server transport. It's suitable for command-line and desktop tools and does not require additional web dependencies. The starter activates McpServerAutoConfiguration for server components, specifications, and change notifications. ```xml org.springframework.ai spring-ai-starter-mcp-server ``` -------------------------------- ### Tool Callback Changes in Spring AI M7 to M8 Source: https://docs.spring.io/spring-ai/reference/upgrade-notes Illustrates a breaking change in tool callback registration between Spring AI versions M7 and M8. Code using the deprecated `tools()` method for tool callbacks will silently fail in M8. This example shows the M7 syntax that is no longer functional. ```java // This worked in M7 but silently fails in M8 ChatClient chatClient = new OpenAiChatClient(api) .tools(List.of( new Tool("get_current_weather", "Get the current weather in a given location", new ToolSpecification.ToolParameter("location", "The city and state, e.g. San Francisco, CA", true)) )) .toolCallbacks(List.of( new ToolCallback("get_current_weather", (toolName, params) -> { // Weather retrieval logic return Map.of("temperature", 72, "unit", "fahrenheit", "description", "Sunny"); }) )); ``` -------------------------------- ### Configure Spring AI MCP Client in application.properties/yml Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs This configuration block shows how to set up the MCP client properties in Spring Boot's application configuration files. It includes settings for enabling the client, naming, versioning, request timeouts, and defining SSE or stdio connections. ```yaml spring: ai: mcp: client: enabled: true name: my-mcp-client version: 1.0.0 request-timeout: 30s type: SYNC # or ASYNC for reactive applications sse: connections: server1: url: http://localhost:8080 server2: url: http://otherserver:8081 stdio: root-change-notification: false connections: server1: command: /path/to/server args: - --port=8080 - --mode=production env: API_KEY: your-api-key DEBUG: "true" ``` -------------------------------- ### Access Native PGVector Client (Java) Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector This Java code snippet shows how to retrieve the native `JdbcTemplate` client from a `PgVectorStore` instance in Spring AI. This allows for executing PostgreSQL-specific operations not directly exposed by the `VectorStore` interface. It requires the Spring AI context and a configured `PgVectorStore` bean. ```java PgVectorStore vectorStore = context.getBean(PgVectorStore.class); Optional nativeClient = vectorStore.getNativeClient(); if (nativeClient.isPresent()) { JdbcTemplate jdbc = nativeClient.get(); // Use the native client for PostgreSQL-specific operations } ``` -------------------------------- ### Weaviate GraphQL Filter Conversion Example Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/weaviate Illustrates how portable filter expressions are converted into Weaviate's proprietary GraphQL filter format. This example shows the conversion of a text expression for country and year filtering. ```yaml operator: And operands: [{ operator: Or operands: [{ Path: ["meta_country"] operator: Equal valueText: "UK" }, { path: ["meta_country"] operator: Equal valueText: "NL" }] }, { path: ["meta_year"] operator: GreaterThanEqual valueNumber: 2020 }] ``` -------------------------------- ### Spring Boot Application with MCP Server and Custom Tools (Java) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs This Java code illustrates setting up a Spring Boot application with the MCP server and integrating custom tools. It defines a service with a tool-annotated method and configures a `ToolCallbackProvider` bean to make these tools available to the MCP server. The auto-configuration handles the registration of these tools. ```java @Service public class WeatherService { @Tool(description = "Get weather information by city name") public String getWeather(String cityName) { // Implementation } } @SpringBootApplication public class McpServerApplication { private static final Logger logger = LoggerFactory.getLogger(McpServerApplication.class); public static void main(String[] args) { SpringApplication.run(McpServerApplication.class, args); } @Bean public ToolCallbackProvider weatherTools(WeatherService weatherService) { return MethodToolCallbackProvider.builder().toolObjects(weatherService).build(); } } ``` -------------------------------- ### WebMVC Server Configuration (Properties) Source: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs This configuration snippet demonstrates how to configure the Spring AI MCP Server for WebMVC transport via application properties. It includes server details, instructions, and enables specific capabilities like tool, resource, prompt, and completion. ```properties spring: ai: mcp: server: name: webmvc-mcp-server version: 1.0.0 type: SYNC instructions: "This server provides weather information tools and resources" sse-message-endpoint: /mcp/messages capabilities: tool: true resource: true prompt: true completion: true ``` -------------------------------- ### Add Spring AI BOM and Dependencies for Gradle Source: https://docs.spring.io/spring-ai/reference/getting-started Configures Gradle to use the Spring AI Bill of Materials (BOM) for dependency management and adds a specific Spring AI starter dependency. This ensures consistent dependency versions and includes the necessary components for using Spring AI modules. ```gradle dependencies { implementation platform("org.springframework.ai:spring-ai-bom:1.0.0-SNAPSHOT") // Replace the following with the starter dependencies of specific modules you wish to use implementation 'org.springframework.ai:spring-ai-openai' } ``` -------------------------------- ### Using PromptTemplate with StructuredOutputConverter (Java) Source: https://docs.spring.io/spring-ai/reference/api/structured-output-converter Demonstrates how to integrate the StructuredOutputConverter with Spring's PromptTemplate. It shows appending format instructions to a user prompt, preparing it for an LLM call. ```java StructuredOutputConverter outputConverter = ... String userInputTemplate = """ ... user text input .... {format} """; // user input with a "format" placeholder. Prompt prompt = new Prompt( PromptTemplate.builder() .template(this.userInputTemplate) .variables(Map.of(..., "format", this.outputConverter.getFormat())) // replace the "format" placeholder with the converter's format. .build().createMessage() ); Copied! ``` -------------------------------- ### Custom StringTemplate Renderer with Custom Delimiters Source: https://docs.spring.io/spring-ai/reference/api/prompt Demonstrates how to configure a StringTemplate renderer with custom start and end delimiters ('<' and '>'). This allows for flexible variable identification within templates. ```java PromptTemplate promptTemplate = PromptTemplate.builder() .renderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) .template("\"Tell me the names of 5 movies whose soundtrack was composed by .\"") .build(); String prompt = promptTemplate.render(Map.of("composer", "John Williams")); ``` -------------------------------- ### Add Qdrant Starter Dependency for Gradle Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/qdrant This snippet demonstrates how to include the Qdrant vector store starter dependency in your Gradle build file. This addition activates Spring AI's automatic configuration for the Qdrant Vector Store. ```gradle dependencies { implementation 'org.springframework.ai:spring-ai-starter-vector-store-qdrant' } ``` -------------------------------- ### Customizing PromptTemplate Delimiters Source: https://docs.spring.io/spring-ai/reference/api/prompt Illustrates how to configure PromptTemplate with custom start and end delimiter tokens, like '<' and '>', to avoid conflicts when including JSON or other structured data within prompts. ```java PromptTemplate promptTemplate = PromptTemplate.builder() .renderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) .template(""" Tell me the names of 5 movies whose soundtrack was composed by . """) .build(); String prompt = promptTemplate.render(Map.of("composer", "John Williams")); ``` -------------------------------- ### Spring AI TypesenseVectorStore Usage Example Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/typesense Demonstrates how to auto-wire and use the TypesenseVectorStore in a Spring application. It includes adding documents with metadata and performing a similarity search based on a query. ```java @Autowired VectorStore vectorStore; // ... List documents = List.of( new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")), new Document("The World is Big and Salvation Lurks Around the Corner"), new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2"))); // Add the documents to Typesense vectorStore.add(documents); // Retrieve documents similar to a query List results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build()); ``` -------------------------------- ### Add Qdrant Starter Dependency for Maven Source: https://docs.spring.io/spring-ai/reference/api/vectordbs/qdrant This snippet shows how to add the Qdrant vector store starter dependency to your Maven project's pom.xml file. This dependency is necessary to enable Spring AI's auto-configuration for the Qdrant Vector Store. ```xml org.springframework.ai spring-ai-starter-vector-store-qdrant ``` -------------------------------- ### Update Gradle Dependencies (Groovy) Source: https://docs.spring.io/spring-ai/reference/upgrade-notes Demonstrates the change in Gradle dependency declarations for Spring AI starters. Similar to Maven, the artifact ID naming convention has been updated for models and vector stores. ```groovy // BEFORE implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter' implementation 'org.springframework.ai:spring-ai-redis-store-spring-boot-starter' // AFTER implementation 'org.springframework.ai:spring-ai-starter-model-openai' implementation 'org.springframework.ai:spring-ai-starter-vector-store-redis' ``` -------------------------------- ### Initialize ContextualQueryAugmenter in Java Source: https://docs.spring.io/spring-ai/reference/api/retrieval-augmented-generation Demonstrates the basic initialization of the ContextualQueryAugmenter using its builder pattern in Java. This sets up the augmenter with default configurations. ```java QueryAugmenter queryAugmenter = ContextualQueryAugmenter.builder().build(); ``` -------------------------------- ### Basic Prompt Creation with PromptTemplate Source: https://docs.spring.io/spring-ai/reference/api/prompt Demonstrates creating a simple prompt using PromptTemplate and populating it with placeholder values. This is useful for basic question-answering or text generation tasks. ```java PromptTemplate promptTemplate = new PromptTemplate("Tell me a {adjective} joke about {topic}"); Prompt prompt = promptTemplate.create(Map.of("adjective", adjective, "topic", topic)); return chatModel.call(prompt).getResult(); ``` -------------------------------- ### Access Client Capabilities via Exchange (Java) Source: https://docs.spring.io/spring-ai/reference/upgrade-notes Illustrates how methods previously accessed directly from the server are now accessed through the 'exchange' object. This centralizes access to server-related functionalities within the exchange context. ```java // Before ClientCapabilities capabilities = server.getClientCapabilities(); CreateMessageResult result = server.createMessage(new CreateMessageRequest(...)); // After ClientCapabilities capabilities = exchange.getClientCapabilities(); CreateMessageResult result = exchange.createMessage(new CreateMessageRequest(...)); ``` -------------------------------- ### Configure Repositories for Gradle Source: https://docs.spring.io/spring-ai/reference/getting-started Sets up Gradle to use Maven Central, Spring milestone, Spring snapshot, and Central Portal snapshot repositories. This allows Gradle to resolve dependencies from these various artifact sources. ```gradle repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } maven { url 'https://repo.spring.io/snapshot' } maven { name = 'Central Portal Snapshots' url = 'https://central.sonatype.com/repository/maven-snapshots/' } } ```