Class: LLMs::Executors::OpenAICompatibleExecutor

Inherits:
BaseExecutor
  • Object
show all
Defined in:
lib/llms/executors/open_ai_compatible_executor.rb

Direct Known Subclasses

HuggingFaceExecutor

Constant Summary

Constants inherited from BaseExecutor

BaseExecutor::DEFAULT_TEMPERATURE

Instance Attribute Summary

Attributes inherited from BaseExecutor

#available_tools, #base_url, #client, #last_error, #last_received_message, #last_received_message_id, #last_sent_message, #last_usage_data, #max_completion_tokens, #max_thinking_tokens, #max_tokens, #model_name, #system_prompt, #temperature, #thinking_effort, #thinking_mode

Instance Method Summary collapse

Methods inherited from BaseExecutor

#execute_prompt, #initialize

Constructor Details

This class inherits a constructor from LLMs::Executors::BaseExecutor

Instance Method Details

#execute_conversation(conversation, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/llms/executors/open_ai_compatible_executor.rb', line 10

def execute_conversation(conversation, &block)
  if block_given?
    stream_conversation(conversation) do |handler|
      handler.on(:text_delta) do |event|
        yield event.text
      end
    end
  else
    send_conversation(conversation)
  end
end

#send_conversation(conversation) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/llms/executors/open_ai_compatible_executor.rb', line 51

def send_conversation(conversation)
  init_new_request(conversation)

  start_time = Time.now
  begin
    http_response = client_request
  rescue StandardError => e
    @last_error = {'error' => e.message, 'backtrace' => e.backtrace}
    @last_usage_data = nil
    @last_received_message = nil
    return nil
  end
  execution_time = Time.now - start_time

  if http_response && (http_response['error'] || http_response['errors'])
    @last_error = http_response
    return nil
  end

  @last_received_message_id = LLMs::Adapters::OpenAICompatibleMessageAdapter.find_message_id(http_response)
  @last_received_message = LLMs::Adapters::OpenAICompatibleMessageAdapter.message_from_api_format(http_response)
  @last_usage_data = calculate_usage(http_response, execution_time)

  @last_received_message
end

#stream_conversation(conversation) {|emitter| ... } ⇒ Object

Yields:

  • (emitter)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llms/executors/open_ai_compatible_executor.rb', line 22

def stream_conversation(conversation)
  init_new_request(conversation)

  emitter = Stream::EventEmitter.new
  yield emitter if block_given?

  start_time = Time.now
  begin
    http_response, stream_parsed_response = stream_client_request(emitter)
  rescue StandardError => e
    @last_error = {'error' => e.message, 'backtrace' => e.backtrace}
    return nil
  end
  execution_time = Time.now - start_time

  if http_response && (http_response['error'] || http_response['errors'])
    @last_error = http_response
    return nil
  end

  response_data = stream_parsed_response || http_response

  @last_received_message_id = LLMs::Adapters::OpenAICompatibleMessageAdapter.find_message_id(response_data)
  @last_received_message = LLMs::Adapters::OpenAICompatibleMessageAdapter.message_from_api_format(response_data)
  @last_usage_data = calculate_usage(response_data, execution_time)

  @last_received_message
end