gpt4free/etc/tool/copilot.py

251 lines
7.1 KiB
Python
Raw Normal View History

2024-01-20 17:16:18 +00:00
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent.parent))
import g4f
import json
import os
import re
import requests
from typing import Union
from github import Github
from github.PullRequest import PullRequest
g4f.debug.logging = True
g4f.debug.version_check = False
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
2024-01-21 16:26:26 +00:00
GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY')
2024-01-20 17:36:04 +00:00
G4F_PROVIDER = os.getenv('G4F_PROVIDER')
2024-01-20 17:16:18 +00:00
G4F_MODEL = os.getenv('G4F_MODEL') or g4f.models.gpt_4
def get_pr_details(github: Github) -> PullRequest:
"""
2024-01-21 14:07:29 +00:00
Retrieves the details of the pull request from GitHub.
Args:
github (Github): The Github object to interact with the GitHub API.
2024-01-20 17:16:18 +00:00
Returns:
2024-01-21 14:07:29 +00:00
PullRequest: An object representing the pull request.
2024-01-20 17:16:18 +00:00
"""
2024-01-21 16:26:26 +00:00
with open('./pr_number', 'r') as file:
2024-01-21 19:04:43 +00:00
pr_number = file.read().strip()
2024-01-21 16:38:26 +00:00
if not pr_number:
return
2024-01-20 17:16:18 +00:00
2024-01-21 16:26:26 +00:00
repo = github.get_repo(GITHUB_REPOSITORY)
2024-01-21 19:04:43 +00:00
pull = repo.get_pull(int(pr_number))
2024-01-20 17:16:18 +00:00
return pull
def get_diff(diff_url: str) -> str:
"""
2024-01-21 14:07:29 +00:00
Fetches the diff of the pull request from a given URL.
2024-01-20 17:16:18 +00:00
Args:
2024-01-21 14:07:29 +00:00
diff_url (str): URL to the pull request diff.
2024-01-20 17:16:18 +00:00
Returns:
2024-01-21 14:07:29 +00:00
str: The diff of the pull request.
2024-01-20 17:16:18 +00:00
"""
response = requests.get(diff_url)
response.raise_for_status()
return response.text
def read_json(text: str) -> dict:
2024-01-21 14:07:29 +00:00
"""
Parses JSON code block from a string.
Args:
text (str): A string containing a JSON code block.
Returns:
dict: A dictionary parsed from the JSON code block.
"""
2024-01-20 17:16:18 +00:00
match = re.search(r"```(json|)\n(?P<code>[\S\s]+?)\n```", text)
if match:
text = match.group("code")
try:
return json.loads(text.strip())
except json.JSONDecodeError:
print("No valid json:", text)
2024-01-21 14:07:29 +00:00
return {}
2024-01-20 17:16:18 +00:00
2024-01-21 14:07:29 +00:00
def read_text(text: str) -> str:
"""
Extracts text from a markdown code block.
Args:
text (str): A string containing a markdown code block.
Returns:
str: The extracted text.
"""
2024-01-20 17:16:18 +00:00
match = re.search(r"```(markdown|)\n(?P<text>[\S\s]+?)\n```", text)
if match:
return match.group("text")
return text
2024-01-21 14:07:29 +00:00
def get_ai_response(prompt: str, as_json: bool = True) -> Union[dict, str]:
2024-01-20 17:16:18 +00:00
"""
Gets a response from g4f API based on the prompt.
Args:
prompt (str): The prompt to send to g4f.
2024-01-21 14:07:29 +00:00
as_json (bool): Whether to parse the response as JSON.
2024-01-20 17:16:18 +00:00
Returns:
2024-01-21 14:07:29 +00:00
Union[dict, str]: The parsed response from g4f, either as a dictionary or a string.
2024-01-20 17:16:18 +00:00
"""
response = g4f.ChatCompletion.create(
G4F_MODEL,
[{'role': 'user', 'content': prompt}],
G4F_PROVIDER,
ignore_stream_and_auth=True
)
2024-01-21 14:07:29 +00:00
return read_json(response) if as_json else read_text(response)
2024-01-20 17:16:18 +00:00
2024-01-21 14:07:29 +00:00
def analyze_code(pull: PullRequest, diff: str)-> list[dict]:
2024-01-20 17:16:18 +00:00
"""
Analyzes the code changes in the pull request.
Args:
2024-01-21 14:07:29 +00:00
pull (PullRequest): The pull request object.
2024-01-20 17:16:18 +00:00
diff (str): The diff of the pull request.
Returns:
2024-01-21 14:07:29 +00:00
list[dict]: A list of comments generated by the analysis.
2024-01-20 17:16:18 +00:00
"""
comments = []
changed_lines = []
current_file_path = None
offset_line = 0
for line in diff.split('\n'):
if line.startswith('+++ b/'):
current_file_path = line[6:]
2024-01-23 22:48:11 +00:00
changed_lines = []
2024-01-20 17:16:18 +00:00
elif line.startswith('@@'):
match = re.search(r'\+([0-9]+?),', line)
if match:
offset_line = int(match.group(1))
elif current_file_path:
2024-01-21 18:35:10 +00:00
if (line.startswith('\\') or line.startswith('diff')) and changed_lines:
prompt = create_analyze_prompt(changed_lines, pull, current_file_path)
2024-01-20 17:16:18 +00:00
response = get_ai_response(prompt)
for review in response.get('reviews', []):
review['path'] = current_file_path
comments.append(review)
current_file_path = None
2024-01-23 22:48:11 +00:00
elif line.startswith('-'):
changed_lines.append(line)
else:
2024-01-20 17:54:32 +00:00
changed_lines.append(f"{offset_line}:{line}")
offset_line += 1
2024-01-20 17:16:18 +00:00
return comments
2024-01-21 18:35:10 +00:00
def create_analyze_prompt(changed_lines: list[str], pull: PullRequest, file_path: str):
2024-01-20 17:16:18 +00:00
"""
Creates a prompt for the g4f model.
Args:
2024-01-21 14:07:29 +00:00
changed_lines (list[str]): The lines of code that have changed.
pull (PullRequest): The pull request object.
file_path (str): The path to the file being reviewed.
2024-01-20 17:16:18 +00:00
Returns:
str: The generated prompt.
"""
2024-01-20 17:54:32 +00:00
code = "\n".join(changed_lines)
2024-01-20 17:16:18 +00:00
example = '{"reviews": [{"line": <line_number>, "body": "<review comment>"}]}'
return f"""Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: {example}
- Do not give positive comments or compliments.
- Provide comments and suggestions ONLY if there is something to improve, otherwise "reviews" should be an empty array.
- Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code.
- IMPORTANT: NEVER suggest adding comments to the code.
Review the following code diff in the file "{file_path}" and take the pull request title and description into account when writing the response.
Pull request title: {pull.title}
Pull request description:
---
{pull.body}
---
Each line is prefixed by its number. Code to review:
```
{code}
```
"""
def create_review_prompt(pull: PullRequest, diff: str):
"""
2024-01-21 14:07:29 +00:00
Creates a prompt to create a review comment.
2024-01-20 17:16:18 +00:00
Args:
2024-01-21 14:07:29 +00:00
pull (PullRequest): The pull request object.
diff (str): The diff of the pull request.
2024-01-20 17:16:18 +00:00
Returns:
2024-01-21 14:07:29 +00:00
str: The generated prompt for review.
2024-01-20 17:16:18 +00:00
"""
return f"""Your task is to review a pull request. Instructions:
2024-01-21 18:35:10 +00:00
- Write in name of g4f copilot. Don't use placeholder.
2024-01-20 17:16:18 +00:00
- Write the review in GitHub Markdown format.
- Thank the author for contributing to the project.
Pull request author: {pull.user.name}
Pull request title: {pull.title}
Pull request description:
---
{pull.body}
---
Diff:
```diff
{diff}
```
"""
def main():
try:
github = Github(GITHUB_TOKEN)
pull = get_pr_details(github)
2024-01-21 16:38:26 +00:00
if not pull:
print(f"No PR number found")
exit()
2024-01-23 04:03:26 +00:00
if pull.get_reviews().totalCount > 0 or pull.get_issue_comments().totalCount > 0:
2024-01-21 18:35:10 +00:00
print(f"Has already a review")
exit()
2024-01-20 17:16:18 +00:00
diff = get_diff(pull.diff_url)
except Exception as e:
2024-01-21 14:07:29 +00:00
print(f"Error get details: {e.__class__.__name__}: {e}")
2024-01-20 17:16:18 +00:00
exit(1)
try:
review = get_ai_response(create_review_prompt(pull, diff), False)
except Exception as e:
print(f"Error create review: {e}")
exit(1)
try:
comments = analyze_code(pull, diff)
except Exception as e:
print(f"Error analyze: {e}")
exit(1)
print("Comments:", comments)
try:
2024-01-21 18:35:10 +00:00
if comments:
2024-01-21 14:07:29 +00:00
pull.create_review(body=review, comments=comments)
else:
2024-01-23 01:13:36 +00:00
pull.create_issue_comment(body=review)
2024-01-20 17:16:18 +00:00
except Exception as e:
print(f"Error posting review: {e}")
exit(1)
if __name__ == "__main__":
main()