Update webapp.py

This commit is contained in:
Joseph Cheung 2023-02-20 21:06:47 +08:00
parent d2be38fd5f
commit e753999663

View file

@ -743,107 +743,107 @@ def search():
number_of_results = 0 number_of_results = 0
# OPENAI GPT # OPENAI GPT
if number_of_results != 0: url_pair = {}
url_pair = {} prompt = ""
prompt = "" for res in results:
for res in results: if 'url' not in res: continue
if 'url' not in res: continue if 'content' not in res: continue
if 'content' not in res: continue if 'title' not in res: continue
if res['content'] == '': continue if res['content'] == '': continue
new_url = 'https://url'+str(len(url_pair)+1) new_url = 'https://url'+str(len(url_pair)+1)
url_pair[new_url] = res['url'] url_pair[new_url] = res['url']
res['title'] = res['title'].replace("التغريدات مع الردود بواسطة","") res['title'] = res['title'].replace("التغريدات مع الردود بواسطة","")
res['content'] = res['content'].replace("Translate Tweet. ","") res['content'] = res['content'].replace("Translate Tweet. ","")
res['content'] = res['content'].replace("Learn more ","") res['content'] = res['content'].replace("Learn more ","")
res['content'] = res['content'].replace("Translate Tweet.","") res['content'] = res['content'].replace("Translate Tweet.","")
res['content'] = res['content'].replace("Learn more.","") res['content'] = res['content'].replace("Learn more.","")
tmp_prompt = res['title'] +'\n'+ res['content'] + '\n' + new_url +'\n' tmp_prompt = res['title'] +'\n'+ res['content'] + '\n' + new_url +'\n'
if len(prompt)+len(tmp_prompt)<3000: if len(prompt)+len(tmp_prompt)<3000:
prompt += tmp_prompt +'\n' prompt += tmp_prompt +'\n'
if prompt != "": if prompt != "":
gpt = "" gpt = ""
gpt_url = "https://api.openai.com/v1/engines/text-davinci-003/completions" gpt_url = "https://api.openai.com/v1/engines/text-davinci-003/completions"
gpt_headers = { gpt_headers = {
"Authorization": "Bearer "+os.environ['GPTKEY'], "Authorization": "Bearer "+os.environ['GPTKEY'],
"Content-Type": "application/json", "Content-Type": "application/json",
"OpenAI-Organization": os.environ['GPTORG'] "OpenAI-Organization": os.environ['GPTORG']
}
if original_search_query != search_query.query:
gpt_data = {
"prompt": prompt+"\n以上是问题 " + original_search_query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
} }
else:
gpt_data = {
"prompt": prompt+"\n以上是关键词 " + search_query.query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
gpt_response = requests.post(gpt_url, headers=gpt_headers, data=json.dumps(gpt_data))
gpt_json = gpt_response.json()
if 'choices' in gpt_json:
gpt = gpt_json['choices'][0]['text']
gpt = gpt.replace("简报:","").replace("简报:","")
for urls in url_pair.keys():
gpt = gpt.replace(urls,url_pair[urls])
rgpt = gpt
if gpt and gpt!="":
if original_search_query != search_query.query: if original_search_query != search_query.query:
gpt_data = { gpt = "Search 为您搜索:" + search_query.query + "\n\n" + gpt
"prompt": prompt+"\n以上是问题 " + original_search_query + " 的搜索结果用简体中文分条总结问题的答案在文中用markdown脚注指对应内容来源链接", for i in range(1,16):
"max_tokens": 1000, gpt = gpt.replace("["+str(i)+"] http","[^"+str(i)+"]: http").replace("["+str(i)+"]http","[^"+str(i)+"]: http").replace("["+str(i)+"]","[^"+str(i)+"]")
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
else:
gpt_data = {
"prompt": prompt+"\n以上是关键词 " + search_query.query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
gpt_response = requests.post(gpt_url, headers=gpt_headers, data=json.dumps(gpt_data))
gpt_json = gpt_response.json()
if 'choices' in gpt_json:
gpt = gpt_json['choices'][0]['text']
gpt = gpt.replace("简报:","").replace("简报:","")
for urls in url_pair.keys():
gpt = gpt.replace(urls,url_pair[urls])
rgpt = gpt rgpt = gpt
gpt = markdown.markdown( gpt , extensions=['footnotes'])
if gpt and gpt!="": for urls in url_pair.keys():
if original_search_query != search_query.query: gpt = gpt.replace("#fn:"+urls.replace("https://url",""),url_pair[urls])
gpt = "Search 为您搜索:" + search_query.query + "\n\n" + gpt gpt = gpt.replace("#fn:url"+urls.replace("https://url",""),url_pair[urls])
for i in range(1,16): gpt = re.sub(r'<div class="footnote">(.*?)</div>', '', gpt, flags=re.DOTALL)
gpt = gpt.replace("["+str(i)+"] http","[^"+str(i)+"]: http").replace("["+str(i)+"]http","[^"+str(i)+"]: http").replace("["+str(i)+"]","[^"+str(i)+"]") gpt = gpt + '''<style>
rgpt = gpt a.footnote-ref{
gpt = markdown.markdown( gpt , extensions=['footnotes']) position: relative;
display: inline-flex;
for urls in url_pair.keys(): align-items: center;
gpt = gpt.replace("#fn:"+urls.replace("https://url",""),url_pair[urls]) justify-content: center;
gpt = gpt.replace("#fn:url"+urls.replace("https://url",""),url_pair[urls]) font-size: 10px;
gpt = re.sub(r'<div class="footnote">(.*?)</div>', '', gpt, flags=re.DOTALL) font-weight: 600;
gpt = gpt + '''<style> vertical-align: top;
a.footnote-ref{ top: 5px;
position: relative; margin: 2px 2px 2px;
display: inline-flex; min-width: 14px;
align-items: center; height: 14px;
justify-content: center; border-radius: 3px;
font-size: 10px; color: rgb(18, 59, 182);
font-weight: 600; background: rgb(209, 219, 250);
vertical-align: top; outline: transparent solid 1px;
top: 5px; }
margin: 2px 2px 2px; </style>
min-width: 14px; '''
height: 14px; for i in range(1, 16):
border-radius: 3px; rgpt = rgpt.replace(f"[{i}]", "")
color: rgb(18, 59, 182); rgpt = rgpt.replace(f"[^{i}]", "")
background: rgb(209, 219, 250); gptbox = {
outline: transparent solid 1px; 'infobox': 'GPT3',
} 'id': 'gpt'+str(len(prompt)),
</style> 'content': gpt,
''' }
for i in range(1, 16): result_container.infoboxes.append(gptbox)
rgpt = rgpt.replace(f"[{i}]", "")
rgpt = rgpt.replace(f"[^{i}]", "")
gptbox = {
'infobox': 'GPT3',
'id': 'gpt'+str(len(prompt)),
'content': gpt,
}
result_container.infoboxes.append(gptbox)
# checkin for a external bang # checkin for a external bang