XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Improve doctypes in copilot

fd0507a4
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +49 -24
Modified etc/tool/copilot.py +49 -24
@@ -21,10 +21,13 @@ G4F_MODEL = os.getenv('G4F_MODEL') or g4f.models.gpt_4
21 21
22 22 def get_pr_details(github: Github) -> PullRequest:
23 23 """
24 Rteurns the details of the pull request from GitHub.
24 Retrieves the details of the pull request from GitHub.
25
26 Args:
27 github (Github): The Github object to interact with the GitHub API.
25 28
26 29 Returns:
27 PullRequest: A PullRequest instance.
30 PullRequest: An object representing the pull request.
28 31 """
29 32 with open(os.getenv('GITHUB_EVENT_PATH', ''), 'r') as file:
30 33 data = json.load(file)
@@ -36,19 +39,28 @@ def get_pr_details(github: Github) -> PullRequest:
36 39
37 40 def get_diff(diff_url: str) -> str:
38 41 """
39 Fetches the diff of the pull request.
42 Fetches the diff of the pull request from a given URL.
40 43
41 44 Args:
42 pull (PullRequest): Pull request.
45 diff_url (str): URL to the pull request diff.
43 46
44 47 Returns:
45 str or None: The diff of the pull request or None if not available.
48 str: The diff of the pull request.
46 49 """
47 50 response = requests.get(diff_url)
48 51 response.raise_for_status()
49 52 return response.text
50 53
51 54 def read_json(text: str) -> dict:
55 """
56 Parses JSON code block from a string.
57
58 Args:
59 text (str): A string containing a JSON code block.
60
61 Returns:
62 dict: A dictionary parsed from the JSON code block.
63 """
52 64 match = re.search(r"```(json|)\n(?P<code>[\S\s]+?)\n```", text)
53 65 if match:
54 66 text = match.group("code")
@@ -56,23 +68,33 @@ def read_json(text: str) -> dict:
56 68 return json.loads(text.strip())
57 69 except json.JSONDecodeError:
58 70 print("No valid json:", text)
59 return {}
71 return {}
60 72
61 def read_text(text: str) -> dict:
73 def read_text(text: str) -> str:
74 """
75 Extracts text from a markdown code block.
76
77 Args:
78 text (str): A string containing a markdown code block.
79
80 Returns:
81 str: The extracted text.
82 """
62 83 match = re.search(r"```(markdown|)\n(?P<text>[\S\s]+?)\n```", text)
63 84 if match:
64 85 return match.group("text")
65 86 return text
66 87
67 def get_ai_response(prompt, as_json: bool = True) -> Union[dict, str]:
88 def get_ai_response(prompt: str, as_json: bool = True) -> Union[dict, str]:
68 89 """
69 90 Gets a response from g4f API based on the prompt.
70 91
71 92 Args:
72 93 prompt (str): The prompt to send to g4f.
94 as_json (bool): Whether to parse the response as JSON.
73 95
74 96 Returns:
75 dict: The parsed response from g4f.
97 Union[dict, str]: The parsed response from g4f, either as a dictionary or a string.
76 98 """
77 99 response = g4f.ChatCompletion.create(
78 100 G4F_MODEL,
@@ -80,20 +102,18 @@ def get_ai_response(prompt, as_json: bool = True) -> Union[dict, str]:
80 102 G4F_PROVIDER,
81 103 ignore_stream_and_auth=True
82 104 )
83 if as_json:
84 return read_json(response)
85 return read_text(response)
105 return read_json(response) if as_json else read_text(response)
86 106
87 def analyze_code(pull: PullRequest, diff: str)-> list:
107 def analyze_code(pull: PullRequest, diff: str)-> list[dict]:
88 108 """
89 109 Analyzes the code changes in the pull request.
90 110
91 111 Args:
112 pull (PullRequest): The pull request object.
92 113 diff (str): The diff of the pull request.
93 pr_details (dict): Details of the pull request.
94 114
95 115 Returns:
96 list: List of comments generated by the analysis.
116 list[dict]: A list of comments generated by the analysis.
97 117 """
98 118 comments = []
99 119 changed_lines = []
@@ -122,13 +142,14 @@ def analyze_code(pull: PullRequest, diff: str)-> list:
122 142
123 143 return comments
124 144
125 def create_prompt(changed_lines: list, pull: PullRequest, file_path: str):
145 def create_prompt(changed_lines: list[str], pull: PullRequest, file_path: str):
126 146 """
127 147 Creates a prompt for the g4f model.
128 148
129 149 Args:
130 diff (str): The line of code to analyze.
131 pr_details (dict): Details of the pull request.
150 changed_lines (list[str]): The lines of code that have changed.
151 pull (PullRequest): The pull request object.
152 file_path (str): The path to the file being reviewed.
132 153
133 154 Returns:
134 155 str: The generated prompt.
@@ -159,16 +180,17 @@ Each line is prefixed by its number. Code to review:
159 180
160 181 def create_review_prompt(pull: PullRequest, diff: str):
161 182 """
162 Creates a prompt to create a review.
183 Creates a prompt to create a review comment.
163 184
164 185 Args:
165 diff (str): The line of code to analyze.
186 pull (PullRequest): The pull request object.
187 diff (str): The diff of the pull request.
166 188
167 189 Returns:
168 str: The generated prompt.
190 str: The generated prompt for review.
169 191 """
170 192 return f"""Your task is to review a pull request. Instructions:
171 - Your name / you are copilot.
193 - Write in name of you "g4f-copilot".
172 194 - Write the review in GitHub Markdown format.
173 195 - Thank the author for contributing to the project.
174 196 - Point out that you might leave a few comments on the files.
@@ -192,7 +214,7 @@ def main():
192 214 pull = get_pr_details(github)
193 215 diff = get_diff(pull.diff_url)
194 216 except Exception as e:
195 print(f"Error get details: {e}")
217 print(f"Error get details: {e.__class__.__name__}: {e}")
196 218 exit(1)
197 219 try:
198 220 review = get_ai_response(create_review_prompt(pull, diff), False)
@@ -206,7 +228,10 @@ def main():
206 228 exit(1)
207 229 print("Comments:", comments)
208 230 try:
209 pull.create_review(body=review, comments=comments)
231 if not comments:
232 pull.create_review(body=review, comments=comments)
233 else:
234 pull.create_comment(body=review)
210 235 except Exception as e:
211 236 print(f"Error posting review: {e}")
212 237 exit(1)