Class: Stealth::Nlp::Clu::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(subscription_key: nil, project_name: nil, deployment_name: 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/clu/client.rb', line 8

def initialize(subscription_key: nil, project_name: nil, deployment_name: nil, endpoint: nil, datetime_ref: nil)
  begin
    @subscription_key = subscription_key || Stealth.config.clu.subscription_key
    @project_name = project_name || Stealth.config.clu.project_name
    @deployment_name = deployment_name || Stealth.config.clu.deployment_name
    @endpoint = endpoint || Stealth.config.clu.endpoint
    @datetime_ref = datetime_ref || Stealth.config.clu.datetime_reference
  rescue NoMethodError
    raise(
      Stealth::Errors::ConfigurationError,
      'A `clu` configuration key must be specified directly or in `services.yml`'
    )
  end
end

Instance Method Details

#clientObject



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

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

#endpointObject



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

def endpoint
  "https://#{@endpoint}/language/:analyze-conversations?api-version=2023-04-01"
end

#understand(query:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stealth/nlp/clu/client.rb', line 37

def understand(query:)
  body = MultiJson.dump({
    'kind' => 'Conversation',
    'analysisInput' => {
      'conversationItem' => {
        'participantId' => '1',
        'id' => '1',
        'modality' => 'text',
        'language' => 'en',
        'text' => query
      }
    },
    'parameters' => {
      'projectName' => @project_name,
      'deploymentName' => @deployment_name,
      'stringIndexType' => 'TextElement_V8',
      'verbose' => true
    }
  })

  Stealth::Logger.l(
    topic: :nlp,
    message: 'Performing NLP lookup via Microsoft CLU'
  )

  result = client.post(endpoint, body: body)
  Result.new(result: result)
end