Created
April 17, 2023 03:00
-
-
Save mberman84/6cb59595b388599c8037b8b06eda7ea4 to your computer and use it in GitHub Desktop.
This file contains 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
import openai | |
import os | |
import subprocess | |
from github import Github, GithubException | |
from dotenv import load_dotenv | |
load_dotenv() | |
def commit_and_push_folder_to_github(github_repo, folder_path): | |
try: | |
# Get the list of files in the remote GitHub repository | |
remote_files = [content.path for content in github_repo.get_contents("")] | |
except GithubException as e: | |
if e.status == 404: | |
# If the repository is empty, set remote_files to an empty list | |
remote_files = [] | |
else: | |
raise e | |
# Get the list of files in the local folder | |
local_files = [os.path.relpath(os.path.join(root, name), folder_path) | |
for root, _, files in os.walk(folder_path) for name in files] | |
# Check if the lists of files match | |
if set(remote_files) == set(local_files): | |
print("The folder has already been pushed to the GitHub repository.") | |
return | |
# Get the repository's SSH URL | |
repo_ssh_url = github_repo.ssh_url | |
# Change the current working directory to the target folder | |
os.chdir(folder_path) | |
# Initialize a Git repository in the folder | |
print("Initializing Git repository...") | |
subprocess.run(["git", "init"]) | |
# Add the remote repository | |
print("Adding remote repository...") | |
subprocess.run(["git", "remote", "add", "origin", repo_ssh_url]) | |
# Add all files in the folder to the Git repository | |
print("Adding all files to the Git repository...") | |
subprocess.run(["git", "add", "."]) | |
# Commit the changes | |
print("Committing changes...") | |
subprocess.run(["git", "commit", "-m", "Initial commit"]) | |
# Push the changes to the remote GitHub repository | |
print("Pushing changes to the remote GitHub repository...") | |
subprocess.run(["git", "push", "-u", "origin", "main"]) | |
print("Successfully pushed the folder to the GitHub repository.") | |
def get_current_code(github_repo): | |
try: | |
# Load the file content from the GitHub repository | |
file_content = github_repo.get_contents("coderGPT.py") | |
current_code = file_content.decoded_content.decode("utf-8") | |
except GithubException as e: | |
if e.status == 404: | |
current_code = "" | |
else: | |
raise e | |
return current_code | |
def not_stopping_condition(iteration): | |
# Set a stopping condition, e.g., a maximum number of iterations | |
max_iterations = 10 | |
return iteration < max_iterations | |
def get_user_suggestions(): | |
# Collect user input for new directions or features | |
user_input = input("Enter suggestions for new directions or features: ") | |
return user_input.split(",") | |
def review_code(openai, code): | |
prompt = f"Please review the following Python code and provide suggestions for improvements:\n\n{code}\n" | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant specialized in code review and improvement suggestions."}, | |
{"role": "user", "content": prompt} | |
], | |
max_tokens=150, | |
temperature=0.5, | |
top_p=1, | |
) | |
suggestions = response.choices[0].message.content.strip() | |
return suggestions | |
def implement_suggestions(openai, current_code, combined_suggestions, model='gpt-3.5-turbo'): | |
suggestions_text = '\n'.join(combined_suggestions) | |
prompt = f"Given the following Python code:\n\n{current_code}\n\nPlease implement these suggestions:\n{suggestions_text}\n\nImproved code:" | |
response = openai.ChatCompletion.create( | |
engine=model, | |
prompt=prompt, | |
max_tokens=400, | |
n=1, | |
stop=None, | |
temperature=0.5, | |
) | |
improved_code = response.choices[0].text.strip() | |
return improved_code | |
def get_approved_code(current_code, improved_code): | |
# Show the user the current and improved code for approval | |
print("Current code:\n", current_code) | |
print("\nImproved code:\n", improved_code) | |
approval = input("\nDo you approve the improved code? (yes/no): ") | |
return improved_code if approval.lower() == "yes" else current_code | |
def commit_code(github_repo, approved_code, iteration): | |
# Create a commit message with the current iteration | |
commit_message = f"Commit for iteration {iteration}" | |
# Get the current SHA of the main branch | |
main_branch = github_repo.get_branch("main") | |
main_sha = main_branch.commit.sha | |
# Get the contents of the current file | |
file = github_repo.get_contents("path/to/your/file.py", ref="main") | |
# Update the contents of the file with the approved code | |
github_repo.update_file( | |
path=file.path, | |
message=commit_message, | |
content=approved_code, | |
sha=file.sha, | |
branch="main" | |
) | |
def main(): | |
# Initialize ChatGPT 4 agents (using the same API key for both) | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
# Initialize GitHub instance | |
github_access_token = os.environ["GITHUB_ACCESS_TOKEN"] | |
github_instance = Github(github_access_token) | |
# Get the repository or create a new one if it doesn't exist | |
repo_name = os.environ.get("REPO_NAME") | |
if not repo_name: | |
repo_name = input("Enter a repository name: ") | |
user = github_instance.get_user() | |
try: | |
github_repo = user.get_repo(repo_name) | |
print(f"Repository found: {repo_name}") | |
except GithubException: | |
github_repo = user.create_repo(repo_name, description="Generated by ChatGPT", private=True) | |
print(f"Repository not found. Created a new repository: {repo_name}") | |
# Get the path of the current script's directory | |
script_directory = os.path.dirname(os.path.realpath(__file__)) | |
# Check if the repository is empty | |
try: | |
github_repo.get_contents("") | |
print("Files already exist in the repository.") | |
except GithubException as e: | |
if e.status == 404: | |
# Commit and push the entire folder to the GitHub repository | |
print("Committing and pushing files to the repository...") | |
commit_and_push_folder_to_github(github_repo, script_directory) | |
current_code = get_current_code(github_repo) | |
# Start the iterative process | |
iteration = 0 | |
while not_stopping_condition(iteration): | |
print(f"\nIteration {iteration}") | |
# Get user suggestions for new directions or features | |
print("Getting user suggestions...") | |
user_suggestions = get_user_suggestions() | |
# Agent 2 suggests improvements to the current code | |
print("Reviewing code with ChatGPT...") | |
agent2_suggestions = review_code(openai, current_code) | |
# Combine user suggestions and Agent 2 suggestions | |
combined_suggestions = user_suggestions + agent2_suggestions | |
# Agent 1 generates an improved version of the code based on combined suggestions | |
print("Implementing suggestions with ChatGPT...") | |
improved_code = implement_suggestions(openai, current_code, combined_suggestions) | |
# Compare the current and improved code and present to the user for approval | |
approved_code = get_approved_code(current_code, improved_code) | |
# Commit the approved code to GitHub | |
print("Committing approved code to GitHub...") | |
commit_code(github_repo, approved_code, iteration) | |
# Update the current code and iteration count | |
current_code = approved_code | |
iteration += 1 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment