Commit e0546b7

mo khan <mo@mokhan.ca>
2025-10-08 17:13:24
refactor: create client in ctor
1 parent 3618ec2
Changed files (2)
lib/net/llm/openai.rb
@@ -3,35 +3,38 @@
 module Net
   module Llm
     class OpenAI
-      attr_reader :api_key, :base_url, :model
+      DEFAULT_TIMEOUT = 60 * 2
 
-      def initialize(api_key:, base_url: "https://api.openai.com/v1", model: "gpt-4o-mini")
+      attr_reader :api_key, :base_url, :model, :client
+
+      def initialize(api_key:, base_url: "https://api.openai.com/v1", model: "gpt-4o-mini", timeout: DEFAULT_TIMEOUT)
         @api_key = api_key
         @base_url = base_url
+        @client = Net::Hippie::Client.new(read_timeout: timeout, open_timeout: timeout)
         @model = model
       end
 
-      def chat(messages, tools, timeout: DEFAULT_TIMEOUT)
-        response = client(timeout).post(
+      def chat(messages, tools)
+        response = client.post(
           "#{base_url}/chat/completions",
-          headers: auth_headers,
+          headers: headers,
           body: { model: model, messages: messages, tools: tools, tool_choice: "auto" }
         )
         handle_response(response)
       end
 
-      def models(timeout: DEFAULT_TIMEOUT)
-        response = client(timeout).get(
+      def models
+        response = client.get(
           "#{base_url}/models",
-          headers: auth_headers
+          headers: headers
         )
         handle_response(response)
       end
 
-      def embeddings(input, model: "text-embedding-ada-002", timeout: DEFAULT_TIMEOUT)
-        response = client(timeout).post(
+      def embeddings(input, model: "text-embedding-ada-002")
+        response = client.post(
           "#{base_url}/embeddings",
-          headers: auth_headers,
+          headers: headers,
           body: { model: model, input: input }
         )
         handle_response(response)
@@ -39,14 +42,7 @@ module Net
 
       private
 
-      def client(timeout)
-        Net::Hippie::Client.new(
-          read_timeout: timeout,
-          open_timeout: timeout
-        )
-      end
-
-      def auth_headers
+      def headers
         { "Authorization" => Net::Hippie.bearer_auth(api_key) }
       end
 
lib/net/llm.rb
@@ -10,6 +10,5 @@ require "json"
 module Net
   module Llm
     class Error < StandardError; end
-    DEFAULT_TIMEOUT = 60 * 2
   end
 end