Class ObservationChatModelListener

java.lang.Object
dev.langchain4j.observation.listener.ObservationChatModelListener
All Implemented Interfaces:
ChatModelListener

@Experimental public class ObservationChatModelListener extends Object implements ChatModelListener
Will use observations and micrometer metrics to generate telemetry based on the ChatModelListener lifecycle.

Observations will handle request durations and tracing. Observation lifecycle and context management is handled here.

There is a Micrometer DistributionSummary responsible to record token usage.

  • Constructor Details

    • ObservationChatModelListener

      public ObservationChatModelListener(io.micrometer.observation.ObservationRegistry observationRegistry, io.micrometer.core.instrument.MeterRegistry meterRegistry)
  • Method Details

    • onRequest

      public void onRequest(ChatModelRequestContext requestContext)
      Description copied from interface: ChatModelListener
      This method is called before the request is sent to the model.
      Specified by:
      onRequest in interface ChatModelListener
      Parameters:
      requestContext - The request context. It contains the ChatRequest and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onResponse

      public void onResponse(ChatModelResponseContext responseContext)
      Description copied from interface: ChatModelListener
      This method is called after the response is received from the model.
      Specified by:
      onResponse in interface ChatModelListener
      Parameters:
      responseContext - The response context. It contains ChatResponse, corresponding ChatRequest and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.
    • onError

      public void onError(ChatModelErrorContext errorContext)
      Description copied from interface: ChatModelListener
      This method is called when an error occurs during interaction with the model.
      Specified by:
      onError in interface ChatModelListener
      Parameters:
      errorContext - The error context. It contains the error, corresponding ChatRequest, partial ChatResponse (if available) and attributes. The attributes can be used to pass data between methods of this listener or between multiple listeners.