Class: RSpec::LLM::Adapters::Base
- Inherits:
-
Object
- Object
- RSpec::LLM::Adapters::Base
- 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>.
Defined Under Namespace
Classes: UnsupportedClientError
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.wrap(obj) ⇒ Object
Wrap an arbitrary client object in the right adapter.
Instance Method Summary collapse
-
#chat(_messages) ⇒ Object
Send a chat.
-
#chat_structured(messages, schema: nil) ⇒ Object
Send a chat with an optional structured output schema.
-
#embed(_text) ⇒ Object
Embed text.
-
#initialize(client) ⇒ Base
constructor
A new instance of Base.
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
#client ⇒ Object (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.
39 40 41 |
# File 'lib/rspec/llm/adapters/base.rb', line 39 def chat() raise NotImplementedError end |
#chat_structured(messages, schema: nil) ⇒ Object
Send a chat with an optional structured output schema. When a RubyLLM::Schema class is provided via schema:, adapters that support structured output will return a Hash instead of a String. Adapters that do not support structured output fall back to plain #chat and return a String; callers are responsible for parsing that fallback.
48 49 50 |
# File 'lib/rspec/llm/adapters/base.rb', line 48 def chat_structured(, schema: nil) # rubocop:disable Lint/UnusedMethodArgument chat() end |
#embed(_text) ⇒ Object
Embed text. Returns Array<Float>. Optional — adapters may raise NotImplementedError if the underlying client doesn’t support it.
54 55 56 |
# File 'lib/rspec/llm/adapters/base.rb', line 54 def (_text) raise NotImplementedError end |