Module: RubyLLM::Providers::Mistral::Chat

Included in:
RubyLLM::Providers::Mistral
Defined in:
lib/ruby_llm/providers/mistral/chat.rb

Overview

Chat methods for Mistral API

Class Method Summary collapse

Class Method Details

.append_formatted_content(content_blocks, formatted_content) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 71

def append_formatted_content(content_blocks, formatted_content)
  if formatted_content.is_a?(Array)
    content_blocks.concat(formatted_content)
  elsif formatted_content
    content_blocks << { type: 'text', text: formatted_content }
  end
end

.build_thinking_blocks(thinking) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 55

def build_thinking_blocks(thinking)
  return [] unless thinking

  if thinking.text
    [{
      type: 'thinking',
      thinking: [{ type: 'text', text: thinking.text }],
      signature: thinking.signature
    }.compact]
  elsif thinking.signature
    [{ type: 'thinking', signature: thinking.signature }]
  else
    []
  end
end

.format_content_with_thinking(msg) ⇒ Object

rubocop:enable Metrics/ParameterLists



35
36
37
38
39
40
41
42
43
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 35

def format_content_with_thinking(msg)
  formatted_content = OpenAI::Media.format_content(msg.content)
  return formatted_content unless msg.role == :assistant && msg.thinking

  content_blocks = build_thinking_blocks(msg.thinking)
  append_formatted_content(content_blocks, formatted_content)

  content_blocks
end

.format_messages(messages) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 14

def format_messages(messages)
  messages.map do |msg|
    {
      role: format_role(msg.role),
      content: format_content_with_thinking(msg),
      tool_calls: OpenAI::Tools.format_tool_calls(msg.tool_calls),
      tool_call_id: msg.tool_call_id
    }.compact
  end
end

.format_role(role) ⇒ Object



10
11
12
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 10

def format_role(role)
  role.to_s
end

.render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



26
27
28
29
30
31
32
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 26

def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil)
  payload = super
  payload.delete(:stream_options)
  payload.delete(:reasoning_effort)
  warn_on_unsupported_thinking(model, thinking)
  payload
end

.warn_on_unsupported_thinking(model, thinking) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/providers/mistral/chat.rb', line 45

def warn_on_unsupported_thinking(model, thinking)
  return unless thinking&.enabled?
  return if model.id.to_s.include?('magistral')

  RubyLLM.logger.warn(
    'Mistral thinking is only supported on Magistral models. ' \
    "Ignoring thinking settings for #{model.id}."
  )
end