Class AiServiceInvocation
java.lang.Object
dev.langchain4j.test.service.AiServiceInvocation
What happened during one AI Service invocation: what it produced, the tools it executed, the sources it retrieved,
the calls to models made during it (its own calls to its chat model and the tokens they used, and the calls of the
components it runs), how it ended, and the AI Service invocations that its work started (for example its tools,
guardrails or content retrievers).
Captured with AiServiceCapture (see AiServiceCapture.invocation()). Checked with
LangChain4jAssertions.assertThat(...).
The tool assertions mark the tool executions they check, so that
AiServiceInvocationAssert.hasNoMoreToolExecutions() can tell which ones were not checked. These marks are
kept in the invocation, so do not share a capture between tests: capture in each test.
-
Method Summary
Modifier and TypeMethodDescriptionReturns the calls to chat models made during this invocation (seemodelCalls()), those of the AI Service to its own chat model and those of the components it runs.Returns the calls to decision models made during this invocation (seemodelCalls()).Returns the calls to embedding models made during this invocation (seemodelCalls()), for example to embed the query of a retrieval.error()Returns the error the invocation failed with, ornullif it did not fail.Returns why the final response finished, ornullif the model did not report it.Returns the first request that the chat model of the AI Service answered: the request of the first of theownChatModelCalls()that returned a response.Returns the context of the invocation: its id, the AI Service interface and method, the method arguments and more.booleanReturns whether the invocation was cancelled before it completed.Returns the last request that the chat model of the AI Service answered: the request of the last of theownChatModelCalls()that returned a response.Returns the last response of the chat model of the AI Service.Returns the calls to models made during this invocation, in the order they started: those of the AI Service to its own chat model (seeownChatModelCalls()) and those of the components it runs, for example a query transformer, an embedding model used for retrieval, a scoring model, a moderation model or a judge in a guardrail.Returns the calls to moderation models made during this invocation (seemodelCalls()).Returns the AI Service invocations started by the work of this invocation (for example by its tools, guardrails or content retrievers), in the order they started.output()Returns what the AI Service method produced: for a method that returns a value directly, that value, for example aStringor the object of a method with structured output; for a method that returns aResult, aCompletableFutureor another asynchronous value, the value inside it; for a streaming method (one that returns aTokenStreamor a publisher such as aFlux), the text of the final response.Returns the calls of the AI Service to its own chat model, in the order they started: for example one call, then one more after each round of tool executions, or after an output guardrail asked the model again.Returns the calls to scoring models made during this invocation (seemodelCalls()), for example to re-rank the retrieved contents.sources()Returns the contents retrieved for retrieval-augmented generation (RAG) during this invocation, empty if nothing was retrieved.Returns the tokens used, summed over the calls that the AI Service itself made to its chat model during the invocation (for example, before and after executing tools).Returns the executed tools, in the order of execution, also those whose execution failed (seeToolExecution.hasFailed()).toString()
-
Method Details
-
invocationContext
Returns the context of the invocation: its id, the AI Service interface and method, the method arguments and more. -
output
Returns what the AI Service method produced:- for a method that returns a value directly, that value, for example a
Stringor the object of a method with structured output; - for a method that returns a
Result, aCompletableFutureor another asynchronous value, the value inside it; - for a streaming method (one that returns a
TokenStreamor a publisher such as aFlux), the text of the final response. Text that the model streamed in earlier responses, before it called tools, is not included: to check everything that was streamed, collect it withonPartialResponse(...), or seeAiServiceCapture.lambdaOutputItems()when the lambda returns a publisher.
- Throws:
AssertionError- if the invocation failed or was cancelled, or if the AI Service method returnsvoid
- for a method that returns a value directly, that value, for example a
-
outputText
Returns the text produced by the AI Service method: the answer of a method that returns aString(also inside aResultor aCompletableFuture), or the text of the final response of a streaming method (seeoutput()).nullif such a method produced no text, for example a streaming method whose final response has only tool calls.- Throws:
AssertionError- if the invocation failed or was cancelled, or if the method does not produce text, for example a method with structured output (useoutput()then) or a method that returns nothing
-
firstRequest
Returns the first request that the chat model of the AI Service answered: the request of the first of theownChatModelCalls()that returned a response. It holds the messages the AI Service built for the model (for example the system message, the messages from the chat memory and the user message, with the retrieved contents) and the tools it offered, with the default request parameters of the chat model (for example the model name or the temperature set on the model).- Throws:
AssertionError- if the chat model did not answer any request (for example when the invocation failed before), or if it did not notify its listeners (seeownChatModelCalls())
-
lastRequest
Returns the last request that the chat model of the AI Service answered: the request of the last of theownChatModelCalls()that returned a response. When the AI Service executed tools, it holds the whole conversation that led to the final answer: the messages of the first request, the tool calls of the model and the tool results.- Throws:
AssertionError- if the chat model did not answer any request (for example when the invocation failed before), or if it did not notify its listeners (seeownChatModelCalls())
-
lastResponse
Returns the last response of the chat model of the AI Service. When the AI Service executed tools, this is the response after the last tool results, usually the final answer. Its token usage is that of this call only, also for streaming methods; seetokenUsage()for all the calls.- Throws:
AssertionError- if the chat model did not answer any request (for example when the invocation failed before), or if it did not notify its listeners (seeownChatModelCalls())
-
toolExecutions
Returns the executed tools, in the order of execution, also those whose execution failed (seeToolExecution.hasFailed()). The tool executions of nested invocations are not included.- Throws:
AssertionError- if the AI Service did not report whether its tool executions failed (seeToolExecutedEvent.isError()):ToolExecution.hasFailed()would then befalsefor every execution
-
sources
Returns the contents retrieved for retrieval-augmented generation (RAG) during this invocation, empty if nothing was retrieved.- Throws:
AssertionError- if the AI Service has no retrieval augmentor, or did not report what it retrieved
-
tokenUsage
Returns the tokens used, summed over the calls that the AI Service itself made to its chat model during the invocation (for example, before and after executing tools). Models called by other components are not included, for example by a query transformer, a query router, a guardrail or an embedding model, and neither are the tokens of nested invocations.nullif the model did not report them, or if no call of the chat model was captured (seeownChatModelCalls()). -
finishReason
Returns why the final response finished, ornullif the model did not report it. -
error
Returns the error the invocation failed with, ornullif it did not fail. -
isCancelled
public boolean isCancelled()Returns whether the invocation was cancelled before it completed. -
modelCalls
Returns the calls to models made during this invocation, in the order they started: those of the AI Service to its own chat model (seeownChatModelCalls()) and those of the components it runs, for example a query transformer, an embedding model used for retrieval, a scoring model, a moderation model or a judge in a guardrail. The calls of nested invocations are not included, and neither are the calls of models that bypass the listeners LangChain4j notifies (seeModelCall). -
ownChatModelCalls
Returns the calls of the AI Service to its own chat model, in the order they started: for example one call, then one more after each round of tool executions, or after an output guardrail asked the model again. Each request is the one the chat model received, with its default request parameters. Calls that failed are included (seeModelCall.hasFailed()).They are captured from the listeners of the chat model, so the list is empty for a chat model that does not notify its listeners, for example one that overrides
chat(...)instead of implementingdoChat(...). -
chatModelCalls
Returns the calls to chat models made during this invocation (seemodelCalls()), those of the AI Service to its own chat model and those of the components it runs. -
embeddingModelCalls
Returns the calls to embedding models made during this invocation (seemodelCalls()), for example to embed the query of a retrieval. -
scoringModelCalls
Returns the calls to scoring models made during this invocation (seemodelCalls()), for example to re-rank the retrieved contents. -
moderationModelCalls
Returns the calls to moderation models made during this invocation (seemodelCalls()). -
decisionModelCalls
Returns the calls to decision models made during this invocation (seemodelCalls()). -
nestedInvocations
Returns the AI Service invocations started by the work of this invocation (for example by its tools, guardrails or content retrievers), in the order they started. An integration that does not report which invocation started another one (seeInvocationContext.parentInvocationId()) has no nested invocations: the invocations it starts on the capture thread are captured as separate ones. -
toString
-