Class: Ask::Document
- Inherits:
-
Object
- Object
- Ask::Document
- 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.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The text content of this document.
-
#id ⇒ String?
readonly
Optional unique identifier.
-
#metadata ⇒ Hash
readonly
Arbitrary metadata (source, page, chunk index, etc.).
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Two documents are equal if their content and metadata match.
- #hash ⇒ Object
-
#initialize(content:, metadata: {}, id: nil) ⇒ Document
constructor
A new instance of Document.
- #inspect ⇒ String
-
#to_h ⇒ Hash
Serializable representation.
-
#to_json(*args) ⇒ String
JSON representation.
Constructor Details
#initialize(content:, metadata: {}, id: nil) ⇒ Document
Returns a new instance of Document.
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
#content ⇒ String (readonly)
Returns the text content of this document.
25 26 27 |
# File 'lib/ask/document.rb', line 25 def content @content end |
#id ⇒ String? (readonly)
Returns optional unique identifier.
31 32 33 |
# File 'lib/ask/document.rb', line 31 def id @id end |
#metadata ⇒ Hash (readonly)
Returns 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 |
#hash ⇒ Object
61 62 63 |
# File 'lib/ask/document.rb', line 61 def hash [@content, @metadata].hash end |
#inspect ⇒ 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_h ⇒ Hash
Returns 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.
49 50 51 |
# File 'lib/ask/document.rb', line 49 def to_json(*args) to_h.to_json(*args) end |