Class: SqlGenius::Core::Ai::Client
- Inherits:
-
Object
- Object
- SqlGenius::Core::Ai::Client
- Defined in:
- lib/sql_genius/core/ai/client.rb
Overview
HTTP client for OpenAI-compatible chat completion APIs. Construct with a Core::Ai::Config; call #chat with a messages array.
Defined Under Namespace
Classes: ApiError, NotConfigured, TooManyRedirects
Constant Summary collapse
- MAX_REDIRECTS =
3
Instance Method Summary collapse
- #chat(messages:, temperature: 0) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
19 20 21 |
# File 'lib/sql_genius/core/ai/client.rb', line 19 def initialize(config) @config = config end |
Instance Method Details
#chat(messages:, temperature: 0) ⇒ Object
23 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 |
# File 'lib/sql_genius/core/ai/client.rb', line 23 def chat(messages:, temperature: 0) if @config.client return @config.client.call(messages: , temperature: temperature) end raise NotConfigured, "AI is not configured" unless @config.enabled? body = if anthropic? build_anthropic_body(, temperature) else build_openai_body(, temperature) end response = post_with_redirects(URI(@config.endpoint), body.to_json) parsed = JSON.parse(response.body) if parsed["error"] raise ApiError, "AI API error: #{parsed["error"]["message"] || parsed["error"]}" end content = if anthropic? parsed.dig("content", 0, "text") else parsed.dig("choices", 0, "message", "content") end raise ApiError, "No content in AI response" if content.nil? parse_json_content(content) end |