|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe RubyLLM::Chat do |
| 6 | + include_context 'with configured RubyLLM' |
| 7 | + |
| 8 | + describe '#with_messages' do |
| 9 | + it 'uses all messages by default when no scope is configured' do |
| 10 | + chat = RubyLLM.chat |
| 11 | + provider = chat.instance_variable_get(:@provider) |
| 12 | + |
| 13 | + allow(provider).to receive(:complete).and_return( |
| 14 | + RubyLLM::Message.new(role: :assistant, content: 'Test response') |
| 15 | + ) |
| 16 | + |
| 17 | + chat.add_message(role: :user, content: 'one') |
| 18 | + chat.add_message(role: :assistant, content: 'two') |
| 19 | + |
| 20 | + chat.complete |
| 21 | + |
| 22 | + expect(provider).to have_received(:complete).with( |
| 23 | + chat.messages, |
| 24 | + hash_including( |
| 25 | + tools: chat.tools, |
| 26 | + temperature: anything, |
| 27 | + model: chat.model, |
| 28 | + params: chat.params, |
| 29 | + headers: chat.headers, |
| 30 | + schema: chat.schema |
| 31 | + ) |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + it 'applies a callable scope to the messages passed to the provider' do |
| 36 | + chat = RubyLLM.chat |
| 37 | + provider = chat.instance_variable_get(:@provider) |
| 38 | + |
| 39 | + allow(provider).to receive(:complete).and_return( |
| 40 | + RubyLLM::Message.new(role: :assistant, content: 'Scoped response') |
| 41 | + ) |
| 42 | + |
| 43 | + first = chat.add_message(role: :user, content: 'first') |
| 44 | + second = chat.add_message(role: :assistant, content: 'second') |
| 45 | + |
| 46 | + chat.with_messages { |msgs| [msgs.last] } |
| 47 | + |
| 48 | + chat.complete |
| 49 | + |
| 50 | + expect(provider).to have_received(:complete) do |messages_arg, **_options| |
| 51 | + expect(messages_arg).to eq([second]) |
| 52 | + expect(messages_arg).not_to include(first) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + it 'can clear the scope by calling without a block' do |
| 57 | + chat = RubyLLM.chat |
| 58 | + provider = chat.instance_variable_get(:@provider) |
| 59 | + |
| 60 | + allow(provider).to receive(:complete).and_return( |
| 61 | + RubyLLM::Message.new(role: :assistant, content: 'Cleared scope response') |
| 62 | + ) |
| 63 | + |
| 64 | + chat.add_message(role: :user, content: 'one') |
| 65 | + chat.add_message(role: :assistant, content: 'two') |
| 66 | + |
| 67 | + chat.with_messages { |msgs| [msgs.last] } |
| 68 | + chat.with_messages |
| 69 | + |
| 70 | + chat.complete |
| 71 | + |
| 72 | + expect(provider).to have_received(:complete).with( |
| 73 | + chat.messages, |
| 74 | + hash_including( |
| 75 | + tools: chat.tools, |
| 76 | + temperature: anything, |
| 77 | + model: chat.model, |
| 78 | + params: chat.params, |
| 79 | + headers: chat.headers, |
| 80 | + schema: chat.schema |
| 81 | + ) |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | + |
0 commit comments