diff --git a/models/spring-ai-azure-openai/README.md b/models/spring-ai-azure-openai/README.md index feca1e872..7be0dd68d 100644 --- a/models/spring-ai-azure-openai/README.md +++ b/models/spring-ai-azure-openai/README.md @@ -1,3 +1,5 @@ # Azure OpenAI -Visit the Spring AI [Azure OpenAI Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/azure-openai-chat.html). \ No newline at end of file +Visit the Spring AI + - [Azure OpenAI Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/azure-openai-chat.html). + - [Azure OpenAI Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/azure-openai-embeddigs.html). \ No newline at end of file diff --git a/models/spring-ai-huggingface/README.md b/models/spring-ai-huggingface/README.md new file mode 100644 index 000000000..31705c61a --- /dev/null +++ b/models/spring-ai-huggingface/README.md @@ -0,0 +1,5 @@ +# Huggingface Chat + +Visit the Spring AI + - [Huggingface Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/huggingface.html). + diff --git a/models/spring-ai-ollama/README.md b/models/spring-ai-ollama/README.md index d699a1138..dceb49544 100644 --- a/models/spring-ai-ollama/README.md +++ b/models/spring-ai-ollama/README.md @@ -1,3 +1,5 @@ # Ollama Chat -Visit the Spring AI [Ollama Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/ollama-chat.html). +Visit the Spring AI + - [Ollama Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/ollama-chat.html). + - [Ollama Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/ollama-embeddings.html). diff --git a/models/spring-ai-openai/README.md b/models/spring-ai-openai/README.md index ca845762f..4cd211712 100644 --- a/models/spring-ai-openai/README.md +++ b/models/spring-ai-openai/README.md @@ -1,3 +1,6 @@ # OpenAI Chat -Visit the Spring AI [OpenAI Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/openai-chat.html). +Visit the Spring AI + - [OpenAI Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/openai-chat.html). + - [OpenAI Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/openai-embeddings.html). + diff --git a/models/spring-ai-postgresml/README.md b/models/spring-ai-postgresml/README.md new file mode 100644 index 000000000..3ce4454f5 --- /dev/null +++ b/models/spring-ai-postgresml/README.md @@ -0,0 +1,4 @@ +# PostgresML Embeddings + +Visit the Spring AI + - [PostgresML Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/postgresml-embeddings.html). diff --git a/models/spring-ai-transformers/README.md b/models/spring-ai-transformers/README.md new file mode 100644 index 000000000..6395d203c --- /dev/null +++ b/models/spring-ai-transformers/README.md @@ -0,0 +1,6 @@ +# Transformers (ONNX) Embeddings + +Visit the Spring AI: + - [Transformers Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/onnx.html). + + diff --git a/models/spring-ai-vertex-ai/README.md b/models/spring-ai-vertex-ai/README.md index 37ba14723..9f31280b7 100644 --- a/models/spring-ai-vertex-ai/README.md +++ b/models/spring-ai-vertex-ai/README.md @@ -1,37 +1,6 @@ +# Vertex AI -# Vertex AI API client for the Generative Language model +Visit the Spring AI: + - [VertexAI Chat Documentation](https://docs.spring.io/spring-ai/reference/api/clients/vertexai-chat.html). + - [VertexAI Embedding Documentation](https://docs.spring.io/spring-ai/reference/api/embeddings/vertexai-embeddings.html). -The [Generative Language](https://developers.generativeai.google/api/rest/generativelanguage) PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to comprehend and generate natural language through a series of prompts. The PaLM API is based on Google's next generation LLM, PaLM. It excels at a variety of different tasks like code generation, reasoning, and writing. You can use the PaLM API to build generative AI applications for use cases like content generation, dialogue agents, summarization and classification systems, and more. - -Based on the [Models REST API](https://developers.generativeai.google/api/rest/generativelanguage/models). - -## Prerequisite - -To access the PaLM2 REST API you need to obtain an access API KEY form [makersuite](https://makersuite.google.com/app/apikey). -Note: Currently it is not available outside US, but you can use VPN for testing. - -## PaLM API - -The VertexAI, ChatClient and EmbeddingClient are built on top the [VertexAiApi.java](./src/main/java/org/springframework/ai/vertex/api/VertexAiApi.java) client library: - -![PaLM API](./src/test/resources/Google%20Generative%20AI%20-%20PaLM2%20REST%20API.jpg) - -Following snippets show how to use the `VertexAiApi` client directly: - -```java - -VertexAiApi vertexAiApi = new VertexAiApi(< YOUR PALM_API_KEY>); - -// Generate -var prompt = new MessagePrompt(List.of(new Message("0", "Hello, how are you?"))); - -GenerateMessageRequest request = new GenerateMessageRequest(prompt); - -GenerateMessageResponse response = vertexAiApi.generateMessage(request); - -// Embed text -Embedding embedding = vertexAiApi.embedText("Hello, how are you?"); - -// Batch embedding -List embeddings = vertexAiApi.batchEmbedText(List.of("Hello, how are you?", "I am fine, thank you!")); -``` diff --git a/models/spring-ai-vertex-ai/src/test/java/org/springframework/ai/vertex/VertexAiEmbeddingClientIT.java b/models/spring-ai-vertex-ai/src/test/java/org/springframework/ai/vertex/VertexAiEmbeddingClientIT.java index 2ba33406a..dfced101f 100644 --- a/models/spring-ai-vertex-ai/src/test/java/org/springframework/ai/vertex/VertexAiEmbeddingClientIT.java +++ b/models/spring-ai-vertex-ai/src/test/java/org/springframework/ai/vertex/VertexAiEmbeddingClientIT.java @@ -1,18 +1,17 @@ package org.springframework.ai.vertex; +import java.util.List; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.springframework.ai.embedding.EmbeddingResponse; -import org.springframework.ai.vertex.VertexAiEmbeddingClient; import org.springframework.ai.vertex.api.VertexAiApi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc index 34f32722d..fcfecda6c 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc @@ -3,14 +3,15 @@ * xref:getting-started.adoc[Getting Started] * xref:api/index.adoc[] ** xref:api/embeddings.adoc[] -*** xref:api/embeddings/onnx.adoc[] *** xref:api/embeddings/openai-embeddings.adoc[] *** xref:api/embeddings/ollama-embeddings.adoc[] *** xref:api/embeddings/azure-openai-embeddings.adoc[] *** xref:api/embeddings/postgresml-embeddings.adoc[] -*** xref:api/bedrock.adoc[Amazon Bedrock Embedding] +*** xref:api/embeddings/vertexai-embeddings.adoc[] +*** xref:api/bedrock.adoc[Amazon Bedrock Embeddings] **** xref:api/embeddings/bedrock-cohere-embedding.adoc[] **** xref:api/embeddings/bedrock-titan-embedding.adoc[] +*** xref:api/embeddings/onnx.adoc[] ** xref:api/chatclient.adoc[] *** xref:api/clients/openai-chat.adoc[] *** xref:api/clients/azure-openai-chat.adoc[] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/azure-openai-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/azure-openai-chat.adoc index f3c791aa1..596edf4ea 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/azure-openai-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/azure-openai-chat.adoc @@ -135,7 +135,7 @@ public class ChatController { return Map.of("generation", chatClient.generate(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-anthropic.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-anthropic.adoc index c7610ebd7..ea1b2ce07 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-anthropic.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-anthropic.adoc @@ -142,7 +142,7 @@ public class ChatController { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-cohere.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-cohere.adoc index bd872cdf3..33c256429 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-cohere.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-cohere.adoc @@ -140,7 +140,7 @@ public class ChatController { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-llama2.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-llama2.adoc index 857796358..fa1d02b86 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-llama2.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-llama2.adoc @@ -138,7 +138,7 @@ public class ChatController { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-titan.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-titan.adoc index d07d0ec01..6d3b269c1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-titan.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/bedrock/bedrock-titan.adoc @@ -136,7 +136,7 @@ public class ChatController { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/ollama-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/ollama-chat.adoc index bcb44023d..fde279c89 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/ollama-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/ollama-chat.adoc @@ -153,7 +153,7 @@ public class ChatController { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/openai-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/openai-chat.adoc index dabb65eaa..619c1c6be 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/openai-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/openai-chat.adoc @@ -134,12 +134,12 @@ public class ChatController { this.chatClient = chatClient; } - @GetMapping("/open-ai/generate") + @GetMapping("/ai/generate") public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/vertexai-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/vertexai-chat.adoc index 9f0943757..26aa0cfce 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/vertexai-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/vertexai-chat.adoc @@ -10,7 +10,7 @@ To access the PaLM2 REST API you need to obtain an access API KEY form link:http NOTE: Currently the PaLM API it is not available outside US, but you can use VPN for testing. -The Spring AI project defines a configuration property named `spring.ai.vertex.ai.api-key` that you should set to the value of the `API Key` obtained from openai.com. +The Spring AI project defines a configuration property named `spring.ai.vertex.ai.api-key` that you should set to the value of the `API Key` obtained. Exporting an environment variable is one way to set that configuration property: [source,shell] @@ -45,7 +45,7 @@ TIP: Refer to the xref:getting-started.adoc#_dependency_management[Dependency Ma === Chat Properties -The prefix `spring.ai.vertex.ai` is used as the property prefix that lets you connect to OpenAI. +The prefix `spring.ai.vertex.ai` is used as the property prefix that lets you connect to VertexAI. [cols="3,5,1"] |==== @@ -72,7 +72,7 @@ TIP: All properties prefixed with `spring.ai.vertex.ai.chat.options` can be over === Chat Options [[chat-options]] -The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertex/VertexAiChatOptions.java[VertexAiChatOptions.java] provides model configurations, such as the temperature, the topK, etc. +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai/src/main/java/org/springframework/ai/vertex/VertexAiChatOptions.java[VertexAiChatOptions.java] provides model configurations, such as the temperature, the topK, etc. On start-up, the default options can be configured with the `VertexAiChatClient(api, options)` constructor or the `spring.ai.vertex.ai.chat.options.*` properties. @@ -94,7 +94,7 @@ TIP: In addition to the model specific `VertexAiChatOptions` you can use a porta === Sample Controller (Auto-configuration) -https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-openai-spring-boot-starter` to your pom (or gradle) dependencies. +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-vertex-ai-spring-boot-starter` to your pom (or gradle) dependencies. Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the VertexAi Chat client: @@ -122,12 +122,12 @@ public class ChatController { this.chatClient = chatClient; } - @GetMapping("/open-ai/generate") + @GetMapping("/ai/generate") public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { return Map.of("generation", chatClient.call(message)); } - @GetMapping("/open-ai/generateStream") + @GetMapping("/ai/generateStream") public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { Prompt prompt = new Prompt(new UserMessage(message)); return chatClient.stream(prompt); @@ -137,7 +137,7 @@ public class ChatController { == Manual Configuration -The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertex/VertexAiChatClient.java[OpenAiChatClient] implements the `ChatClient` and uses the <> to connect to the VertexAI service. +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertex/VertexAiChatClient.java[VertexAiChatClient] implements the `ChatClient` and uses the <> to connect to the VertexAI service. Add the `spring-ai-vertex-ai` dependency to your project's Maven `pom.xml` file: @@ -181,7 +181,7 @@ The `VertexAiChatOptions.Builder` is fluent options builder. === Low-level VertexAiApi Client [[low-level-api]] -The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertex/api/VertexAiApi.java[VertexAiApi] provides is lightweight Java client for VertexAiApi Chat API. +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai/src/main/java/org/springframework/ai/vertex/api/VertexAiApi.java[VertexAiApi] provides is lightweight Java client for VertexAiApi Chat API. Following class diagram illustrates the `VertexAiApi` chat interfaces and building blocks: diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings.adoc index aa82d3855..79ffe4fb5 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings.adoc @@ -157,3 +157,4 @@ Internally the various `EmbeddingClient` implementations use different low-level * xref:api/embeddings/postgresml-embeddings.adoc[Spring AI PostgresML Embeddings] * xref:api/embeddings/bedrock-cohere-embedding.adoc[Spring AI Bedrock Cohere Embeddings] * xref:api/embeddings/bedrock-titan-embedding.adoc[Spring AI Bedrock Titan Embeddings] +* xref:api/embeddings/vertexai-embeddings.adoc[Spring AI VertexAI Embeddings] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-cohere-embedding.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-cohere-embedding.adoc index 6f2db5979..76fc17220 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-cohere-embedding.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-cohere-embedding.adoc @@ -1,4 +1,4 @@ -= Cohere Embedding += Cohere Embeddings Provides Bedrock Cohere Embedding client. Integrate generative AI capabilities into essential apps and workflows that improve business outcomes. @@ -168,7 +168,7 @@ var cohereEmbeddingApi =new CohereEmbeddingBedrockApi( EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper()); -var embeddingClient new BedrockCohereEmbeddingClient(cohereEmbeddingApi); +var embeddingClient = new BedrockCohereEmbeddingClient(cohereEmbeddingApi); EmbeddingResponse embeddingResponse = embeddingClient .embedForResponse(List.of("Hello World", "World is big and salvation is near")); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-titan-embedding.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-titan-embedding.adoc index dd25e6d5b..4ce76fbb1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-titan-embedding.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-titan-embedding.adoc @@ -1,4 +1,4 @@ -= Titan Embedding += Titan Embeddings Provides Bedrock Titan Embedding client. link:https://aws.amazon.com/bedrock/titan/[Amazon Titan] foundation models (FMs) provide customers with a breadth of high-performing image, multimodal, and text model choices, via a fully managed API. diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/vertexai-embeddings.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/vertexai-embeddings.adoc new file mode 100644 index 000000000..e5188e31b --- /dev/null +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/vertexai-embeddings.adoc @@ -0,0 +1,172 @@ += VertexAI Embeddings + +The link:https://developers.generativeai.google/api/rest/generativelanguage[Generative Language] PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to comprehend and generate natural language through a series of prompts. The PaLM API is based on Google's next generation LLM, PaLM. It excels at a variety of different tasks like code generation, reasoning, and writing. You can use the PaLM API to build generative AI applications for use cases like content generation, dialogue agents, summarization and classification systems, and more. + +Based on the link:https://developers.generativeai.google/api/rest/generativelanguage/models[Models REST API]. + +== Prerequisites + +To access the PaLM2 REST API you need to obtain an access API KEY form link:https://makersuite.google.com/app/apikey[makersuite]. + +NOTE: Currently the PaLM API it is not available outside US, but you can use VPN for testing. + +The Spring AI project defines a configuration property named `spring.ai.vertex.ai.api-key` that you should set to the value of the `API Key` obtained. +Exporting an environment variable is one way to set that configuration property: + +[source,shell] +---- +export SPRING_AI_VERTEX_AI_API_KEY= +---- + +== Auto-configuration + +Spring AI provides Spring Boot auto-configuration for the VertexAI Embedding Client. +To enable it add the following dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-vertex-ai-spring-boot-starter + 0.8.0-SNAPSHOT + +---- + +or to your Gradle `build.gradle` build file. + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-vertex-ai-spring-boot-starter:0.8.0-SNAPSHOT' +} +---- + +TIP: Refer to the xref:getting-started.adoc#_dependency_management[Dependency Management] section to add Milestone and/or Snapshot Repositories to your build file. + +=== Chat Properties + +The prefix `spring.ai.vertex.ai` is used as the property prefix that lets you connect to VertexAI. + +[cols="3,5,1"] +|==== +| Property | Description | Default + +| spring.ai.vertex.ai.ai.base-url | The URL to connect to | https://generativelanguage.googleapis.com/v1beta3 +| spring.ai.vertex.ai.api-key | The API Key | - +|==== + +The prefix `spring.ai.vertex.ai.chat` is the property prefix that lets you configure the chat client implementation for VertexAI Chat. + +[cols="3,5,1"] +|==== +| Property | Description | Default + +| spring.ai.vertex.ai.chat.model | This is the https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text-embeddings[Vertex Embedding model] to use | chat-bison-001 +|==== + + +=== Sample Controller (Auto-configuration) + +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-vertex-ai-spring-boot-starter` to your pom (or gradle) dependencies. + +Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the VertexAi Chat client: + +[source,application.properties] +---- +spring.ai.vertex.ai.api-key=YOUR_API_KEY +spring.ai.vertex.ai.embedding.model=chat-bison-001 +---- + +TIP: replace the `api-key` with your VertexAI credentials. + +This will create a `VertexAiEmbeddingClient` implementation that you can inject into your class. +Here is an example of a simple `@Controller` class that uses the chat client for text generations. + +[source,java] +---- +@RestController +public class EmbeddingController { + + private final EmbeddingClient embeddingClient; + + @Autowired + public EmbeddingController(EmbeddingClient embeddingClient) { + this.embeddingClient = embeddingClient; + } + + @GetMapping("/ai/embedding") + public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { + EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message)); + return Map.of("embedding", embeddingResponse); + } +} +---- + +== Manual Configuration + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai/src/main/java/org/springframework/ai/vertex/VertexAiEmbeddingClient.java[VertexAiEmbeddingClient] implements the `EmbeddingClient` and uses the <> to connect to the VertexAI service. + +Add the `spring-ai-vertex-ai` dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-vertex-ai + 0.8.0-SNAPSHOT + +---- + +or to your Gradle `build.gradle` build file. + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-vertex-ai:0.8.0-SNAPSHOT' +} +---- + +TIP: Refer to the xref:getting-started.adoc#_dependency_management[Dependency Management] section to add Milestone and/or Snapshot Repositories to your build file. + +Next, create a `VertexAiEmbeddingClient` and use it for text generations: + +[source,java] +---- +VertexAiApi vertexAiApi = new VertexAiApi(< YOUR PALM_API_KEY>); + +var embeddingClient = new VertexAiEmbeddingClient(vertexAiApi); + +EmbeddingResponse embeddingResponse = embeddingClient + .embedForResponse(List.of("Hello World", "World is big and salvation is near")); +---- + +=== Low-level VertexAiApi Client [[low-level-api]] + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai/src/main/java/org/springframework/ai/vertex/api/VertexAiApi.java[VertexAiApi] provides is lightweight Java client for VertexAiApi Chat API. + +Following class diagram illustrates the `VertexAiApi` chat interfaces and building blocks: + +image::vertex-ai-chat-low-level-api.jpg[w=800,align="center"] + +Here is a simple snippet how to use the api programmatically: + +[source,java] +---- +VertexAiApi vertexAiApi = new VertexAiApi(< YOUR PALM_API_KEY>); + +// Generate +var prompt = new MessagePrompt(List.of(new Message("0", "Hello, how are you?"))); + +GenerateMessageRequest request = new GenerateMessageRequest(prompt); + +GenerateMessageResponse response = vertexAiApi.generateMessage(request); + +// Embed text +Embedding embedding = vertexAiApi.embedText("Hello, how are you?"); + +// Batch embedding +List embeddings = vertexAiApi.batchEmbedText(List.of("Hello, how are you?", "I am fine, thank you!")); +---- + + +