Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

当我启用api时,送入文本过长时程序会报错,返回空值。

Open renllll opened this issue 1 year ago • 2 comments

当我启用api时,送入文本过长终端报以下错误,返回空值,如何解决这个报错 以上代码运行是报以下错误,如何解决 response返回为空,在终端显示报错如下 Traceback (most recent call last): File "/media/remotesense/c076bdaf-88b9-4573-88f1-b4bdb3af31834/jack/Langchain-Chatchat-master/server/utils.py", line 36, in wrap_done await fn File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/base.py", line 385, in acall raise e File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/base.py", line 379, in acall await self._acall(inputs, run_manager=run_manager) File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/llm.py", line 275, in _acall response = await self.agenerate([inputs], run_manager=run_manager) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/llm.py", line 142, in agenerate return await self.llm.agenerate_prompt( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 554, in agenerate_prompt return await self.agenerate( ^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 514, in agenerate raise exceptions[0] File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 617, in _agenerate_with_cache return await self._agenerate( ^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_community/chat_models/openai.py", line 522, in _agenerate return await agenerate_from_stream(stream_iter) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 87, in agenerate_from_stream async for chunk in stream: File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/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-04-23 15:13:58,995 - utils.py[line:40] - ERROR: TypeError: Caught exception: object of type 'NoneType' has no len()

renllll avatar Apr 23 '24 07:04 renllll

The error you're encountering, TypeError: object of type 'NoneType' has no len(), suggests that the operation len() is being called on a None object. This happens in the line if len(chunk["choices"]) == 0:, indicating chunk["choices"] might be None.

To address this issue, ensure you check if chunk["choices"] is None before attempting to get its length. Modify the code as follows:

if chunk["choices"] is not None and len(chunk["choices"]) == 0:

This adjustment prevents the TypeError by verifying chunk["choices"] is not None before determining its length.

Additionally, if encountering this error when sending overly long text to the API, verify if there's a maximum request size limit for the API and ensure your text doesn't exceed this limit. If necessary, consider breaking the text into smaller segments and sending multiple requests.

This error typically indicates an operation expected a certain type (like a string or list) but received a None type instead. Always check the API response for success and ensure you're receiving the expected data type before proceeding with operations dependent on the response.

Details

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.

To continue the conversation, mention @dosu.

dosubot[bot] avatar Apr 23 '24 07:04 dosubot[bot]

api设置最大长度是在哪设置的,调用api总报以下错误 2024-04-24 17:06:58 | INFO | httpx | HTTP Request: POST http://127.0.0.1:20002/worker_generate_stream "HTTP/1.1 200 OK" 2024-04-24 17:06:59,665 - utils.py[line:38] - ERROR: 'NoneType' object is not subscriptable Traceback (most recent call last): File "/media/remotesense/c076bdaf-88b9-4573-88f1-b4bdb3af31834/jack/Langchain-Chatchat-master/server/utils.py", line 36, in wrap_done await fn File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/base.py", line 385, in acall raise e File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/base.py", line 379, in acall await self._acall(inputs, run_manager=run_manager) File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/llm.py", line 275, in _acall response = await self.agenerate([inputs], run_manager=run_manager) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain/chains/llm.py", line 142, in agenerate return await self.llm.agenerate_prompt( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 554, in agenerate_prompt return await self.agenerate( ^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 514, in agenerate raise exceptions[0] File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 617, in _agenerate_with_cache return await self._agenerate( ^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_community/chat_models/openai.py", line 522, in _agenerate return await agenerate_from_stream(stream_iter) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 87, in agenerate_from_stream async for chunk in stream: File "/home/remotesense/anaconda3/envs/chatchat/lib/python3.11/site-packages/langchain_community/chat_models/openai.py", line 495, in _astream choice = chunk["choices"][0] ~~~~~~~~~~~~~~~~^^^ TypeError: 'NoneType' object is not subscriptable 2024-04-24 17:06:59,665 - utils.py[line:40] - ERROR: TypeError: Caught exception: 'NoneType' object is not subscriptable

renllll avatar Apr 24 '24 09:04 renllll

因为超过了4096 token,你要在model config或者对应请求api调整max token, 比如zhipu只有4096 如果你上传的内容和提问超过4096就无法回答

zRzRzRzRzRzRzR avatar May 05 '24 04:05 zRzRzRzRzRzRzR