Class: RSpec::LLM::Adapters::RubyLLM
- Defined in:
- lib/rspec/llm/adapters/ruby_llm.rb
Overview
Adapter for the ruby_llm gem (github.com/crmne/ruby_llm). Wraps a RubyLLM::Chat instance and exposes the common adapter surface.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #chat(messages) ⇒ Object
-
#chat_structured(messages, schema: nil) ⇒ Object
Uses
with_schemaon the underlying RubyLLM::Chat client when a schema is provided, yielding a Hash response instead of raw text. - #embed(text) ⇒ Object
-
#with_embedder(callable) ⇒ Object
Override the embedder (useful in tests).
Methods inherited from Base
Constructor Details
This class inherits a constructor from RSpec::LLM::Adapters::Base
Instance Method Details
#chat(messages) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 9 def chat() normalized = () last = normalized.last system_msgs = normalized[0..-2].select { |m| m[:role] == "system" } if system_msgs.any? && client.respond_to?(:with_instructions) system_msgs.each do |m| client.with_instructions(m[:content]) end end response = client.ask(last[:content]) extract_content(response) end |
#chat_structured(messages, schema: nil) ⇒ Object
Uses with_schema on the underlying RubyLLM::Chat client when a schema is provided, yielding a Hash response instead of raw text. Falls back to plain #chat when the client doesn’t support with_schema.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 26 def chat_structured(, schema: nil) return chat() unless schema && client.respond_to?(:with_schema) normalized = () last = normalized.last system_msgs = normalized[0..-2].select { |m| m[:role] == "system" } if system_msgs.any? && client.respond_to?(:with_instructions) system_msgs.each do |m| client.with_instructions(m[:content]) end end client.with_schema(schema) response = client.ask(last[:content]) extract_content(response) end |
#embed(text) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 43 def (text) return @embedder.call(text) if @embedder raise NotImplementedError, "RubyLLM.embed is not available" unless defined?(::RubyLLM) && ::RubyLLM.respond_to?(:embed) result = ::RubyLLM.(text) vectors = result.respond_to?(:vectors) ? result.vectors : result vectors.is_a?(Array) && vectors.first.is_a?(Array) ? vectors.first : vectors end |
#with_embedder(callable) ⇒ Object
Override the embedder (useful in tests). Accepts a callable.
54 55 56 57 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 54 def (callable) @embedder = callable self end |