Skip to content

Commit

Permalink
[ref] Refactor diff size handling based on model and mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hari0205 committed Jul 7, 2024
1 parent 15c8ed0 commit a7c690d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
61 changes: 56 additions & 5 deletions porunga/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,57 @@ def suggest(file_path, num_messages):
click.echo("No difference detected. Start making changes to files")
return

# If diff is too large for GPT 4 send 1st chunk only (saving api costs)
if len(diff_output) > 16000:
diff_output = diff_output[:16000]
# Get the mode
mode = keyring.get_password(SERVICEID, "PORUNGA_MODE") or "precise"
model = keyring.get_password(SERVICEID, "PORUNGA_MODEL_NAME") or "gpt-4o"

# If diff is too large (saving api costs)
if model.startswith("gpt-4"):
match (mode):
# If you care about cost
case "cost":
if len(diff_output) > 16000:
diff_output = diff_output[:16000]

# If you care about precision
case "precise":
if len(diff_output) > 60000:
diff_output = diff_output[:60000]

case _:
if len(diff_output) > 30000:
diff_output = diff_output[:30000]

elif model.startswith("gpt-3"):
match (mode):
# If you care about cost
case "cost":
if len(diff_output) > 3000:
diff_output = diff_output[:3000]

# If you care about precision
case "precise":
if len(diff_output) > 7000:
diff_output = diff_output[:7000]

case _:
if len(diff_output) > 5000:
diff_output = diff_output[:5000]

# Generate initial commit message suggestions
messages = suggest_commit_message(diff_output, num_messages)
if isinstance(messages, (Exception)):
click.secho(
"An error occurred when trying to generate suggestions", fg="red"
f"An error occurred when trying to generate suggestions {messages}",
err=True,
fg="red",
)
return
messages = parse_messages(messages=messages)
if isinstance(messages, (Exception)):
click.secho("An error occurred when trying to parse suggestions", fg="red")
click.secho(
"An error occurred when trying to parse suggestions", err=True, fg="red"
)
return

while True:
Expand Down Expand Up @@ -139,7 +176,21 @@ def suggest(file_path, num_messages):

if selected_message == "Regenerate suggestions":
messages = suggest_commit_message(diff_output, num_messages)
if isinstance(messages, (Exception)):
click.secho(
f"An error occurred when trying to generate suggestions {messages}",
err=True,
fg="red",
)
return
messages = parse_messages(messages)
if isinstance(messages, (Exception)):
click.secho(
"An error occurred when trying to parse suggestions",
err=True,
fg="red",
)
return
continue

# Prompt user to edit the selected message
Expand Down
11 changes: 6 additions & 5 deletions porunga/llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import keyring
import os
import openai
from langchain.prompts import PromptTemplate
from langchain.output_parsers import XMLOutputParser
from langchain_core.exceptions import OutputParserException
Expand Down Expand Up @@ -53,8 +54,9 @@ def suggest_commit_message(diff, x):

llm = ChatOpenAI(
temperature=0,
model_name=keyring.get_password(SERVICEID, "MODEL_NAME")
model_name=keyring.get_password(SERVICEID, "PORUNGA_MODEL_NAME")
or os.environ.get("MODEL_NAME")
or keyring.get_password(SERVICEID, "PORUNGA_MODE")
or "gpt-4o",
api_key=keyring.get_password(SERVICEID, "OPENAI_API_KEY"),
timeout=1500,
Expand All @@ -77,12 +79,11 @@ def suggest_commit_message(diff, x):
# Method 3 (Recommended)
chain = few_shot_prompt | llm | XMLOutputParser()
op = chain.invoke({"diff": diff, "x": x})
except OutputParserException as e:
except OutputParserException:
# Custom Error class
print(e)
return ParseError()
except Exception as e:
print(e)
return Exception
print(e.args[0])
return Exception(e.args[0])
else:
return op
4 changes: 3 additions & 1 deletion porunga/utils/exceptions/parse_error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ParseError(Exception):
def __init__(self, message="An unexpected error occurred while parsing the output"):
def __init__(
self, message="An unexpected error occurred while parsing the output.Try Again"
):
self.message = message
super().__init__(self.message)

0 comments on commit a7c690d

Please sign in to comment.