Class: Spectre::Openai::Completions
- Inherits:
-
Object
- Object
- Spectre::Openai::Completions
- 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
-
.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.
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
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.}: #{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.}" rescue Net::OpenTimeout, Net::ReadTimeout => e raise "Request Timeout: #{e.}" end |