|
| 1 | +# ADK LangChain4j Integration Library |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The ADK LangChain4j Integration library provides a bridge between the Agent Development Kit (ADK) and |
| 6 | +[LangChain4j](https://docs.langchain4j.dev/). |
| 7 | +The main purpose of this module is to allow ADK to have access to all the LLM providers supported |
| 8 | +by the LangChain4j ecosystem (e.g., Anthropic, OpenAI, Ollama, Google Gemini, and many more). |
| 9 | + |
| 10 | +This library supports multiple AI providers, function calling (tools), streaming responses, |
| 11 | +and automatically maps ADK models and mechanisms to their LangChain4j counterparts. |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +### Maven Dependencies |
| 16 | + |
| 17 | +To use ADK Java with the LangChain4j integration in your application, |
| 18 | +add the following dependencies to your `pom.xml`: |
| 19 | + |
| 20 | +#### Basic Setup |
| 21 | + |
| 22 | +```xml |
| 23 | +<dependencies> |
| 24 | + <!-- ADK Core --> |
| 25 | + <dependency> |
| 26 | + <groupId>com.google.adk</groupId> |
| 27 | + <artifactId>google-adk</artifactId> |
| 28 | + <version>1.0.0</version> <!-- Use the most recent ADK version --> |
| 29 | + </dependency> |
| 30 | + |
| 31 | + <!-- ADK LangChain4j Integration --> |
| 32 | + <dependency> |
| 33 | + <groupId>com.google.adk</groupId> |
| 34 | + <artifactId>google-adk-langchain4j</artifactId> |
| 35 | + <version>1.0.0</version> <!-- Use the most recent ADK version --> |
| 36 | + </dependency> |
| 37 | +</dependencies> |
| 38 | +``` |
| 39 | + |
| 40 | +#### Provider-Specific Dependencies |
| 41 | + |
| 42 | +You'll also need to add the LangChain4j provider dependencies for the AI services you want to use. |
| 43 | +Refer to the [full list](https://docs.langchain4j.dev/category/language-models) |
| 44 | +of supported models that you want to use in your project. |
| 45 | + |
| 46 | +**Anthropic** (Claude): |
| 47 | +```xml |
| 48 | +<dependency> |
| 49 | + <groupId>dev.langchain4j</groupId> |
| 50 | + <artifactId>langchain4j-anthropic</artifactId> |
| 51 | +</dependency> |
| 52 | +``` |
| 53 | + |
| 54 | +**OpenAI** and compatible models: |
| 55 | +```xml |
| 56 | +<dependency> |
| 57 | + <groupId>dev.langchain4j</groupId> |
| 58 | + <artifactId>langchain4j-open-ai</artifactId> |
| 59 | +</dependency> |
| 60 | +``` |
| 61 | + |
| 62 | +**Ollama** (local models): |
| 63 | +```xml |
| 64 | +<dependency> |
| 65 | + <groupId>dev.langchain4j</groupId> |
| 66 | + <artifactId>langchain4j-ollama</artifactId> |
| 67 | +</dependency> |
| 68 | +``` |
| 69 | + |
| 70 | +*(You can use any other `langchain4j-*` module supported by LangChain4j).* |
| 71 | + |
| 72 | +## Quick Start Examples |
| 73 | + |
| 74 | +Once you have the dependencies set up, you can create a simple ADK agent using any LangChain4j chat model. |
| 75 | +You just need to wrap the `ChatModel` into the ADK `LangChain4j` model builder. |
| 76 | + |
| 77 | +### Anthropic Example |
| 78 | + |
| 79 | +```java |
| 80 | +import com.google.adk.agents.LlmAgent; |
| 81 | +import com.google.adk.models.langchain4j.LangChain4j; |
| 82 | +import dev.langchain4j.model.anthropic.AnthropicChatModel; |
| 83 | + |
| 84 | +// 1. Initialize LangChain4j model |
| 85 | +AnthropicChatModel claudeModel = AnthropicChatModel.builder() |
| 86 | + .apiKey(System.getenv("ANTHROPIC_API_KEY")) |
| 87 | + .modelName("claude-sonnet-4-6") |
| 88 | + .build(); |
| 89 | + |
| 90 | +// 2. Wrap the LangChain4j model for ADK |
| 91 | +LangChain4j adkModel = LangChain4j.builder() |
| 92 | + .chatModel(claudeModel) |
| 93 | + .modelName("claude-sonnet-4-6") |
| 94 | + .build(); |
| 95 | + |
| 96 | +// 3. Create your agent |
| 97 | +LlmAgent agent = LlmAgent.builder() |
| 98 | + .name("science-teacher") |
| 99 | + .description("A helpful science teacher") |
| 100 | + .model(adkModel) |
| 101 | + .instruction("You are a helpful science teacher that explains concepts clearly.") |
| 102 | + .build(); |
| 103 | +``` |
| 104 | + |
| 105 | +### OpenAI Example |
| 106 | + |
| 107 | +```java |
| 108 | +import com.google.adk.agents.LlmAgent; |
| 109 | +import com.google.adk.models.langchain4j.LangChain4j; |
| 110 | +import dev.langchain4j.model.openai.OpenAiChatModel; |
| 111 | + |
| 112 | +// 1. Initialize LangChain4j model |
| 113 | +OpenAiChatModel gptModel = OpenAiChatModel.builder() |
| 114 | + .apiKey(System.getenv("OPENAI_API_KEY")) |
| 115 | + .modelName("gpt-4o-mini") |
| 116 | + .build(); |
| 117 | + |
| 118 | +// 2. Wrap the LangChain4j model for ADK |
| 119 | +LangChain4j adkModel = LangChain4j.builder() |
| 120 | + .chatModel(gptModel) |
| 121 | + .modelName("gpt-4o-mini") |
| 122 | + .build(); |
| 123 | + |
| 124 | +// 3. Create your agent |
| 125 | +LlmAgent agent = LlmAgent.builder() |
| 126 | + .name("friendly-assistant") |
| 127 | + .description("A friendly assistant") |
| 128 | + .model(adkModel) |
| 129 | + .instruction("You are a friendly assistant.") |
| 130 | + .build(); |
| 131 | +``` |
| 132 | + |
| 133 | +### Ollama Example (Local Models) |
| 134 | + |
| 135 | +```java |
| 136 | +import com.google.adk.agents.LlmAgent; |
| 137 | +import com.google.adk.models.langchain4j.LangChain4j; |
| 138 | +import dev.langchain4j.model.ollama.OllamaChatModel; |
| 139 | + |
| 140 | +// 1. Initialize LangChain4j model for Ollama |
| 141 | +OllamaChatModel ollamaModel = OllamaChatModel.builder() |
| 142 | + .baseUrl("http://localhost:11434") |
| 143 | + .modelName("llama3") |
| 144 | + .build(); |
| 145 | + |
| 146 | +// 2. Wrap the LangChain4j model for ADK |
| 147 | +LangChain4j adkModel = LangChain4j.builder() |
| 148 | + .chatModel(ollamaModel) |
| 149 | + .modelName("llama3") |
| 150 | + .build(); |
| 151 | + |
| 152 | +// 3. Create your agent |
| 153 | +LlmAgent agent = LlmAgent.builder() |
| 154 | + .name("local-agent") |
| 155 | + .description("A local assistant running on Ollama") |
| 156 | + .model(adkModel) |
| 157 | + .instruction("You are an assistant running locally.") |
| 158 | + .build(); |
| 159 | +``` |
| 160 | + |
| 161 | +## Advanced Usage |
| 162 | + |
| 163 | +### Streaming Responses |
| 164 | + |
| 165 | +The integration fully supports streaming models from LangChain4j using the `streamingChatModel` property of the `LangChain4j` builder. |
| 166 | +Make sure to use a `StreamingChatModel` instead of a regular `ChatModel` from LangChain4j. |
| 167 | + |
| 168 | +```java |
| 169 | +import dev.langchain4j.model.anthropic.AnthropicStreamingChatModel; |
| 170 | + |
| 171 | +// 1. Initialize a LangChain4j STREAMING chat model |
| 172 | +AnthropicStreamingChatModel streamingModel = AnthropicStreamingChatModel.builder() |
| 173 | + .apiKey(System.getenv("ANTHROPIC_API_KEY")) |
| 174 | + .modelName("claude-sonnet-4-6") |
| 175 | + .build(); |
| 176 | + |
| 177 | +// 2. Wrap it for ADK using `streamingChatModel` config |
| 178 | +LangChain4j adkStreamingModel = LangChain4j.builder() |
| 179 | + .streamingChatModel(streamingModel) |
| 180 | + .modelName("claude-sonnet-4-6") |
| 181 | + .build(); |
| 182 | + |
| 183 | +// 3. Create your agent as usual |
| 184 | +LlmAgent agent = LlmAgent.builder() |
| 185 | + .name("streaming-agent") |
| 186 | + .model(adkStreamingModel) |
| 187 | + .instruction("You answer questions as fast as possible.") |
| 188 | + .build(); |
| 189 | +``` |
| 190 | + |
| 191 | +### Function Calling (Tools) |
| 192 | + |
| 193 | +ADK tools are automatically mapped to LangChain4j tools under the hood. |
| 194 | +You can configure them as you usually do in ADK: |
| 195 | + |
| 196 | +```java |
| 197 | +LlmAgent agent = LlmAgent.builder() |
| 198 | + .name("weather-agent") |
| 199 | + .model(adkModel) |
| 200 | + .instruction("If asked about weather, you MUST call the `getWeather` function.") |
| 201 | + .tools(FunctionTool.create(ToolExample.class, "getWeather")) |
| 202 | + .build(); |
| 203 | +``` |
0 commit comments