Class: PromptEngine::PlaygroundExecutor

Inherits:
Object
  • Object
show all
Defined in:
app/services/prompt_engine/playground_executor.rb

Constant Summary collapse

MODELS =
{
  "anthropic" => "claude-3-5-sonnet-20241022",
  "openai" => "gpt-4o"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt:, provider:, api_key:, parameters: {}) ⇒ PlaygroundExecutor

Returns a new instance of PlaygroundExecutor.



10
11
12
13
14
15
# File 'app/services/prompt_engine/playground_executor.rb', line 10

def initialize(prompt:, provider:, api_key:, parameters: {})
  @prompt = prompt
  @provider = provider
  @api_key = api_key
  @parameters = parameters || {}
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'app/services/prompt_engine/playground_executor.rb', line 3

def api_key
  @api_key
end

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'app/services/prompt_engine/playground_executor.rb', line 3

def parameters
  @parameters
end

#promptObject (readonly)

Returns the value of attribute prompt.



3
4
5
# File 'app/services/prompt_engine/playground_executor.rb', line 3

def prompt
  @prompt
end

#providerObject (readonly)

Returns the value of attribute provider.



3
4
5
# File 'app/services/prompt_engine/playground_executor.rb', line 3

def provider
  @provider
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'app/services/prompt_engine/playground_executor.rb', line 17

def execute
  validate_inputs!

  start_time = Time.current

  # Replace parameters in prompt content
  parser = ParameterParser.new(prompt.content)
  processed_content = parser.replace_parameters(parameters)

  # Configure RubyLLM with the appropriate API key
  configure_ruby_llm

  # Create chat instance with the model
  chat = RubyLLM.chat(model: MODELS[provider])

  # Apply temperature if specified
  if prompt.temperature.present?
    chat = chat.with_temperature(prompt.temperature)
  end

  # Apply system message if present
  if prompt.system_message.present?
    chat = chat.with_instructions(prompt.system_message)
  end

  # Execute the prompt
  # Note: max_tokens may need to be passed differently depending on RubyLLM version
  response = chat.ask(processed_content)

  execution_time = (Time.current - start_time).round(3)

  # Handle response based on its structure
  response_content = if response.respond_to?(:content)
                      response.content
  elsif response.is_a?(String)
                      response
  else
                      response.to_s
  end

  # Try to get token count if available
  token_count = if response.respond_to?(:input_tokens) && response.respond_to?(:output_tokens)
                  (response.input_tokens || 0) + (response.output_tokens || 0)
  else
                  0 # Default if token information isn't available
  end

  {
    response: response_content,
    execution_time: execution_time,
    token_count: token_count,
    model: MODELS[provider],
    provider: provider
  }
rescue StandardError => e
  handle_error(e)
end