Class: Raif::Llms::OpenAiBase

Inherits:
Raif::Llm
  • Object
show all
Includes:
Concerns::Llms::OpenAi::JsonSchemaValidation
Defined in:
app/models/raif/llms/open_ai_base.rb

Direct Known Subclasses

OpenAiCompletions, OpenAiResponses

Instance Method Summary collapse

Instance Method Details

#perform_model_completion!(model_completion, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/raif/llms/open_ai_base.rb', line 6

def perform_model_completion!(model_completion, &block)
  if supports_temperature?
    model_completion.temperature ||= default_temperature
  else
    Raif.logger.warn "Temperature is not supported for #{api_name}. Ignoring temperature parameter."
    model_completion.temperature = nil
  end

  parameters = build_request_parameters(model_completion)

  response = connection.post(api_path) do |req|
    req.body = parameters
    req.options.on_data = streaming_chunk_handler(model_completion, &block) if model_completion.stream_response?
  end

  unless model_completion.stream_response?
    update_model_completion(model_completion, response.body)
  end

  model_completion
end