Interface InvocationContext
- All Known Implementing Classes:
DefaultInvocationContext
ChatModel.- Since:
- 1.6.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classA builder class to createInvocationContextinstances. -
Method Summary
Modifier and TypeMethodDescriptionstatic InvocationContext.Builderbuilder()Creates a new instance of theBuilderclass for constructing instances ofDefaultInvocationContext.The chat memory id parameter of the methoddefault ChatRequestParametersReturns the defaultChatRequestParametersof the chat model configured on the AI service.The fully-qualified name of the AI Service interface where the invocation was initiated fromUnique identifier for an entire AI Service invocation.The invocation parametersdefault Map<Class<? extends LangChain4jManaged>, LangChain4jManaged> LangChain4j managed parametersThe arguments passed into the AI Service methodThe method name oninterfaceName()where the invocation was initiated fromdefault TypeThe declared return type of the AI Service method, including its type arguments (for exampleList<String>orTokenStream).default ModelProviderReturns theModelProviderof the chat model configured on the AI service.default UserMessageTheUserMessagecreated from the AI Service method and its prompt template, before any transformation (RAG augmentation, content injection, input guardrails, output format instructions).default UUIDTheinvocationId()of the parent invocation: the AI Service invocation whose own work started this invocation.Retrieves the point in time when the invocation occurred.default InvocationContext.BuilderConverts the current instance ofDefaultInvocationContextinto aInvocationContext.Builder, pre-populated with the current values of the instance.default UserMessageTheUserMessageto be sent to the LLM.
-
Method Details
-
invocationId
UUID invocationId()Unique identifier for an entire AI Service invocation. AI Services created withAiServicestreat each subscription to a publisher returned by an AI Service method as a separate invocation, with its own identifier. -
parentInvocationId
TheinvocationId()of the parent invocation: the AI Service invocation whose own work started this invocation. This is the case when one of these parts of an AI Service calls another AI Service:- a tool, a tool error handler or the strategy for hallucinated tool names
- a tool provider
- an input or output guardrail
- a component of the retrieval augmentor (for example a query transformer or a content retriever)
- the system or user message provider, or the system message transformer
- the chat memory (including its
ChatMemoryStore) or the chat memory provider - the moderation model
- the
chatRequestTransformer - the chat model itself (for example your own
ChatModelimplementation)
It is
nullwhen the AI Service is called from your own code (for example from a handler of aTokenStreamor from a listener, also from those of an AI Service that itself has a parent), when there is no parent, or when thisInvocationContextimplementation does not track it.The parent is also known when LangChain4j itself runs that part on another thread (for example tools executed concurrently, or a blocking content retriever offloaded to an executor). But if your own code in that part hands the call to another thread (for example with
CompletableFuture.supplyAsync(..., executor)), it isnull. In rare cases, when futures are shared between invocations, it can even name another invocation. So treat it as best effort.It is filled by the AI Service implementation that LangChain4j provides. Frameworks that replace that implementation (for example through
AiServicesFactory) returnnulluntil they add support for it, also for AI Services they create throughAiServices.builder(...).It is set when the AI Service method is called: later runs of the same
TokenStreamkeep it.Agents run by an agentic workflow (sequence, parallel, loop, conditional) or by a supervisor currently have no parent. An agent called from a tool of another agent has one.
- Returns:
- the id of the parent invocation, or
nullif there is none or it is not known - Since:
- 1.23.0
-
interfaceName
String interfaceName()The fully-qualified name of the AI Service interface where the invocation was initiated from- See Also:
-
methodName
String methodName()The method name oninterfaceName()where the invocation was initiated from -
methodReturnType
The declared return type of the AI Service method, including its type arguments (for exampleList<String>orTokenStream).Frameworks that create the context with their own AI Service implementation return
nulluntil they add support for it.- Returns:
- the return type of the AI Service method, or
nullif it is not known - Since:
- 1.24.0
-
methodArguments
-
userMessage
TheUserMessageto be sent to the LLM. This is the message after all transformations (RAG augmentation, content injection, input guardrails, output format instructions).- Since:
- 1.13.0
-
originalUserMessage
TheUserMessagecreated from the AI Service method and its prompt template, before any transformation (RAG augmentation, content injection, input guardrails, output format instructions). It is what the user asked, without the retrieved content, for example to decide which tools or models are relevant to the question.- Since:
- 1.21.0
-
chatMemoryId
Object chatMemoryId()The chat memory id parameter of the method -
defaultRequestParameters
Returns the defaultChatRequestParametersof the chat model configured on the AI service.- Since:
- 1.16.0
-
modelProvider
Returns theModelProviderof the chat model configured on the AI service.- Since:
- 1.16.0
-
invocationParameters
InvocationParameters invocationParameters()The invocation parameters -
managedParameters
LangChain4j managed parameters- Since:
- 1.8.0
-
timestamp
Instant timestamp()Retrieves the point in time when the invocation occurred. -
toBuilder
Converts the current instance ofDefaultInvocationContextinto aInvocationContext.Builder, pre-populated with the current values of the instance.When this is a
DefaultInvocationContext, a copy built with the sameinvocationId()shares whether the invocation has already reported its end (seeInvocationEnd). -
builder
Creates a new instance of theBuilderclass for constructing instances ofDefaultInvocationContext.
-