Class: AIHype::AIMatcher
- Inherits:
-
Object
- Object
- AIHype::AIMatcher
- Defined in:
- lib/aihype/ai_matcher.rb
Constant Summary collapse
- CLAUDE_API_URL =
'https://api.anthropic.com/v1/messages'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
-
.rate_limiter ⇒ Object
Class-level rate limiter shared across all instances.
Instance Method Summary collapse
- #evaluate(prompt_text, blacklist_rules) ⇒ Object
- #http_post(payload) ⇒ Object
-
#initialize(api_key: nil, timeout: nil, model: nil) ⇒ AIMatcher
constructor
A new instance of AIMatcher.
Constructor Details
#initialize(api_key: nil, timeout: nil, model: nil) ⇒ AIMatcher
Returns a new instance of AIMatcher.
23 24 25 26 27 |
# File 'lib/aihype/ai_matcher.rb', line 23 def initialize(api_key: nil, timeout: nil, model: nil) @api_key = api_key || Env.anthropic_api_key @timeout = timeout || Env.api_timeout @model = model || select_model end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/aihype/ai_matcher.rb', line 11 def api_key @api_key end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
11 12 13 |
# File 'lib/aihype/ai_matcher.rb', line 11 def model @model end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
11 12 13 |
# File 'lib/aihype/ai_matcher.rb', line 11 def timeout @timeout end |
Class Method Details
.rate_limiter ⇒ Object
Class-level rate limiter shared across all instances
16 17 18 19 20 21 |
# File 'lib/aihype/ai_matcher.rb', line 16 def self.rate_limiter @rate_limiter ||= RateLimiter.new( requests_per_minute: Env.rate_limit_rpm, window_size: Env.rate_limit_window ) end |
Instance Method Details
#evaluate(prompt_text, blacklist_rules) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/aihype/ai_matcher.rb', line 29 def evaluate(prompt_text, blacklist_rules) return fallback_result('No API key provided') if @api_key.nil? || @api_key.empty? request_payload = build_request(prompt_text, blacklist_rules) response = http_post(request_payload) parse_response(response) rescue Faraday::TimeoutError fallback_result('AI timeout') rescue Faraday::UnauthorizedError fallback_result('Authentication failed') rescue Faraday::ConnectionFailed, Faraday::Error fallback_result('Network error') rescue JSON::ParserError, StandardError => e fallback_result("Invalid response: #{e.}") end |
#http_post(payload) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aihype/ai_matcher.rb', line 45 def http_post(payload) # Apply rate limiting before making request self.class.rate_limiter.throttle conn = Faraday.new(url: CLAUDE_API_URL) do |f| f.request :json f.response :json f.adapter Faraday.default_adapter f..timeout = @timeout f..open_timeout = @timeout end response = conn.post do |req| req.headers['anthropic-version'] = '2023-06-01' req.headers['x-api-key'] = @api_key req.headers['content-type'] = 'application/json' req.body = payload end extract_claude_response(response.body) end |