CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-bedrock

AWS Bedrock integration for LangChain4j enabling Java applications to interact with various LLM providers through a unified interface

Overview
Eval results
Files

tool-calling.mddocs/guides/

Tool Calling (Function Calling)

Extend model capabilities with custom tools.

Defining 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;
    }
}

Using Tools with Chat Model

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 tool

Tool Specifications

import 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());

Multiple Tools

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();

Tool Execution

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")

Tool Call Handling

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
    }
}

Complete Example

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."

Supported Models

Not all Bedrock models support tool calling. Check model capabilities:

  • Claude 3.x series: ✓ Supported
  • Amazon Nova Pro: ✓ Supported
  • Amazon Nova Lite: ✓ Supported

Limitations

  • Model support: Only certain models support tool calling
  • Tool complexity: Complex tools may not work reliably
  • Tool count: Some models limit the number of tools per request
  • Execution: You're responsible for executing the tools

Related:

  • Chat Models API
  • Model Reference

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-bedrock

docs

index.md

README.md

tile.json