Class: Spectre::Openai::Completions

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/openai/completions.rb

Constant Summary collapse

API_URL =
'https://api.openai.com/v1/chat/completions'
DEFAULT_MODEL =
'gpt-4o-mini'

Class Method Summary collapse

Class Method Details

.create(user_prompt:, system_prompt: "You are a helpful assistant.", assistant_prompt: nil, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil) ⇒ String

Class method to generate a completion based on a user prompt

Parameters:

  • user_prompt (String)

    the user’s input to generate a completion for

  • system_prompt (String) (defaults to: "You are a helpful assistant.")

    an optional system prompt to guide the AI’s behavior

  • assistant_prompt (String) (defaults to: nil)

    an optional assistant prompt to provide context for the assistant’s behavior

  • model (String) (defaults to: DEFAULT_MODEL)

    the model to be used for generating completions, defaults to DEFAULT_MODEL

  • json_schema (Hash, nil) (defaults to: nil)

    an optional JSON schema to enforce structured output

  • max_tokens (Integer) (defaults to: nil)

    the maximum number of tokens for the completion (default: 50)

Returns:

  • (String)

    the generated completion text

Raises:

  • (APIKeyNotConfiguredError)

    if the API key is not set

  • (RuntimeError)

    for general API errors or unexpected issues



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spectre/openai/completions.rb', line 24

def self.create(user_prompt:, system_prompt: "You are a helpful assistant.", assistant_prompt: nil, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil)
  api_key = Spectre.api_key
  raise APIKeyNotConfiguredError, "API key is not configured" unless api_key

  uri = URI(API_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.read_timeout = 10 # seconds
  http.open_timeout = 10 # seconds

  request = Net::HTTP::Post.new(uri.path, {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{api_key}"
  })

  request.body = generate_body(user_prompt, system_prompt, assistant_prompt, model, json_schema, max_tokens).to_json
  response = http.request(request)

  unless response.is_a?(Net::HTTPSuccess)
    raise "OpenAI API Error: #{response.code} - #{response.message}: #{response.body}"
  end

  parsed_response = JSON.parse(response.body)

  # Check if the response contains a refusal
  if parsed_response.dig('choices', 0, 'message', 'refusal')
    raise "Refusal: #{parsed_response.dig('choices', 0, 'message', 'refusal')}"
  end

  # Check if the finish reason is "length", indicating incomplete response
  if parsed_response.dig('choices', 0, 'finish_reason') == "length"
    raise "Incomplete response: The completion was cut off due to token limit."
  end

  # Return the structured output if it's included
  parsed_response.dig('choices', 0, 'message', 'content')
rescue JSON::ParserError => e
  raise "JSON Parse Error: #{e.message}"
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise "Request Timeout: #{e.message}"
end