Method: Boxcars::Cohere#client

Defined in:
lib/boxcars/engine/cohere.rb

#client(prompt:, inputs: {}, **kwargs) ⇒ Object

Get an answer from the engine.

Parameters:

  • prompt (String)

    The prompt to use when asking the engine.

  • cohere_api_key (String)

    Optional api key to use when asking the engine. Defaults to Boxcars.configuration.cohere_api_key.

  • kwargs (Hash)

    Additional parameters to pass to the engine if wanted.



64
65
66
67
68
69
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
96
# File 'lib/boxcars/engine/cohere.rb', line 64

def client(prompt:, inputs: {}, **kwargs)
  start_time = Time.now
  response_data = { response_obj: nil, parsed_json: nil, success: false, error: nil, status_code: nil }
  current_params = llm_params.merge(kwargs)
  current_prompt_object = prompt.is_a?(Array) ? prompt.first : prompt
  api_request_params = nil

  begin
    api_key = Boxcars.configuration.cohere_api_key(**kwargs)
    api_request_params = current_prompt_object.as_prompt(inputs:, prefixes: default_prefixes,
                                                         show_roles: true).merge(current_params)
    api_request_params[:message] = api_request_params.delete(:prompt)
    api_request_params[:stop_sequences] = api_request_params.delete(:stop) if api_request_params.key?(:stop)

    Boxcars.debug("Prompt after formatting:#{api_request_params[:message]}", :cyan) if Boxcars.configuration.log_prompts

    raw_response = _cohere_api_call(api_request_params, api_key)
    _process_cohere_response(raw_response, response_data)
  rescue StandardError => e
    _handle_cohere_error(e, response_data)
  ensure
    call_context = {
      start_time:,
      prompt_object: current_prompt_object,
      inputs:,
      api_request_params:,
      current_params:
    }
    _track_cohere_observability(call_context, response_data)
  end

  _cohere_handle_call_outcome(response_data:)
end