Skip to content

Commit 705969a

Browse files
committed
Fix #155: Incorrect temperature parameter for OpenAI gpt-4o-search-preview and gpt-4o-mini-search-preview models
Now OpenAI search models work!
1 parent dcb6689 commit 705969a

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

lib/ruby_llm/providers/openai/capabilities.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,13 @@ def special_prefix_format(prefix)
215215
end
216216
end
217217

218-
def normalize_temperature(temperature, model_id)
218+
def self.normalize_temperature(temperature, model_id)
219219
if model_id.match?(/^o\d/)
220220
RubyLLM.logger.debug "Model #{model_id} requires temperature=1.0, ignoring provided value"
221221
1.0
222+
elsif model_id.match?(/-search/)
223+
RubyLLM.logger.debug "Model #{model_id} does not accept temperature parameter, removing"
224+
nil
222225
else
223226
temperature
224227
end

lib/ruby_llm/providers/openai/chat.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ def completion_url
1212
module_function
1313

1414
def render_payload(messages, tools:, temperature:, model:, stream: false)
15-
{
15+
payload = {
1616
model: model,
1717
messages: format_messages(messages),
18-
temperature: temperature,
1918
stream: stream
20-
}.tap do |payload|
21-
if tools.any?
22-
payload[:tools] = tools.map { |_, tool| tool_for(tool) }
23-
payload[:tool_choice] = 'auto'
24-
end
25-
payload[:stream_options] = { include_usage: true } if stream
19+
}
20+
21+
# Only include temperature if it's not nil (some models don't accept it)
22+
payload[:temperature] = temperature unless temperature.nil?
23+
24+
if tools.any?
25+
payload[:tools] = tools.map { |_, tool| tool_for(tool) }
26+
payload[:tool_choice] = 'auto'
2627
end
28+
29+
payload[:stream_options] = { include_usage: true } if stream
30+
payload
2731
end
2832

2933
def parse_completion_response(response)

spec/ruby_llm/providers/openai/capabilities_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
end
1414
end
1515

16-
it 'preserves temperature for non-O1 models' do
17-
non_o1_models = ['gpt-4', 'gpt-4o', 'claude-3-opus']
16+
it 'returns nil for search preview models' do
17+
search_models = %w[gpt-4o-search-preview gpt-4o-mini-search-preview]
1818

19-
non_o1_models.each do |model|
19+
search_models.each do |model|
20+
result = described_class.normalize_temperature(0.7, model)
21+
expect(result).to be_nil
22+
end
23+
end
24+
25+
it 'preserves temperature for standard models' do
26+
standard_models = ['gpt-4', 'gpt-4o', 'gpt-4o-mini', 'claude-3-opus']
27+
28+
standard_models.each do |model|
2029
result = described_class.normalize_temperature(0.7, model)
2130
expect(result).to eq(0.7)
2231
end

0 commit comments

Comments
 (0)