Class LangChain4jSupport
This class provides factory methods to create Tasks and JudgeLMs
from LangChain4j components.
RAG Evaluation
// 1. Define your AiService to return Result<String>
interface Assistant {
Result<String> chat(String userMessage);
}
// 2. Build your assistant
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.retrievalAugmentor(DefaultRetrievalAugmentor.builder()
.queryTransformer(compressingQueryTransformer)
.contentRetriever(retriever)
.contentAggregator(reRankingAggregator)
.build())
.build();
// 3. Create a Task for evaluation
Task task = LangChain4jSupport.ragTask(assistant::chat);
// 4. Run evaluation with some metrics
Experiment.builder()
.task(task)
.evaluators(List.of(faithfulness, contextRelevancy))
.build()
.run();
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic JudgeLMasJudge(dev.langchain4j.model.chat.ChatModel model) Creates aJudgeLMfrom a LangChain4jChatModel.static AsyncTaskasyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall) Creates anAsyncTaskfor RAG evaluation from a function that returnsResult.static AsyncTaskasyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey) Creates anAsyncTaskfor RAG evaluation with custom key names.static AsyncTaskasyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey, Executor executor) Creates anAsyncTaskfor RAG evaluation that dispatches each blocking assistant call on the suppliedExecutor(or the commonForkJoinPoolwhenexecutorisnull).static AsyncTaskasyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, Executor executor) static AsyncTaskasyncTask(dev.langchain4j.model.chat.ChatModel model) Creates a simpleAsyncTaskfor Q&A evaluation from a LangChain4jChatModel.static AsyncTaskCreates a simpleAsyncTaskfor Q&A evaluation that writes the response under a caller-chosen key.static AsyncTaskCreates a simpleAsyncTaskfor Q&A evaluation that dispatches each blockingmodel.chat(...)call on the suppliedExecutor(or the commonForkJoinPoolwhenexecutorisnull).static AsyncTaskstatic TaskcustomTask(Task taskFunction) Creates a flexibleTaskthat allows full control over output mapping.extractTexts(List<dev.langchain4j.rag.content.Content> contents) Extracts text content from a list of LangChain4jContentobjects.extractTextsWithMetadata(List<dev.langchain4j.rag.content.Content> contents) Extracts text content with metadata from a list of LangChain4jContentobjects.static MeasuredTaskmeasuredRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String modelId, PriceTable prices) Creates a measured RAGMeasuredTaskfrom a function returningResult, capturing thetoken usage, latency, and (when aPriceTableis supplied) cost alongside the output and retrieved context.static MeasuredTaskmeasuredRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey, String modelId, PriceTable prices) Creates a measured RAGMeasuredTaskwith custom key names.static MeasuredTaskmeasuredTask(dev.langchain4j.model.chat.ChatModel model, String modelId, PriceTable prices) Creates a measured Q&AMeasuredTaskthat captures token usage, latency, and (when aPriceTableis supplied) cost, lighting up the run's metrics cards.static MeasuredTaskmeasuredTask(dev.langchain4j.model.chat.ChatModel model, String modelId, PriceTable prices, String outputKey) Creates a measured Q&AMeasuredTaskthat writes the response under a caller-chosen key.static TaskCreates a RAG evaluationTaskfrom a function that returnsResult.static TaskragTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey) Creates a RAG evaluationTaskwith custom key names.static TasksimpleTask(dev.langchain4j.model.chat.ChatModel model) Creates a simpleTaskfor Q&A evaluation.static TasksimpleTask(dev.langchain4j.model.chat.ChatModel model, String outputKey) Creates a simpleTaskfor Q&A evaluation that writes the response under a caller-chosen key.static AgentTracetoAgentTrace(dev.langchain4j.service.Result<?> result) Builds anAgentTracefrom a LangChain4jResult.static ToolCalltoToolCall(dev.langchain4j.service.tool.ToolExecution execution) Converts a single LangChain4jToolExecutionto aToolCall.toToolCalls(dev.langchain4j.service.Result<?> result) ExtractsToolCalls from a LangChain4jResultin execution order.static ToolDefinitiontoToolDefinition(dev.langchain4j.agent.tool.ToolSpecification specification) Converts a singleToolSpecificationto aToolDefinition.static List<ToolDefinition> toToolDefinitions(List<dev.langchain4j.agent.tool.ToolSpecification> specifications) Converts LangChain4jToolSpecifications toToolDefinitions so tool calls can be evaluated against the tools the agent was given.
-
Field Details
-
OUTPUT_KEY
Default key for the model output in evaluation results.- See Also:
-
CONTEXT_KEY
Default key for additional context in evaluation results.- See Also:
-
INPUT_KEY
Default key for reading input from dataset examples.- See Also:
-
-
Method Details
-
asJudge
Creates aJudgeLMfrom a LangChain4jChatModel.Use this to create judges for LLM-based evaluators like
LLMJudgeEvaluator,FaithfulnessEvaluator, etc.Example:
ChatModel gemini = VertexAiGeminiChatModel.builder()...build(); JudgeLM judge = LangChain4jSupport.asJudge(gemini); var evaluator = LLMJudgeEvaluator.builder() .judge(judge) .criteria("Is the response helpful?") .build();- Parameters:
model- the ChatModel to use as judge- Returns:
- a JudgeLM that delegates to the ChatModel
-
simpleTask
Creates a simpleTaskfor Q&A evaluation.The task reads "input" from the example and returns a Map with "output".
Example:
ChatModel model = OpenAiChatModel.builder()...build(); Task task = LangChain4jSupport.simpleTask(model); // Dataset examples just need "input" Example example = Example.of("What is 2+2?", "4");- Parameters:
model- the ChatModel to evaluate- Returns:
- a Task suitable for the Experiment
-
simpleTask
Creates a simpleTaskfor Q&A evaluation that writes the response under a caller-chosen key.Behaves like
simpleTask(ChatModel)but lets you override thedefault output keywhen your evaluators or dataset expect a different name.Example:
ChatModel model = OpenAiChatModel.builder()...build(); Task task = LangChain4jSupport.simpleTask(model, "answer");- Parameters:
model- the ChatModel to evaluateoutputKey- the key for the output in the result map- Returns:
- a Task suitable for the Experiment
-
ragTask
Creates a RAG evaluationTaskfrom a function that returnsResult.This is the primary integration point for RAG evaluation. LangChain4j's Result class already contains the retrieved sources via
result.sources().Example:
interface Assistant { Result<String> chat(String userMessage); } Assistant assistant = AiServices.builder(Assistant.class) .chatModel(chatModel) .retrievalAugmentor(retrievalAugmentor) .build(); Task task = LangChain4jSupport.ragTask(assistant::chat);- Parameters:
assistantCall- a function that takes the input string and returns a Result- Returns:
- a Task suitable for evaluation
-
ragTask
public static Task ragTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey) Creates a RAG evaluationTaskwith custom key names.Use this when your dataset or evaluators expect different keys.
Example:
// Dataset uses "question" instead of "input" Task task = LangChain4jSupport.ragTask( assistant::chat, "question", // input key "answer", // output key "retrievalContext" // context key );- Parameters:
assistantCall- a function that takes the input string and returns a ResultinputKey- the key to read from example inputsoutputKey- the key for the output in the result mapcontextKey- the key for the retrieval context in the result map- Returns:
- a Task suitable for RAG evaluation
-
customTask
Creates a flexibleTaskthat allows full control over output mapping.Use this for complex scenarios where you want to capture additional data beyond what the standard RAG task implementation provides.
Example:
Task task = LangChain4jSupport.customTask(example -> { String query = example.input(); // Track the latency long start = System.currentTimeMillis(); Result<String> result = assistant.chat(query); long duration = System.currentTimeMillis() - start; return Map.of( "output", result.content(), "context", LangChain4jSupport.extractTexts(result.sources()), "latencyMs", duration, "sourceCount", result.sources().size() ); });- Parameters:
taskFunction- a function that takes an Example and returns outputs- Returns:
- a Task suitable for Experiment
-
measuredTask
public static MeasuredTask measuredTask(dev.langchain4j.model.chat.ChatModel model, String modelId, PriceTable prices) Creates a measured Q&AMeasuredTaskthat captures token usage, latency, and (when aPriceTableis supplied) cost, lighting up the run's metrics cards.This is the metrics-bearing counterpart to
simpleTask(ChatModel). Where the plainsimpleTaskreturns aTaskwhose result structurally cannot carryCallMetrics, this returns aMeasuredTask, so switch the builder call from.task(...)to.measuredTask(...):PriceTable prices = (model, in, out) -> ...; // your price map, or null for tokens+latency only Experiment.builder() .measuredTask(LangChain4jSupport.measuredTask(model, "<your-model>", prices)) .evaluators(...) .build() .run();The call uses the
ChatRequest-based overload ofChatModelso theChatResponse'sTokenUsageis available; the String-returningchat(String)used bysimpleTaskdoes not expose usage. When usage is absent the token fields are null; whenpricesis null (or returns null) the cost stays null and only the Tokens and Latency cards light up. Never throws on missing metrics.- Parameters:
model- the ChatModel to evaluate, never nullmodelId- the model id used as thePriceTablelookup key, or null to skip pricingprices- the price lookup, or null to capture tokens and latency only- Returns:
- a MeasuredTask suitable for
Experiment.builder().measuredTask(...) - Throws:
IllegalArgumentException- ifmodelis null
-
measuredTask
public static MeasuredTask measuredTask(dev.langchain4j.model.chat.ChatModel model, String modelId, PriceTable prices, String outputKey) Creates a measured Q&AMeasuredTaskthat writes the response under a caller-chosen key.Behaves like
measuredTask(ChatModel, String, PriceTable)but lets you override thedefault output key.- Parameters:
model- the ChatModel to evaluate, never nullmodelId- the model id used as thePriceTablelookup key, or null to skip pricingprices- the price lookup, or null to capture tokens and latency onlyoutputKey- the key for the output in the result map, never null- Returns:
- a MeasuredTask suitable for
Experiment.builder().measuredTask(...) - Throws:
IllegalArgumentException- ifmodeloroutputKeyis null
-
measuredRagTask
public static MeasuredTask measuredRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String modelId, PriceTable prices) Creates a measured RAGMeasuredTaskfrom a function returningResult, capturing thetoken usage, latency, and (when aPriceTableis supplied) cost alongside the output and retrieved context.Metrics-bearing counterpart to
ragTask(Function); use.measuredTask(...)on the builder. When the Result carries no usage the token fields are null; a nullprices(or a null lookup result) leaves cost null and lights only the Tokens and Latency cards.- Parameters:
assistantCall- a function that takes the input string and returns a Result, never nullmodelId- the model id used as thePriceTablelookup key, or null to skip pricingprices- the price lookup, or null to capture tokens and latency only- Returns:
- a MeasuredTask suitable for RAG evaluation
- Throws:
IllegalArgumentException- ifassistantCallis null
-
measuredRagTask
public static MeasuredTask measuredRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey, String modelId, PriceTable prices) Creates a measured RAGMeasuredTaskwith custom key names.- Parameters:
assistantCall- a function that takes the input string and returns a Result, never nullinputKey- the key to read from example inputs, never nulloutputKey- the key for the output in the result map, never nullcontextKey- the key for the retrieval context in the result map, never nullmodelId- the model id used as thePriceTablelookup key, or null to skip pricingprices- the price lookup, or null to capture tokens and latency only- Returns:
- a MeasuredTask suitable for RAG evaluation
- Throws:
IllegalArgumentException- ifassistantCall,inputKey,outputKey, orcontextKeyis null
-
asyncRagTask
public static AsyncTask asyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall) Creates anAsyncTaskfor RAG evaluation from a function that returnsResult.Async version of
ragTask(Function): the blocking assistant call is dispatched on the commonForkJoinPoolviaCompletableFuture.supplyAsync(java.util.function.Supplier), so the experiment's async execution path can keep many calls in flight without a thread blocked per example. The output and retrieved context are written under thedefaultandcontextkeys.Note: because the call blocks on the common pool, the experiment's
parallelismbounds how many invocations are launched, but the effective concurrency of the blocking call is also limited by the common pool (~one less than the CPU count), which is shared process-wide. For higher, isolated concurrency use theExecutor-accepting overload (asyncRagTask(Function, java.util.concurrent.Executor)) to run calls on a pool you control.Example:
interface Assistant { Result<String> chat(String userMessage); } Assistant assistant = AiServices.builder(Assistant.class) .chatModel(chatModel) .retrievalAugmentor(retrievalAugmentor) .build(); AsyncTask task = LangChain4jSupport.asyncRagTask(assistant::chat); Experiment.builder() .asyncTask(task) .parallelism(8) .evaluators(List.of(faithfulness, contextRelevancy)) .build() .run();- Parameters:
assistantCall- a function that takes the input string and returns a Result, never null- Returns:
- an AsyncTask suitable for
Experiment.builder().asyncTask(...) - Throws:
IllegalArgumentException- ifassistantCallis null
-
asyncRagTask
public static AsyncTask asyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, Executor executor) Creates anAsyncTaskfor RAG evaluation with default key names, dispatching each blocking assistant call on the suppliedExecutorso you control and isolate concurrency.- Parameters:
assistantCall- a function that takes the input string and returns a Result, never nullexecutor- the executor each blocking call runs on, never null- Returns:
- an AsyncTask suitable for RAG evaluation
- Throws:
IllegalArgumentException- ifassistantCallorexecutoris null
-
asyncRagTask
public static AsyncTask asyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey) Creates anAsyncTaskfor RAG evaluation with custom key names.Behaves like
asyncRagTask(Function)but reads the input frominputKeyand writes the output and context underoutputKeyandcontextKey, for datasets or evaluators that use different key names.- Parameters:
assistantCall- a function that takes the input string and returns a Result, never nullinputKey- the key to read from example inputs, never nulloutputKey- the key for the output in the result map, never nullcontextKey- the key for the retrieval context in the result map, never null- Returns:
- an AsyncTask suitable for RAG evaluation
- Throws:
IllegalArgumentException- if any argument is null
-
asyncRagTask
public static AsyncTask asyncRagTask(Function<String, dev.langchain4j.service.Result<String>> assistantCall, String inputKey, String outputKey, String contextKey, Executor executor) Creates anAsyncTaskfor RAG evaluation that dispatches each blocking assistant call on the suppliedExecutor(or the commonForkJoinPoolwhenexecutorisnull).Supplying an executor lets you control and isolate concurrency: the experiment's
parallelismbounds in-flight invocations, and a pool sized to match gives true parallel blocking calls instead of the common pool's ~CPU-count, process-wide ceiling.- Parameters:
assistantCall- a function that takes the input string and returns a Result, never nullinputKey- the key to read from example inputs, never nulloutputKey- the key for the output in the result map, never nullcontextKey- the key for the retrieval context in the result map, never nullexecutor- the executor each blocking call runs on, ornullfor the common pool- Returns:
- an AsyncTask suitable for RAG evaluation
- Throws:
IllegalArgumentException- ifassistantCall,inputKey,outputKey, orcontextKeyis null
-
asyncTask
Creates a simpleAsyncTaskfor Q&A evaluation from a LangChain4jChatModel.Async version of
simpleTask(ChatModel): the blockingmodel.chat(...)call is dispatched on the commonForkJoinPoolviaCompletableFuture.supplyAsync(java.util.function.Supplier). The response is written under thedefault output key.Example:
ChatModel model = OpenAiChatModel.builder()...build(); AsyncTask task = LangChain4jSupport.asyncTask(model);- Parameters:
model- the ChatModel to evaluate, never null- Returns:
- an AsyncTask suitable for
Experiment.builder().asyncTask(...) - Throws:
IllegalArgumentException- ifmodelis null
-
asyncTask
Creates a simpleAsyncTaskfor Q&A evaluation with the default output key, dispatching each blockingmodel.chat(...)call on the suppliedExecutorso you control and isolate concurrency.- Parameters:
model- the ChatModel to evaluate, never nullexecutor- the executor each blocking call runs on, never null- Returns:
- an AsyncTask suitable for
Experiment.builder().asyncTask(...) - Throws:
IllegalArgumentException- ifmodelorexecutoris null
-
asyncTask
Creates a simpleAsyncTaskfor Q&A evaluation that writes the response under a caller-chosen key.Behaves like
asyncTask(ChatModel)but lets you override thedefault output key.- Parameters:
model- the ChatModel to evaluate, never nulloutputKey- the key for the output in the result map, never null- Returns:
- an AsyncTask suitable for
Experiment.builder().asyncTask(...) - Throws:
IllegalArgumentException- if any argument is null
-
asyncTask
public static AsyncTask asyncTask(dev.langchain4j.model.chat.ChatModel model, String outputKey, Executor executor) Creates a simpleAsyncTaskfor Q&A evaluation that dispatches each blockingmodel.chat(...)call on the suppliedExecutor(or the commonForkJoinPoolwhenexecutorisnull).Supplying an executor lets you control and isolate concurrency: the experiment's
parallelismbounds in-flight invocations, and a pool sized to match gives true parallel blocking calls instead of the common pool's ~CPU-count, process-wide ceiling.- Parameters:
model- the ChatModel to evaluate, never nulloutputKey- the key for the output in the result map, never nullexecutor- the executor each blocking call runs on, ornullfor the common pool- Returns:
- an AsyncTask suitable for
Experiment.builder().asyncTask(...) - Throws:
IllegalArgumentException- ifmodeloroutputKeyis null
-
extractTexts
Extracts text content from a list of LangChain4jContentobjects.This is useful when building custom Tasks.
- Parameters:
contents- the list of Content from result.sources()- Returns:
- list of text strings, empty list if contents is null
-
extractTextsWithMetadata
public static List<Map<String,Object>> extractTextsWithMetadata(List<dev.langchain4j.rag.content.Content> contents) Extracts text content with metadata from a list of LangChain4jContentobjects.Returns a list of maps, where each map contains:
text- the segment textmetadata- the segment metadata as a map
This is useful when you need source attribution in evaluations.
- Parameters:
contents- the list of Content from result.sources()- Returns:
- list of maps containing text and metadata
-
toAgentTrace
Builds anAgentTracefrom a LangChain4jResult.The result's
content()becomes the final response and itstoolExecutions()becomeToolCalls carrying the tool name, parsed arguments, and the tool result string. Use it to evaluate tool-calling agents built withAiServicesthat returnResult<T>.Result<String> result = assistant.chat(userMessage); AgentTrace trace = LangChain4jSupport.toAgentTrace(result); EvalTestCase testCase = trace.toTestCase(userMessage, tools);- Parameters:
result- the LangChain4j result (may be null)- Returns:
- an agent trace, never null
-
toToolCalls
ExtractsToolCalls from a LangChain4jResultin execution order.- Parameters:
result- the result (may be null)- Returns:
- the tool calls, or an empty list when there are none
-
toToolCall
Converts a single LangChain4jToolExecutionto aToolCall.- Parameters:
execution- the tool execution- Returns:
- the tool call
-
toToolDefinitions
public static List<ToolDefinition> toToolDefinitions(List<dev.langchain4j.agent.tool.ToolSpecification> specifications) Converts LangChain4jToolSpecifications toToolDefinitions so tool calls can be evaluated against the tools the agent was given.- Parameters:
specifications- the tool specifications (may be null)- Returns:
- the tool definitions, or an empty list
-
toToolDefinition
public static ToolDefinition toToolDefinition(dev.langchain4j.agent.tool.ToolSpecification specification) Converts a singleToolSpecificationto aToolDefinition.- Parameters:
specification- the tool specification- Returns:
- the tool definition
-