Class: ActiveAI::Behavior::LLM::WriteFunctionCall

Inherits:
ActiveAI::Behavior::LLM show all
Defined in:
lib/activeai/behavior/llm/write_function_call.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) ⇒ WriteFunctionCall

Returns a new instance of WriteFunctionCall.



2
3
4
5
6
# File 'lib/activeai/behavior/llm/write_function_call.rb', line 2

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



8
9
10
11
12
# File 'lib/activeai/behavior/llm/write_function_call.rb', line 8

def base_prompt
  (@state[:examples].map do |example|
    "/* #{example[:description]} */\n#{example[:code]}"
  end + [""]).join("<|endoftext|>")
end

#call(comment) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/activeai/behavior/llm/write_function_call.rb', line 14

def call(comment)
  prompt = base_prompt
  prompt += "/* #{comment} */\n"

  complete_result = complete(prompt) # this still breaks sometimes by generating like 50 functions instead of stopping. think i fixed it? forgot an <|endoftext|> at the end

  # puts prompt
  # puts complete_result

  completion = complete_result['choices'][0]['text']
  completion = completion.strip.gsub("\n", "\\n") # fixes parsing errors (in JSON??)

  matcher = /(.*?)\((.*)\)/m # should be made ungreedy, but then i dont see when it over-completes because it fails silently
  matches = matcher.match(completion)

  if matches.nil?
    # binding.pry
    raise "Unmatched router response in #{self.class}"
  end

  return {
    text: completion.strip,
    path: matches[1],
    params: matches[2].presence && JSON.parse(matches[2])
  }
end