From f85ac84f7ad28172c7ec957bdad9af0db07b1bb9 Mon Sep 17 00:00:00 2001 From: "majiahui@haimaqingfan.com" Date: Tue, 11 Apr 2023 18:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E5=8D=95=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=20=E7=9B=AE=E5=BD=95=E9=97=AE=E9=A2=98=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=20=E6=91=98=E8=A6=81=E5=AD=97=E6=95=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E5=86=B3=20=E5=8F=82=E8=80=83=E6=96=87?= =?UTF-8?q?=E7=8C=AE=E5=B8=A6=E6=A0=87=E9=A2=98=E9=97=AE=E9=A2=98=E8=A7=A3?= =?UTF-8?q?=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chatgpt_post.py | 7 +++---- flask_serve_1.py | 35 +++++++++++++++++++++++++---------- serve_config_1.py | 6 ++++-- 循环生成目录测试时间.py | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 循环生成目录测试时间.py diff --git a/chatgpt_post.py b/chatgpt_post.py index 56cc2f7..7a31c3d 100644 --- a/chatgpt_post.py +++ b/chatgpt_post.py @@ -1,7 +1,8 @@ import requests import json -OPENAI_API_KEY = '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' +prompt = "请帮我根据题目为“{}”生成一个论文目录只含有一级目录和二级目录".format("煤业集团水文监测系统建设项目设计与实现") +OPENAI_API_KEY = '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' url = "https://api.openai.com/v1/chat/completions" headers = { @@ -11,9 +12,7 @@ headers = { data = { "model": "gpt-3.5-turbo", "messages": [ - {"role": "user", "content": "请帮我改写这句话:在城市发展进程当中,逐渐呈现出一些综合性的大型建筑群。"}, - {"role": "assistant", "content": "随着城市的发展,综合性大型建筑群正在逐渐出现。"}, - {"role": "user", "content": "这句话我不满意,再改一下帮我"} + {"role": "user", "content": prompt}, ], "temperature": 0.7 } diff --git a/flask_serve_1.py b/flask_serve_1.py index fbf7920..daf9c53 100644 --- a/flask_serve_1.py +++ b/flask_serve_1.py @@ -31,9 +31,6 @@ lock = threading.RLock() pool = redis.ConnectionPool(host=config.reids_ip, port=config.reids_port, max_connections=50, db=config.reids_db, password=config.reids_password) redis_ = redis.Redis(connection_pool=pool, decode_responses=True) -thanks = "致谢" -references = "参考文献" - flask_serves_env = "http://{}:{}".format(localhostip,config.flask_port) paper_download_url = flask_serves_env + "/download?filename_path={}/paper.docx" @@ -406,7 +403,23 @@ def threading_mulu(api_key, title, uuid): mulu_list = str(mulu).split("\n") mulu_list = [i.strip() for i in mulu_list if i != ""] - print(mulu_list) + mulu_list_bool = [] + for i in mulu_list: + result_biaoti_list = re.findall(config.pantten_biaoti, i) + if result_biaoti_list != []: + mulu_list_bool.append((i, "一级标题")) + else: + mulu_list_bool.append((i, "二级标题")) + + mulu_list_bool_part = mulu_list_bool[:3] + + if mulu_list_bool_part[0][1] != "一级标题": + redis_.lpush(redis_title, json.dumps({"id": uuid, "title": title}, ensure_ascii=False)) # 加入redis + redis_.persist(redis_key_name_openaikey_list) + if mulu_list_bool_part[0][1] == mulu_list_bool_part[1][1] == "一级标题": + redis_.lpush(redis_title, json.dumps({"id": uuid, "title": title}, ensure_ascii=False)) # 加入redis + redis_.persist(redis_key_name_openaikey_list) + cun_bool = False table_of_contents = [mulu_list[0]] @@ -430,15 +443,17 @@ def threading_mulu(api_key, title, uuid): # table_of_contents = table_of_contents[:3] + table_of_contents[-1:] # print(table_of_contents) - thanks_references_bool_table = table_of_contents[-3:] + thanks_references_bool_table = table_of_contents[-4:] # thanks = "致谢" # references = "参考文献" - if references in thanks_references_bool_table: - table_of_contents.remove(references) - - if thanks in thanks_references_bool_table: - table_of_contents.remove(thanks) + for i in thanks_references_bool_table: + if config.references in i: + table_of_contents.remove(i) + if config.thanks in i: + table_of_contents.remove(i) + if config.excursus in i: + table_of_contents.remove(i) # table_of_contents.append(thanks) # table_of_contents.append(references) diff --git a/serve_config_1.py b/serve_config_1.py index 5363122..bfd0113 100644 --- a/serve_config_1.py +++ b/serve_config_1.py @@ -3,15 +3,16 @@ class Config: # 目录提取拼接相关参数 self.pantten_second_biaoti = '[2二ⅡⅠ][、.]\s{0,}?[\u4e00-\u9fa5]+' self.pantten_other_biaoti = '[2-9二三四五六七八九ⅡⅢⅣⅤⅥⅦⅧⅨ][、.]\s{0,}?[\u4e00-\u9fa5]+' + self.pantten_biaoti = '[1-9一二三四五六七八九ⅠⅡⅢⅣⅤⅥⅦⅧⅨ][、.]\s{0,}?[\u4e00-\u9fa5a-zA-Z]+' # chatgpt 接口相关参数 - self.mulu_prompt = "请帮我根据题目为“{}”生成一个论文目录" + self.mulu_prompt = "请帮我根据题目为“{}”生成一个论文目录其中只含有一级目录和二级标题目录" self.first_title_prompt = "论文题目是“{}”,目录是“{}”,请把其中的大标题“{}”的内容补充完整,补充内容字数在800字左右" self.small_title_prompt = "论文题目是“{}”,目录是“{}”,请把其中的小标题“{}”的内容补充完整,补充内容字数在800字左右" self.references_prompt = "论文题目是“{}”,目录是“{}”,请为这篇论文生成15篇左右的参考文献,要求其中有有中文参考文献不低于12篇,英文参考文献不低于2篇" self.thank_prompt = "请以“{}”为题写一篇论文的致谢" self.kaitibaogao_prompt = "请以《{}》为题目生成研究的主要的内容、背景、目的、意义,要求不少于1500字" - self.chinese_abstract_prompt = "请以《{}》为题目生成论文摘要,要求生成的字数在800字左右" + self.chinese_abstract_prompt = "请以《{}》为题目生成论文摘要,要求生成的字数在600字左右" self.english_abstract_prompt = "请把“{}”这段文字翻译成英文" self.chinese_keyword_prompt = "请为“{}”这段论文摘要生成3-5个关键字" self.english_keyword_prompt = "请把“{}”这几个关键字翻译成英文" @@ -29,6 +30,7 @@ class Config: # 流程相关参数 self.thanks = "致谢" self.references = "参考文献" + self.excursus = "附录" # flask port self.flask_port = "14002" diff --git a/循环生成目录测试时间.py b/循环生成目录测试时间.py new file mode 100644 index 0000000..0a43c9b --- /dev/null +++ b/循环生成目录测试时间.py @@ -0,0 +1,39 @@ +import requests +import json +import time + +prompt = "请帮我根据题目为“{}”生成一个论文目录" +def requests_chatgpt(api_key,title): + + OPENAI_API_KEY = api_key + url = "https://api.openai.com/v1/chat/completions" + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {OPENAI_API_KEY}" + } + data = { + "model": "gpt-3.5-turbo", + "messages": [ + {"role": "user", "content": prompt.format(title)}, + ], + "temperature": 0.7 + } + + response = requests.post(url, + headers=headers, + data=json.dumps(data), + timeout=1000) + + res = response.json() + print(res) + print(res["choices"][0]["message"]["content"]) + + +start = time.time() + +for i in range(10): + requests_chatgpt("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","大型商业建筑人员疏散设计研究") + +end = time.time() + +print(end-start) \ No newline at end of file