Class: Apologist::Corpus::Client
- Inherits:
-
Object
- Object
- Apologist::Corpus::Client
- Defined in:
- lib/Apologist/corpus/client.rb
Instance Method Summary collapse
- #initialize(client:) ⇒ void constructor
-
#log_corpus_impression(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a corpus item was shown to a user.
-
#log_corpus_referral(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a user was referred to a corpus item.
-
#log_corpus_referral_redirect(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records a referral for a corpus item and, when a
urlis supplied, issues a 302 redirect to it. -
#log_corpus_view(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a user viewed a specific corpus item.
-
#search_corpus(request_options: {}, **params) ⇒ Apologist::Corpus::Types::SearchCorpusResponse
Performs a semantic search across the agent's corpus of knowledge.
Constructor Details
#initialize(client:) ⇒ void
9 10 11 |
# File 'lib/Apologist/corpus/client.rb', line 9 def initialize(client:) @client = client end |
Instance Method Details
#log_corpus_impression(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a corpus item was shown to a user
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/Apologist/corpus/client.rb', line 117 def log_corpus_impression(request_options: {}, **params) params = Apologist::Internal::Types::Utils.normalize_keys(params) request_data = Apologist::Corpus::Types::ImpressionRequest.new(params).to_h non_body_param_names = %w[model id] body = request_data.except(*non_body_param_names) request = Apologist::Internal::JSON::Request.new( base_url: [:base_url], method: "POST", path: "corpus/#{URI.encode_uri_component(params[:model].to_s)}/#{URI.encode_uri_component(params[:id].to_s)}/impression", body: body, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Apologist::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Apologist::Types::SuccessResponse.load(response.body) else error_class = Apologist::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#log_corpus_referral(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a user was referred to a corpus item
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/Apologist/corpus/client.rb', line 216 def log_corpus_referral(request_options: {}, **params) params = Apologist::Internal::Types::Utils.normalize_keys(params) request_data = Apologist::Corpus::Types::ReferralRequest.new(params).to_h non_body_param_names = %w[model id] body = request_data.except(*non_body_param_names) request = Apologist::Internal::JSON::Request.new( base_url: [:base_url], method: "POST", path: "corpus/#{URI.encode_uri_component(params[:model].to_s)}/#{URI.encode_uri_component(params[:id].to_s)}/referral", body: body, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Apologist::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Apologist::Types::SuccessResponse.load(response.body) else error_class = Apologist::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#log_corpus_referral_redirect(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records a referral for a corpus item and, when a url is supplied, issues a 302 redirect to it. Without a
url, responds with a success message. Requires either the search API entitlement or a same-origin request.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/Apologist/corpus/client.rb', line 168 def log_corpus_referral_redirect(request_options: {}, **params) params = Apologist::Internal::Types::Utils.normalize_keys(params) query_params = {} query_params["prompt_id"] = params[:prompt_id] if params.key?(:prompt_id) query_params["user_id"] = params[:user_id] if params.key?(:user_id) query_params["url"] = params[:url] if params.key?(:url) request = Apologist::Internal::JSON::Request.new( base_url: [:base_url], method: "GET", path: "corpus/#{URI.encode_uri_component(params[:model].to_s)}/#{URI.encode_uri_component(params[:id].to_s)}/referral", query: query_params, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Apologist::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Apologist::Types::SuccessResponse.load(response.body) else error_class = Apologist::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#log_corpus_view(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse
Records that a user viewed a specific corpus item
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/Apologist/corpus/client.rb', line 70 def log_corpus_view(request_options: {}, **params) params = Apologist::Internal::Types::Utils.normalize_keys(params) request_data = Apologist::Corpus::Types::ViewRequest.new(params).to_h non_body_param_names = %w[model id] body = request_data.except(*non_body_param_names) request = Apologist::Internal::JSON::Request.new( base_url: [:base_url], method: "POST", path: "corpus/#{URI.encode_uri_component(params[:model].to_s)}/#{URI.encode_uri_component(params[:id].to_s)}/view", body: body, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Apologist::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Apologist::Types::SuccessResponse.load(response.body) else error_class = Apologist::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |
#search_corpus(request_options: {}, **params) ⇒ Apologist::Corpus::Types::SearchCorpusResponse
Performs a semantic search across the agent's corpus of knowledge
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Apologist/corpus/client.rb', line 27 def search_corpus(request_options: {}, **params) params = Apologist::Internal::Types::Utils.normalize_keys(params) request = Apologist::Internal::JSON::Request.new( base_url: [:base_url], method: "POST", path: "corpus/search", body: Apologist::Corpus::Types::CorpusSearchRequest.new(params).to_h, request_options: ) begin response = @client.send(request) rescue Net::HTTPRequestTimeout raise Apologist::Errors::TimeoutError end code = response.code.to_i if code.between?(200, 299) Apologist::Corpus::Types::SearchCorpusResponse.load(response.body) else error_class = Apologist::Errors::ResponseError.subclass_for_code(code) raise error_class.new(response.body, code: code) end end |