AWS Bedrock integration for LangChain4j enabling Java applications to interact with various LLM providers through a unified interface
Extend model capabilities with custom tools.
import dev.langchain4j.agent.tool.Tool;
public class WeatherService {
@Tool("Get current weather for a location")
public String getCurrentWeather(String location, String unit) {
// Implementation
return "Sunny, 72°F in " + location;
}
@Tool("Get weather forecast for next N days")
public String getForecast(String location, int days) {
// Implementation
return days + "-day forecast for " + location;
}
}import dev.langchain4j.service.AiServices;
interface WeatherAssistant {
String chat(String userMessage);
}
WeatherService weatherService = new WeatherService();
WeatherAssistant assistant = AiServices.builder(WeatherAssistant.class)
.chatLanguageModel(model)
.tools(weatherService)
.build();
String response = assistant.chat("What's the weather in Paris?");
// Model automatically calls getCurrentWeather toolimport dev.langchain4j.agent.tool.ToolSpecification;
List<ToolSpecification> tools = toolsFrom(new WeatherService());
ChatResponse response = model.chat(ChatRequest.builder()
.messages(UserMessage.from("What's the weather?"))
.toolSpecifications(tools)
.build());public class Calculator {
@Tool("Add two numbers")
int add(int a, int b) { return a + b; }
@Tool("Multiply two numbers")
int multiply(int a, int b) { return a * b; }
}
public class WebSearch {
@Tool("Search the web")
String search(String query) {
// Implementation
return "Search results for: " + query;
}
}
// Use multiple tool sources
WeatherAssistant assistant = AiServices.builder(WeatherAssistant.class)
.chatLanguageModel(model)
.tools(new Calculator(), new WebSearch(), new WeatherService())
.build();The model decides when to call tools based on user input:
// Model will call appropriate tool(s)
String response1 = assistant.chat("What's 5 + 3?");
// Calls Calculator.add(5, 3)
String response2 = assistant.chat("What's the weather in Tokyo?");
// Calls WeatherService.getCurrentWeather("Tokyo", "celsius")
String response3 = assistant.chat("Calculate 7 * 6 and check the weather in London");
// Calls both Calculator.multiply(7, 6) and WeatherService.getCurrentWeather("London", "celsius")ChatResponse response = model.chat(request);
// Check if model called tools
if (response.aiMessage().hasToolExecutionRequests()) {
List<ToolExecutionRequest> requests = response.aiMessage().toolExecutionRequests();
for (ToolExecutionRequest request : requests) {
String toolName = request.name();
String arguments = request.arguments();
// Execute tool and provide result back to model
}
}public class MathService {
@Tool("Add two numbers")
public int add(int a, int b) {
return a + b;
}
@Tool("Calculate factorial")
public long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
BedrockChatModel model = BedrockChatModel.builder()
.region(Region.US_EAST_1)
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.build();
interface MathAssistant {
String chat(String message);
}
MathAssistant assistant = AiServices.builder(MathAssistant.class)
.chatLanguageModel(model)
.tools(new MathService())
.build();
String response = assistant.chat("What is 5 + 3 and what is factorial of 5?");
System.out.println(response);
// "5 + 3 = 8, and the factorial of 5 is 120."Not all Bedrock models support tool calling. Check model capabilities:
Related: