Class: AsktiveRecord::Adapters::OpenAI
- Defined in:
- lib/asktive_record/adapters/openai.rb
Overview
OpenAI adapter for AsktiveRecord. Wraps the ruby-openai gem to provide LLM communication capabilities.
Constant Summary collapse
- DEFAULT_MODEL =
"gpt-4o-mini"- DEFAULT_TEMPERATURE =
0.2- DEFAULT_MAX_TOKENS =
250
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#chat(prompt, options = {}) ⇒ String?
Send a prompt to OpenAI and return the text response.
-
#default_model_name ⇒ String
Returns the default model name for OpenAI.
-
#initialize(api_key:, model_name: nil) ⇒ OpenAI
constructor
A new instance of OpenAI.
Methods inherited from Base
Constructor Details
#initialize(api_key:, model_name: nil) ⇒ OpenAI
Returns a new instance of OpenAI.
22 23 24 25 |
# File 'lib/asktive_record/adapters/openai.rb', line 22 def initialize(api_key:, model_name: nil) super @client = nil end |
Instance Method Details
#chat(prompt, options = {}) ⇒ String?
Send a prompt to OpenAI and return the text response.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/asktive_record/adapters/openai.rb', line 34 def chat(prompt, = {}) response = client.chat( parameters: { model: resolved_model_name, messages: [{ role: "user", content: prompt }], temperature: .fetch(:temperature, DEFAULT_TEMPERATURE), max_tokens: .fetch(:max_tokens, DEFAULT_MAX_TOKENS) } ) response.dig("choices", 0, "message", "content")&.strip rescue ::OpenAI::Error => e raise ApiError, "OpenAI API error: #{e.}" end |
#default_model_name ⇒ String
Returns the default model name for OpenAI.
51 52 53 |
# File 'lib/asktive_record/adapters/openai.rb', line 51 def default_model_name DEFAULT_MODEL end |