Skip to content

Commit

Permalink
self review diffs v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitamalinov committed Apr 26, 2024
1 parent edadfa3 commit 036b0b8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
61 changes: 54 additions & 7 deletions services/openai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
from openai.types.beta.threads import Run, ThreadMessage, MessageContentText
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput


# Local imports
from config import OPENAI_FINAL_STATUSES, OPENAI_MODEL_ID, TIMEOUT_IN_SECONDS
from services.openai.functions import (
GET_REMOTE_FILE_CONTENT,
functions,
COMMIT_MULTIPLE_CHANGES_TO_REMOTE_BRANCH,
WHY_MODIFYING_DIFFS,
)
from services.openai.init import create_openai_client
from services.openai.instructions import (
SYSTEM_INSTRUCTION_FOR_AGENT,
SYSTEM_INSTRUCTION_FOR_AGENT_REVIEW_DIFFS,
)
from utils.file_manager import clean_specific_lines, correct_hunk_headers, split_diffs
from services.github.github_manager import update_comment
from services.github.github_manager import (
update_comment,
commit_changes_to_remote_branch,
)

from utils.file_manager import (
clean_specific_lines,
correct_hunk_headers,
split_diffs,
extract_file_name,
)


def create_assistant() -> tuple[Assistant, str]:
Expand All @@ -49,10 +58,6 @@ def create_assistant() -> tuple[Assistant, str]:
# {"type": "code_interpreter"},
# {"type": "retrieval"},
{"type": "function", "function": GET_REMOTE_FILE_CONTENT},
{
"type": "function",
"function": COMMIT_MULTIPLE_CHANGES_TO_REMOTE_BRANCH,
},
{"type": "function", "function": WHY_MODIFYING_DIFFS},
],
model=OPENAI_MODEL_ID,
Expand Down Expand Up @@ -168,6 +173,48 @@ def run_assistant(
self_review_run, self_review_input_output_data = wait_on_run(
run=self_review_run, thread=thread, token=token, run_name="review diffs"
)

# Get the response
messages: SyncCursorPage[ThreadMessage] = get_response(thread=thread)
messages_list = list(messages)
if not messages_list:
raise ValueError("No messages in the list.")
latest_message: ThreadMessage = messages_list[0]
if isinstance(latest_message.content[0], MessageContentText):
value: str = latest_message.content[0].text.value
output_data += json.dumps(latest_message.content[0].text.value)
else:
raise ValueError("Last message content is not text.")
print(f"Last message: {value}\n")

# Clean the diff text and split it
diff: str = clean_specific_lines(text=value)
text_diffs: list[str] = split_diffs(diff_text=diff)
output: list[str] = []
for diff in text_diffs:
diff = correct_hunk_headers(diff_text=diff)
print(f"Diff: {repr(diff)}\n")
output.append(diff)

# Commit the changes to the new remote branch
for diff in output:
file_path: str = extract_file_name(diff_text=diff)
print(
f"{time.strftime('%H:%M:%S', time.localtime())} File path: {file_path}.\n"
)
commit_changes_to_remote_branch(
branch=new_branch,
commit_message=f"Update {file_path}",
diff_text=diff,
file_path=file_path,
owner=owner,
repo=repo,
token=token,
)
print(
f"{time.strftime('%H:%M:%S', time.localtime())} Changes committed to {new_branch}.\n"
)

input_data += self_review_input_output_data
output_data = self_review_input_output_data

Expand Down
41 changes: 1 addition & 40 deletions services/openai/functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# flake8: noqa
# Standard imports
from typing import Any, Union
from typing import Any

# Third-party imports
from openai.types import shared_params
Expand Down Expand Up @@ -44,23 +44,6 @@
}


DIFFS: dict[str, Union[str, dict[str, str]]] = {
"type": "array",
"description": "The diffs generated by assistant api to be patched to a remote github branch.",
"items": {"type": "string"},
}

NEW_BRANCH: dict[str, str] = {
"type": "string",
"description": "Name of the new branch to be created.",
}

TOKEN: dict[str, str] = {
"type": "string",
"description": "The github token used to authenticate requests to github",
}


# Assistant API sometime adds a token as an input which is the reason for catchall '*args and *kwargs'
def why_modifying_diffs(why: str, *args: str, **kwargs: str) -> None:
"""Assistant API function calling to explain why it's going to modify the diffs before actually modifying the diffs."""
Expand All @@ -72,27 +55,6 @@ def why_modifying_diffs(why: str, *args: str, **kwargs: str) -> None:
"description": "Reason for modifying this diff",
}

COMMIT_MULTIPLE_CHANGES_TO_REMOTE_BRANCH: shared_params.FunctionDefinition = {
"name": "commit_multiple_changes_to_remote_branch",
"description": "Commits diffs to GitHub remote branch. Inputs are the array diffs, new_branch, owner, repo, and token",
"parameters": {
"type": "object",
"properties": {
"diffs": DIFFS,
"new_branch": NEW_BRANCH,
"owner": OWNER,
"repo": REPO,
"token": TOKEN,
},
"required": [
"diffs",
"new_branch",
"owner",
"repo",
"token",
],
},
}
WHY_MODIFYING_DIFFS: shared_params.FunctionDefinition = {
"name": "why_modifying_diffs",
"description": "Explain why are you modifying the diffs before you actually modify the diffs. Only argument is 'why' which is a string.",
Expand All @@ -106,6 +68,5 @@ def why_modifying_diffs(why: str, *args: str, **kwargs: str) -> None:
# Define functions
functions: dict[str, Any] = {
"get_remote_file_content": get_remote_file_content,
"commit_multiple_changes_to_remote_branch": commit_multiple_changes_to_remote_branch,
"why_modifying_diffs": why_modifying_diffs,
}
4 changes: 2 additions & 2 deletions services/openai/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
SYSTEM_INSTRUCTION_FOR_AGENT_REVIEW_DIFFS = """
Please review the diffs you created from your previous response.
Ensure that you have followed the steps and instructions outlined in pr_body that was pass in the first message of this thread.
If everything is correct, please commit the changes.
If there are any issues, please use why_modifying_diffs function to explain why you're modifying it, then fix the diff. Lastly, commit the changes using commit_multiple_changes_to_remote_branch function.
If everything is correct, return all the diffs in the unified diff format.
If there are any issues, please use why_modifying_diffs function to explain why you're modifying it. Then return all the diffs in the unified diff format with the new changes.
"""

SYSTEM_INSTRUCTION_FOR_WRITING_PR = '''
Expand Down

0 comments on commit 036b0b8

Please sign in to comment.