Class: AIRefactor::AIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_refactor/ai_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(platform: "openai", model: "gpt-4-turbo", temperature: 0.7, max_tokens: 1500, timeout: 60, verbose: false) ⇒ AIClient

Returns a new instance of AIClient.



5
6
7
8
9
10
11
12
13
# File 'lib/ai_refactor/ai_client.rb', line 5

def initialize(platform: "openai", model: "gpt-4-turbo", temperature: 0.7, max_tokens: 1500, timeout: 60, verbose: false)
  @platform = platform
  @model = model
  @temperature = temperature
  @max_tokens = max_tokens
  @timeout = timeout
  @verbose = verbose
  @client = configure
end

Instance Method Details

#generate!(messages) {|finished_reason, content, response| ... } ⇒ Object

Yields:

  • (finished_reason, content, response)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ai_refactor/ai_client.rb', line 15

def generate!(messages)
  finished_reason, content, response = case @platform
  when "openai"
    openai_parse_response(
      @client.chat(
        parameters: {
          messages: messages,
          model: @model,
          temperature: @temperature,
          max_tokens: @max_tokens
        }
      )
    )
  when "anthropic"
    anthropic_parse_response(
      @client.messages(
        parameters: {
          system: messages.find { |m| m[:role] == "system" }&.fetch(:content, nil),
          messages: messages.select { |m| m[:role] != "system" },
          model: @model,
          max_tokens: @max_tokens
        }
      )
    )
  else
    raise "Invalid platform: #{@platform}"
  end
  yield finished_reason, content, response
end