Class: Boxcars::Groq

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

Overview

A engine that uses Groq’s API.

Constant Summary collapse

DEFAULT_PARAMS =
{
  model: "llama3-70b-8192",
  temperature: 0.1,
  max_tokens: 4096 # Groq API might have specific limits or naming for this
}.freeze
DEFAULT_NAME =

Groq API might have specific limits or naming for this

"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

#user_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#extract_answer, #generate, #generation_info, #get_num_tokens

Constructor Details

#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 20, **kwargs) ⇒ Groq

Returns a new instance of Groq.



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

def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, prompts: [], batch_size: 20, **kwargs)
  user_id = kwargs.delete(:user_id)
  @groq_params = DEFAULT_PARAMS.merge(kwargs) # Corrected typo here
  @prompts = prompts
  @batch_size = batch_size
  super(description:, name:, user_id:)
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



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

def batch_size
  @batch_size
end

#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

#model_kwargsObject (readonly)

Returns the value of attribute model_kwargs.



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

def model_kwargs
  @model_kwargs
end

#promptsObject (readonly)

Returns the value of attribute prompts.



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

def prompts
  @prompts
end

Class Method Details

.groq_client(groq_api_key: nil) ⇒ Object

Renamed from open_ai_client to groq_client for clarity



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

def self.groq_client(groq_api_key: nil)
  access_token = Boxcars.configuration.groq_api_key(groq_api_key:)
  ::OpenAI::Client.new(access_token:, uri_base: "https://api.groq.com/openai/v1")
  # Adjusted uri_base to include /v1 as is common for OpenAI-compatible APIs
end

Instance Method Details

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



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
81
# File 'lib/boxcars/engine/groq.rb', line 42

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.is_a?(Array) ? prompt.first : prompt
  api_request_params = nil # Initialize

  begin
    clnt = Groq.groq_client(groq_api_key:)
    api_request_params = _prepare_groq_request_params(current_prompt_object, inputs, current_params)

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

    _execute_and_process_groq_call(clnt, api_request_params, response_data)
  rescue ::OpenAI::Error => e
    _handle_openai_error_for_groq(e, response_data)
  rescue StandardError => e
    _handle_standard_error_for_groq(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

  # If there's an error, raise it to maintain backward compatibility with existing tests
  raise response_data[:error] if response_data[:error]

  response_data[:parsed_json]
end

#conversation_model?(_model_name) ⇒ Boolean

Groq models are typically conversational.

Returns:

  • (Boolean)


38
39
40
# File 'lib/boxcars/engine/groq.rb', line 38

def conversation_model?(_model_name)
  true
end

#run(question) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/boxcars/engine/groq.rb', line 83

def run(question, **)
  prompt = Prompt.new(template: question)
  response = client(prompt:, inputs: {}, **)
  answer = extract_answer(response)
  Boxcars.debug("Answer: #{answer}", :cyan)
  answer
end