Class: ActiveAI::Behavior::LLM::FollowStructuredExamples

Inherits:
ActiveAI::Behavior::LLM show all
Defined in:
lib/activeai/behavior/llm/follow_structured_examples.rb

Constant Summary

Constants inherited from ActiveAI::Behavior::LLM

LINE_SEPARATOR

Instance Method Summary collapse

Methods inherited from ActiveAI::Behavior::LLM

#complete, #extract_keys

Constructor Details

#initialize(llm, state) ⇒ FollowStructuredExamples

state is an instruction, and a list of examples with key/value pairs would be nice to do casting, but not now i dont think..



5
6
7
8
9
# File 'lib/activeai/behavior/llm/follow_structured_examples.rb', line 5

def initialize(llm, state)
  super(llm)
  @state = state
  # TODO raise errors if not expected thingies available in the config
end

Instance Method Details

#base_promptObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/activeai/behavior/llm/follow_structured_examples.rb', line 11

def base_prompt
  [
    @state['instruction'],
    @state['examples'].map do |example|
      example.map do |key, value|
        "#{key}: #{value}"
      end.join("\n")
    end.join(LINE_SEPARATOR)
  ].join(LINE_SEPARATOR)
end

#call(input = {}, extract: []) ⇒ Object

TODO cool splat stuff?



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/activeai/behavior/llm/follow_structured_examples.rb', line 22

def call(input={}, extract: []) # TODO cool splat stuff?
  prompt = base_prompt + LINE_SEPARATOR

  prompt += input.map do |key, value|
    "#{key}: #{value}"
  end.join("\n")

  complete_result = complete(prompt)
  completion = complete_result['choices'][0]['text']

  return extract_keys(completion, extract)
end