GaiZhenbiao / ChuanhuChatGPT

GUI for ChatGPT API and many LLMs. Supports agents, file-based QA, GPT finetuning and query with web search. All with a neat UI.
https://huggingface.co/spaces/JohnSmith9982/ChuanhuChatGPT
GNU General Public License v3.0
15.07k stars 2.28k forks source link

[Bug]: 循环引用的问题会导致无法使用知识库功能 #1133

Closed kasperlmc closed 2 months ago

kasperlmc commented 2 months ago

这个bug是否已存在现有issue了?

错误表现

屏幕截图 2024-05-22 102431 屏幕截图 2024-05-22 102453

因为要使用知识库的功能,必须要调用base_model.py中的handle_file_upload方法, 而handle_file_upload方法会调用到index_func.py中的construct_index函数, 但是index_func.py又使用from modules.utils import 引用了utils中的全部内容, 同时在utils中的1432行出现了from .models.base_model import BaseLLMModel,使得utils又对base_model 进行了引用,进而导致了循环引用的问题,使得base_model.py的from ..index_func import 无法生效,进而导致handle_file_upload方法无法调用到index_func.py中的construct_index函数,导致知识库的功能出错。

复现操作

正常部署了最新版本代码,然后问答正常,但是知识库功能不正常。

错误日志

2024-05-15 13:48:29,424 [INFO] [base_model.py:492] 加载索引中……
Traceback (most recent call last):
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/queueing.py", line 566, in process_events
    response = await route_utils.call_process_api(
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/route_utils.py", line 261, in call_process_api
    output = await app.get_blocks().process_api(
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/blocks.py", line 1786, in process_api
    result = await self.call_function(
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/blocks.py", line 1350, in call_function
    prediction = await utils.async_iteration(iterator)
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/utils.py", line 583, in async_iteration
    return await iterator.__anext__()
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/utils.py", line 576, in __anext__
    return await anyio.to_thread.run_sync(
  File "/usr/local/python38/lib/python3.8/site-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/usr/local/python38/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/usr/local/python38/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/utils.py", line 559, in run_sync_iterator_async
    return next(iterator)
  File "/usr/local/python38/lib/python3.8/site-packages/gradio/utils.py", line 742, in gen_wrapper
    response = next(iterator)
  File "/home/luxun/ChuanhuChatGPT/modules/utils.py", line 41, in predict
    for i in iter:
  File "/home/luxun/ChuanhuChatGPT/modules/models/base_model.py", line 632, in predict
    ) = self.prepare_inputs(
  File "/home/luxun/ChuanhuChatGPT/modules/models/base_model.py", line 493, in prepare_inputs
    index = construct_index(
NameError: name 'construct_index' is not defined

运行环境

帮助解决

补充说明

No response

ThorinKong commented 2 months ago

貌似utils.py引用BaseLLMModel只是为了类型注解 在utils.py中把 from .models.base_model import BaseLLMModel移动到if TYPE_CHECKING:里就行了