Skip to content

Commit

Permalink
GPT-commit: Update requirements and openai_service for WYX-CLI feature.
Browse files Browse the repository at this point in the history
Modified `requirements.txt` to update project dependencies. Modified `openai_service.py` in the `services` module to add functionality for integrating OpenAI's ChatGPT API for smart commit messages in WYX-CLI.
  • Loading branch information
hwixley committed Apr 7, 2024
1 parent 4d8265b commit e804915
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
numpy==1.24.2
openai==0.27.4
numpy==1.26.4
openai==1.9.0
opencv_python==4.7.0.72
termcolor==1.1.0
termcolor==2.1.0
27 changes: 14 additions & 13 deletions src/commands/scripts/services/openai_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os, sys
import openai
from openai import OpenAI

# client = OpenAI(api_key=self.API_KEY)
from logger import error, info
from termcolor import colored

def read_file(file_path):
with open(file_path, "r") as f:
return f.read()

class OpenAIService:

FILE_PATH = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -35,36 +37,35 @@ def __init__(self):
error("No API key found.")
sys.exit()

openai.api_key = self.API_KEY
self.client = OpenAI(api_key=self.API_KEY)


def format_message(self, role, message):
return { "role": role, "content": message }

def get_response(self, prompt, chat_history: list = []):
history = chat_history + [self.format_message("user", prompt[:self.MAX_TOKENS])]
completion = openai.ChatCompletion.create(
model=self.ENGINE,
messages=history
)
completion = self.client.chat.completions.create(model=self.ENGINE,
messages=history)
response = completion.choices[0].message.content
return response

def get_git_diff(self):
return f"`git diff` output: {os.popen('git diff').read()}, and `git status` output: {os.popen('git status').read()}."

def get_commit_title(self):
chat_history = [self.ASSISTANT_MESSAGE_DEV]
title_prompt = f"You are in a team of developers working on a project. Write a 1 line commit message less than or equal to 50 characters technically describing the following bash git outputs. {self.get_git_diff()} Do not mention anything about the branch these changes were made on. Mention specifically which functions, classes or variables were modified/created/deleted and why."
title_response = self.get_response(title_prompt, chat_history)
return f"GPT-commit: {title_response}"

def get_commit_description(self):
chat_history = [self.ASSISTANT_MESSAGE_DEV]
title = self.get_commit_title()
description_prompt = f"You are in a team of developers working on a project. Write a 2 line commit message technically describing the following bash git outputs. {self.get_git_diff()} Do not repeat the title \"{title}\", and do not mention anything about the branch these changes were made on. Mention specifically which functions, classes or variables were modified/created/deleted and why."
description_response = self.get_response(description_prompt, chat_history)
return (title, description_response)

def get_smart_commit(self):
title, description = self.get_commit_description()
return f"{title}\n{description}"
Expand All @@ -84,15 +85,15 @@ def conversate(self):
elif latest_question == "save" and len(chat_history) > 1:
self.save_chat_history(chat_history[1:])
print(colored("\n\nSaving conversation...\n","yellow"))

else:
question_response = self.get_response(latest_question, chat_history)

chat_history.append(self.format_message("user", latest_question))
chat_history.append(self.format_message("assistant", question_response))

print(colored("\n🤖:", "blue") + f" {question_response}")

print(colored("\n" + self.SEPARATOR, "blue"))

def save_chat_history(self, chat_history: list):
Expand Down

0 comments on commit e804915

Please sign in to comment.