Class: Apologist::Chat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/Apologist/chat/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/Apologist/chat/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#create_chat_completion(request_options: {}, **params) ⇒ Apologist::Types::ChatCompletionResponse

Creates a chat completion using the agent's configured model. Supports both streaming and non-streaming responses.

Examples:

client.chat.create_chat_completion(request: {
  key: "value"
})

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/Apologist/chat/client.rb', line 106

def create_chat_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "chat/completions",
    body: Apologist::Types::ChatCompletionRequest.new(params).to_h,
    request_options: 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::ChatCompletionResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#feedback_completion(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse

Adds user feedback to a specific chat completion

Examples:

client.chat.feedback_completion(
  id: "id",
  feedback: "feedback"
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/Apologist/chat/client.rb', line 237

def feedback_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Chat::Types::FeedbackRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "chat/completions/#{URI.encode_uri_component(params[:id].to_s)}/feedback",
    body: body,
    request_options: 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

#flag_completion(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse

Updates the flagged status of a specific chat completion

Examples:

client.chat.flag_completion(
  id: "id",
  flagged: true
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/Apologist/chat/client.rb', line 192

def flag_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Chat::Types::FlagRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "chat/completions/#{URI.encode_uri_component(params[:id].to_s)}/flag",
    body: body,
    request_options: 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

#get_chat_completion(request_options: {}, **params) ⇒ Apologist::Chat::Types::GetChatCompletionResponse

Returns a single chat completion (prompt) by numeric id or UUID, including applied tags, guardrail/cta metadata, share metadata, and automation results.

Examples:

client.chat.get_chat_completion(id: "id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/Apologist/chat/client.rb', line 322

def get_chat_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "chat/completions/#{URI.encode_uri_component(params[:id].to_s)}",
    request_options: 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::Chat::Types::GetChatCompletionResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#like_completion(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse

Updates the like status of a specific chat completion

Examples:

client.chat.like_completion(
  id: "id",
  liked: true
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/Apologist/chat/client.rb', line 147

def like_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Chat::Types::LikeRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "chat/completions/#{URI.encode_uri_component(params[:id].to_s)}/like",
    body: body,
    request_options: 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

#list_chat_completions(request_options: {}, **params) ⇒ Apologist::Chat::Types::ListChatCompletionsResponse

Returns a paginated list of chat completions (prompts) for the agent, with applied tags expanded as { id, name } and share metadata.

Examples:

client.chat.list_chat_completions

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :page (Integer, nil)
  • :per_page (Integer, nil)
  • :agent_id (String, nil)
  • :channel_id (String, nil)
  • :bible_id (String, nil)
  • :cached (String, nil)
  • :client (String, nil)
  • :config_id (String, nil)
  • :conversation_id (String, nil)
  • :device_id (String, nil)
  • :flagged (String, nil)
  • :favorited (String, nil)
  • :language (String, nil)
  • :liked (String, nil)
  • :session_id (String, nil)
  • :user_id (String, nil)
  • :min_timestamp (String, nil)
  • :max_timestamp (String, nil)

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/Apologist/chat/client.rb', line 46

def list_chat_completions(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["per_page"] = params[:per_page] if params.key?(:per_page)
  query_params["agent_id"] = params[:agent_id] if params.key?(:agent_id)
  query_params["channel_id"] = params[:channel_id] if params.key?(:channel_id)
  query_params["bible_id"] = params[:bible_id] if params.key?(:bible_id)
  query_params["cached"] = params[:cached] if params.key?(:cached)
  query_params["client"] = params[:client] if params.key?(:client)
  query_params["config_id"] = params[:config_id] if params.key?(:config_id)
  query_params["conversation_id"] = params[:conversation_id] if params.key?(:conversation_id)
  query_params["device_id"] = params[:device_id] if params.key?(:device_id)
  query_params["flagged"] = params[:flagged] if params.key?(:flagged)
  query_params["favorited"] = params[:favorited] if params.key?(:favorited)
  query_params["language"] = params[:language] if params.key?(:language)
  query_params["liked"] = params[:liked] if params.key?(:liked)
  query_params["session_id"] = params[:session_id] if params.key?(:session_id)
  query_params["user_id"] = params[:user_id] if params.key?(:user_id)
  query_params["min_timestamp"] = params[:min_timestamp] if params.key?(:min_timestamp)
  query_params["max_timestamp"] = params[:max_timestamp] if params.key?(:max_timestamp)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "chat/completions",
    query: query_params,
    request_options: 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::Chat::Types::ListChatCompletionsResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#share_completion(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse

Creates a share record for a specific chat completion

Examples:

client.chat.share_completion(id: "id")

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/Apologist/chat/client.rb', line 279

def share_completion(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Chat::Types::ShareRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "chat/completions/#{URI.encode_uri_component(params[:id].to_s)}/share",
    body: body,
    request_options: 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