diff --git a/crazy_functions/Langchain知识库.py b/crazy_functions/Langchain知识库.py index 4fee382..3edb215 100644 --- a/crazy_functions/Langchain知识库.py +++ b/crazy_functions/Langchain知识库.py @@ -18,17 +18,19 @@ def 知识库问答(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro chatbot.append(("这是什么功能?", "[Local Message] 从一批文件(txt, md, tex)中读取数据构建知识库, 然后进行问答。")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + # resolve deps try: - import zh_langchain + from zh_langchain import construct_vector_store from langchain.embeddings.huggingface import HuggingFaceEmbeddings from .crazy_utils import knowledge_archive_interface except Exception as e: chatbot.append( ["依赖不足", - "导入依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade langchain zh_langchain```。"] + "导入依赖失败。正在尝试自动安装,请查看终端的输出或耐心等待..."] ) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 - return + from .crazy_utils import try_install_deps + try_install_deps(['zh_langchain==0.1.9']) # < --------------------读取参数--------------- > if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg") @@ -52,7 +54,7 @@ def 知识库问答(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro print('Checking Text2vec ...') from langchain.embeddings.huggingface import HuggingFaceEmbeddings with ProxyNetworkActivate(): # 临时地激活代理网络 - HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese", model_kwargs={'device': 'cpu'}) + HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese") # < -------------------构建知识库--------------- > chatbot.append(['
'.join(file_manifest), "正在构建知识库..."]) @@ -86,6 +88,7 @@ def 读取知识库作答(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst resp, prompt = kai.answer_with_archive_by_id(txt, kai_id) chatbot.append((txt, '[Local Message] ' + prompt)) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive( inputs=prompt, inputs_show_user=txt, llm_kwargs=llm_kwargs, chatbot=chatbot, history=[], diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index a447d22..96301ff 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -638,7 +638,7 @@ class knowledge_archive_interface(): print('Checking Text2vec ...') from langchain.embeddings.huggingface import HuggingFaceEmbeddings with ProxyNetworkActivate(): # 临时地激活代理网络 - self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese", model_kwargs={'device': 'cpu'}) + self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese") return self.text2vec_large_chinese @@ -694,3 +694,7 @@ class knowledge_archive_interface(): self.threadLock.release() return resp, prompt +def try_install_deps(deps): + for dep in deps: + import subprocess, sys + subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--user', dep]) diff --git a/requirements.txt b/requirements.txt index feeca7c..1d70323 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ ./docs/gradio-3.32.2-py3-none-any.whl -zh_langchain==0.1.6 tiktoken>=0.3.3 requests[socks] transformers diff --git a/toolbox.py b/toolbox.py index 6903684..4e92b4f 100644 --- a/toolbox.py +++ b/toolbox.py @@ -783,8 +783,9 @@ class ProxyNetworkActivate(): from toolbox import get_conf proxies, = get_conf('proxies') if 'no_proxy' in os.environ: os.environ.pop('no_proxy') - os.environ['HTTP_PROXY'] = proxies['http'] - os.environ['HTTPS_PROXY'] = proxies['https'] + if proxies is not None: + if 'http' in proxies: os.environ['HTTP_PROXY'] = proxies['http'] + if 'https' in proxies: os.environ['HTTPS_PROXY'] = proxies['https'] return self def __exit__(self, exc_type, exc_value, traceback):