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
- #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 |
#embed(text) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 23 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.
34 35 36 37 |
# File 'lib/rspec/llm/adapters/ruby_llm.rb', line 34 def (callable) @embedder = callable self end |