Class: DSPy::Document
- Inherits:
-
Object
- Object
- DSPy::Document
- 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
-
#base64 ⇒ Object
readonly
Returns the value of attribute base64.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url: nil, base64: nil, data: nil, content_type: nil) ⇒ Document
constructor
A new instance of Document.
- #to_anthropic_format ⇒ Object
- #to_base64 ⇒ Object
- #to_gemini_format ⇒ Object
- #to_openai_format ⇒ Object
- #to_ruby_llm_attachment ⇒ Object
- #validate_for_provider!(provider) ⇒ Object
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
#base64 ⇒ Object (readonly)
Returns the value of attribute base64.
21 22 23 |
# File 'lib/dspy/document.rb', line 21 def base64 @base64 end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
21 22 23 |
# File 'lib/dspy/document.rb', line 21 def content_type @content_type end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/dspy/document.rb', line 21 def data @data end |
#url ⇒ Object (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_format ⇒ Object
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_base64 ⇒ Object
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_format ⇒ Object
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_format ⇒ Object
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_attachment ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/dspy/document.rb', line 80 def 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 |