From 99ec5b01a80870fee064793577a23e519ada15d3 Mon Sep 17 00:00:00 2001 From: kaede10 Date: Thu, 19 Oct 2023 18:51:52 +0800 Subject: [PATCH] update gpt api url --- pr-message/config.yaml | 7 ++-- pr-message/requirements.txt | 1 + pr-message/src/config/init_config.py | 9 +--- pr-message/src/gpt/chat_gpt.py | 53 +++++++++--------------- pr-message/src/gpt/gpt.py | 10 ++--- pr-message/src/handle/comment_command.py | 17 ++++++-- pr-message/src/handle/diff.py | 14 ++++--- pr-message/src/router/router.py | 14 ++++++- pr-message/src/utils/utile_tool.py | 3 +- 9 files changed, 65 insertions(+), 63 deletions(-) diff --git a/pr-message/config.yaml b/pr-message/config.yaml index 72776fa..9e1913c 100644 --- a/pr-message/config.yaml +++ b/pr-message/config.yaml @@ -1,12 +1,11 @@ access_token: -# gitee official website domain name, No risk, hereby declare -gitee_host: +gitee_host: gpt: use: max_token_length: encoding_name: - host: + url: + limit: - Authorization: diff --git a/pr-message/requirements.txt b/pr-message/requirements.txt index 730d5fa..d7dbc77 100644 --- a/pr-message/requirements.txt +++ b/pr-message/requirements.txt @@ -4,3 +4,4 @@ loguru==0.7.0 PyYAML==6.0.1 Requests==2.31.0 tiktoken==0.3.0 +werkzeug==2.2.2 \ No newline at end of file diff --git a/pr-message/src/config/init_config.py b/pr-message/src/config/init_config.py index acacb82..71fefb6 100644 --- a/pr-message/src/config/init_config.py +++ b/pr-message/src/config/init_config.py @@ -1,13 +1,11 @@ import os import yaml - from gitee.gitee_api import GiteeApiCaller from gpt.gpt import Gpt from handle.diff import Diff_Prompt def init_config(path): - print(os.getcwd()) with open(path, "r", encoding="utf-8") as f: config = yaml.safe_load(f) @@ -17,11 +15,8 @@ def init_config(path): config["gpt"]["use"], config["gpt"]["max_token_length"], config["gpt"]["encoding_name"], - config["gpt"]["host"], - config["gpt"]["Authorization"], + config["gpt"]["url"], + config["gpt"]["limit"] ) Diff_Prompt.init_config_attr() - - - diff --git a/pr-message/src/gpt/chat_gpt.py b/pr-message/src/gpt/chat_gpt.py index d84cf44..f101fb3 100644 --- a/pr-message/src/gpt/chat_gpt.py +++ b/pr-message/src/gpt/chat_gpt.py @@ -1,86 +1,71 @@ import requests import tiktoken - +from loguru import logger from gpt.gpt import Gpt class ChatGpt(Gpt): def get_answer(prompt): - url = "{openai_host}/v1/chat/completions".format(openai_host=ChatGpt.host) - data = { "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": ( - "您将充当 git 中提交消息的作者。" - "您的任务是在传统git提交中创建清晰且全面的提交消息,详细清晰的解释更改内容。 我将向您发送“git diff --staged”命令的输出,然后您将其转换为提交消息。" - "行长度不得超过 74 个字符。" - "用中文回答。" - "使用如下模板:" - "修改了那个文件\n" - "- 修改细节1\n" - "- 修改细节2\n" + "您将充当 git 中提交消息的作者。" + "您的任务是在传统git提交中创建清晰且全面的提交消息,详细清晰的解释更改内容。 我将向您发送“git diff --staged”命令的输出,然后您将以\"+\"和\"-\"开头的行内容转换为提交消息。" + "行长度不得超过 74 个字符。" + "用中文回答。" + "使用如下模板:" + "修改了哪个文件\n" + "- 修改细节\n" ), }, { "role": "user", "content": prompt, - } + }, ], "temperature": 0.7, } - - response = requests.post( - url, json=data, headers={"Authorization": "Bearer " + ChatGpt.Authorization} + ChatGpt.url, json=data ) - if response.status_code != 200: - print("get answer error") - print(response.status_code) + logger.info("get answer error") + logger.info(response.status_code) pr = response.json() - return pr["choices"][0]["message"]["content"] - - + return pr def get_summary(content): - url = "{openai_host}/v1/chat/completions".format(openai_host=ChatGpt.host) data = { "model": "gpt-3.5-turbo", "messages": [ { "role": "system", - "content": ( - "您的任务是高度概括总结我给您的输入内容。" - "用中文回答。" - ), + "content": ("您的任务是高度概括总结我给您的输入内容。" "用中文回答。"), }, { "role": "user", "content": content, - } + }, ], "temperature": 0.7, } response = requests.post( - url, json=data, headers={"Authorization": "Bearer " + ChatGpt.Authorization} + ChatGpt.url, json=data ) if response.status_code != 200: - print("get answer error") - print(response.status_code) + logger.info("get answer error") + logger.info(response.status_code) pr = response.json() - return pr["choices"][0]["message"]["content"] - - - + return pr def num_tokens_from_string(string: str) -> int: encoding = tiktoken.get_encoding(ChatGpt.encoding_name) diff --git a/pr-message/src/gpt/gpt.py b/pr-message/src/gpt/gpt.py index 6d7cbc4..ecb3855 100644 --- a/pr-message/src/gpt/gpt.py +++ b/pr-message/src/gpt/gpt.py @@ -5,15 +5,15 @@ class Gpt(metaclass=ABCMeta): use = "" max_token_length = 0 encoding_name = "" - host = "" - Authorization = "" + url = "" + limit = 5 - def init_config_attr(use, max_token_length, encoding_name, host, Authorization): + def init_config_attr(use, max_token_length, encoding_name, url, limit): Gpt.use = use Gpt.max_token_length = max_token_length Gpt.encoding_name = encoding_name - Gpt.host = host - Gpt.Authorization = Authorization + Gpt.url = url + Gpt.limit = limit @abstractmethod def get_answer(prompt): diff --git a/pr-message/src/handle/comment_command.py b/pr-message/src/handle/comment_command.py index 4d6e08b..9cc20de 100644 --- a/pr-message/src/handle/comment_command.py +++ b/pr-message/src/handle/comment_command.py @@ -18,23 +18,33 @@ def summary_message(data): logger.error("no diff") return + logger.info(diff_url) + diff = requests.get(diff_url) + if diff.status_code != 200: logger.error("get diff error") return - diff.encoding = "utf-8" diff_text = diff.text + logger.info(diff_text) results = handle_diff(diff_text) if results is None: logger.error("can't get prompts") return + logger.info("handle success") + comment_list = [] + api_limit = GptClassFactory.create_class().limit + cnt = 0 for result in results: + cnt += 1 + if cnt > api_limit: + break answer = GptClassFactory.create_class().get_answer(result) if answer is None: continue @@ -42,6 +52,7 @@ def summary_message(data): time.sleep(10) + logger.info(comment_list) comment = "" for single_comment in comment_list: @@ -73,6 +84,6 @@ def summary_message(data): logger.error("no number") return - pr = PullRequestComments(owner, repo, number, comment, None, None, None) + pr_client = PullRequestComments(owner, repo, number, comment, None, None, None) - pr.submit_pull_request_comments() + pr_client.submit_pull_request_comments() diff --git a/pr-message/src/handle/diff.py b/pr-message/src/handle/diff.py index da509bf..b51e95e 100644 --- a/pr-message/src/handle/diff.py +++ b/pr-message/src/handle/diff.py @@ -27,12 +27,14 @@ def handle_diff(diff): if diff_content_out_of_length(diff): diff_array = cut_diff_by_file_diffs(diff) - for single_diff in diff_array: - if diff_content_out_of_length(single_diff): - result_array = cut_single_diff(single_diff) - prompt_list.extend(result_array) - else: - prompt_list.append(single_diff) + for single_diff in diff_array: + if diff_content_out_of_length(single_diff): + result_array = cut_single_diff(single_diff) + prompt_list.extend(result_array) + else: + prompt_list.append(single_diff) + else: + prompt_list.append(diff) return prompt_list diff --git a/pr-message/src/router/router.py b/pr-message/src/router/router.py index 8221e8c..a872549 100644 --- a/pr-message/src/router/router.py +++ b/pr-message/src/router/router.py @@ -1,5 +1,6 @@ from flask import request from flask import Flask +from loguru import logger from handle.task import assgin_task from utils.background_task import start_thread @@ -10,9 +11,9 @@ app = Flask(__name__) @app.route("/hook/analyze", methods=["POST"]) def analyze(): - data = request.get_json() + json_data = request.get_json() - start_thread(assgin_task, data) + start_thread(assgin_task, json_data) return "Processing completed" @@ -20,6 +21,15 @@ def analyze(): @app.before_request def before_request(): headers = request.headers + # if headers.get("User-Agent") != "Robot-Gitee-Access": + # return "Bad Request: unknown User-Agent Header", 400 + + # if headers.get("X-Gitee-Event") == "": + # return "Bad Request: Missing X-Gitee-Event Header", 400 + + # uuid = headers.get("X-Gitee-Timestamp") + # if uuid == "": + # return "Bad Request: Missing X-Gitee-Timestamp Header", 400 def start_router(): diff --git a/pr-message/src/utils/utile_tool.py b/pr-message/src/utils/utile_tool.py index 700d27f..c41524f 100644 --- a/pr-message/src/utils/utile_tool.py +++ b/pr-message/src/utils/utile_tool.py @@ -1,3 +1,2 @@ - def split_string(string, length): - return [string[i:i+length] for i in range(0, len(string), length)] \ No newline at end of file + return [string[i : i + length] for i in range(0, len(string), length)] -- Gitee