chatchat-space / Langchain-Chatchat

Langchain-Chatchat(原Langchain-ChatGLM)基于 Langchain 与 ChatGLM, Qwen 与 Llama 等语言模型的 RAG 与 Agent 应用 | Langchain-Chatchat (formerly langchain-ChatGLM), local knowledge based LLM (like ChatGLM, Qwen and Llama) RAG and Agent app with langchain
Apache License 2.0
31.08k stars 5.42k forks source link

文件对话报错:packages/langchain_community/chat_models/openai.py", line 495, in _astream TypeError: object of type 'NoneType' has no len() #3097

Closed MarkHe735 closed 4 months ago

MarkHe735 commented 6 months ago

问题描述 / Problem Description 文件对话报错:packages/langchain_community/chat_models/openai.py", line 495, in _astream TypeError: object of type 'NoneType' has no len()

复现问题的步骤 / Steps to Reproduce 拖拽并上传英文文档后,提示词输入翻译为中文,前台没有输出,后台报错:TypeError: object of type 'NoneType' has no len()

实际结果 / Actual Result

环境信息 / Environment Information 操作系统:Linux-5.15.0-94-generic-x86_64-with-glibc2.35. python版本:3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] 项目版本:v0.2.10 langchain版本:0.0.354. fastchat版本:0.2.35

当前使用的分词器:ChineseRecursiveTextSplitter 当前启动的LLM模型:['Qwen-14B-Chat'] @ cuda {'device': 'cuda', 'gpus': '0,1,2,3', 'host': '0.0.0.0', 'infer_turbo': False, 'max_gpu_memory': '32GiB', 'model_path': '/home/hdw/python_projs/models/Qwen-14B-Chat', 'model_path_exists': True, 'num_gpus': 1, 'port': 20002} 当前Embbedings模型: piccolo-large-zh @ cuda

**附加信息:日志: INFO: 127.0.0.1:56076 - "POST /llm_model/list_running_models HTTP/1.1" 200 OK 2024-02-28 01:58:38,174 - _client.py[line:1027] - INFO: HTTP Request: POST http://127.0.0.1:7861/llm_model/list_running_models "HTTP/1.1 200 OK" INFO: 127.0.0.1:56076 - "POST /llm_model/list_config_models HTTP/1.1" 200 OK 2024-02-28 01:58:38,229 - _client.py[line:1027] - INFO: HTTP Request: POST http://127.0.0.1:7861/llm_model/list_config_models "HTTP/1.1 200 OK" INFO: 127.0.0.1:56076 - "POST /chat/file_chat HTTP/1.1" 200 OK /home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class langchain_community.chat_models.openai.ChatOpenAI was deprecated in langchain-community 0.0.10 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run pip install -U langchain-openai and import as from langchain_openai import ChatOpenAI. warn_deprecated( 2024-02-28 01:58:38,470 - _client.py[line:1027] - INFO: HTTP Request: POST http://127.0.0.1:7861/chat/file_chat "HTTP/1.1 200 OK" 2024-02-28 01:58:38,523 - base.py[line:36] - INFO: thread 219585 开始操作:tmpc36osenw。 2024-02-28 01:58:38,525 - base.py[line:40] - INFO: thread 219585 结束操作:tmpc36osenw。

Entering new LLMChain chain... Prompt after formatting: user: <指令>根据已知信息,简洁和专业的来回答问题。如果无法从中得到答案,请说 “根据已知信息无法回答该问题”,不允许在答案中添加编造成分,答案请使用中文。 </指令> <已知信息>30K 40K 50K 60K 70K 80K 90K 100K 0.14 0.15 0.16 0.17 0.18 0.19 0.20 XL/8 XL/4 XL/2 0 0.25M 0.50M 0.75M 1.00M 1.25M 1.50M 1.75M 2.00M 2.25M 2.50M 2.75M 3.00M 3.25M 3.50M 3.75M 4.00M 4.25M 4.50M 4.75M 5.00M 5.25M 5.50M 5.75M 6.00M 6.25M 6.50M 6.75M 7.00M [59] Aaron Van Den Oord, Oriol Vinyals, et al. Neural discrete representation learning. Advances in neural information pro- cessing systems, 30, 2017. 2 [60] Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszko- reit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. In NeurIPS, 2017. 1, 2, 5 [61] Tete Xiao, Piotr Dollar, Mannat Singh, Eric Mintun, Trevor Darrell, and Ross Girshick. Early convolutions help trans- formers see better. In NeurIPS, 2021. 6 [62] Jiahui Yu, Yuanzhong Xu, Jing Yu Koh, Thang Luong, Gunjan Baid, Zirui Wang, Vijay Vasudevan, Alexander Ku, Yinfei Yang, Burcu Karagol Ayan, et al. Scaling autore- 0 10K 20K 30K 40K 50K 60K 70K 80K 90K 100K 0.14 0.15 0.16 0.17 0.18 0.19 0.20 L/8 L/4 L/2 0 100K 200K 300K 400K 500K 600K 700K 800K 900K 1M Training Iterations 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.20 0.21 Training Loss 0 10K 20K 30K 40K 50K 60K 70K 80K 90K 100K 0.14 0.15 0.16</已知信息> <问题>请翻译为中文</问题> 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:59774 - "POST /list_models HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | controller | names: ['http://127.0.0.1:20002'], queue_lens: [0.0], ret: http://127.0.0.1:20002 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:59782 - "POST /get_worker_address HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:48016 - "POST /worker_get_conv_template HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:48028 - "POST /model_details HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:48044 - "POST /count_token HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:53750 - "POST /v1/chat/completions HTTP/1.1" 200 OK 2024-02-28 01:58:38,798 - _client.py[line:1758] - INFO: HTTP Request: POST http://127.0.0.1:20000/v1/chat/completions "HTTP/1.1 200 OK" 2024-02-28 01:58:38 | INFO | stdout | INFO: 127.0.0.1:48048 - "POST /worker_generate_stream HTTP/1.1" 200 OK 2024-02-28 01:58:38 | INFO | httpx | HTTP Request: POST http://127.0.0.1:20002/worker_generate_stream "HTTP/1.1 200 OK" 2024-02-28 01:58:39,952 - utils.py[line:38] - ERROR: object of type 'NoneType' has no len() Traceback (most recent call last): File "/home/hdw/python_projs/Langchain-Chatchat_chat/server/utils.py", line 36, in wrap_done await fn File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/base.py", line 385, in acall raise e File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/base.py", line 379, in acall await self._acall(inputs, run_manager=run_manager) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/llm.py", line 275, in _acall response = await self.agenerate([inputs], run_manager=run_manager) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/llm.py", line 142, in agenerate return await self.llm.agenerate_prompt( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 554, in agenerate_prompt return await self.agenerate( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 514, in agenerate raise exceptions[0] File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 617, in _agenerate_with_cache return await self._agenerate( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_community/chat_models/openai.py", line 522, in _agenerate return await agenerate_from_stream(stream_iter) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 87, in agenerate_from_stream async for chunk in stream: File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_community/chat_models/openai.py", line 493, in _astream if len(chunk["choices"]) == 0: TypeError: object of type 'NoneType' has no len() 2024-02-28 01:58:39,957 - utils.py[line:40] - ERROR: TypeError: Caught exception: object of type 'NoneType' has no len() Traceback (most recent call last): File "/home/hdw/python_projs/Langchain-Chatchat_chat/server/utils.py", line 36, in wrap_done await fn File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/base.py", line 385, in acall raise e File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/base.py", line 379, in acall await self._acall(inputs, run_manager=run_manager) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/llm.py", line 275, in _acall response = await self.agenerate([inputs], run_manager=run_manager) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain/chains/llm.py", line 142, in agenerate return await self.llm.agenerate_prompt( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 554, in agenerate_prompt return await self.agenerate( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 514, in agenerate raise exceptions[0] File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 617, in _agenerate_with_cache return await self._agenerate( File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_community/chat_models/openai.py", line 522, in _agenerate return await agenerate_from_stream(stream_iter) File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py", line 87, in agenerate_from_stream async for chunk in stream: File "/home/hdw/miniconda3/envs/langchain_chatchat/lib/python3.10/site-packages/langchain_community/chat_models/openai.py", line 493, in _astream if len(chunk["choices"]) == 0: TypeError: object of type 'NoneType' has no len()

dosubot[bot] commented 6 months ago

🤖

Hey there, @MarkHe735! Nice to see you diving into the depths of code again. Hope this one's a fun puzzle for us to solve together. 😉

Based on the error message you provided, it seems that the issue is related to the chunk["choices"] variable in the _astream method of the openai.py file. This variable is expected to be a list, but in your case, it appears to be None, hence the TypeError: object of type 'NoneType' has no len() error.

There are a few similar issues in the Langchain-Chatchat repository that have been resolved:

  1. TypeError: object of type 'NoneType' has no len() - The suggested solution here is to ensure that your GPU has enough memory if you are running the models on a GPU. If you are running the models on a CPU and still encountering the error, it might be due to the model not being loaded properly. One user suggested replacing baichuan2-13b with baichuan2-13b-chat-4bits and installing a specific version of bitsandbytes for Windows.

  2. [BUG] model_worker多卡加载后,模型返回结果明显错误 - This issue suggests that the problem may be due to the fact that multi-GPU setup is only used to share memory load and not for actual parallel computation, which means that the model is still running on a single GPU. This could explain why the results are not as expected when using multiple GPUs.

  3. langchain-chathat 调用Dify应用的api接口时报错: ERROR: TypeError: Caught exception: object of type 'NoneType' has no len() - The suggested solution here is to check if chunk["choices"] is None before trying to get its length.

Based on these solutions, you could try the following:

  1. Check your GPU memory usage. If it's high, try moving the embedding model to the CPU to free up some GPU memory.

  2. If you're running the models on a CPU, try replacing baichuan2-13b with baichuan2-13b-chat-4bits and installing the specific version of bitsandbytes mentioned above.

  3. Modify the openai.py file to check if chunk["choices"] is None before trying to get its length.

Please note that these solutions are based on user comments and may not work for everyone. If you are still facing issues, it might be helpful to provide more details about the error and your setup for further assistance.

As for the _astream method in the openai.py file, I wasn't able to find an exact function within the repository. However, based on the name and common usage, it's likely that this method is used for asynchronous streaming of data.

The expected type and structure of chunk["choices"] in the _astream method is likely to be a list of dictionaries, with each dictionary representing a choice from the output of an AI model. This is based on the common structure of responses from AI language models, such as those provided by OpenAI's GPT models.

I hope this helps! If you have any other questions or need further clarification, please let me know.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
github-actions[bot] commented 5 months ago

这个问题已经被标记为 stale ,因为它已经超过 30 天没有任何活动。