|
| 1 | +# Documentation for the ADK Spring AI Library |
| 2 | + |
| 3 | +## 📖 Overview |
| 4 | +The `google-adk-spring-ai` library provides an integration layer between the Google Agent Development Kit (ADK) and the Spring AI project. It allows developers to use Spring AI's `ChatModel`, `StreamingChatModel`, and `EmbeddingModel` as `BaseLlm` and `Embedding` implementations within the ADK framework. |
| 5 | + |
| 6 | +The library handles the conversion between ADK's request/response formats and Spring AI's prompt/chat response formats. It also includes auto-configuration to automatically expose Spring AI models as ADK `SpringAI` and `SpringAIEmbedding` beans in a Spring Boot application. |
| 7 | + |
| 8 | +## 🛠️ Building |
| 9 | +To include this library in your project, use the following Maven coordinates: |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>com.google.adk</groupId> |
| 14 | + <artifactId>google-adk-spring-ai</artifactId> |
| 15 | + <version>0.3.1-SNAPSHOT</version> |
| 16 | +</dependency> |
| 17 | +``` |
| 18 | + |
| 19 | +You will also need to include a dependency for the specific Spring AI model you want to use, for example: |
| 20 | +```xml |
| 21 | +<dependency> |
| 22 | + <groupId>org.springframework.ai</groupId> |
| 23 | + <artifactId>spring-ai-openai</artifactId> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +## 🚀 Usage |
| 28 | +The primary way to use this library is through Spring Boot auto-configuration. By including the `google-adk-spring-ai` dependency and a Spring AI model dependency (e.g., `spring-ai-openai`), the library will automatically create a `SpringAI` bean. This bean can then be injected and used as a `BaseLlm` in the ADK. |
| 29 | + |
| 30 | +**Example `application.properties`:** |
| 31 | +```properties |
| 32 | +# OpenAI configuration |
| 33 | +spring.ai.openai.api-key=${OPENAI_API_KEY} |
| 34 | +spring.ai.openai.chat.options.model=gpt-4o-mini |
| 35 | +spring.ai.openai.chat.options.temperature=0.7 |
| 36 | + |
| 37 | +# ADK Spring AI configuration |
| 38 | +adk.spring-ai.model=gpt-4o-mini |
| 39 | +adk.spring-ai.validation.enabled=true |
| 40 | +``` |
| 41 | + |
| 42 | +**Example usage in a Spring service:** |
| 43 | +```java |
| 44 | +import com.google.adk.models.BaseLlm; |
| 45 | +import com.google.adk.models.LlmRequest; |
| 46 | +import com.google.adk.models.LlmResponse; |
| 47 | +import org.springframework.beans.factory.annotation.Autowired; |
| 48 | +import org.springframework.stereotype.Service; |
| 49 | +import reactor.core.publisher.Mono; |
| 50 | + |
| 51 | +@Service |
| 52 | +public class MyAgentService { |
| 53 | + |
| 54 | + private final BaseLlm llm; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + public MyAgentService(BaseLlm llm) { |
| 58 | + this.llm = llm; |
| 59 | + } |
| 60 | + |
| 61 | + public Mono<String> generateResponse(String prompt) { |
| 62 | + LlmRequest request = LlmRequest.builder() |
| 63 | + .addText(prompt) |
| 64 | + .build(); |
| 65 | + return Mono.from(llm.generateContent(request)) |
| 66 | + .map(llmResponse -> llmResponse.content().get().parts().get(0).text().get()); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## 📚 API Reference |
| 72 | +### Key Classes |
| 73 | +- **`SpringAI`**: The main class that wraps a Spring AI `ChatModel` and/or `StreamingChatModel` and implements the ADK `BaseLlm` interface. |
| 74 | + - **Methods**: |
| 75 | + - `generateContent(LlmRequest llmRequest, boolean stream)`: Generates content, either streaming or non-streaming, by calling the underlying Spring AI model. It converts the ADK `LlmRequest` to a Spring AI `Prompt` and the `ChatResponse` back to an ADK `LlmResponse`. |
| 76 | + |
| 77 | +- **`SpringAIEmbedding`**: Wraps a Spring AI `EmbeddingModel` to be used for generating embeddings within the ADK framework. |
| 78 | + |
| 79 | +- **`SpringAIAutoConfiguration`**: The Spring Boot auto-configuration class that automatically discovers and configures `SpringAI` and `SpringAIEmbedding` beans based on the `ChatModel`, `StreamingChatModel`, and `EmbeddingModel` beans present in the application context. |
| 80 | + |
| 81 | +- **`SpringAIProperties`**: A configuration properties class (`@ConfigurationProperties("adk.spring-ai")`) that allows for customization of the Spring AI integration. |
| 82 | + - **Properties**: |
| 83 | + - `model`: The model name to use. |
| 84 | + - `validation.enabled`: Whether to enable configuration validation. |
| 85 | + - `validation.fail-fast`: Whether to fail fast on validation errors. |
| 86 | + - `observability.enabled`: Whether to enable observability features. |
0 commit comments