Class: RSpec::LLM::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/llm/adapters/base.rb

Overview

Abstract adapter. Subclasses normalize ruby_llm / langchainrb / fake clients to a common minimal surface: #chat(messages) -> String, and optionally #embed(text) -> Array<Float>.

Direct Known Subclasses

Fake, Langchain, RubyLLM

Defined Under Namespace

Classes: UnsupportedClientError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



30
31
32
# File 'lib/rspec/llm/adapters/base.rb', line 30

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



34
35
36
# File 'lib/rspec/llm/adapters/base.rb', line 34

def client
  @client
end

Class Method Details

.wrap(obj) ⇒ Object

Wrap an arbitrary client object in the right adapter. Uses class-name string matching so that ruby_llm / langchainrb aren’t hard required.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec/llm/adapters/base.rb', line 14

def self.wrap(obj)
  return obj if obj.is_a?(Base)

  class_name = obj.class.name.to_s
  case class_name
  when /\ARubyLLM::Chat/, "RubyLLM"
    RubyLLM.new(obj)
  when /\ALangchain::LLM::/
    Langchain.new(obj)
  else
    raise UnsupportedClientError,
          "Unsupported LLM client: #{class_name.empty? ? obj.inspect : class_name}. " \
          "Wrap it in a subclass of RSpec::LLM::Adapters::Base."
  end
end

Instance Method Details

#chat(_messages) ⇒ Object

Send a chat. ‘messages` may be a String (treated as a single user message) or an Array of { role:, content: } hashes. Returns the assistant’s text content as a String.

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/rspec/llm/adapters/base.rb', line 39

def chat(_messages)
  raise NotImplementedError
end

#embed(_text) ⇒ Object

Embed text. Returns Array<Float>. Optional — adapters may raise NotImplementedError if the underlying client doesn’t support it.

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/rspec/llm/adapters/base.rb', line 45

def embed(_text)
  raise NotImplementedError
end