windows11本地安装部署langchain-chatchat api接口报错

文章描述了在Langchain-chatchat环境中遇到的接口调试问题,主要表现为404和400错误,源于max_tokens参数设置为0导致的。解决方法是移除请求体中的max_tokens参数,以恢复正常请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Langchain-chatchat

提示:Langchain服务已经正常启动,swagger-ui页面也正常访问的情况下出现接口调试报错问题


在这里插入图片描述


前言

报错接口如下:

2023-11-03 15:23:50 | INFO | stdout | INFO:     127.0.0.1:53907 - "POST /v1/chat/chat HTTP/1.1" 404 Not Found
2023-11-03 15:24:01 | INFO | stdout | INFO:     127.0.0.1:53920 - "POST /v1 HTTP/1.1" 404 Not Found
2023-11-03 15:24:23 | INFO | stdout | INFO:     127.0.0.1:53938 - "GET /v1 HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:54053 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO:     127.0.0.1:54053 - "GET /docs HTTP/1.1" 200 OK
openai.api_key='EMPTY'
openai.api_base='http://127.0.0.1:20000/v1'
model='chatglm2-6b' messages=[OpenAiMessage(role='user', content='hello')] temperature=0.7 n=1 max_tokens=0 stop=[] stream=False presence_penalty=0 frequency_penalty=0
INFO:     127.0.0.1:54064 - "POST /chat/fastchat HTTP/1.1" 200 OK
2023-11-03 15:26:58 | INFO | stdout | INFO:     127.0.0.1:54065 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:26:58,486 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:26:58,487 - openai_chat.py[line:52] - ERROR: APIError: 获取ChatCompletion时出错:Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400)
openai.api_key='EMPTY'
openai.api_base='http://127.0.0.1:20000/v1'
model='chatglm2-6b' messages=[OpenAiMessage(role='user', content='hello')] temperature=0.7 n=1 max_tokens=0 stop=[] stream=False presence_penalty=0 frequency_penalty=0
INFO:     127.0.0.1:54064 - "POST /chat/fastchat HTTP/1.1" 200 OK
2023-11-03 15:27:02 | INFO | stdout | INFO:     127.0.0.1:54071 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:02,322 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:02,323 - openai_chat.py[line:52] - ERROR: APIError: 获取ChatCompletion时出错:Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400)
openai.api_key='EMPTY'
openai.api_base='http://127.0.0.1:20000/v1'
model='chatglm2-6b' messages=[OpenAiMessage(role='user', content='hello')] temperature=0.7 n=1 max_tokens=0 stop=[] stream=False presence_penalty=0 frequency_penalty=0
INFO:     127.0.0.1:54064 - "POST /chat/fastchat HTTP/1.1" 200 OK
2023-11-03 15:27:02 | INFO | stdout | INFO:     127.0.0.1:54073 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:02,968 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:02,970 - openai_chat.py[line:52] - ERROR: APIError: 获取ChatCompletion时出错:Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400)
123
INFO:     127.0.0.1:54096 - "POST /chat/chat HTTP/1.1" 200 OK
{'cache': None, 'verbose': True, 'callbacks': [<langchain.callbacks.streaming_aiter.AsyncIteratorCallbackHandler object at 0x000001DE719FE610>], 'callback_manager': None, 'tags': None, 'metadata': None, 'client': <class 'openai.api_resources.chat_completion.ChatCompletion'>, 'model_name': 'chatglm2-6b', 'temperature': 0.7, 'model_kwargs': {}, 'openai_api_key': 'EMPTY', 'openai_api_base': 'http://127.0.0.1:20000/v1', 'openai_organization': '', 'openai_proxy': '', 'request_timeout': None, 'max_retries': 6, 'streaming': True, 'n': 1, 'max_tokens': 0, 'tiktoken_model_name': None}
2023-11-03 15:27:36 | INFO | stdout | INFO:     127.0.0.1:54097 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:36,492 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:36,493 - before_sleep.py[line:65] - WARNING: Retrying langchain.chat_models.openai.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400).
2023-11-03 15:27:40 | INFO | stdout | INFO:     127.0.0.1:54103 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:40,498 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:40,499 - before_sleep.py[line:65] - WARNING: Retrying langchain.chat_models.openai.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400).
2023-11-03 15:27:44 | INFO | stdout | INFO:     127.0.0.1:54109 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:44,521 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:44,524 - before_sleep.py[line:65] - WARNING: Retrying langchain.chat_models.openai.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400).
2023-11-03 15:27:48 | INFO | stdout | INFO:     127.0.0.1:54113 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:48,537 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:48,537 - before_sleep.py[line:65] - WARNING: Retrying langchain.chat_models.openai.acompletion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised APIError: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400).
2023-11-03 15:27:56 | INFO | stdout | INFO:     127.0.0.1:54124 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:27:56,572 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:27:56,574 - before_sleep.py[line:65] - WARNING: Retrying langchain.chat_models.openai.acompletion_with_retry.<locals>._completion_with_retry in 10.0 seconds as it raised APIError: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400).
2023-11-03 15:28:06 | INFO | stdout | INFO:     127.0.0.1:54138 - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
2023-11-03 15:28:06,598 - util.py[line:67] - INFO: message='OpenAI API response' path=http://127.0.0.1:20000/v1/chat/completions processing_ms=None request_id=None response_code=400
2023-11-03 15:28:06,604 - utils.py[line:26] - ERROR: APIError: Caught exception: Invalid response object from API: '{"object":"error","message":"0 is less than the minimum of 1 - \'max_tokens\'","code":40302}' (HTTP response code was 400)

问题分析

在这里插入图片描述

请求体重自带 【max_tokens】,如果有max_tokens=0,不为null,就去请求方案openAI接口,我们只要掉本地api接口就可了。所以解决方案就是将这个参数从请求体中删掉就可以正常请求了,这个问题困扰了我两天,真的是太反人类了。
希望对大家有帮助。

解决问题方案

删除【max_tokens】参数。
在这里插入图片描述

<think>嗯,用户想了解如何部署LangChain-Chatchat。我之前有整理过相关的步骤,可能需要再确认一下最新的情况。首先,用户可能对LangChain-Chatchat不太熟悉,所以应该先简单介绍一下这个项目是什么,用途是什么,这样他们能有个基本概念。 接下来,部署步骤需要分点说明,可能用户是开发者或者有一定技术背景的人,所以步骤要详细但不过于复杂。需要包括环境准备、克隆仓库、安装依赖、配置模型路径、初始化数据库、运行项目这些主要步骤。另外,用户可能会遇到一些常见问题,比如依赖冲突、端口被占用或者模型下载问题,应该给出解决方案。 要注意的是,用户可能使用的是不同的操作系统,比如Windows、Linux或者Mac,所以在命令上可能需要区分,比如Linux/macOS用sudo,Windows用其他方式。同时,模型下载部分可能需要说明从Hugging Face或者ModelScope下载,特别是大文件可能需要用git lfs,或者直接wget,这部分要详细点,避免用户卡在这里。 另外,配置文件中修改模型路径是关键步骤,用户如果路径设置不正确会导致程序无法运行,所以需要强调检查路径是否正确。数据库初始化部分,如果用户没有正确执行,可能后续会报错,可能需要提醒他们注意这一步。 运行项目的时候,用户可能需要知道如何访问,用浏览器打开本地端口,比如127.0.0.1:7860,可能需要提到如果端口被占用如何修改端口号,或者使用nohup让服务在后台运行。 最后,常见问题部分要简明扼要,给出可能的错误和解决方法。比如依赖问题建议用虚拟环境,模型路径问题要确认是否正确,端口占用的话修改配置文件。同时,提醒用户如果问题依旧,可以去GitHub提issue。 需要检查一下所有步骤是否逻辑清晰,没有遗漏重要环节。比如,是否提到了安装git lfs,因为下载模型可能需要这个。另外,是否强调了Python版本要求,比如3.8+,以及PyTorch的正确安装方式,比如根据CUDA版本选择命令。 可能还需要注意权限问题,比如在克隆仓库或者安装依赖时是否需要sudo,但Python的虚拟环境一般不建议用sudo,所以可能需要提醒用户创建虚拟环境时不用sudo,只在系统包安装时可能需要。 最后,确保所有命令正确无误,特别是换行符和路径符号,不同操作系统可能不同,比如Windows用反斜杠,Linux/macOS用正斜杠,但配置文件中的路径可能需要统一格式。 总结下来,回答的结构应该是先简介项目,然后分步骤详细说明部署流程,接着是常见问题处理,最后注意事项和参考链接。这样用户能一步步跟着操作,遇到问题也能找到解决方法。</think>**LangChain-Chatchat 项目简介** LangChain-Chatchat 是一个基于 LangChain 和大型语言模型(LLM)的开源对话系统,支持知识库问答、多轮对话、Agent 任务等功能。适合用于构建企业级知识库助手或智能对话应用。以下是部署流程的详细步骤: --- ### **部署步骤** #### 1. **环境准备** - **Python 3.8+**:推荐使用 Conda 管理环境: ```bash conda create -n chatenv python=3.8 conda activate chatenv ``` - **PyTorch**:根据 CUDA 版本安装(无 GPU 则安装 CPU 版本): ```bash # CUDA 11.7 示例 pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 ``` - **Git LFS**(下载大模型必需): ```bash # Ubuntu/Debian sudo apt-get install git-lfs git lfs install ``` #### 2. **克隆代码仓库** ```bash git clone https://github.com/chatchat-space/Langchain-Chatchat.git cd Langchain-Chatchat ``` #### 3. **安装依赖** ```bash pip install -r requirements.txt pip install -r requirements_api.txt # 如需 API 接口 pip install -r requirements_webui.txt # 如需 Web UI ``` #### 4. **下载预训练模型** - 从 Hugging Face 或 ModelScope 下载模型,例如: ```bash # 使用 modelscope 下载 pip install modelscope from modelscope import snapshot_download model_dir = snapshot_download("ZhipuAI/chatglm3-6b", revision="v1.0.0") ``` - 或手动下载模型文件到 `Langchain-Chatchat/models/` 目录。 #### 5. **修改配置文件** - 复制并修改配置文件模板: ```bash cp configs/model_config.py.example configs/model_config.py cp configs/server_config.py.example configs/server_config.py ``` - 在 `model_config.py` 中指定模型路径,例如: ```python "chatglm3-6b": { "local_model_path": "/path/to/chatglm3-6b", # 修改为实际路径 "api_base_url": "http://localhost:8000", } ``` #### 6. **初始化知识库数据库** ```bash python init_database.py --recreate-vs # 强制重建向量库 ``` #### 7. **启动服务** - **一键启动所有模块**: ```bash python startup.py -a # 包含 API、Web UI、Embedding 模型等 ``` - **自定义启动**(可选): ```bash python startup.py --all-webui # 仅启动 Web UI ``` #### 8. **访问服务** - 浏览器打开 `http://127.0.0.1:7860`(默认端口 7860,可在 `server_config.py` 中修改)。 --- ### **常见问题解决** 1. **依赖冲突**:建议使用虚拟环境,或通过 `pip install --upgrade` 更新包。 2. **模型加载失败**:检查配置文件中的模型路径是否正确,确保模型文件完整。 3. **端口占用**:修改 `server_config.py` 中的 `DEFAULT_BIND_HOST` 和 `DEFAULT_SERVER_PORT`。 4. **知识库构建失败**:确保 `init_database.py` 已运行,且文档格式为 `.txt` 或 `.md`。 --- ### **注意事项** - **硬件要求**:LLM 模型需要显存(如 6B 模型需 12GB+ 显存),CPU 模式速度较慢。 - **网络问题**:模型下载可能需要代理,国内用户建议使用 ModelScope 镜像。 - **更新代码**:定期执行 `git pull` 拉取最新版本,并重新安装依赖。 **参考链接**: - GitHub 仓库: [https://github.com/chatchat-space/Langchain-Chatchat](https://github.com/chatchat-space/Langchain-Chatchat) - 官方文档: [https://www.langchain.com.cn](https://www.langchain.com.cn)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值