Class: ObsceneGpt::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/obscene_gpt/detector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, schema: nil, prompt: nil, request_timeout: nil) ⇒ Detector

Returns a new instance of Detector.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/obscene_gpt/detector.rb', line 8

def initialize(api_key: nil, model: nil, schema: nil, prompt: nil, request_timeout: nil)
  api_key ||= ObsceneGpt.configuration.api_key

  @client = OpenAI::Client.new(
    access_token: api_key,
    request_timeout: request_timeout || ObsceneGpt.configuration.request_timeout,
  )
  @model = model || ObsceneGpt.configuration.model
  @schema = schema || ObsceneGpt.configuration.schema
  @prompt = prompt || ObsceneGpt.configuration.prompt
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/obscene_gpt/detector.rb', line 6

def client
  @client
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/obscene_gpt/detector.rb', line 6

def model
  @model
end

#promptObject (readonly)

Returns the value of attribute prompt.



6
7
8
# File 'lib/obscene_gpt/detector.rb', line 6

def prompt
  @prompt
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/obscene_gpt/detector.rb', line 6

def schema
  @schema
end

Instance Method Details

#detect(text) ⇒ Object

Detects whether the given text contains obscene content See #detect_many for more details



38
39
40
# File 'lib/obscene_gpt/detector.rb', line 38

def detect(text)
  detect_many([text])[0]
end

#detect_many(texts) ⇒ Array<Hash>

Detects whether the given texts contain obscene content

Parameters:

  • texts (Array<String>)

    The texts to analyze

Returns:

  • (Array<Hash>)

    An array of hashes containing the detection result with keys:

    • :obscene [Boolean] Whether the text contains obscene content

    • :confidence [Float] Confidence score (0.0 to 1.0)

    • :reasoning [String] Explanation for the classification (only for full schema)

    • :categories [Array<String>] Categories of inappropriate content found (only for full schema)



27
28
29
30
31
32
33
34
# File 'lib/obscene_gpt/detector.rb', line 27

def detect_many(texts)
  if ObsceneGpt.configuration.test_mode
    test_detector = ObsceneGpt.configuration.test_detector_class.new(schema: @schema)
    return test_detector.detect_many(texts)
  end

  run_detect_many(texts)
end