Class: RCrewAI::LLMClients::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/rcrewai/llm_clients/openai.rb

Direct Known Subclasses

Azure

Constant Summary collapse

BASE_URL =
'https://api.openai.com/v1'

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#complete

Constructor Details

#initialize(config = RCrewAI.configuration) ⇒ OpenAI

Returns a new instance of OpenAI.



16
17
18
19
# File 'lib/rcrewai/llm_clients/openai.rb', line 16

def initialize(config = RCrewAI.configuration)
  super
  @base_url = BASE_URL
end

Instance Method Details

#chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) ⇒ Object



21
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
# File 'lib/rcrewai/llm_clients/openai.rb', line 21

def chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options)
  payload = {
    model: config.model,
    messages: messages,
    temperature: options[:temperature] || config.temperature,
    max_tokens: options[:max_tokens] || config.max_tokens
  }.compact

  payload[:top_p] = options[:top_p] if options[:top_p]
  payload[:frequency_penalty] = options[:frequency_penalty] if options[:frequency_penalty]
  payload[:presence_penalty] = options[:presence_penalty] if options[:presence_penalty]
  payload[:stop] = options[:stop] if options[:stop]

  if tools && !tools.empty?
    payload[:tools] = ProviderSchema.for_many(:openai, tools)
    payload[:tool_choice] = tool_choice if tool_choice != :auto
  end

  if stream
    payload[:stream] = true
    payload[:stream_options] = { include_usage: true }
    stream_chat(payload, stream)
  else
    plain_chat(payload)
  end
end

#modelsObject



52
53
54
55
56
57
# File 'lib/rcrewai/llm_clients/openai.rb', line 52

def models
  url = "#{@base_url}/models"
  response = http_client.get(url, {}, build_headers.merge(auth_header))
  result = handle_response(response)
  result['data'].map { |model| model['id'] }
end

#supports_native_tools?(model: config.model) ⇒ Boolean

rubocop:disable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


48
49
50
# File 'lib/rcrewai/llm_clients/openai.rb', line 48

def supports_native_tools?(model: config.model) # rubocop:disable Lint/UnusedMethodArgument
  true
end