Class: Boxcars::Cohere
- Includes:
- OpenAICompatibleChatHelpers, UnifiedObservability
- Defined in:
- lib/boxcars/engine/cohere.rb
Overview
An engine that uses Cohere’s API.
Constant Summary collapse
- DEFAULT_PARAMS =
The default parameters to use when asking the engine.
{ model: "command-a-03-2025", max_tokens: 4000, max_input_tokens: 1000, temperature: 0.2 }.freeze
- DEFAULT_NAME =
The default name of the engine.
"Cohere engine"- DEFAULT_DESCRIPTION =
The default description of the engine.
"useful for when you need to use Cohere AI to answer questions. " \ "You should ask targeted questions"
Instance Attribute Summary collapse
-
#llm_params ⇒ Object
readonly
Returns the value of attribute llm_params.
Attributes inherited from Engine
Instance Method Summary collapse
- #chat(params, cohere_api_key) ⇒ Object
-
#client(prompt:, inputs: {}, **kwargs) ⇒ Object
Calls Cohere and returns the parsed response object.
- #default_params ⇒ Object
- #default_prefixes ⇒ Object
-
#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, **kwargs) ⇒ Cohere
constructor
Initializes a Cohere engine instance.
Methods inherited from Engine
#add_usage_detail!, #aggregate_generate_usage!, #aggregate_token_usage_details!, #append_generate_choices!, #capabilities, #extract_answer, #generate, #generate_one, #generation_info, #get_num_tokens, #normalize_generate_response, #process_generate_prompt!, #process_generate_response!, #run, #supports?, #usage_nested_token_value, #usage_token_value, #validate_response!
Constructor Details
#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, **kwargs) ⇒ Cohere
Initializes a Cohere engine instance.
27 28 29 30 31 32 33 |
# File 'lib/boxcars/engine/cohere.rb', line 27 def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, **kwargs) raise ArgumentError, "unknown keyword: :prompts" if kwargs.key?(:prompts) user_id = kwargs.delete(:user_id) @llm_params = DEFAULT_PARAMS.merge(kwargs) super(description:, name:, batch_size: 20, user_id:) end |
Instance Attribute Details
#llm_params ⇒ Object (readonly)
Returns the value of attribute llm_params.
10 11 12 |
# File 'lib/boxcars/engine/cohere.rb', line 10 def llm_params @llm_params end |
Instance Method Details
#chat(params, cohere_api_key) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/boxcars/engine/cohere.rb', line 35 def chat(params, cohere_api_key) Boxcars::OptionalDependency.require!("faraday", feature: "Boxcars::Cohere") ensure_cohere_api_key!( cohere_api_key, error_class: Boxcars::ConfigurationError, message: "Cohere API key not set" ) parse_cohere_response_body(post_cohere_chat(params, cohere_api_key).body) end |
#client(prompt:, inputs: {}, **kwargs) ⇒ Object
Calls Cohere and returns the parsed response object.
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 |
# File 'lib/boxcars/engine/cohere.rb', line 47 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 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 |
#default_params ⇒ Object
81 82 83 |
# File 'lib/boxcars/engine/cohere.rb', line 81 def default_params llm_params end |
#default_prefixes ⇒ Object
85 86 87 |
# File 'lib/boxcars/engine/cohere.rb', line 85 def default_prefixes { system: "SYSTEM: ", user: "USER: ", assistant: "CHATBOT: ", history: :history } end |