Class: DSPy::Document

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

Constant Summary collapse

SUPPORTED_FORMATS =
%w[application/pdf].freeze
MAX_SIZE_BYTES =

32MB limit

32 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, base64: nil, data: nil, content_type: nil) ⇒ Document

Returns a new instance of Document.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dspy/document.rb', line 26

def initialize(url: nil, base64: nil, data: nil, content_type: nil)
  validate_input!(url, base64, data)

  if url
    @url = url
    @content_type = content_type || infer_content_type_from_url(url)
  elsif base64
    raise ArgumentError, "content_type is required when using base64" unless content_type

    @base64 = base64
    @content_type = content_type
    validate_size!(Base64.decode64(base64).bytesize)
  elsif data
    raise ArgumentError, "content_type is required when using data" unless content_type

    @data = data
    @content_type = content_type
    validate_size!(data.size)
  end

  validate_content_type!
end

Instance Attribute Details

#base64Object (readonly)

Returns the value of attribute base64.



21
22
23
# File 'lib/dspy/document.rb', line 21

def base64
  @base64
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



21
22
23
# File 'lib/dspy/document.rb', line 21

def content_type
  @content_type
end

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/dspy/document.rb', line 21

def data
  @data
end

#urlObject (readonly)

Returns the value of attribute url.



21
22
23
# File 'lib/dspy/document.rb', line 21

def url
  @url
end

Instance Method Details

#to_anthropic_formatObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dspy/document.rb', line 54

def to_anthropic_format
  if url
    {
      type: 'document',
      source: {
        type: 'url',
        url: url
      }
    }
  else
    {
      type: 'document',
      source: {
        type: 'base64',
        media_type: content_type,
        data: to_base64
      }
    }
  end
end

#to_base64Object



88
89
90
91
92
93
# File 'lib/dspy/document.rb', line 88

def to_base64
  return base64 if base64
  return Base64.strict_encode64(data.pack('C*')) if data

  nil
end

#to_gemini_formatObject



75
76
77
78
# File 'lib/dspy/document.rb', line 75

def to_gemini_format
  raise DSPy::LM::IncompatibleDocumentFeatureError,
        "Gemini document inputs are not supported in this release. Use Anthropic directly or Anthropic via RubyLLM."
end

#to_openai_formatObject



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

def to_openai_format
  raise DSPy::LM::IncompatibleDocumentFeatureError,
        "OpenAI document inputs are not supported in this release. Use Anthropic directly or Anthropic via RubyLLM."
end

#to_ruby_llm_attachmentObject



80
81
82
83
84
85
86
# File 'lib/dspy/document.rb', line 80

def to_ruby_llm_attachment
  if url
    url
  else
    RubyLLMInlineAttachment.new(to_binary, path: 'document.pdf')
  end
end

#validate_for_provider!(provider) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dspy/document.rb', line 95

def validate_for_provider!(provider)
  case provider
  when 'anthropic'
    true
  when 'openai'
    raise DSPy::LM::IncompatibleDocumentFeatureError,
          "OpenAI document inputs are not supported in this release. Use Anthropic directly or Anthropic via RubyLLM."
  when 'gemini'
    raise DSPy::LM::IncompatibleDocumentFeatureError,
          "Gemini document inputs are not supported in this release. Use Anthropic directly or Anthropic via RubyLLM."
  else
    raise DSPy::LM::IncompatibleDocumentFeatureError,
          "Unknown provider '#{provider}'. Document inputs are currently supported only for Anthropic."
  end
end