|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Stealth |
| 4 | + module Nlp |
| 5 | + module Clu |
| 6 | + class Client < Stealth::Nlp::Client |
| 7 | + |
| 8 | + def initialize(subscription_key: nil, project_name: nil, deployment_name: nil, endpoint: nil, datetime_ref: nil) |
| 9 | + begin |
| 10 | + @subscription_key = subscription_key || Stealth.config.clu.subscription_key |
| 11 | + @project_name = project_name || Stealth.config.clu.project_name |
| 12 | + @deployment_name = deployment_name || Stealth.config.clu.deployment_name |
| 13 | + @endpoint = endpoint || Stealth.config.clu.endpoint |
| 14 | + @datetime_ref = datetime_ref || Stealth.config.clu.datetime_reference |
| 15 | + rescue NoMethodError |
| 16 | + raise( |
| 17 | + Stealth::Errors::ConfigurationError, |
| 18 | + 'A `clu` configuration key must be specified directly or in `services.yml`' |
| 19 | + ) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def endpoint |
| 24 | + "https://#{@endpoint}/language/:analyze-conversations?api-version=2023-04-01" |
| 25 | + end |
| 26 | + |
| 27 | + def client |
| 28 | + @client ||= begin |
| 29 | + headers = { |
| 30 | + 'Content-Type' => 'application/json', |
| 31 | + 'Ocp-Apim-Subscription-Key' => @subscription_key |
| 32 | + } |
| 33 | + HTTP.timeout(connect: 15, read: 60).headers(headers) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def understand(query:) |
| 38 | + body = MultiJson.dump({ |
| 39 | + 'kind' => 'Conversation', |
| 40 | + 'analysisInput' => { |
| 41 | + 'conversationItem' => { |
| 42 | + 'participantId' => '1', |
| 43 | + 'id' => '1', |
| 44 | + 'modality' => 'text', |
| 45 | + 'language' => 'en', |
| 46 | + 'text' => query |
| 47 | + } |
| 48 | + }, |
| 49 | + 'parameters' => { |
| 50 | + 'projectName' => @project_name, |
| 51 | + 'deploymentName' => @deployment_name, |
| 52 | + 'stringIndexType' => 'TextElement_V8', |
| 53 | + 'verbose' => true |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + Stealth::Logger.l( |
| 58 | + topic: :nlp, |
| 59 | + message: 'Performing NLP lookup via Microsoft CLU' |
| 60 | + ) |
| 61 | + |
| 62 | + result = client.post(endpoint, body: body) |
| 63 | + Result.new(result: result) |
| 64 | + end |
| 65 | + |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +ENTITY_TYPES = %i(number currency email percentage phone age |
| 72 | + url ordinal geo dimension temp datetime duration) |
0 commit comments