Skip to content

Commit

Permalink
Merge pull request #307 from Cloud-Code-AI/305-bug-merging-summary-do…
Browse files Browse the repository at this point in the history
…esnt-work-as-expected

fix: updated merge description logic
  • Loading branch information
sauravpanda authored Jul 8, 2024
2 parents f346218 + 4ef5965 commit b973bbc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/code_review/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
user="kaizen/example",
)

pr_desc = PRDescriptionGenerator(llm_provider=LLMProvider())
topics = clean_keys(review_data.topics, "important")
review_desc = create_pr_review_text(topics)
comments, topics = create_review_comments(topics)
Expand All @@ -38,8 +37,9 @@
print(f"\nComment and topics: \n {comments}, \n{topics}")
print(review_data)
print("---------------Generate desc-------------")
pr_desc = PRDescriptionGenerator(llm_provider=LLMProvider())
desc_data = pr_desc.generate_pull_request_desc(
diff_text=diff_text,
diff_text=None,
pull_request_title=pr_title,
pull_request_desc="",
pull_request_files=pr_files,
Expand Down
89 changes: 74 additions & 15 deletions kaizen/generator/pr_description.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List, Dict
from typing import Optional, List, Dict, Generator
import logging
from dataclasses import dataclass
import json
Expand Down Expand Up @@ -50,7 +50,7 @@ def generate_pull_request_desc(
if not diff_text and not pull_request_files:
raise Exception("Both diff_text and pull_request_files are empty!")

if self.provider.is_inside_token_limit(PROMPT=prompt):
if diff_text and self.provider.is_inside_token_limit(PROMPT=prompt):
desc = self._process_full_diff(prompt, user, reeval_response)
else:
desc = self._process_files(
Expand Down Expand Up @@ -94,9 +94,40 @@ def _process_files(
pull_request_desc: str,
user: Optional[str],
reeval_response: bool,
) -> str:
) -> List[Dict]:
self.logger.debug("Processing based on files")
descs = []
file_descs = []
for file_review in self._process_files_generator(
pull_request_files,
pull_request_title,
pull_request_desc,
user,
reeval_response,
):
file_descs.extend(file_descs)

prompt = MERGE_PR_DESCRIPTION_PROMPT.format(DESCS=json.dumps(file_descs))
resp, usage = self.provider.chat_completion_with_json(prompt, user=user)
self.total_usage = self.provider.update_usage(self.total_usage, usage)

return resp["desc"]

def _process_files_generator(
self,
pull_request_files: List[Dict],
pull_request_title: str,
pull_request_desc: str,
user: Optional[str],
reeval_response: bool,
) -> Generator[List[Dict], None, None]:
combined_diff_data = ""
available_tokens = self.provider.available_tokens(
PR_FILE_DESCRIPTION_PROMPT.format(
PULL_REQUEST_TITLE=pull_request_title,
PULL_REQUEST_DESC=pull_request_desc,
CODE_DIFF="",
)
)

for file in pull_request_files:
patch_details = file.get("patch")
Expand All @@ -106,26 +137,54 @@ def _process_files(
filename.split(".")[-1] not in parser.EXCLUDED_FILETYPES
and patch_details is not None
):
prompt = PR_FILE_DESCRIPTION_PROMPT.format(
PULL_REQUEST_TITLE=pull_request_title,
PULL_REQUEST_DESC=pull_request_desc,
CODE_DIFF=patch_details,
temp_prompt = (
combined_diff_data
+ f"\n---->\nFile Name: {filename}\nPatch Details: \n{patch_details}"
)

if not self.provider.is_inside_token_limit(PROMPT=prompt):
if available_tokens - self.provider.get_token_count(temp_prompt) > 0:
combined_diff_data = temp_prompt
continue

resp, usage = self.provider.chat_completion_with_json(prompt, user=user)
self.total_usage = self.provider.update_usage(self.total_usage, usage)
yield self._process_file_chunk(
combined_diff_data,
pull_request_title,
pull_request_desc,
user,
reeval_response,
)
combined_diff_data = (
f"\n---->\nFile Name: {filename}\nPatch Details: {patch_details}"
)

if reeval_response:
resp = self._reevaluate_response(prompt, resp, user)
descs.append(resp["desc"])
if combined_diff_data:
yield self._process_file_chunk(
combined_diff_data,
pull_request_title,
pull_request_desc,
user,
reeval_response,
)

prompt = MERGE_PR_DESCRIPTION_PROMPT.format(DESCS=json.dumps(descs))
def _process_file_chunk(
self,
diff_data: str,
pull_request_title: str,
pull_request_desc: str,
user: Optional[str],
reeval_response: bool,
) -> List[Dict]:
prompt = PR_FILE_DESCRIPTION_PROMPT.format(
PULL_REQUEST_TITLE=pull_request_title,
PULL_REQUEST_DESC=pull_request_desc,
CODE_DIFF=diff_data,
)
resp, usage = self.provider.chat_completion_with_json(prompt, user=user)
self.total_usage = self.provider.update_usage(self.total_usage, usage)

if reeval_response:
resp = self._reevaluate_response(prompt, resp, user)

return resp["desc"]

def _reevaluate_response(self, prompt: str, resp: str, user: Optional[str]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion kaizen/reviewer/code_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def review_pull_request(
if not diff_text and not pull_request_files:
raise Exception("Both diff_text and pull_request_files are empty!")

if self.provider.is_inside_token_limit(PROMPT=prompt):
if diff_text and self.provider.is_inside_token_limit(PROMPT=prompt):
reviews = self._process_full_diff(prompt, user, reeval_response)
else:
reviews = self._process_files(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "kaizen-cloudcode"
version = "0.3.5"
version = "0.3.6"
description = "An intelligent coding companion that accelerates your development workflow by providing efficient assistance, enabling you to craft high-quality code more rapidly."
authors = ["Saurav Panda <saurav.panda@cloudcode.ai>"]
license = "Apache2.0"
Expand Down

0 comments on commit b973bbc

Please sign in to comment.