Class: PromptEngine::OpenAiEvalsClient

Inherits:
Object
  • Object
show all
Defined in:
app/clients/prompt_engine/open_ai_evals_client.rb

Defined Under Namespace

Classes: APIError, AuthenticationError, NotFoundError, RateLimitError

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ OpenAiEvalsClient

Returns a new instance of OpenAiEvalsClient.



10
11
12
13
14
# File 'app/clients/prompt_engine/open_ai_evals_client.rb', line 10

def initialize(api_key: nil)
  # Try to get API key from: 1) parameter, 2) Settings, 3) Rails credentials
  @api_key = api_key || fetch_api_key_from_settings || Rails.application.credentials.dig(:openai, :api_key)
  raise AuthenticationError, "OpenAI API key not configured" if @api_key.blank?
end

Instance Method Details

#create_eval(name:, data_source_config:, testing_criteria:) ⇒ Object



16
17
18
19
20
21
22
# File 'app/clients/prompt_engine/open_ai_evals_client.rb', line 16

def create_eval(name:, data_source_config:, testing_criteria:)
  post("/evals", {
    name: name,
    data_source_config: data_source_config,
    testing_criteria: testing_criteria
  })
end

#create_run(eval_id:, name:, data_source:) ⇒ Object



24
25
26
27
28
29
# File 'app/clients/prompt_engine/open_ai_evals_client.rb', line 24

def create_run(eval_id:, name:, data_source:)
  post("/evals/#{eval_id}/runs", {
    name: name,
    data_source: data_source
  })
end

#get_run(eval_id:, run_id:) ⇒ Object



31
32
33
# File 'app/clients/prompt_engine/open_ai_evals_client.rb', line 31

def get_run(eval_id:, run_id:)
  get("/evals/#{eval_id}/runs/#{run_id}")
end

#upload_file(file_path, purpose: "evals") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/clients/prompt_engine/open_ai_evals_client.rb', line 35

def upload_file(file_path, purpose: "evals")
  uri = URI("#{BASE_URL}/files")
  request = Net::HTTP::Post.new(uri)
  request["Authorization"] = "Bearer #{@api_key}"

  File.open(file_path, "rb") do |file|
    form_data = [
      [ "purpose", purpose ],
      [ "file", file, { filename: File.basename(file_path) } ]
    ]
    request.set_form(form_data, "multipart/form-data")

    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    handle_response(response)
  end
rescue Errno::ENOENT => e
  raise APIError, "File not found: #{file_path}"
rescue => e
  raise APIError, "File upload failed: #{e.message}"
end