Class: Stealth::Nlp::Luis::Client

Inherits:
Client
  • Object
show all
Defined in:
lib/stealth/nlp/luis/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(subscription_key: nil, app_id: nil, endpoint: nil, datetime_ref: nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stealth/nlp/luis/client.rb', line 8

def initialize(subscription_key: nil, app_id: nil, endpoint: nil, datetime_ref: nil)
  begin
    @subscription_key = subscription_key || Stealth.config.luis.subscription_key
    @app_id = app_id || Stealth.config.luis.app_id
    @endpoint = endpoint || Stealth.config.luis.endpoint
    @datetime_ref = datetime_ref || Stealth.config.luis.datetime_reference
    @slot = Stealth.env.development? ? 'staging' : 'production'
  rescue NoMethodError
    raise(
      Stealth::Errors::ConfigurationError,
      'A `luis` configuration key must be specified directly or in `services.yml`'
    )
  end
end

Instance Method Details

#clientObject



27
28
29
30
31
32
33
34
# File 'lib/stealth/nlp/luis/client.rb', line 27

def client
  @client ||= begin
    headers = {
      'Content-Type' => 'application/json'
    }
    HTTP.timeout(connect: 15, read: 60).headers(headers)
  end
end

#endpointObject



23
24
25
# File 'lib/stealth/nlp/luis/client.rb', line 23

def endpoint
  "https://#{@endpoint}/luis/prediction/v3.0/apps/#{@app_id}/slots/#{@slot}/predict"
end

#understand(query:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stealth/nlp/luis/client.rb', line 36

def understand(query:)
  params = {
    'subscription-key'    => @subscription_key
  }

  body = MultiJson.dump({
    'query'               => query,
    'options'             => {
      'datetimeReference' => @datetime_ref,
    }
  })

  Stealth::Logger.l(
    topic: :nlp,
    message: 'Performing NLP lookup via Microsoft LUIS'
  )
  result = client.post(endpoint, params: params, body: body)
  Result.new(result: result)
end