-
-
Save swerner/4c2e6c6e02b21dabe7d741e7becb4cb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "./method_generator.rb" | |
class Calculator | |
include MethodGenerator | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "./calculator.rb" | |
require "pry" | |
calc = Calculator.new | |
# Try calc.add(2,3) | |
binding.pry | |
calc.add(2,3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Oops! Sorry about that, added the rest of the prompt I was using
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
Your prompt at line 52 in the method_generator.rb file is cut-off. You might try using something like this: