Method: ActiveAI::Behavior::LLM::WriteFunctionCall#call

Defined in:
lib/activeai/behavior/llm/write_function_call.rb

#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