Spaces:
Paused
Paused
up
Browse filesFiles changed (20)
- README.md+5-3
- app.py+1-1
- config.py+73-2
- crazy_functional.py+37-5
- crazy_functions/latex_fns/latex_toolbox.py+4-1
- crazy_functions/pdf_fns/parse_pdf.pyAdded+25
- crazy_functions/批量翻译PDF文档_多线程.py+126-24
- crazy_functions/辅助功能.pyAdded+43
- docker-compose.yml+8-3
- docs/GithubAction+ChatGLM+Moss+1
- request_llm/bridge_all.py+31-2
- request_llm/bridge_chatgpt.py+3-5
- request_llm/bridge_chatgpt_website.py-15
- request_llm/bridge_llama2.pyAdded+91
- request_llm/bridge_qianfan.pyAdded+164
- request_llm/local_llm_class.py+2-2
- requirements.txt+2-1
- tests/test_plugins.py+3-3
- tests/test_utils.py+3
- version+2-2
38 unmodified lines39404142434445132 unmodified lines1781791801811821831841 unmodified line186187188189190191192120 unmodified lines31331431531631731838 unmodified lines功能(⭐= 近期新增功能) | 描述--- | ---⭐[接入新模型](https://github.com/binary-husky/gpt_academic/wiki/%E5%A6%82%E4%BD%95%E5%88%87%E6%8D%A2%E6%A8%A1%E5%9E%8B)! | ⭐阿里达摩院[通义千问](https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary),上海AI-Lab[书生](https://github.com/InternLM/InternLM),讯飞[星火](https://xinghuo.xfyun.cn/)一键润色 | 支持一键润色、一键查找论文语法错误一键中英互译 | 一键中英互译一键代码解释 | 显示代码、解释代码、生成代码、给代码加注释132 unmodified lines```P.S. 如果需要依赖Latex的插件功能,请见Wiki。另外,您也可以直接使用docker-compose获取Latex功能(修改docker-compose.yml,保留方案4并删除其他方案)。2. ChatGPT + ChatGLM2 + MOSS(需要熟悉Docker)[](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml)``` sh1 unmodified linedocker-compose up```3. ChatGPT + LLAMA + 盘古 + RWKV(需要熟悉Docker)[](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-jittorllms.yml)``` sh120 unmodified lines### II:版本:- version 3.5(Todo): 使用自然语言调用本项目的所有函数插件(高优先级)- version 3.46: 支持完全脱手操作的实时语音对话- version 3.45: 支持自定义ChatGLM2微调模型- version 3.44: 正式支持Azure,优化界面易用性38 unmodified lines39404142434445132 unmodified lines1781791801811821831841 unmodified line186187188189190191192120 unmodified lines31331431531631731831932038 unmodified lines功能(⭐= 近期新增功能) | 描述--- | ---⭐[接入新模型](https://github.com/binary-husky/gpt_academic/wiki/%E5%A6%82%E4%BD%95%E5%88%87%E6%8D%A2%E6%A8%A1%E5%9E%8B)! | 百度[千帆](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu)与文心一言, [通义千问](https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary),上海AI-Lab[书生](https://github.com/InternLM/InternLM),讯飞[星火](https://xinghuo.xfyun.cn/),[LLaMa2](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)一键润色 | 支持一键润色、一键查找论文语法错误一键中英互译 | 一键中英互译一键代码解释 | 显示代码、解释代码、生成代码、给代码加注释132 unmodified lines```P.S. 如果需要依赖Latex的插件功能,请见Wiki。另外,您也可以直接使用docker-compose获取Latex功能(修改docker-compose.yml,保留方案4并删除其他方案)。2. ChatGPT + ChatGLM2 + MOSS + LLAMA2 + 通义千问(需要熟悉[Nvidia Docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian)运行时)[](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml)``` sh1 unmodified linedocker-compose up```3. ChatGPT + LLAMA + 盘古 + RWKV(需要熟悉[Nvidia Docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian)运行时)[](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-jittorllms.yml)``` sh120 unmodified lines### II:版本:- version 3.5(Todo): 使用自然语言调用本项目的所有函数插件(高优先级)- version 3.49: 支持百度千帆平台和文心一言- version 3.48: 支持阿里达摩院通义千问,上海AI-Lab书生,讯飞星火- version 3.46: 支持完全脱手操作的实时语音对话- version 3.45: 支持自定义ChatGLM2微调模型- version 3.44: 正式支持Azure,优化界面易用性
3 unmodified lines456789103 unmodified linesimport subprocess, syssubprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-stable-fork'])import gradio as grif gr.__version__ not in ['3.28.3','3.32.3']: assert False, "需要特殊依赖,请务必用 pip install -r requirements.txt 指令安装依赖,详情信息见requirements.txt"from request_llm.bridge_all import predictfrom toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到3 unmodified lines456789103 unmodified linesimport subprocess, syssubprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-stable-fork'])import gradio as grif gr.__version__ not in ['3.28.3','3.32.2']: assert False, "需要特殊依赖,请务必用 pip install -r requirements.txt 指令安装依赖,详情信息见requirements.txt"from request_llm.bridge_all import predictfrom toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
10 unmodified lines1112131415161751 unmodified lines6970717273747571 unmodified lines14714814910 unmodified linesAPI_KEY = "此处填API密钥" # 可同时填写多个API-KEY,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey3,azure-apikey4"# [step 2]>> 改为True应用代理,如果直接在海外服务器部署,此处不修改USE_PROXY = Falseif USE_PROXY:"""51 unmodified lines# OpenAI模型选择是(gpt4现在只对申请成功的人开放)LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm"AVAIL_LLM_MODELS = ["newbing-free", "gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo"]# ChatGLM(2) Finetune Model Path (如果使用ChatGLM2微调模型,需要把"chatglmft"加入AVAIL_LLM_MODELS中)ChatGLM_PTUNING_CHECKPOINT = "" # 例如"/home/hmp/ChatGLM2-6B/ptuning/output/6b-pt-128-1e-2/checkpoint-100"71 unmodified lines# 自定义API KEY格式CUSTOM_API_KEY_PATTERN = ""10 unmodified lines111213141516171819202151 unmodified lines7374757677787971 unmodified lines15115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922010 unmodified linesAPI_KEY = "此处填API密钥" # 可同时填写多个API-KEY,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey3,azure-apikey4"# [step 1]>> API_KEY = "sk-123456789xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx123456789"。极少数情况下,还需要填写组织(格式如org-123456789abcdefghijklmno的),请向下翻,找 API_ORG 设置项API_KEY = "此处填API密钥" # 可同时填写多个API-KEY,用英文逗号分割,例如API_KEY = "sk-openaikey1,sk-openaikey2,fkxxxx-api2dkey3,azure-apikey4"# [step 2]>> 改为True应用代理,如果直接在海外服务器部署,此处不修改;如果使用本地或无地域限制的大模型时,此处也不需要修改USE_PROXY = Falseif USE_PROXY:"""51 unmodified lines# OpenAI模型选择是(gpt4现在只对申请成功的人开放)LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm"AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo", "spark", "azure-gpt-3.5"]# ChatGLM(2) Finetune Model Path (如果使用ChatGLM2微调模型,需要把"chatglmft"加入AVAIL_LLM_MODELS中)ChatGLM_PTUNING_CHECKPOINT = "" # 例如"/home/hmp/ChatGLM2-6B/ptuning/output/6b-pt-128-1e-2/checkpoint-100"71 unmodified lines# 自定义API KEY格式CUSTOM_API_KEY_PATTERN = ""# HUGGINGFACE的TOKEN,下载LLAMA时起作用 https://huggingface.co/docs/hub/security-tokensHUGGINGFACE_ACCESS_TOKEN = "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"# GROBID服务器地址(填写多个可以均衡负载),用于高质量地读取PDF文档# 获取方法:复制以下空间https://huggingface.co/spaces/qingxu98/grobid,设为public,然后GROBID_URL = "https://(你的hf用户名如qingxu98)-(你的填写的空间名如grobid).hf.space"GROBID_URLS = ["https://qingxu98-grobid.hf.space","https://qingxu98-grobid2.hf.space","https://qingxu98-grobid3.hf.space","https://shaocongma-grobid.hf.space","https://FBR123-grobid.hf.space",]"""在线大模型配置关联关系示意图│├── "gpt-3.5-turbo" 等openai模型│ ├── API_KEY│ ├── CUSTOM_API_KEY_PATTERN(不常用)│ ├── API_ORG(不常用)│ └── API_URL_REDIRECT(不常用)│├── "azure-gpt-3.5" 等azure模型│ ├── API_KEY│ ├── AZURE_ENDPOINT│ ├── AZURE_API_KEY│ ├── AZURE_ENGINE│ └── API_URL_REDIRECT│├── "spark" 星火认知大模型│ ├── XFYUN_APPID│ ├── XFYUN_API_SECRET│ └── XFYUN_API_KEY│├── "claude-1-100k" 等claude模型│ └── ANTHROPIC_API_KEY│├── "stack-claude"│ ├── SLACK_CLAUDE_BOT_ID│ └── SLACK_CLAUDE_USER_TOKEN│├── "qianfan" 百度千帆大模型库│ ├── BAIDU_CLOUD_QIANFAN_MODEL│ ├── BAIDU_CLOUD_API_KEY│ └── BAIDU_CLOUD_SECRET_KEY│├── "newbing" Newbing接口不再稳定,不推荐使用├── NEWBING_STYLE└── NEWBING_COOKIES插件在线服务配置依赖关系示意图│├── 语音功能│ ├── ENABLE_AUDIO│ ├── ALIYUN_TOKEN│ ├── ALIYUN_APPKEY│ ├── ALIYUN_ACCESSKEY│ └── ALIYUN_SECRET│├── PDF文档精准解析│ └── GROBID_URLS"""
23 unmodified lines24252627282910 unmodified lines40414243444546281 unmodified lines3283293303313323333347 unmodified lines3423433443453463473483 unmodified lines3523533543553563578 unmodified lines3663673683693703713724 unmodified lines37737837938038138238323 unmodified linesfrom crazy_functions.对话历史存档 import 对话历史存档from crazy_functions.对话历史存档 import 载入对话历史存档from crazy_functions.对话历史存档 import 删除所有本地对话历史记录from crazy_functions.批量Markdown翻译 import Markdown英译中function_plugins = {10 unmodified lines"AsButton":False,"Function": HotReload(删除所有本地对话历史记录)},"[测试功能] 解析Jupyter Notebook文件": {"Color": "stop","AsButton":False,"Function": HotReload(解析ipynb文件),281 unmodified linestry:from crazy_functions.Langchain知识库 import 知识库问答function_plugins.update({"[功能尚不稳定] 构建知识库(请先上传文件素材)": {"Color": "stop","AsButton": False,"AdvancedArgs": True,7 unmodified linestry:from crazy_functions.Langchain知识库 import 读取知识库作答function_plugins.update({"[功能尚不稳定] 知识库问答": {"Color": "stop","AsButton": False,"AdvancedArgs": True,3 unmodified lines})except:print('Load function plugin failed')try:from crazy_functions.Latex输出PDF结果 import Latex英文纠错加PDF对比8 unmodified lines})from crazy_functions.Latex输出PDF结果 import Latex翻译中文并重新编译PDFfunction_plugins.update({"Arixv翻译(输入arxivID)[需Latex]": {"Color": "stop","AsButton": False,"AdvancedArgs": True,4 unmodified lines}})function_plugins.update({"本地论文翻译(上传Latex压缩包)[需Latex]": {"Color": "stop","AsButton": False,"AdvancedArgs": True,23 unmodified lines2425262728293010 unmodified lines414243444546474849505152281 unmodified lines3343353363373383393407 unmodified lines3483493503513523533543 unmodified lines3583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883898 unmodified lines3983994004014024034044 unmodified lines40941041141241341441523 unmodified linesfrom crazy_functions.对话历史存档 import 对话历史存档from crazy_functions.对话历史存档 import 载入对话历史存档from crazy_functions.对话历史存档 import 删除所有本地对话历史记录from crazy_functions.辅助功能 import 清除缓存from crazy_functions.批量Markdown翻译 import Markdown英译中function_plugins = {10 unmodified lines"AsButton":False,"Function": HotReload(删除所有本地对话历史记录)},"清除所有缓存文件(请谨慎操作)": {"Color": "stop","AsButton": False, # 加入下拉菜单中"Function": HotReload(清除缓存)},"解析Jupyter Notebook文件": {"Color": "stop","AsButton":False,"Function": HotReload(解析ipynb文件),281 unmodified linestry:from crazy_functions.Langchain知识库 import 知识库问答function_plugins.update({"构建知识库(请先上传文件素材)": {"Color": "stop","AsButton": False,"AdvancedArgs": True,7 unmodified linestry:from crazy_functions.Langchain知识库 import 读取知识库作答function_plugins.update({"知识库问答": {"Color": "stop","AsButton": False,"AdvancedArgs": True,3 unmodified lines})except:print('Load function plugin failed')try:from crazy_functions.交互功能函数模板 import 交互功能模板函数function_plugins.update({"交互功能模板函数": {"Color": "stop","AsButton": False,"Function": HotReload(交互功能模板函数)}})except:print('Load function plugin failed')# try:# from crazy_functions.chatglm微调工具 import 微调数据集生成# function_plugins.update({# "黑盒模型学习: 微调数据集生成 (先上传数据集)": {# "Color": "stop",# "AsButton": False,# "AdvancedArgs": True,# "ArgsReminder": "针对数据集输入(如 绿帽子*深蓝色衬衫*黑色运动裤)给出指令,例如您可以将以下命令复制到下方: --llm_to_learn=azure-gpt-3.5 --prompt_prefix='根据下面的服装类型提示,想象一个穿着者,对这个人外貌、身处的环境、内心世界、过去经历进行描写。要求:100字以内,用第二人称。' --system_prompt=''",# "Function": HotReload(微调数据集生成)# }# })# except:# print('Load function plugin failed')try:from crazy_functions.Latex输出PDF结果 import Latex英文纠错加PDF对比8 unmodified lines})from crazy_functions.Latex输出PDF结果 import Latex翻译中文并重新编译PDFfunction_plugins.update({"Arixv论文精细翻译(输入arxivID)[需Latex]": {"Color": "stop","AsButton": False,"AdvancedArgs": True,4 unmodified lines}})function_plugins.update({"本地Latex论文精细翻译(上传Latex项目)[需Latex]": {"Color": "stop","AsButton": False,"AdvancedArgs": True,
280 unmodified lines281282283284285286287288289280 unmodified linesdef find_tex_file_ignore_case(fp):dir_name = os.path.dirname(fp)base_name = os.path.basename(fp)if not base_name.endswith('.tex'): base_name+='.tex'if os.path.exists(pj(dir_name, base_name)): return pj(dir_name, base_name)# go case in-sensitiveimport globfor f in glob.glob(dir_name+'/*.tex'):base_name_s = os.path.basename(fp)280 unmodified lines281282283284285286287288289290291292280 unmodified linesdef find_tex_file_ignore_case(fp):dir_name = os.path.dirname(fp)base_name = os.path.basename(fp)# 如果输入的文件路径是正确的if os.path.exists(pj(dir_name, base_name)): return pj(dir_name, base_name)# 如果不正确,试着加上.tex后缀试试if not base_name.endswith('.tex'): base_name+='.tex'if os.path.exists(pj(dir_name, base_name)): return pj(dir_name, base_name)# 如果还找不到,解除大小写限制,再试一次import globfor f in glob.glob(dir_name+'/*.tex'):base_name_s = os.path.basename(fp)
12345678910111213141516171819202122232425import requestsimport randomfrom functools import lru_cacheclass GROBID_OFFLINE_EXCEPTION(Exception): pass
def get_avail_grobid_url(): from toolbox import get_conf GROBID_URLS, = get_conf('GROBID_URLS') if len(GROBID_URLS) == 0: return None try: _grobid_url = random.choice(GROBID_URLS) # 随机负载均衡 if _grobid_url.endswith('/'): _grobid_url = _grobid_url.rstrip('/') res = requests.get(_grobid_url+'/api/isalive') if res.text=='true': return _grobid_url else: return None except: return None
@lru_cache(maxsize=32)def parse_pdf(pdf_path, grobid_url): import scipdf # pip install scipdf_parser if grobid_url.endswith('/'): grobid_url = grobid_url.rstrip('/') article_dict = scipdf.parse_pdf_to_dict(pdf_path, grobid_url=grobid_url) return article_dict
12345678910111213141514 unmodified lines30313233343536373839404142434445464748493 unmodified lines5354555657585960616263646566676869707172737465 unmodified lines14014114214314414514614754 unmodified lines202203204205206207from toolbox import CatchException, report_execption, write_results_to_filefrom toolbox import update_ui, promote_file_to_downloadzonefrom .crazy_utils import request_gpt_model_in_new_thread_with_ui_alivefrom .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiencyfrom .crazy_utils import read_and_clean_pdf_textfrom colorful import *@CatchExceptiondef 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt, web_port):import globimport os# 基本信息:功能、贡献者chatbot.append(["函数插件功能?",14 unmodified lines# 清空历史,以免输入溢出history = []# 检测输入参数,如没有给定输入参数,直接退出if os.path.exists(txt):project_folder = txtelse:if txt == "":txt = '空空如也的输入栏'report_execption(chatbot, history,a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")yield from update_ui(chatbot=chatbot, history=history) # 刷新界面return# 搜索需要处理的文件清单file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.pdf', recursive=True)]# 如果没找到任何文件if len(file_manifest) == 0:3 unmodified linesreturn# 开始正式执行任务yield from 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt)def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, sys_prompt):import osimport copyimport tiktokenTOKEN_LIMIT_PER_FRAGMENT = 1280generated_conclusion_files = []generated_html_files = []for index, fp in enumerate(file_manifest):# 读取PDF文件file_content, page_one = read_and_clean_pdf_text(fp)file_content = file_content.encode('utf-8', 'ignore').decode() # avoid reading non-utf8 charspage_one = str(page_one).encode('utf-8', 'ignore').decode() # avoid reading non-utf8 chars# 递归地切割PDF文件from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdffrom request_llm.bridge_all import model_info65 unmodified linestrans = kch.add_row(a=orig, b=trans)create_report_file_name = f"{os.path.basename(fp)}.trans.html"ch.save_file(create_report_file_name)generated_html_files.append(f'./gpt_log/{create_report_file_name}')except:from toolbox import trimmed_format_excprint('writing html result failed:', trimmed_format_exc())54 unmodified linesdef save_file(self, file_name):with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f:f.write(self.html_string.encode('utf-8', 'ignore').decode())1234567891011121314151617181914 unmodified lines34353637383940414243443 unmodified lines4849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617765 unmodified lines24324424524624724824954 unmodified lines304305306307308309from toolbox import CatchException, report_execption, write_results_to_filefrom toolbox import update_ui, promote_file_to_downloadzone, update_ui_lastest_msg, disable_auto_promotionfrom toolbox import write_history_to_file, get_log_folderfrom .crazy_utils import request_gpt_model_in_new_thread_with_ui_alivefrom .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiencyfrom .crazy_utils import read_and_clean_pdf_textfrom .pdf_fns.parse_pdf import parse_pdf, get_avail_grobid_urlfrom colorful import *import globimport osimport math@CatchExceptiondef 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):disable_auto_promotion(chatbot)# 基本信息:功能、贡献者chatbot.append(["函数插件功能?",14 unmodified lines# 清空历史,以免输入溢出history = []from .crazy_utils import get_files_from_everythingsuccess, file_manifest, project_folder = get_files_from_everything(txt, type='.pdf')# 检测输入参数,如没有给定输入参数,直接退出if not success:if txt == "": txt = '空空如也的输入栏'# 如果没找到任何文件if len(file_manifest) == 0:3 unmodified linesreturn# 开始正式执行任务grobid_url = get_avail_grobid_url()if grobid_url is not None:yield from 解析PDF_基于GROBID(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, grobid_url)else:yield from update_ui_lastest_msg("GROBID服务不可用,请检查config中的GROBID_URL。作为替代,现在将执行效果稍差的旧版代码。", chatbot, history, delay=3)yield from 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)def 解析PDF_基于GROBID(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, grobid_url):import copyimport tiktokenTOKEN_LIMIT_PER_FRAGMENT = 1280generated_conclusion_files = []generated_html_files = []DST_LANG = "中文"for index, fp in enumerate(file_manifest):chatbot.append(["当前进度:", f"正在连接GROBID服务,请稍候: {grobid_url}\n如果等待时间过长,请修改config中的GROBID_URL,可修改成本地GROBID服务。"]); yield from update_ui(chatbot=chatbot, history=history) # 刷新界面article_dict = parse_pdf(fp, grobid_url)print(article_dict)prompt = "以下是一篇学术论文的基本信息:\n"# titletitle = article_dict.get('title', '无法获取 title'); prompt += f'title:{title}\n\n'# authorsauthors = article_dict.get('authors', '无法获取 authors'); prompt += f'authors:{authors}\n\n'# abstractabstract = article_dict.get('abstract', '无法获取 abstract'); prompt += f'abstract:{abstract}\n\n'# commandprompt += f"请将题目和摘要翻译为{DST_LANG}。"meta = [f'# Title:\n\n', title, f'# Abstract:\n\n', abstract ]# 单线,获取文章meta信息paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(inputs=prompt,inputs_show_user=prompt,llm_kwargs=llm_kwargs,chatbot=chatbot, history=[],sys_prompt="You are an academic paper reader。",)# 多线,翻译inputs_array = []inputs_show_user_array = []# get_token_numfrom request_llm.bridge_all import model_infoenc = model_info[llm_kwargs['llm_model']]['tokenizer']def get_token_num(txt): return len(enc.encode(txt, disallowed_special=()))from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdfdef break_down(txt):raw_token_num = get_token_num(txt)if raw_token_num <= TOKEN_LIMIT_PER_FRAGMENT:return [txt]else:# raw_token_num > TOKEN_LIMIT_PER_FRAGMENT# find a smooth token limit to achieve even seperationcount = int(math.ceil(raw_token_num / TOKEN_LIMIT_PER_FRAGMENT))token_limit_smooth = raw_token_num // count + countreturn breakdown_txt_to_satisfy_token_limit_for_pdf(txt, get_token_fn=get_token_num, limit=token_limit_smooth)for section in article_dict.get('sections'):if len(section['text']) == 0: continuesection_frags = break_down(section['text'])for i, fragment in enumerate(section_frags):heading = section['heading']if len(section_frags) > 1: heading += f'Part-{i+1}'inputs_array.append(f"你需要翻译{heading}章节,内容如下: \n\n{fragment}")inputs_show_user_array.append(f"# {heading}\n\n{fragment}")gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(inputs_array=inputs_array,inputs_show_user_array=inputs_show_user_array,llm_kwargs=llm_kwargs,chatbot=chatbot,history_array=[meta for _ in inputs_array],sys_prompt_array=["请你作为一个学术翻译,负责把学术论文准确翻译成中文。注意文章中的每一句话都要翻译。" for _ in inputs_array],)res_path = write_history_to_file(meta + ["# Meta Translation" , paper_meta_info] + gpt_response_collection, file_basename=None, file_fullname=None)promote_file_to_downloadzone(res_path, rename_file=os.path.basename(fp)+'.md', chatbot=chatbot)generated_conclusion_files.append(res_path)ch = construct_html()orig = ""trans = ""gpt_response_collection_html = copy.deepcopy(gpt_response_collection)for i,k in enumerate(gpt_response_collection_html):if i%2==0:gpt_response_collection_html[i] = inputs_show_user_array[i//2]else:gpt_response_collection_html[i] = gpt_response_collection_html[i]final = ["", "", "一、论文概况", "", "Abstract", paper_meta_info, "二、论文翻译", ""]final.extend(gpt_response_collection_html)for i, k in enumerate(final):if i%2==0:orig = kif i%2==1:trans = kch.add_row(a=orig, b=trans)create_report_file_name = f"{os.path.basename(fp)}.trans.html"html_file = ch.save_file(create_report_file_name)generated_html_files.append(html_file)promote_file_to_downloadzone(html_file, rename_file=os.path.basename(html_file), chatbot=chatbot)chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))yield from update_ui(chatbot=chatbot, history=history) # 刷新界面def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):import copyTOKEN_LIMIT_PER_FRAGMENT = 1280generated_conclusion_files = []generated_html_files = []for index, fp in enumerate(file_manifest):# 读取PDF文件file_content, page_one = read_and_clean_pdf_text(fp)file_content = file_content.encode('utf-8', 'ignore').decode() # avoid reading non-utf8 charspage_one = str(page_one).encode('utf-8', 'ignore').decode() # avoid reading non-utf8 chars# 递归地切割PDF文件from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdffrom request_llm.bridge_all import model_info65 unmodified linestrans = kch.add_row(a=orig, b=trans)create_report_file_name = f"{os.path.basename(fp)}.trans.html"generated_html_files.append(ch.save_file(create_report_file_name))except:from toolbox import trimmed_format_excprint('writing html result failed:', trimmed_format_exc())54 unmodified linesdef save_file(self, file_name):with open(os.path.join(get_log_folder(), file_name), 'w', encoding='utf8') as f:f.write(self.html_string.encode('utf-8', 'ignore').decode())return os.path.join(get_log_folder(), file_name)
12345678910111213141516171819202122232425262728293031323334353637383940414243# encoding: utf-8# @Time : 2023/4/19# @Author : Spike# @Descr :from toolbox import update_uifrom toolbox import CatchException, report_execption, write_results_to_file, get_log_folderfrom crazy_functions.crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
@CatchExceptiondef 猜你想问(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): if txt: show_say = txt prompt = txt+'\n回答完问题后,再列出用户可能提出的三个问题。' else: prompt = history[-1]+"\n分析上述回答,再列出用户可能提出的三个问题。" show_say = '分析上述回答,再列出用户可能提出的三个问题。' gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=prompt, inputs_show_user=show_say, llm_kwargs=llm_kwargs, chatbot=chatbot, history=history, sys_prompt=system_prompt ) chatbot[-1] = (show_say, gpt_say) history.extend([show_say, gpt_say]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
@CatchExceptiondef 清除缓存(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): chatbot.append(['清除本地缓存数据', '执行中. 删除 gpt_log & private_upload']) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
import shutil, os gpt_log_dir = os.path.join(os.path.dirname(__file__), '..', 'gpt_log') private_upload_dir = os.path.join(os.path.dirname(__file__), '..', 'private_upload') shutil.rmtree(gpt_log_dir, ignore_errors=True) shutil.rmtree(private_upload_dir, ignore_errors=True)
chatbot.append(['清除本地缓存数据', '执行完成']) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面15 unmodified lines1617181920216 unmodified lines282930313233341 unmodified line363738394041424344454610 unmodified lines57585960616215 unmodified linesAVAIL_LLM_MODELS: ' ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "newbing"] 'WEB_PORT: ' 22303 'ADD_WAIFU: ' True '# DEFAULT_WORKER_NUM: ' 10 '# AUTHENTICATION: ' [("username", "passwd"), ("username2", "passwd2")] '6 unmodified lines### ===================================================### 【方案二】 如果需要运行ChatGLM本地模型### ===================================================version: '3'services:1 unmodified lineimage: ghcr.io/binary-husky/gpt_academic_chatglm_moss:master # (Auto Built by Dockerfile: docs/Dockerfile+ChatGLM)environment:# 请查阅 `config.py` 以查看所有的配置信息API_KEY: ' sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,fkxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 'USE_PROXY: ' True 'proxies: ' { "http": "socks5h://localhost:10880", "https": "socks5h://localhost:10880", } 'LLM_MODEL: ' gpt-3.5-turbo 'AVAIL_LLM_MODELS: ' ["chatglm", "moss", "gpt-3.5-turbo", "gpt-4", "newbing"] 'LOCAL_MODEL_DEVICE: ' cuda 'DEFAULT_WORKER_NUM: ' 10 'WEB_PORT: ' 12303 '10 unmodified linescommand: >bash -c "python3 -u main.py"### ===================================================### 【方案三】 如果需要运行ChatGPT + LLAMA + 盘古 + RWKV本地模型### ===================================================15 unmodified lines161718192021226 unmodified lines293031323334351 unmodified line373839404142434445464710 unmodified lines5859606162636465666715 unmodified linesAVAIL_LLM_MODELS: ' ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "newbing"] 'WEB_PORT: ' 22303 'ADD_WAIFU: ' True '# THEME: ' Chuanhu-Small-and-Beautiful '# DEFAULT_WORKER_NUM: ' 10 '# AUTHENTICATION: ' [("username", "passwd"), ("username2", "passwd2")] '6 unmodified lines### ===================================================### 【方案二】 如果需要运行ChatGLM + Qwen + MOSS等本地模型### ===================================================version: '3'services:1 unmodified lineimage: ghcr.io/binary-husky/gpt_academic_chatglm_moss:master # (Auto Built by Dockerfile: docs/Dockerfile+ChatGLM)environment:# 请查阅 `config.py` 以查看所有的配置信息API_KEY: ' sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 'USE_PROXY: ' True 'proxies: ' { "http": "socks5h://localhost:10880", "https": "socks5h://localhost:10880", } 'LLM_MODEL: ' gpt-3.5-turbo 'AVAIL_LLM_MODELS: ' ["chatglm", "qwen", "moss", "gpt-3.5-turbo", "gpt-4", "newbing"] 'LOCAL_MODEL_DEVICE: ' cuda 'DEFAULT_WORKER_NUM: ' 10 'WEB_PORT: ' 12303 '10 unmodified linescommand: >bash -c "python3 -u main.py"# P.S. 通过对 command 进行微调,可以便捷地安装额外的依赖# command: ># bash -c "pip install -r request_llm/requirements_qwen.txt && python3 -u main.py"### ===================================================### 【方案三】 如果需要运行ChatGPT + LLAMA + 盘古 + RWKV本地模型### ===================================================
17 unmodified lines18192021222317 unmodified linesRUN git clone https://github.com/OpenLMLab/MOSS.git request_llm/mossRUN python3 -m pip install -r requirements.txtRUN python3 -m pip install -r request_llm/requirements_moss.txtRUN python3 -m pip install -r request_llm/requirements_chatglm.txtRUN python3 -m pip install -r request_llm/requirements_newbing.txt17 unmodified lines1819202122232417 unmodified linesRUN git clone https://github.com/OpenLMLab/MOSS.git request_llm/mossRUN python3 -m pip install -r requirements.txtRUN python3 -m pip install -r request_llm/requirements_moss.txtRUN python3 -m pip install -r request_llm/requirements_qwen.txtRUN python3 -m pip install -r request_llm/requirements_chatglm.txtRUN python3 -m pip install -r request_llm/requirements_newbing.txt
18 unmodified lines192021222324140 unmodified lines165166167168169170171189 unmodified lines36136236336436536636717 unmodified lines38538638738838939018 unmodified linesfrom .bridge_chatglm import predict_no_ui_long_connection as chatglm_nouifrom .bridge_chatglm import predict as chatglm_uicolors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044']class LazyloadTiktoken(object):140 unmodified lines"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,},}# -=-=-=-=-=-=- 以下部分是新加入的模型,可能附带额外依赖 -=-=-=-=-=-=-189 unmodified lines"chatgpt_website": {"fn_with_ui": chatgpt_website_ui,"fn_without_ui": chatgpt_website_noui,"endpoint": None,"max_token": 4096,"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,17 unmodified lines})except:print(trimmed_format_exc())18 unmodified lines192021222324252627282930140 unmodified lines171172173174175176177178179180181182183184189 unmodified lines37437537637737837938017 unmodified lines39839940040140240340440540640740840941041141241341441541641741841918 unmodified linesfrom .bridge_chatglm import predict_no_ui_long_connection as chatglm_nouifrom .bridge_chatglm import predict as chatglm_uifrom .bridge_chatglm import predict_no_ui_long_connection as chatglm_nouifrom .bridge_chatglm import predict as chatglm_uifrom .bridge_qianfan import predict_no_ui_long_connection as qianfan_nouifrom .bridge_qianfan import predict as qianfan_uicolors = ['#FF00FF', '#00FFFF', '#FF0000', '#990099', '#009999', '#990044']class LazyloadTiktoken(object):140 unmodified lines"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,},"qianfan": {"fn_with_ui": qianfan_ui,"fn_without_ui": qianfan_noui,"endpoint": None,"max_token": 2000,"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,},}# -=-=-=-=-=-=- 以下部分是新加入的模型,可能附带额外依赖 -=-=-=-=-=-=-189 unmodified lines"chatgpt_website": {"fn_with_ui": chatgpt_website_ui,"fn_without_ui": chatgpt_website_noui,"endpoint": openai_endpoint,"max_token": 4096,"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,17 unmodified lines})except:print(trimmed_format_exc())if "llama2" in AVAIL_LLM_MODELS: # llama2try:from .bridge_llama2 import predict_no_ui_long_connection as llama2_nouifrom .bridge_llama2 import predict as llama2_uimodel_info.update({"llama2": {"fn_with_ui": llama2_ui,"fn_without_ui": llama2_noui,"endpoint": None,"max_token": 4096,"tokenizer": tokenizer_gpt35,"token_cnt": get_token_num_gpt35,}})except:print(trimmed_format_exc())
176 unmodified lines1771781791801811821831841851861871881891901 unmodified line19219319419519619719817 unmodified lines216217218219220221222176 unmodified linesyield from update_ui(chatbot=chatbot, history=history, msg="非Openai官方接口返回了错误:" + chunk.decode()) # 刷新界面return# print(chunk.decode()[6:])if is_head_of_the_stream and (r'"object":"error"' not in chunk.decode()):# 数据流的第一帧不携带contentis_head_of_the_stream = False; continueif chunk:try:chunk_decoded = chunk.decode()# 前者是API2D的结束条件,后者是OPENAI的结束条件if ('data: [DONE]' in chunk_decoded) or (len(json.loads(chunk_decoded[6:])['choices'][0]["delta"]) == 0):# 判定为数据流的结束,gpt_replying_buffer也写完了1 unmodified linebreak# 处理数据流的主体chunkjson = json.loads(chunk_decoded[6:])status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"# 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk_decoded[6:])['choices'][0]["delta"]["content"]history[-1] = gpt_replying_buffer17 unmodified lineshistory = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'],max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])) # history至少释放二分之一chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长, 或历史数据过长. 历史缓存数据已部分释放, 您可以请再次尝试. (若再次失败则更可能是因为输入过长.)")# history = [] # 清除历史elif "does not exist" in error_msg:chatbot[-1] = (chatbot[-1][0], f"[Local Message] Model {llm_kwargs['llm_model']} does not exist. 模型不存在, 或者您没有获得体验资格.")elif "Incorrect API key" in error_msg:176 unmodified lines1771781791801811821831841851861871881891 unmodified line19119219319419519619717 unmodified lines215216217218219220176 unmodified linesyield from update_ui(chatbot=chatbot, history=history, msg="非Openai官方接口返回了错误:" + chunk.decode()) # 刷新界面returnchunk_decoded = chunk.decode()if is_head_of_the_stream and (r'"object":"error"' not in chunk_decoded) and (r"choices" not in chunk_decoded):# 数据流的第一帧不携带contentis_head_of_the_stream = False; continueif chunk:try:# 前者是API2D的结束条件,后者是OPENAI的结束条件if ('data: [DONE]' in chunk_decoded) or (len(json.loads(chunk_decoded[6:])['choices'][0]["delta"]) == 0):# 判定为数据流的结束,gpt_replying_buffer也写完了1 unmodified linebreak# 处理数据流的主体chunkjson = json.loads(chunk_decoded[6:])status_text = f"finish_reason: {chunkjson['choices'][0].get('finish_reason', 'null')}"# 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk_decoded[6:])['choices'][0]["delta"]["content"]history[-1] = gpt_replying_buffer17 unmodified lineshistory = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'],max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])) # history至少释放二分之一chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长, 或历史数据过长. 历史缓存数据已部分释放, 您可以请再次尝试. (若再次失败则更可能是因为输入过长.)")elif "does not exist" in error_msg:chatbot[-1] = (chatbot[-1][0], f"[Local Message] Model {llm_kwargs['llm_model']} does not exist. 模型不存在, 或者您没有获得体验资格.")elif "Incorrect API key" in error_msg:
117 unmodified lines118119120121122123124125126127128129130131132133111 unmodified lines245246247248249250251252253254255256257258117 unmodified lineschatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容additional_fn代表点击的哪个按钮,按钮见functional.py"""if is_any_api_key(inputs):chatbot._cookies['api_key'] = inputschatbot.append(("输入已识别为openai的api_key", what_keys(inputs)))yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") # 刷新界面returnelif not is_any_api_key(chatbot._cookies['api_key']):chatbot.append((inputs, "缺少api_key。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。"))yield from update_ui(chatbot=chatbot, history=history, msg="缺少api_key") # 刷新界面returnif additional_fn is not None:from core_functional import handle_core_functionalityinputs, history = handle_core_functionality(additional_fn, inputs, history, chatbot)111 unmodified linesif not is_any_api_key(llm_kwargs['api_key']):raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。")api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])headers = {"Content-Type": "application/json","Authorization": f"Bearer {api_key}"}if API_ORG.startswith('org-'): headers.update({"OpenAI-Organization": API_ORG})if llm_kwargs['llm_model'].startswith('azure-'): headers.update({"api-key": api_key})conversation_cnt = len(history) // 2117 unmodified lines118119120121122123111 unmodified lines235236237238239240241242243117 unmodified lineschatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容additional_fn代表点击的哪个按钮,按钮见functional.py"""if additional_fn is not None:from core_functional import handle_core_functionalityinputs, history = handle_core_functionality(additional_fn, inputs, history, chatbot)111 unmodified linesif not is_any_api_key(llm_kwargs['api_key']):raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。")headers = {"Content-Type": "application/json",}conversation_cnt = len(history) // 2
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091model_name = "LLaMA"cmd_to_install = "`pip install -r request_llm/requirements_chatglm.txt`"
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom toolbox import update_ui, get_conf, ProxyNetworkActivatefrom multiprocessing import Process, Pipefrom .local_llm_class import LocalLLMHandle, get_local_llm_predict_fns, SingletonLocalLLMfrom threading import Thread
# ------------------------------------------------------------------------------------------------------------------------# 🔌💻 Local Model# ------------------------------------------------------------------------------------------------------------------------@SingletonLocalLLMclass GetONNXGLMHandle(LocalLLMHandle):
def load_model_info(self): # 🏃♂️🏃♂️🏃♂️ 子进程执行 self.model_name = model_name self.cmd_to_install = cmd_to_install
def load_model_and_tokenizer(self): # 🏃♂️🏃♂️🏃♂️ 子进程执行 import os, glob import os import platform huggingface_token, device = get_conf('HUGGINGFACE_ACCESS_TOKEN', 'LOCAL_MODEL_DEVICE') assert len(huggingface_token) != 0, "没有填写 HUGGINGFACE_ACCESS_TOKEN" with open(os.path.expanduser('~/.cache/huggingface/token'), 'w') as f: f.write(huggingface_token) model_id = 'meta-llama/Llama-2-7b-chat-hf' with ProxyNetworkActivate(): self._tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=huggingface_token) # use fp16 model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=huggingface_token).eval() if device.startswith('cuda'): model = model.half().to(device) self._model = model
return self._model, self._tokenizer
def llm_stream_generator(self, **kwargs): # 🏃♂️🏃♂️🏃♂️ 子进程执行 def adaptor(kwargs): query = kwargs['query'] max_length = kwargs['max_length'] top_p = kwargs['top_p'] temperature = kwargs['temperature'] history = kwargs['history'] console_slience = kwargs.get('console_slience', True) return query, max_length, top_p, temperature, history, console_slience def convert_messages_to_prompt(query, history): prompt = "" for a, b in history: prompt += f"\n[INST]{a}[/INST]" prompt += "\n{b}" + b prompt += f"\n[INST]{query}[/INST]" return prompt query, max_length, top_p, temperature, history, console_slience = adaptor(kwargs) prompt = convert_messages_to_prompt(query, history) # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=- # code from transformers.llama streamer = TextIteratorStreamer(self._tokenizer) # Run the generation in a separate thread, so that we can fetch the generated text in a non-blocking way. inputs = self._tokenizer([prompt], return_tensors="pt") prompt_tk_back = self._tokenizer.batch_decode(inputs['input_ids'])[0]
generation_kwargs = dict(inputs.to(self._model.device), streamer=streamer, max_new_tokens=max_length) thread = Thread(target=self._model.generate, kwargs=generation_kwargs) thread.start() generated_text = "" for new_text in streamer: generated_text += new_text if not console_slience: print(new_text, end='') yield generated_text.lstrip(prompt_tk_back).rstrip("</s>") if not console_slience: print() # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=- def try_to_import_special_deps(self, **kwargs): # import something that will raise error if the user does not install requirement_*.txt # 🏃♂️🏃♂️🏃♂️ 主进程执行 import importlib importlib.import_module('transformers')
# ------------------------------------------------------------------------------------------------------------------------# 🔌💻 GPT-Academic Interface# ------------------------------------------------------------------------------------------------------------------------predict_no_ui_long_connection, predict = get_local_llm_predict_fns(GetONNXGLMHandle, model_name)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
import time, requests, jsonfrom multiprocessing import Process, Pipefrom functools import wrapsfrom datetime import datetime, timedeltafrom toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history, trimmed_format_exc, get_conf
model_name = '千帆大模型平台'timeout_bot_msg = '[Local Message] Request timeout. Network error.'
def cache_decorator(timeout): cache = {} def decorator(func): @wraps(func) def wrapper(*args, **kwargs): key = (func.__name__, args, frozenset(kwargs.items())) # Check if result is already cached and not expired if key in cache: result, timestamp = cache[key] if datetime.now() - timestamp < timedelta(seconds=timeout): return result
# Call the function and cache the result result = func(*args, **kwargs) cache[key] = (result, datetime.now()) return result return wrapper return decorator
@cache_decorator(timeout=3600)def get_access_token(): """ 使用 AK,SK 生成鉴权签名(Access Token) :return: access_token,或是None(如果错误) """ # if (access_token_cache is None) or (time.time() - last_access_token_obtain_time > 3600): BAIDU_CLOUD_API_KEY, BAIDU_CLOUD_SECRET_KEY = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')
if len(BAIDU_CLOUD_SECRET_KEY) == 0: raise RuntimeError("没有配置BAIDU_CLOUD_SECRET_KEY") if len(BAIDU_CLOUD_API_KEY) == 0: raise RuntimeError("没有配置BAIDU_CLOUD_API_KEY")
url = "https://aip.baidubce.com/oauth/2.0/token" params = {"grant_type": "client_credentials", "client_id": BAIDU_CLOUD_API_KEY, "client_secret": BAIDU_CLOUD_SECRET_KEY} access_token_cache = str(requests.post(url, params=params).json().get("access_token")) return access_token_cache # else: # return access_token_cache
def generate_message_payload(inputs, llm_kwargs, history, system_prompt): conversation_cnt = len(history) // 2 messages = [{"role": "user", "content": system_prompt}] messages.append({"role": "assistant", "content": 'Certainly!'}) 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) return messages
def generate_from_baidu_qianfan(inputs, llm_kwargs, history, system_prompt): BAIDU_CLOUD_QIANFAN_MODEL, = get_conf('BAIDU_CLOUD_QIANFAN_MODEL')
url_lib = { "ERNIE-Bot": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions" , "ERNIE-Bot-turbo": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant" , "BLOOMZ-7B": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/bloomz_7b1",
"Llama-2-70B-Chat": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_70b", "Llama-2-13B-Chat": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_13b", "Llama-2-7B-Chat": "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_7b", }
url = url_lib[BAIDU_CLOUD_QIANFAN_MODEL]
url += "?access_token=" + get_access_token()
payload = json.dumps({ "messages": generate_message_payload(inputs, llm_kwargs, history, system_prompt), "stream": True }) headers = { 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload, stream=True) buffer = "" for line in response.iter_lines(): if len(line) == 0: continue try: dec = line.decode().lstrip('data:') dec = json.loads(dec) incoming = dec['result'] buffer += incoming yield buffer except: if ('error_code' in dec) and ("max length" in dec['error_msg']): raise ConnectionAbortedError(dec['error_msg']) # 上下文太长导致 token 溢出 elif ('error_code' in dec): raise RuntimeError(dec['error_msg'])
def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=[], console_slience=False): """ ⭐多线程方法 函数的说明请见 request_llm/bridge_all.py """ watch_dog_patience = 5 response = ""
for response in generate_from_baidu_qianfan(inputs, llm_kwargs, history, sys_prompt): if len(observe_window) >= 1: observe_window[0] = response if len(observe_window) >= 2: if (time.time()-observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。") return response
def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): """ ⭐单线程方法 函数的说明请见 request_llm/bridge_all.py """ chatbot.append((inputs, ""))
if additional_fn is not None: from core_functional import handle_core_functionality inputs, history = handle_core_functionality(additional_fn, inputs, history, chatbot)
yield from update_ui(chatbot=chatbot, history=history) # 开始接收回复 try: for response in generate_from_baidu_qianfan(inputs, llm_kwargs, history, system_prompt): chatbot[-1] = (inputs, response) yield from update_ui(chatbot=chatbot, history=history) except ConnectionAbortedError as e: from .bridge_all import model_info if len(history) >= 2: history[-1] = ""; history[-2] = "" # 清除当前溢出的输入:history[-2] 是本次输入, history[-1] 是本次输出 history = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'], max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])) # history至少释放二分之一 chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长, 或历史数据过长. 历史缓存数据已部分释放, 您可以请再次尝试. (若再次失败则更可能是因为输入过长.)") yield from update_ui(chatbot=chatbot, history=history, msg="异常") # 刷新界面 return # 总结输出 response = f"[Local Message]: {model_name}响应异常 ..." if response == f"[Local Message]: 等待{model_name}响应中 ...": response = f"[Local Message]: {model_name}响应异常 ..." history.extend([inputs, response]) yield from update_ui(chatbot=chatbot, history=history)127 unmodified lines12812913013113213313426 unmodified lines161162163164165166167127 unmodified lines# chatglm 没有 sys_prompt 接口,因此把prompt加入 historyhistory_feedin = []history_feedin.append(["What can I do?", sys_prompt])for i in range(len(history)//2):history_feedin.append([history[2*i], history[2*i+1]] )26 unmodified lines# 处理历史信息history_feedin = []history_feedin.append(["What can I do?", system_prompt] )for i in range(len(history)//2):history_feedin.append([history[2*i], history[2*i+1]] )127 unmodified lines12812913013113213313426 unmodified lines161162163164165166167127 unmodified lines# chatglm 没有 sys_prompt 接口,因此把prompt加入 historyhistory_feedin = []history_feedin.append([sys_prompt, "Certainly!"])for i in range(len(history)//2):history_feedin.append([history[2*i], history[2*i+1]] )26 unmodified lines# 处理历史信息history_feedin = []history_feedin.append([system_prompt, "Certainly!"])for i in range(len(history)//2):history_feedin.append([history[2*i], history[2*i+1]] )
16 unmodified lines171819202116 unmodified linesnumpyarxivrichwebsocket-clientpypdf2==2.12.116 unmodified lines17181920212216 unmodified linesnumpyarxivrichpypdf2==2.12.1websocket-clientscipdf_parser==0.3
8 unmodified lines910111213141516171 unmodified line192021222324258 unmodified linesfrom tests.test_utils import plugin_testif __name__ == "__main__":plugin_test(plugin='crazy_functions.命令行助手->命令行助手', main_input='查看当前的docker容器列表')plugin_test(plugin='crazy_functions.解析项目源代码->解析一个Python项目', main_input="crazy_functions/test_project/python/dqn")# plugin_test(plugin='crazy_functions.解析项目源代码->解析一个C项目', main_input="crazy_functions/test_project/cpp/cppipc")1 unmodified line# plugin_test(plugin='crazy_functions.批量Markdown翻译->Markdown中译英', main_input="README.md")# plugin_test(plugin='crazy_functions.批量翻译PDF文档_多线程->批量翻译PDF文档', main_input="crazy_functions/test_project/pdf_and_word")# plugin_test(plugin='crazy_functions.谷歌检索小助手->谷歌检索小助手', main_input="https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=auto+reinforcement+learning&btnG=")8 unmodified lines910111213141516171 unmodified line192021222324258 unmodified linesfrom tests.test_utils import plugin_testif __name__ == "__main__":# plugin_test(plugin='crazy_functions.命令行助手->命令行助手', main_input='查看当前的docker容器列表')# plugin_test(plugin='crazy_functions.解析项目源代码->解析一个Python项目', main_input="crazy_functions/test_project/python/dqn")# plugin_test(plugin='crazy_functions.解析项目源代码->解析一个C项目', main_input="crazy_functions/test_project/cpp/cppipc")1 unmodified line# plugin_test(plugin='crazy_functions.批量Markdown翻译->Markdown中译英', main_input="README.md")plugin_test(plugin='crazy_functions.批量翻译PDF文档_多线程->批量翻译PDF文档', main_input='crazy_functions/test_project/pdf_and_word/aaai.pdf')# plugin_test(plugin='crazy_functions.谷歌检索小助手->谷歌检索小助手', main_input="https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=auto+reinforcement+learning&btnG=")
21 unmodified lines222324252627282930313 unmodified lines35363738394021 unmodified linesdef wrapper(*args, **kwargs):_original_stdout = sys.stdoutsys.stdout = open(os.devnull, 'w')for q in func(*args, **kwargs):sys.stdout = _original_stdoutyield qsys.stdout = open(os.devnull, 'w')sys.stdout.close()sys.stdout = _original_stdoutreturn wrapper3 unmodified linesdef wrapper(*args, **kwargs):_original_stdout = sys.stdoutsys.stdout = open(os.devnull, 'w')result = func(*args, **kwargs)sys.stdout.close()sys.stdout = _original_stdout21 unmodified lines2223242526272829303132333 unmodified lines3738394041424321 unmodified linesdef wrapper(*args, **kwargs):_original_stdout = sys.stdoutsys.stdout = open(os.devnull, 'w')sys.stdout.reconfigure(encoding='utf-8')for q in func(*args, **kwargs):sys.stdout = _original_stdoutyield qsys.stdout = open(os.devnull, 'w')sys.stdout.reconfigure(encoding='utf-8')sys.stdout.close()sys.stdout = _original_stdoutreturn wrapper3 unmodified linesdef wrapper(*args, **kwargs):_original_stdout = sys.stdoutsys.stdout = open(os.devnull, 'w')sys.stdout.reconfigure(encoding='utf-8')result = func(*args, **kwargs)sys.stdout.close()sys.stdout = _original_stdout
12345{"version": 3.48,"show_feature": true,"new_feature": "接入阿里通义千问、讯飞星火、上海AI-Lab书生 <-> 优化一键升级 <-> 提高arxiv翻译速度和成功率 <-> 支持自定义APIKEY格式 <-> 临时修复theme的文件丢失问题 <-> 新增实时语音对话插件(自动断句,脱手对话) <-> 支持加载自定义的ChatGLM2微调模型 <-> 动态ChatBot窗口高度 <-> 修复Azure接口的BUG <-> 完善多语言模块 <-> 完善本地Latex矫错和翻译功能 <-> 增加gpt-3.5-16k的支持"}12345{"version": 3.49,"show_feature": true,"new_feature": "支持借助GROBID实现PDF高精度翻译 <-> 接入百度千帆平台和文心一言 <-> 接入阿里通义千问、讯飞星火、上海AI-Lab书生 <-> 优化一键升级 <-> 提高arxiv翻译速度和成功率 <-> 支持自定义APIKEY格式 <-> 临时修复theme的文件丢失问题 <-> 新增实时语音对话插件(自动断句,脱手对话) <-> 支持加载自定义的ChatGLM2微调模型 <-> 动态ChatBot窗口高度 <-> 修复Azure接口的BUG <-> 完善多语言模块"}
README.md+5-3
app.py+1-1
config.py+73-2
crazy_functional.py+37-5
crazy_functions/latex_fns/latex_toolbox.py+4-1
crazy_functions/pdf_fns/parse_pdf.pyAdded+25
crazy_functions/批量翻译PDF文档_多线程.py+126-24
crazy_functions/辅助功能.pyAdded+43
docker-compose.yml+8-3
docs/GithubAction+ChatGLM+Moss+1
request_llm/bridge_all.py+31-2
request_llm/bridge_chatgpt.py+3-5
request_llm/bridge_chatgpt_website.py-15
request_llm/bridge_llama2.pyAdded+91
request_llm/bridge_qianfan.pyAdded+164
request_llm/local_llm_class.py+2-2
requirements.txt+2-1
tests/test_plugins.py+3-3
tests/test_utils.py+3
version+2-2