fw
This commit is contained in:
parent
fff7b8ef91
commit
168f25115b
@ -40,6 +40,9 @@ set_theme = adjust_theme()
|
|||||||
cancel_handles = []
|
cancel_handles = []
|
||||||
with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
||||||
gr.HTML(title_html)
|
gr.HTML(title_html)
|
||||||
|
# To add a Duplicate Space badge
|
||||||
|
gr.HTML('''<center><a href="https://huggingface.co/spaces/qingxu98/gpt-academic?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>请您打开此页面后务必点击上方的“复制空间”(Duplicate Space)按钮!<br/>切忌在“复制空间”(Duplicate Space)之前填入API_KEY或进行提问,否则您的API_KEY将极可能被空间所有者攫取!</center>''')
|
||||||
|
|
||||||
with gr.Row().style(equal_height=True):
|
with gr.Row().style(equal_height=True):
|
||||||
with gr.Column(scale=2):
|
with gr.Column(scale=2):
|
||||||
chatbot = gr.Chatbot()
|
chatbot = gr.Chatbot()
|
||||||
@ -47,7 +50,9 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
|
|||||||
history = gr.State([])
|
history = gr.State([])
|
||||||
with gr.Column(scale=1):
|
with gr.Column(scale=1):
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
|
api_key = gr.Textbox(show_label=False, placeholder="输入API_KEY,输入后自动生效.").style(container=False)
|
||||||
|
with gr.Row():
|
||||||
|
txt = gr.Textbox(show_label=False, placeholder="输入问题.").style(container=False)
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
submitBtn = gr.Button("提交", variant="primary")
|
submitBtn = gr.Button("提交", variant="primary")
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
@ -93,7 +98,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False, css=advanced_css) as de
|
|||||||
return ret
|
return ret
|
||||||
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn] )
|
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn] )
|
||||||
# 整理反复出现的控件句柄组合
|
# 整理反复出现的控件句柄组合
|
||||||
input_combo = [txt, top_p, temperature, chatbot, history, system_prompt]
|
input_combo = [txt, top_p, api_key, temperature, chatbot, history, system_prompt]
|
||||||
output_combo = [chatbot, history, status]
|
output_combo = [chatbot, history, status]
|
||||||
predict_args = dict(fn=predict, inputs=input_combo, outputs=output_combo)
|
predict_args = dict(fn=predict, inputs=input_combo, outputs=output_combo)
|
||||||
empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏
|
empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏
|
||||||
@ -142,4 +147,4 @@ def auto_opentab_delay():
|
|||||||
|
|
||||||
auto_opentab_delay()
|
auto_opentab_delay()
|
||||||
demo.title = "ChatGPT 学术优化"
|
demo.title = "ChatGPT 学术优化"
|
||||||
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, auth=AUTHENTICATION)
|
demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=False)
|
@ -132,7 +132,7 @@ def get_name(_url_):
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 下载arxiv论文并翻译摘要(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 下载arxiv论文并翻译摘要(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
|
|
||||||
CRAZY_FUNCTION_INFO = "下载arxiv论文并翻译摘要,函数插件作者[binary-husky]。正在提取摘要并下载PDF文档……"
|
CRAZY_FUNCTION_INFO = "下载arxiv论文并翻译摘要,函数插件作者[binary-husky]。正在提取摘要并下载PDF文档……"
|
||||||
import glob
|
import glob
|
||||||
@ -172,7 +172,7 @@ def 下载arxiv论文并翻译摘要(txt, top_p, temperature, chatbot, history,
|
|||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
history.append(i_say_show_user); history.append(gpt_say)
|
history.append(i_say_show_user); history.append(gpt_say)
|
||||||
yield chatbot, history, msg
|
yield chatbot, history, msg
|
||||||
|
@ -5,7 +5,7 @@ from toolbox import CatchException, write_results_to_file
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, WEB_PORT):
|
def 全项目切换英文(txt, top_p, api_key, temperature, chatbot, history, sys_prompt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
# 集合文件
|
# 集合文件
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
@ -32,7 +32,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt,
|
|||||||
file_content = f.read()
|
file_content = f.read()
|
||||||
i_say = f'接下来请将以下代码中包含的所有中文转化为英文,只输出代码,文件名是{fp},文件代码是 ```{file_content}```'
|
i_say = f'接下来请将以下代码中包含的所有中文转化为英文,只输出代码,文件名是{fp},文件代码是 ```{file_content}```'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
||||||
mutable_return[index] = gpt_say
|
mutable_return[index] = gpt_say
|
||||||
|
|
||||||
# 所有线程同时开始执行任务函数
|
# 所有线程同时开始执行任务函数
|
||||||
|
@ -3,7 +3,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|||||||
fast_debug = False
|
fast_debug = False
|
||||||
|
|
||||||
|
|
||||||
def 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 解析docx(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, os
|
import time, os
|
||||||
# pip install python-docx 用于docx格式,跨平台
|
# pip install python-docx 用于docx格式,跨平台
|
||||||
# pip install pywin32 用于doc格式,仅支持Win平台
|
# pip install pywin32 用于doc格式,仅支持Win平台
|
||||||
@ -40,7 +40,7 @@ def 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, histo
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature,
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature,
|
||||||
history=[]) # 带超时倒计时
|
history=[]) # 带超时倒计时
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
history.append(i_say_show_user);
|
history.append(i_say_show_user);
|
||||||
@ -66,7 +66,7 @@ def 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, histo
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature,
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature,
|
||||||
history=history) # 带超时倒计时
|
history=history) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
@ -79,7 +79,7 @@ def 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, histo
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 总结word文档(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 总结word文档(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
import glob, os
|
import glob, os
|
||||||
|
|
||||||
# 基本信息:功能、贡献者
|
# 基本信息:功能、贡献者
|
||||||
@ -124,4 +124,4 @@ def 总结word文档(txt, top_p, temperature, chatbot, history, systemPromptTxt,
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 开始正式执行任务
|
# 开始正式执行任务
|
||||||
yield from 解析docx(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析docx(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
@ -57,7 +57,7 @@ def clean_text(raw_text):
|
|||||||
|
|
||||||
return final_text.strip()
|
return final_text.strip()
|
||||||
|
|
||||||
def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 解析PDF(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, glob, os, fitz
|
import time, glob, os, fitz
|
||||||
print('begin analysis on:', file_manifest)
|
print('begin analysis on:', file_manifest)
|
||||||
for index, fp in enumerate(file_manifest):
|
for index, fp in enumerate(file_manifest):
|
||||||
@ -78,7 +78,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
|
|
||||||
print('[2] end gpt req')
|
print('[2] end gpt req')
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
@ -96,7 +96,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature, history=history) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
history.append(i_say); history.append(gpt_say)
|
history.append(i_say); history.append(gpt_say)
|
||||||
@ -107,7 +107,7 @@ def 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, histor
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 批量总结PDF文档(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
import glob, os
|
import glob, os
|
||||||
|
|
||||||
# 基本信息:功能、贡献者
|
# 基本信息:功能、贡献者
|
||||||
@ -151,4 +151,4 @@ def 批量总结PDF文档(txt, top_p, temperature, chatbot, history, systemPromp
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 开始正式执行任务
|
# 开始正式执行任务
|
||||||
yield from 解析PDF(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析PDF(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
@ -61,7 +61,7 @@ def readPdf(pdfPath):
|
|||||||
return outTextList
|
return outTextList
|
||||||
|
|
||||||
|
|
||||||
def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 解析Paper(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
print('begin analysis on:', file_manifest)
|
print('begin analysis on:', file_manifest)
|
||||||
@ -83,7 +83,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
|
|
||||||
print('[2] end gpt req')
|
print('[2] end gpt req')
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
@ -101,7 +101,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature, history=history) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
history.append(i_say); history.append(gpt_say)
|
history.append(i_say); history.append(gpt_say)
|
||||||
@ -113,7 +113,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 批量总结PDF文档pdfminer(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 批量总结PDF文档pdfminer(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
|
|
||||||
@ -147,5 +147,5 @@ def 批量总结PDF文档pdfminer(txt, top_p, temperature, chatbot, history, sys
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或pdf文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex或pdf文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析Paper(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|||||||
fast_debug = False
|
fast_debug = False
|
||||||
|
|
||||||
|
|
||||||
def 生成函数注释(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 生成函数注释(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
print('begin analysis on:', file_manifest)
|
print('begin analysis on:', file_manifest)
|
||||||
for index, fp in enumerate(file_manifest):
|
for index, fp in enumerate(file_manifest):
|
||||||
@ -19,7 +19,7 @@ def 生成函数注释(file_manifest, project_folder, top_p, temperature, chatbo
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
|
|
||||||
print('[2] end gpt req')
|
print('[2] end gpt req')
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
@ -37,7 +37,7 @@ def 生成函数注释(file_manifest, project_folder, top_p, temperature, chatbo
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 批量生成函数注释(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 批量生成函数注释(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -54,4 +54,4 @@ def 批量生成函数注释(txt, top_p, temperature, chatbot, history, systemPr
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 生成函数注释(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 生成函数注释(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
@ -2,7 +2,7 @@ from predict import predict_no_ui
|
|||||||
from toolbox import CatchException, report_execption, write_results_to_file, predict_no_ui_but_counting_down
|
from toolbox import CatchException, report_execption, write_results_to_file, predict_no_ui_but_counting_down
|
||||||
fast_debug = False
|
fast_debug = False
|
||||||
|
|
||||||
def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
print('begin analysis on:', file_manifest)
|
print('begin analysis on:', file_manifest)
|
||||||
for index, fp in enumerate(file_manifest):
|
for index, fp in enumerate(file_manifest):
|
||||||
@ -19,7 +19,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|||||||
msg = '正常'
|
msg = '正常'
|
||||||
|
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
history.append(i_say_show_user); history.append(gpt_say)
|
history.append(i_say_show_user); history.append(gpt_say)
|
||||||
@ -34,7 +34,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature, history=history) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
history.append(i_say); history.append(gpt_say)
|
history.append(i_say); history.append(gpt_say)
|
||||||
@ -47,7 +47,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析项目本身(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
|
file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
|
||||||
@ -65,8 +65,8 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|||||||
|
|
||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
# gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature)
|
# gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature)
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], long_connection=True) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[], long_connection=True) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
history.append(i_say_show_user); history.append(gpt_say)
|
history.append(i_say_show_user); history.append(gpt_say)
|
||||||
@ -79,8 +79,8 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|||||||
|
|
||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
# gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history)
|
# gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature, history=history)
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history, long_connection=True) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature, history=history, long_connection=True) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
history.append(i_say); history.append(gpt_say)
|
history.append(i_say); history.append(gpt_say)
|
||||||
@ -90,7 +90,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个Python项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -105,11 +105,11 @@ def 解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPr
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何python文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个C项目的头文件(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -126,10 +126,10 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个C项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -146,11 +146,11 @@ def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptT
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个Java项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个Java项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -168,11 +168,11 @@ def 解析一个Java项目(txt, top_p, temperature, chatbot, history, systemProm
|
|||||||
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何java文件: {txt}")
|
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何java文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个Rect项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个Rect项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -191,11 +191,11 @@ def 解析一个Rect项目(txt, top_p, temperature, chatbot, history, systemProm
|
|||||||
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何Rect文件: {txt}")
|
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何Rect文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 解析一个Golang项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 解析一个Golang项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -210,4 +210,4 @@ def 解析一个Golang项目(txt, top_p, temperature, chatbot, history, systemPr
|
|||||||
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何golang文件: {txt}")
|
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何golang文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
@ -3,7 +3,7 @@ from toolbox import CatchException, report_execption, write_results_to_file, pre
|
|||||||
fast_debug = False
|
fast_debug = False
|
||||||
|
|
||||||
|
|
||||||
def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
def 解析Paper(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt):
|
||||||
import time, glob, os
|
import time, glob, os
|
||||||
print('begin analysis on:', file_manifest)
|
print('begin analysis on:', file_manifest)
|
||||||
for index, fp in enumerate(file_manifest):
|
for index, fp in enumerate(file_manifest):
|
||||||
@ -20,7 +20,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[]) # 带超时倒计时
|
||||||
|
|
||||||
print('[2] end gpt req')
|
print('[2] end gpt req')
|
||||||
chatbot[-1] = (i_say_show_user, gpt_say)
|
chatbot[-1] = (i_say_show_user, gpt_say)
|
||||||
@ -38,7 +38,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
if not fast_debug:
|
if not fast_debug:
|
||||||
msg = '正常'
|
msg = '正常'
|
||||||
# ** gpt request **
|
# ** gpt request **
|
||||||
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, temperature, history=history) # 带超时倒计时
|
gpt_say = yield from predict_no_ui_but_counting_down(i_say, i_say, chatbot, top_p, api_key, temperature, history=history) # 带超时倒计时
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
history.append(i_say); history.append(gpt_say)
|
history.append(i_say); history.append(gpt_say)
|
||||||
@ -50,7 +50,7 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|||||||
|
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 读文章写摘要(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
import glob, os
|
import glob, os
|
||||||
if os.path.exists(txt):
|
if os.path.exists(txt):
|
||||||
@ -67,4 +67,4 @@ def 读文章写摘要(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|||||||
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
|
||||||
yield chatbot, history, '正常'
|
yield chatbot, history, '正常'
|
||||||
return
|
return
|
||||||
yield from 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
yield from 解析Paper(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)
|
||||||
|
@ -3,7 +3,7 @@ from toolbox import CatchException, report_execption, write_results_to_file
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@CatchException
|
@CatchException
|
||||||
def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def 高阶功能模板函数(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
history = [] # 清空历史,以免输入溢出
|
history = [] # 清空历史,以免输入溢出
|
||||||
chatbot.append(("这是什么功能?", "[Local Message] 请注意,您正在调用一个[函数插件]的模板,该函数面向希望实现更多有趣功能的开发者,它可以作为创建新功能函数的模板。为了做到简单易读,该函数只有25行代码,所以不会实时反馈文字流或心跳,请耐心等待程序输出完成。此外我们也提供可同步处理大量文件的多线程Demo供您参考。您若希望分享新的功能模组,请不吝PR!"))
|
chatbot.append(("这是什么功能?", "[Local Message] 请注意,您正在调用一个[函数插件]的模板,该函数面向希望实现更多有趣功能的开发者,它可以作为创建新功能函数的模板。为了做到简单易读,该函数只有25行代码,所以不会实时反馈文字流或心跳,请耐心等待程序输出完成。此外我们也提供可同步处理大量文件的多线程Demo供您参考。您若希望分享新的功能模组,请不吝PR!"))
|
||||||
yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示
|
yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示
|
||||||
@ -17,7 +17,7 @@ def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPr
|
|||||||
|
|
||||||
# history = [] 每次询问不携带之前的询问历史
|
# history = [] 每次询问不携带之前的询问历史
|
||||||
gpt_say = predict_no_ui_long_connection(
|
gpt_say = predict_no_ui_long_connection(
|
||||||
inputs=i_say, top_p=top_p, temperature=temperature, history=[],
|
inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature, history=[],
|
||||||
sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。") # 请求gpt,需要一段时间
|
sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。") # 请求gpt,需要一段时间
|
||||||
|
|
||||||
chatbot[-1] = (i_say, gpt_say)
|
chatbot[-1] = (i_say, gpt_say)
|
||||||
|
248
predict.py
248
predict.py
@ -1,248 +0,0 @@
|
|||||||
# 借鉴了 https://github.com/GaiZhenbiao/ChuanhuChatGPT 项目
|
|
||||||
|
|
||||||
"""
|
|
||||||
该文件中主要包含三个函数
|
|
||||||
|
|
||||||
不具备多线程能力的函数:
|
|
||||||
1. predict: 正常对话时使用,具备完备的交互功能,不可多线程
|
|
||||||
|
|
||||||
具备多线程调用能力的函数
|
|
||||||
2. predict_no_ui:高级实验性功能模块调用,不会实时显示在界面上,参数简单,可以多线程并行,方便实现复杂的功能逻辑
|
|
||||||
3. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import gradio as gr
|
|
||||||
import logging
|
|
||||||
import traceback
|
|
||||||
import requests
|
|
||||||
import importlib
|
|
||||||
|
|
||||||
# config_private.py放自己的秘密如API和代理网址
|
|
||||||
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
|
||||||
from toolbox import get_conf
|
|
||||||
proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL = \
|
|
||||||
get_conf('proxies', 'API_URL', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY', 'LLM_MODEL')
|
|
||||||
|
|
||||||
timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \
|
|
||||||
'网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。'
|
|
||||||
|
|
||||||
def get_full_error(chunk, stream_response):
|
|
||||||
"""
|
|
||||||
获取完整的从Openai返回的报错
|
|
||||||
"""
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
chunk += next(stream_response)
|
|
||||||
except:
|
|
||||||
break
|
|
||||||
return chunk
|
|
||||||
|
|
||||||
def predict_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""):
|
|
||||||
"""
|
|
||||||
发送至chatGPT,等待回复,一次性完成,不显示中间过程。
|
|
||||||
predict函数的简化版。
|
|
||||||
用于payload比较大的情况,或者用于实现多线、带嵌套的复杂功能。
|
|
||||||
|
|
||||||
inputs 是本次问询的输入
|
|
||||||
top_p, temperature是chatGPT的内部调优参数
|
|
||||||
history 是之前的对话列表
|
|
||||||
(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误,然后raise ConnectionAbortedError)
|
|
||||||
"""
|
|
||||||
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=False)
|
|
||||||
|
|
||||||
retry = 0
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
# make a POST request to the API endpoint, stream=False
|
|
||||||
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
|
||||||
json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
|
|
||||||
except requests.exceptions.ReadTimeout as e:
|
|
||||||
retry += 1
|
|
||||||
traceback.print_exc()
|
|
||||||
if retry > MAX_RETRY: raise TimeoutError
|
|
||||||
if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = json.loads(response.text)["choices"][0]["message"]["content"]
|
|
||||||
return result
|
|
||||||
except Exception as e:
|
|
||||||
if "choices" not in response.text: print(response.text)
|
|
||||||
raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
|
|
||||||
|
|
||||||
|
|
||||||
def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_prompt=""):
|
|
||||||
"""
|
|
||||||
发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免有人中途掐网线。
|
|
||||||
"""
|
|
||||||
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=True)
|
|
||||||
|
|
||||||
retry = 0
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
# make a POST request to the API endpoint, stream=False
|
|
||||||
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
|
||||||
json=payload, stream=True, timeout=TIMEOUT_SECONDS); break
|
|
||||||
except requests.exceptions.ReadTimeout as e:
|
|
||||||
retry += 1
|
|
||||||
traceback.print_exc()
|
|
||||||
if retry > MAX_RETRY: raise TimeoutError
|
|
||||||
if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
|
|
||||||
|
|
||||||
stream_response = response.iter_lines()
|
|
||||||
result = ''
|
|
||||||
while True:
|
|
||||||
try: chunk = next(stream_response).decode()
|
|
||||||
except StopIteration: break
|
|
||||||
if len(chunk)==0: continue
|
|
||||||
if not chunk.startswith('data:'):
|
|
||||||
error_msg = get_full_error(chunk.encode('utf8'), stream_response).decode()
|
|
||||||
if "reduce the length" in error_msg:
|
|
||||||
raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg)
|
|
||||||
else:
|
|
||||||
raise RuntimeError("OpenAI拒绝了请求:" + error_msg)
|
|
||||||
json_data = json.loads(chunk.lstrip('data:'))['choices'][0]
|
|
||||||
delta = json_data["delta"]
|
|
||||||
if len(delta) == 0: break
|
|
||||||
if "role" in delta: continue
|
|
||||||
if "content" in delta: result += delta["content"]; print(delta["content"], end='')
|
|
||||||
else: raise RuntimeError("意外Json结构:"+delta)
|
|
||||||
if json_data['finish_reason'] == 'length':
|
|
||||||
raise ConnectionAbortedError("正常结束,但显示Token不足。")
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='',
|
|
||||||
stream = True, additional_fn=None):
|
|
||||||
"""
|
|
||||||
发送至chatGPT,流式获取输出。
|
|
||||||
用于基础的对话功能。
|
|
||||||
inputs 是本次问询的输入
|
|
||||||
top_p, temperature是chatGPT的内部调优参数
|
|
||||||
history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
|
|
||||||
chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
|
|
||||||
additional_fn代表点击的哪个按钮,按钮见functional.py
|
|
||||||
"""
|
|
||||||
if additional_fn is not None:
|
|
||||||
import functional
|
|
||||||
importlib.reload(functional) # 热更新prompt
|
|
||||||
functional = functional.get_functionals()
|
|
||||||
if "PreProcess" in functional[additional_fn]: inputs = functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话)
|
|
||||||
inputs = functional[additional_fn]["Prefix"] + inputs + functional[additional_fn]["Suffix"]
|
|
||||||
|
|
||||||
if stream:
|
|
||||||
raw_input = inputs
|
|
||||||
logging.info(f'[raw_input] {raw_input}')
|
|
||||||
chatbot.append((inputs, ""))
|
|
||||||
yield chatbot, history, "等待响应"
|
|
||||||
|
|
||||||
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt, stream)
|
|
||||||
history.append(inputs); history.append(" ")
|
|
||||||
|
|
||||||
retry = 0
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
# make a POST request to the API endpoint, stream=True
|
|
||||||
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
|
||||||
json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
|
|
||||||
except:
|
|
||||||
retry += 1
|
|
||||||
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
|
|
||||||
retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""
|
|
||||||
yield chatbot, history, "请求超时"+retry_msg
|
|
||||||
if retry > MAX_RETRY: raise TimeoutError
|
|
||||||
|
|
||||||
gpt_replying_buffer = ""
|
|
||||||
|
|
||||||
is_head_of_the_stream = True
|
|
||||||
if stream:
|
|
||||||
stream_response = response.iter_lines()
|
|
||||||
while True:
|
|
||||||
chunk = next(stream_response)
|
|
||||||
# print(chunk.decode()[6:])
|
|
||||||
if is_head_of_the_stream:
|
|
||||||
# 数据流的第一帧不携带content
|
|
||||||
is_head_of_the_stream = False; continue
|
|
||||||
|
|
||||||
if chunk:
|
|
||||||
try:
|
|
||||||
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
|
||||||
# 判定为数据流的结束,gpt_replying_buffer也写完了
|
|
||||||
logging.info(f'[response] {gpt_replying_buffer}')
|
|
||||||
break
|
|
||||||
# 处理数据流的主体
|
|
||||||
chunkjson = json.loads(chunk.decode()[6:])
|
|
||||||
status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
|
|
||||||
# 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出
|
|
||||||
gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
|
|
||||||
history[-1] = gpt_replying_buffer
|
|
||||||
chatbot[-1] = (history[-2], history[-1])
|
|
||||||
yield chatbot, history, status_text
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
traceback.print_exc()
|
|
||||||
yield chatbot, history, "Json解析不合常规"
|
|
||||||
chunk = get_full_error(chunk, stream_response)
|
|
||||||
error_msg = chunk.decode()
|
|
||||||
if "reduce the length" in error_msg:
|
|
||||||
chatbot[-1] = (chatbot[-1][0], "[Local Message] Input (or history) is too long, please reduce input or clear history by refreshing this page.")
|
|
||||||
history = [] # 清除历史
|
|
||||||
elif "Incorrect API key" in error_msg:
|
|
||||||
chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key provided.")
|
|
||||||
elif "exceeded your current quota" in error_msg:
|
|
||||||
chatbot[-1] = (chatbot[-1][0], "[Local Message] You exceeded your current quota. OpenAI以账户额度不足为由,拒绝服务.")
|
|
||||||
else:
|
|
||||||
from toolbox import regular_txt_to_markdown
|
|
||||||
tb_str = '```\n' + traceback.format_exc() + '```'
|
|
||||||
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk.decode()[4:])}")
|
|
||||||
yield chatbot, history, "Json异常" + error_msg
|
|
||||||
return
|
|
||||||
|
|
||||||
def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
|
|
||||||
"""
|
|
||||||
整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
|
|
||||||
"""
|
|
||||||
headers = {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": f"Bearer {API_KEY}"
|
|
||||||
}
|
|
||||||
|
|
||||||
conversation_cnt = len(history) // 2
|
|
||||||
|
|
||||||
messages = [{"role": "system", "content": system_prompt}]
|
|
||||||
if conversation_cnt:
|
|
||||||
for index in range(0, 2*conversation_cnt, 2):
|
|
||||||
what_i_have_asked = {}
|
|
||||||
what_i_have_asked["role"] = "user"
|
|
||||||
what_i_have_asked["content"] = history[index]
|
|
||||||
what_gpt_answer = {}
|
|
||||||
what_gpt_answer["role"] = "assistant"
|
|
||||||
what_gpt_answer["content"] = history[index+1]
|
|
||||||
if what_i_have_asked["content"] != "":
|
|
||||||
if what_gpt_answer["content"] == "": continue
|
|
||||||
if what_gpt_answer["content"] == timeout_bot_msg: continue
|
|
||||||
messages.append(what_i_have_asked)
|
|
||||||
messages.append(what_gpt_answer)
|
|
||||||
else:
|
|
||||||
messages[-1]['content'] = what_gpt_answer['content']
|
|
||||||
|
|
||||||
what_i_ask_now = {}
|
|
||||||
what_i_ask_now["role"] = "user"
|
|
||||||
what_i_ask_now["content"] = inputs
|
|
||||||
messages.append(what_i_ask_now)
|
|
||||||
|
|
||||||
payload = {
|
|
||||||
"model": LLM_MODEL,
|
|
||||||
"messages": messages,
|
|
||||||
"temperature": temperature, # 1.0,
|
|
||||||
"top_p": top_p, # 1.0,
|
|
||||||
"n": 1,
|
|
||||||
"stream": stream,
|
|
||||||
"presence_penalty": 0,
|
|
||||||
"frequency_penalty": 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
print(f" {LLM_MODEL} : {conversation_cnt} : {inputs}")
|
|
||||||
return headers,payload
|
|
||||||
|
|
||||||
|
|
@ -90,12 +90,12 @@ async def run(context, max_token=512):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def predict_tgui(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', stream = True, additional_fn=None):
|
def predict_tgui(inputs, top_p, api_key, temperature, chatbot=[], history=[], system_prompt='', stream = True, additional_fn=None):
|
||||||
"""
|
"""
|
||||||
发送至chatGPT,流式获取输出。
|
发送至chatGPT,流式获取输出。
|
||||||
用于基础的对话功能。
|
用于基础的对话功能。
|
||||||
inputs 是本次问询的输入
|
inputs 是本次问询的输入
|
||||||
top_p, temperature是chatGPT的内部调优参数
|
top_p, api_key, temperature是chatGPT的内部调优参数
|
||||||
history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
|
history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
|
||||||
chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
|
chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
|
||||||
additional_fn代表点击的哪个按钮,按钮见functional.py
|
additional_fn代表点击的哪个按钮,按钮见functional.py
|
||||||
@ -144,7 +144,7 @@ def predict_tgui(inputs, top_p, temperature, chatbot=[], history=[], system_prom
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def predict_tgui_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""):
|
def predict_tgui_no_ui(inputs, top_p, api_key, temperature, history=[], sys_prompt=""):
|
||||||
raw_input = "What I would like to say is the following: " + inputs
|
raw_input = "What I would like to say is the following: " + inputs
|
||||||
prompt = inputs
|
prompt = inputs
|
||||||
tgui_say = ""
|
tgui_say = ""
|
||||||
|
@ -131,11 +131,11 @@
|
|||||||
|
|
||||||
这个程序文件中包含了几个函数,分别是:
|
这个程序文件中包含了几个函数,分别是:
|
||||||
|
|
||||||
1. `解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)`:通过输入文件路径列表对程序文件进行逐文件分析,根据分析结果做出整体功能和构架的概括,并生成包括每个文件功能的markdown表格。
|
1. `解析源代码(file_manifest, project_folder, top_p, api_key, temperature, chatbot, history, systemPromptTxt)`:通过输入文件路径列表对程序文件进行逐文件分析,根据分析结果做出整体功能和构架的概括,并生成包括每个文件功能的markdown表格。
|
||||||
2. `解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对当前文件夹下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
2. `解析项目本身(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对当前文件夹下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
||||||
3. `解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
3. `解析一个Python项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
||||||
4. `解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有头文件进行逐文件分析,并生成markdown表格。
|
4. `解析一个C项目的头文件(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有头文件进行逐文件分析,并生成markdown表格。
|
||||||
5. `解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有.h、.cpp、.c文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
5. `解析一个C项目(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有.h、.cpp、.c文件及其子文件夹进行逐文件分析,并生成markdown表格。
|
||||||
|
|
||||||
程序中还包含了一些辅助函数和变量,如CatchException装饰器函数,report_execption函数、write_results_to_file函数等。在执行过程中还会调用其他模块中的函数,如toolbox模块的函数和predict模块的函数。
|
程序中还包含了一些辅助函数和变量,如CatchException装饰器函数,report_execption函数、write_results_to_file函数等。在执行过程中还会调用其他模块中的函数,如toolbox模块的函数和predict模块的函数。
|
||||||
|
|
||||||
|
28
toolbox.py
28
toolbox.py
@ -16,13 +16,13 @@ def get_reduce_token_percent(text):
|
|||||||
except:
|
except:
|
||||||
return 0.5, '不详'
|
return 0.5, '不详'
|
||||||
|
|
||||||
def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], sys_prompt='', long_connection=True):
|
def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, api_key, temperature, history=[], sys_prompt='', long_connection=True):
|
||||||
"""
|
"""
|
||||||
调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断
|
调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断
|
||||||
i_say: 当前输入
|
i_say: 当前输入
|
||||||
i_say_show_user: 显示到对话界面上的当前输入,例如,输入整个文件时,你绝对不想把文件的内容都糊到对话界面上
|
i_say_show_user: 显示到对话界面上的当前输入,例如,输入整个文件时,你绝对不想把文件的内容都糊到对话界面上
|
||||||
chatbot: 对话界面句柄
|
chatbot: 对话界面句柄
|
||||||
top_p, temperature: gpt参数
|
top_p, api_key, temperature: gpt参数
|
||||||
history: gpt参数 对话历史
|
history: gpt参数 对话历史
|
||||||
sys_prompt: gpt参数 sys_prompt
|
sys_prompt: gpt参数 sys_prompt
|
||||||
long_connection: 是否采用更稳定的连接方式(推荐)
|
long_connection: 是否采用更稳定的连接方式(推荐)
|
||||||
@ -39,9 +39,9 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
if long_connection:
|
if long_connection:
|
||||||
mutable[0] = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
mutable[0] = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
||||||
else:
|
else:
|
||||||
mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, api_key=api_key, temperature=temperature, history=history, sys_prompt=sys_prompt)
|
||||||
break
|
break
|
||||||
except ConnectionAbortedError as token_exceeded_error:
|
except ConnectionAbortedError as token_exceeded_error:
|
||||||
# 尝试计算比例,尽可能多地保留文本
|
# 尝试计算比例,尽可能多地保留文本
|
||||||
@ -108,9 +108,9 @@ def CatchException(f):
|
|||||||
装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
|
装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
|
||||||
"""
|
"""
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
def decorated(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
||||||
try:
|
try:
|
||||||
yield from f(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)
|
yield from f(txt, top_p, api_key, temperature, chatbot, history, systemPromptTxt, WEB_PORT)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
from check_proxy import check_proxy
|
from check_proxy import check_proxy
|
||||||
from toolbox import get_conf
|
from toolbox import get_conf
|
||||||
@ -313,14 +313,14 @@ def read_single_conf_with_lru_cache(arg):
|
|||||||
try: r = getattr(importlib.import_module('config_private'), arg)
|
try: r = getattr(importlib.import_module('config_private'), arg)
|
||||||
except: r = getattr(importlib.import_module('config'), arg)
|
except: r = getattr(importlib.import_module('config'), arg)
|
||||||
# 在读取API_KEY时,检查一下是不是忘了改config
|
# 在读取API_KEY时,检查一下是不是忘了改config
|
||||||
if arg=='API_KEY':
|
# if arg=='API_KEY':
|
||||||
# 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
|
# # 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
|
||||||
API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", r)
|
# API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", r)
|
||||||
if API_MATCH:
|
# if API_MATCH:
|
||||||
print(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功")
|
# print(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功")
|
||||||
else:
|
# else:
|
||||||
assert False, "正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
|
# assert False, "正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
|
||||||
"(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
|
# "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
|
||||||
if arg=='proxies':
|
if arg=='proxies':
|
||||||
if r is None:
|
if r is None:
|
||||||
print('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。')
|
print('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user