Class: Ask::RAG::Loader::JSON

Inherits:
Base
  • Object
show all
Defined in:
lib/ask/rag/loader/json.rb

Overview

Loads a JSON file as documents.

Supports two modes:

  1. Array of objects — each object becomes one document. Specify content_key and metadata_keys to map fields.
  2. Single object — becomes one document. Specify content_key for which field holds the text.

Examples:

Array of objects

# [{ "title": "...", "body": "...", "tags": [...] }, ...]
loader = Ask::RAG::Loader::JSON.new(content_key: "body")
docs = loader.load("articles.json")

With metadata mapping

loader = Ask::RAG::Loader::JSON.new(
  content_key: "content",
  metadata_keys: ["source", "date"]
)

Instance Method Summary collapse

Methods inherited from Base

#lazy_load

Constructor Details

#initialize(content_key: "content", metadata_keys: nil) ⇒ JSON

Returns a new instance of JSON.

Parameters:

  • content_key (String) (defaults to: "content")

    key whose value becomes document content

  • metadata_keys (Array<String>, nil) (defaults to: nil)

    keys to include in metadata (nil = all keys except content_key)



29
30
31
32
# File 'lib/ask/rag/loader/json.rb', line 29

def initialize(content_key: "content", metadata_keys: nil)
  @content_key = content_key
  @metadata_keys = 
end

Instance Method Details

#load(path) ⇒ Object



34
35
36
37
38
39
# File 'lib/ask/rag/loader/json.rb', line 34

def load(path)
  raw = ::JSON.parse(File.read(path))
  items = raw.is_a?(Array) ? raw : [raw]

  items.map { |item| build_document(item, path) }
end