Class: Boxcars::Groq

Inherits:
Engine show all
Includes:
OpenAICompatibleChatHelpers, UnifiedObservability
Defined in:
lib/boxcars/engine/groq.rb

Overview

An engine that uses Groq’s API.

Constant Summary collapse

DEFAULT_PARAMS =
{
  model: "llama3-70b-8192",
  temperature: 0.1,
  max_tokens: 4096
}.freeze
DEFAULT_NAME =
"Groq engine"
DEFAULT_DESCRIPTION =
"useful for when you need to use Groq AI to answer questions. " \
"You should ask targeted questions"

Instance Attribute Summary collapse

Attributes inherited from Engine

#batch_size, #user_id

Class Method Summary collapse

Instance Method Summary collapse

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, batch_size: 20, **kwargs) ⇒ Groq

Returns a new instance of Groq.

Raises:



22
23
24
25
26
27
28
# File 'lib/boxcars/engine/groq.rb', line 22

def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs)
  raise ArgumentError, "unknown keyword: :prompts" if kwargs.key?(:prompts)

  user_id = kwargs.delete(:user_id)
  @groq_params = DEFAULT_PARAMS.merge(kwargs)
  super(description:, name:, batch_size:, user_id:)
end

Instance Attribute Details

#groq_paramsObject (readonly)

Returns the value of attribute groq_params.



11
12
13
# File 'lib/boxcars/engine/groq.rb', line 11

def groq_params
  @groq_params
end

Class Method Details

.provider_client(groq_api_key: nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/boxcars/engine/groq.rb', line 30

def self.provider_client(groq_api_key: nil)
  access_token = Boxcars.configuration.groq_api_key(groq_api_key:)
  Boxcars::OpenAIClient.build(
    access_token:,
    uri_base: "https://api.groq.com/openai/v1"
  )
end

Instance Method Details

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



38
39
40
41
42
43
44
45
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
# File 'lib/boxcars/engine/groq.rb', line 38

def client(prompt:, inputs: {}, groq_api_key: nil, **kwargs)
  start_time = Time.now
  response_data = { response_obj: nil, parsed_json: nil, success: false, error: nil, status_code: nil }
  current_params = @groq_params.merge(kwargs)
  current_prompt_object = prompt
  api_request_params = nil

  begin
    clnt = self.class.provider_client(groq_api_key:)
    api_request_params = prepare_openai_compatible_chat_request(current_prompt_object, inputs, current_params)

    log_messages_debug(api_request_params[:messages]) if Boxcars.configuration.log_prompts && api_request_params[:messages]

    execute_openai_compatible_chat_call(
      client: clnt,
      api_request_params: api_request_params,
      response_data: response_data,
      success_check: ->(raw) { raw[:choices] },
      unknown_error_message: "Unknown Groq API Error"
    )
  rescue StandardError => e
    handle_openai_compatible_standard_error(e, response_data)
  ensure
    duration_ms = ((Time.now - start_time) * 1000).round
    request_context = {
      prompt: current_prompt_object,
      inputs:,
      conversation_for_api: api_request_params&.dig(:messages),
      user_id:
    }
    track_ai_generation(
      duration_ms:,
      current_params:,
      request_context:,
      response_data:,
      provider: :groq
    )
  end

  raise response_data[:error] if response_data[:error]

  response_data[:parsed_json]
end