Class: Ask::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/document.rb

Overview

A piece of text content with associated metadata.

Document is the universal value object for the RAG pipeline. It flows through every stage: loaded from files by loaders, split into smaller pieces by splitters, embedded and stored by vector stores, and returned by retrieval queries.

Immutable after creation. Two documents are equal when their content and metadata match.

Examples:

doc = Ask::Document.new(
  content: "Ruby was created by Matz in 1995.",
  metadata: { source: "history.pdf", page: 3 }
)
doc.content   # => "Ruby was created by Matz in 1995."
doc.  # => { source: "history.pdf", page: 3 }
doc.id        # => nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, metadata: {}, id: nil) ⇒ Document

Returns a new instance of Document.

Parameters:

  • content (String)

    the text content

  • metadata (Hash) (defaults to: {})

    arbitrary metadata (default: {})

  • id (String, nil) (defaults to: nil)

    optional identifier



36
37
38
39
40
41
# File 'lib/ask/document.rb', line 36

def initialize(content:, metadata: {}, id: nil)
  @content = content.to_s
  @metadata = .dup.freeze
  @id = id
  freeze
end

Instance Attribute Details

#contentString (readonly)

Returns the text content of this document.

Returns:

  • (String)

    the text content of this document



25
26
27
# File 'lib/ask/document.rb', line 25

def content
  @content
end

#idString? (readonly)

Returns optional unique identifier.

Returns:

  • (String, nil)

    optional unique identifier



31
32
33
# File 'lib/ask/document.rb', line 31

def id
  @id
end

#metadataHash (readonly)

Returns arbitrary metadata (source, page, chunk index, etc.).

Returns:

  • (Hash)

    arbitrary metadata (source, page, chunk index, etc.)



28
29
30
# File 'lib/ask/document.rb', line 28

def 
  @metadata
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Two documents are equal if their content and metadata match.



54
55
56
57
58
# File 'lib/ask/document.rb', line 54

def ==(other)
  return false unless other.is_a?(Document)

  @content == other.content && @metadata == other.
end

#hashObject



61
62
63
# File 'lib/ask/document.rb', line 61

def hash
  [@content, @metadata].hash
end

#inspectString

Returns:

  • (String)


66
67
68
69
# File 'lib/ask/document.rb', line 66

def inspect
  preview = @content.length > 60 ? "#{@content[0, 60]}..." : @content
  "#<Ask::Document content=#{preview.inspect}>"
end

#to_hHash

Returns serializable representation.

Returns:

  • (Hash)

    serializable representation



44
45
46
# File 'lib/ask/document.rb', line 44

def to_h
  { content: @content, metadata: @metadata, id: @id }.compact
end

#to_json(*args) ⇒ String

Returns JSON representation.

Returns:

  • (String)

    JSON representation



49
50
51
# File 'lib/ask/document.rb', line 49

def to_json(*args)
  to_h.to_json(*args)
end