Class Response<T>

java.lang.Object
dev.langchain4j.model.output.Response<T>
Type Parameters:
T - The type of content generated by the model.

public class Response<T> extends Object
Represents the response from various types of models, including language, chat, embedding, and moderation models. This class encapsulates the generated content, token usage statistics, finish reason, and response metadata.
  • Constructor Details

    • Response

      public Response(T content)
      Create a new Response.

      Will contain null TokenUsage and FinishReason

      Parameters:
      content - the content to wrap.
    • Response

      public Response(T content, TokenUsage tokenUsage, FinishReason finishReason)
      Create a new Response.
      Parameters:
      content - the content to wrap.
      tokenUsage - the token usage statistics, or null.
      finishReason - the finish reason, or null.
    • Response

      public Response(T content, TokenUsage tokenUsage, FinishReason finishReason, Map<String,Object> metadata)
      Create a new Response.
      Parameters:
      content - the content to wrap.
      tokenUsage - the token usage statistics, or null.
      finishReason - the finish reason, or null.
      metadata - the response metadata, or null.
  • Method Details

    • content

      public T content()
      Get the content.
      Returns:
      the content.
    • tokenUsage

      public TokenUsage tokenUsage()
      Get the token usage statistics.
      Returns:
      the token usage statistics, or null.
    • finishReason

      public FinishReason finishReason()
      Get the finish reason.
      Returns:
      the finish reason, or null.
    • metadata

      public Map<String,Object> metadata()
      Get the response metadata.
      Returns:
      the response metadata.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • from

      public static <T> Response<T> from(T content)
      Create a new Response.
      Type Parameters:
      T - the type of content.
      Parameters:
      content - the content to wrap.
      Returns:
      the new Response.
    • from

      public static <T> Response<T> from(T content, TokenUsage tokenUsage)
      Create a new Response.
      Type Parameters:
      T - the type of content.
      Parameters:
      content - the content to wrap.
      tokenUsage - the token usage statistics.
      Returns:
      the new Response.
    • from

      public static <T> Response<T> from(T content, TokenUsage tokenUsage, FinishReason finishReason)
      Create a new Response.
      Type Parameters:
      T - the type of content.
      Parameters:
      content - the content to wrap.
      tokenUsage - the token usage statistics.
      finishReason - the finish reason.
      Returns:
      the new Response.
    • from

      public static <T> Response<T> from(T content, TokenUsage tokenUsage, FinishReason finishReason, Map<String,Object> metadata)
      Create a new Response.
      Type Parameters:
      T - the type of content.
      Parameters:
      content - the content to wrap.
      tokenUsage - the token usage statistics.
      finishReason - the finish reason.
      metadata - the response metadata.
      Returns:
      the new Response.