Last active
August 30, 2023 16:21
-
-
Save silva96/28631691d72bf3398f71344736fb6afc to your computer and use it in GitHub Desktop.
Little script where OpenAI creates code for you.
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 'fileutils' | |
class AICoder | |
PROMPT = <<-PROMPT.freeze | |
As a Software developer, given a the task, generate the corresponding code. | |
NEVER USE MARKDOWN, ALWAYS USE PLAIN TEXT. | |
Always respond only with code, never add other words. | |
For each file in the response, add the following structure | |
filename:path/to/the_filename_with_extension.ext | |
NEVER FORGET TO USE THAT STRUCTURE. ALWAYS INCLUDE THE KEYWORD filename: | |
Always separate classes and modules in different files | |
NEVER USE MARKDOWN, ALWAYS USE PLAIN TEXT | |
PROMPT | |
OPENAI_ACCESS_TOKEN = 'sk-your-key' | |
def initialize(task_description) | |
@task_description = task_description | |
end | |
def get_response | |
response = client.chat( | |
parameters: { | |
model: 'gpt-4', | |
messages: [ | |
{ role: 'system', content: PROMPT }, | |
{ role: 'user', content: "task: #{@task_description}" } | |
], | |
} | |
) | |
response.dig('choices', 0, 'message', 'content') | |
end | |
def create_code(response) | |
file_hash = parse_response(response) | |
file_hash.each do |file_name, content| | |
save_to_file(file_name.strip, content.gsub(/```.*/, '')) | |
end | |
end | |
private | |
def client | |
@client ||= OpenAI::Client.new(access_token: OPENAI_ACCESS_TOKEN) | |
end | |
def parse_response(response) | |
files = response.split("filename:") | |
files.shift | |
file_hash = {} | |
files.each do |file| | |
lines = file.lines.map { |line| line.gsub("\n", "") } | |
filename = lines.shift | |
file_content = lines.join("\n") | |
file_hash[filename] = file_content | |
end | |
file_hash | |
end | |
def save_to_file(file_name, content) | |
directory = File.dirname(file_name) | |
FileUtils.mkdir_p(directory) unless Dir.exist?(directory) | |
File.open(file_name, 'w') do |f| | |
f.write(content) | |
end | |
puts "Code saved to #{file_name}" | |
end | |
end | |
# Usage: | |
# 1) In your terminal: run "bundle init && bundle add ruby-openai" | |
# 2) In this file: Add your OPEN_AI_KEY to the constant at the top. | |
# 3) To run the code, type in your terminal: | |
# ruby ai_coder.rb "Do something with this and that detailed instructions" | |
# | |
# Example: | |
# ruby ai_coder.rb "Create a TicTacToe game in html, css and js. Show a message when the game is over. Save it in a new folder called tictactoe" | |
if __FILE__ == $0 | |
task_description = ARGV[0] | |
coder = AICoder.new(task_description) | |
response = coder.get_response | |
coder.create_code(response) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment