Class Metadata

java.lang.Object
dev.langchain4j.data.document.Metadata

public class Metadata extends Object
Represents metadata of a Document or a TextSegment. The metadata is stored in a key-value map, where both keys and values are strings. For a Document, the metadata could include information such as the source, creation date, owner, or any other relevant details. For a TextSegment, in addition to metadata copied from a document, it can also include segment-specific information, such as the page number, the position of the segment within the document, chapter, etc.
  • Constructor Details

    • Metadata

      public Metadata()
      Construct a Metadata object with an empty map of key-value pairs.
    • Metadata

      public Metadata(Map<String,String> metadata)
      Constructs a Metadata object from a map of key-value pairs.
      Parameters:
      metadata - the map of key-value pairs; must not be null.
  • Method Details

    • get

      public String get(String key)
      Returns the value associated with the given key.
      Parameters:
      key - the key
      Returns:
      the value associated with the given key, or null if the key is not present.
    • add

      public Metadata add(String key, String value)
      Adds a key-value pair to the metadata.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • add

      @Deprecated public Metadata add(String key, Object value)
      Deprecated.
      Parameters:
      key - the key
      value - the value
      Returns:
      this
    • remove

      public Metadata remove(String key)
      Removes the given key from the metadata.
      Parameters:
      key - the key
      Returns:
      this
    • copy

      public Metadata copy()
      Copies the metadata.
      Returns:
      a copy of this Metadata object.
    • asMap

      public Map<String,String> asMap()
      Get a copy of the metadata as a map of key-value pairs.
      Returns:
      the metadata as a map of key-value pairs.
    • 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 Metadata from(String key, String value)
      Constructs a Metadata object from a single key-value pair.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • from

      @Deprecated public static Metadata from(String key, Object value)
      Deprecated.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • from

      public static Metadata from(Map<String,String> metadata)
      Constructs a Metadata object from a map of key-value pairs.
      Parameters:
      metadata - the map of key-value pairs
      Returns:
      a Metadata object
    • metadata

      public static Metadata metadata(String key, String value)
      Constructs a Metadata object from a single key-value pair.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object
    • metadata

      @Deprecated public static Metadata metadata(String key, Object value)
      Deprecated.
      Parameters:
      key - the key
      value - the value
      Returns:
      a Metadata object