Module: Legion::Extensions::AzureAi::Runners::ContentSafety

Extended by:
Helpers::Client
Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/azure_ai/runners/content_safety.rb

Instance Method Summary collapse

Methods included from Helpers::Client

client, content_safety_client

Instance Method Details

#check_text(content:, endpoint:, api_key:, severity_threshold: 2, categories: nil, api_version: '2024-09-01') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/azure_ai/runners/content_safety.rb', line 12

def check_text(content:, endpoint:, api_key:, severity_threshold: 2,
               categories: nil, api_version: '2024-09-01', **)
  body = { text: content }
  body[:categories] = categories if categories

  path = "/contentsafety/text:analyze?api-version=#{api_version}"
  response = content_safety_client(api_key: api_key, endpoint: endpoint).post(path, body)

  categories_analysis = response.body['categoriesAnalysis'] || []
  blocked_categories = categories_analysis.select { |c| c['severity'] >= severity_threshold }

  {
    result: {
      blocked:    !blocked_categories.empty?,
      reasons:    blocked_categories.map { |c| c['category'] },
      categories: categories_analysis,
      raw:        response.body
    }
  }
end