细分代理场景

This commit is contained in:
binary-husky 2023-09-23 22:43:15 +08:00
parent 409927ef8e
commit 2d8f37baba
7 changed files with 32 additions and 16 deletions

View File

@ -183,6 +183,9 @@ ALLOW_RESET_CONFIG = False
PATH_PRIVATE_UPLOAD = "private_upload" PATH_PRIVATE_UPLOAD = "private_upload"
# 日志文件夹的位置,请勿修改 # 日志文件夹的位置,请勿修改
PATH_LOGGING = "gpt_log" PATH_LOGGING = "gpt_log"
# 除了连接OpenAI之外还有哪些场合允许使用代理请勿修改
WHEN_TO_USE_PROXY = ["Download_LLM", "Download_Gradio_Theme"]
""" """
在线大模型配置关联关系示意图 在线大模型配置关联关系示意图

View File

@ -53,14 +53,14 @@ def 知识库问答(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
print('Checking Text2vec ...') print('Checking Text2vec ...')
from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.embeddings.huggingface import HuggingFaceEmbeddings
with ProxyNetworkActivate(): # 临时地激活代理网络 with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese") HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
# < -------------------构建知识库--------------- > # < -------------------构建知识库--------------- >
chatbot.append(['<br/>'.join(file_manifest), "正在构建知识库..."]) chatbot.append(['<br/>'.join(file_manifest), "正在构建知识库..."])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
print('Establishing knowledge archive ...') print('Establishing knowledge archive ...')
with ProxyNetworkActivate(): # 临时地激活代理网络 with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
kai = knowledge_archive_interface() kai = knowledge_archive_interface()
kai.feed_archive(file_manifest=file_manifest, id=kai_id) kai.feed_archive(file_manifest=file_manifest, id=kai_id)
kai_files = kai.get_loaded_file() kai_files = kai.get_loaded_file()

View File

@ -651,7 +651,7 @@ class knowledge_archive_interface():
from toolbox import ProxyNetworkActivate from toolbox import ProxyNetworkActivate
print('Checking Text2vec ...') print('Checking Text2vec ...')
from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.embeddings.huggingface import HuggingFaceEmbeddings
with ProxyNetworkActivate(): # 临时地激活代理网络 with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese") self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
return self.text2vec_large_chinese return self.text2vec_large_chinese

View File

@ -3,7 +3,7 @@ from transformers import AutoModel, AutoTokenizer
import time import time
import threading import threading
import importlib import importlib
from toolbox import update_ui, get_conf from toolbox import update_ui, get_conf, ProxyNetworkActivate
from multiprocessing import Process, Pipe from multiprocessing import Process, Pipe
load_message = "ChatGLM尚未加载加载需要一段时间。注意取决于`config.py`的配置ChatGLM消耗大量的内存CPU或显存GPU也许会导致低配计算机卡死 ……" load_message = "ChatGLM尚未加载加载需要一段时间。注意取决于`config.py`的配置ChatGLM消耗大量的内存CPU或显存GPU也许会导致低配计算机卡死 ……"
@ -48,16 +48,17 @@ class GetGLMHandle(Process):
while True: while True:
try: try:
if self.chatglm_model is None: with ProxyNetworkActivate('Download_LLM'):
self.chatglm_tokenizer = AutoTokenizer.from_pretrained(_model_name_, trust_remote_code=True) if self.chatglm_model is None:
if device=='cpu': self.chatglm_tokenizer = AutoTokenizer.from_pretrained(_model_name_, trust_remote_code=True)
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).float() if device=='cpu':
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).float()
else:
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).half().cuda()
self.chatglm_model = self.chatglm_model.eval()
break
else: else:
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).half().cuda() break
self.chatglm_model = self.chatglm_model.eval()
break
else:
break
except: except:
retry += 1 retry += 1
if retry > 3: if retry > 3:

View File

@ -30,7 +30,7 @@ class GetONNXGLMHandle(LocalLLMHandle):
with open(os.path.expanduser('~/.cache/huggingface/token'), 'w') as f: with open(os.path.expanduser('~/.cache/huggingface/token'), 'w') as f:
f.write(huggingface_token) f.write(huggingface_token)
model_id = 'meta-llama/Llama-2-7b-chat-hf' model_id = 'meta-llama/Llama-2-7b-chat-hf'
with ProxyNetworkActivate(): with ProxyNetworkActivate('Download_LLM'):
self._tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=huggingface_token) self._tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=huggingface_token)
# use fp16 # use fp16
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=huggingface_token).eval() model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=huggingface_token).eval()

View File

@ -5,7 +5,7 @@ CODE_HIGHLIGHT, ADD_WAIFU, LAYOUT = get_conf('CODE_HIGHLIGHT', 'ADD_WAIFU', 'LAY
def dynamic_set_theme(THEME): def dynamic_set_theme(THEME):
set_theme = gr.themes.ThemeClass() set_theme = gr.themes.ThemeClass()
with ProxyNetworkActivate(): with ProxyNetworkActivate('Download_Gradio_Theme'):
logging.info('正在下载Gradio主题请稍等。') logging.info('正在下载Gradio主题请稍等。')
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-') if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')
if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-') if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-')
@ -16,7 +16,7 @@ def adjust_theme():
try: try:
set_theme = gr.themes.ThemeClass() set_theme = gr.themes.ThemeClass()
with ProxyNetworkActivate(): with ProxyNetworkActivate('Download_Gradio_Theme'):
logging.info('正在下载Gradio主题请稍等。') logging.info('正在下载Gradio主题请稍等。')
THEME, = get_conf('THEME') THEME, = get_conf('THEME')
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-') if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')

View File

@ -956,7 +956,19 @@ class ProxyNetworkActivate():
""" """
这段代码定义了一个名为TempProxy的空上下文管理器, 用于给一小段代码上代理 这段代码定义了一个名为TempProxy的空上下文管理器, 用于给一小段代码上代理
""" """
def __init__(self, task=None) -> None:
self.task = task
if not task:
# 不给定task, 那么我们默认代理生效
self.valid = True
else:
# 给定了task, 我们检查一下
from toolbox import get_conf
WHEN_TO_USE_PROXY, = get_conf('WHEN_TO_USE_PROXY')
self.valid = (task in WHEN_TO_USE_PROXY)
def __enter__(self): def __enter__(self):
if not self.valid: return self
from toolbox import get_conf from toolbox import get_conf
proxies, = get_conf('proxies') proxies, = get_conf('proxies')
if 'no_proxy' in os.environ: os.environ.pop('no_proxy') if 'no_proxy' in os.environ: os.environ.pop('no_proxy')