Skip to content

Instantly share code, notes, and snippets.

@swerner
Last active October 10, 2023 16:32
Show Gist options
  • Save swerner/4c2e6c6e02b21dabe7d741e7becb4cb0 to your computer and use it in GitHub Desktop.
Save swerner/4c2e6c6e02b21dabe7d741e7becb4cb0 to your computer and use it in GitHub Desktop.
require_relative "./method_generator.rb"
class Calculator
include MethodGenerator
end
require_relative "./calculator.rb"
require "pry"
calc = Calculator.new
# Try calc.add(2,3)
binding.pry
calc.add(2,3)
require "openai"
require "tempfile"
module MethodGenerator
def self.included(base)
base.instance_variable_set(:@source_file, caller_locations.first.path)
base.extend ClassMethods
end
module ClassMethods
def source_file_contents
File.read(@source_file) if @source_file
end
end
def method_missing(method_name, *args, &block)
puts "You tried to call #{method_name} which isn't implemented. Calling Openai to request implementation..."
code, defmethod = generate_implementation(method_name, args, self.class.source_file_contents)
puts "Proposed implementation of #{method_name}:\n\r#{code}\n\r"
puts "Do you want to add this implementation to the class? (y/n)"
input = gets.chomp
if input == "y"
tempfile = Tempfile.new("generated_method.rb")
tempfile.write(defmethod)
tempfile.close
system("vim #{tempfile.path}")
tempfile.open
defmethod = tempfile.read
tempfile.close
tempfile.unlink
eval("self.class.#{defmethod}")
self.send(method_name, *args, &block)
end
end
def respond_to_missing?(method_name, include_private = false)
true
end
private
def generate_implementation(method_name, args, file_contents)
client = OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"])
response = client.chat(
parameters: {
model: "gpt-4",
messages: [
{
"role": "user",
"content": "You are an expert ruby programmer. Given the method named: #{method_name}, arguments #{args} and the contents of the file: #{file_contents} as context. What would be your best guess as to the functions implementation? Please use defmethod for returning the implementation so that it can be used to dynamically add functionality to the class if approved as well."
}
],
function_call: {name: "implementation"},
functions: [
{
name: "implementation",
description: "The implementation of the requested method",
parameters: {
type: "object",
properties: {
code: {
type: :string,
description: "The implementation of the requested method using defmethod from ruby"
},
defmethod: {
type: :string,
description: "a oneliner using defmethod that returns the implementation of the requested method"
}
},
required: ["code"]
}
}
]
}
)
message = response.dig("choices", 0, "message")
if message["role"] == "assistant" && message["function_call"]
function_name = message.dig("function_call", "implementation")
args = JSON.parse(
message.dig("function_call", "arguments"), { symbolize_names: true }
)
return [args[:code], args[:defmethod]]
end
end
end
@MadBomber
Copy link

Your prompt at line 52 in the method_generator.rb file is cut-off. You might try using something like this:

            "content": <<~EOS
              You are an expert ruby programmer.
              Given the method named: #{method_name}, arguments #{args}
              and the contents of the file: #{file_contents} as context.
              What would be your best guess as to the method's implementation?
            EOS

@swerner
Copy link
Author

swerner commented Oct 9, 2023

@MadBomber Oops! Sorry about that, added the rest of the prompt I was using

@MadBomber
Copy link

You are welcome. It is an interesting idea. I copied your code into my experiments repo where, like all good experiments, it suggested some new ideas to explore. The one that works the best so far for me is the ore-emptive prompt which reviews a source code file replacing all # TODO: requirement_text comments with AI assisted implementation code.

https://github.com/MadBomber/experiments/tree/master/openai/method_missing_intercept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment