Browse Source

基于单服务器

目录问题解决
摘要字数问题解决
参考文献带标题问题解决
master
majiahui@haimaqingfan.com 2 years ago
parent
commit
f85ac84f7a
  1. 7
      chatgpt_post.py
  2. 35
      flask_serve_1.py
  3. 6
      serve_config_1.py
  4. 39
      循环生成目录测试时间.py

7
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
}

35
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)

6
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"

39
循环生成目录测试时间.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)
Loading…
Cancel
Save