在使用qwen2来进行数据库对话时,报错 UnicodeEncodeError: 'latin-1' codec can't encode characters
具体报错如下:
INFO: 127.0.0.1:50933 - "POST /chat/chat/completions HTTP/1.1" 500 Internal Server Error
2024-08-20 16:18:56,444 httpx 17356 INFO HTTP Request: POST http://127.0.0.1:7861/chat/chat/completions "HTTP/1.1 500 Internal Server Error"
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\dell\anaconda3\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 398, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "C:\Users\dell\anaconda3\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
return await self.app(scope, receive, send)
中间省略...........
File "C:\Users\dell\anaconda3\lib\site-packages\sqlalchemy\pool\base.py", line 896, in __connect
self.dbapi_connection = connection = pool._invoke_creator(self)
File "C:\Users\dell\anaconda3\lib\site-packages\sqlalchemy\engine\create.py", line 643, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\dell\anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 620, in connect
return self.loaded_dbapi.connect(*cargs, **cparams)
File "C:\Users\dell\anaconda3\lib\site-packages\pymysql\connections.py", line 301, in __init__
# self.password = self.password.encode("latin1")
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
一开始看到latin-1,我还以为是数据库连接时没有指定编码格式为utf8所造成的,但是修改后还是无法解决,之后仔细一看是self.password = self.password.encode("latin1"),发现是密码无法使用latin-1解码,随后意识到是密码为中文导致的,可是我的密码不是中文,于是尝试修改相应connections.py文件打印出密码,找到对应代码并修改如下:
if isinstance(self.password, str):
raise ValueError(f"mimacuowu {self.password,user,host,port}")
self.password = self.password.encode("latin1")
重新运行结果如下:
File "C:\Users\dell\anaconda3\lib\site-packages\pymysql\connections.py", line 301, in __init__
raise ValueError(f"mimacuowu {self.password,user,host,port}")
ValueError: mimacuowu ('密码', '用户名', '主机地址', 0)
从这里就可以看出是配置文件的问题,可是我是修改了配置文件的,于是我重新init了一个chatchat,再次运行结果不变。这时候我就发现了一个天大的错误,首先在原始的Langchain-Chatchat\libs\chatchat-server路径下使用相应的五个settings文件的,同时根据官方文档我们一开始设置了一个环境变量:CHATCHAT_ROOT=/path/to/chatchat_data。我本以为这个chatchat_data只是存放运行时产生的数据,可是现在发现在/path/to/chatchat_data这个目录下也有相应的五个settings文件,而初始化所读的配置文件正是这个目录下的。于是修改该目录下的tool_settings文件即可解决问题。
具体tool_settings文件text2sql的配置:
text2sql:
model_name: qwen2-instruct
use: false
sqlalchemy_connect_str: mysql+pymysql://root:root@localhost/feijing
read_only: false
top_k: 50
return_intermediate_steps: true
table_names: ["standard_address"]
table_comments: {"standard_address":"这是一个标准地址表,记录了标准地址相关的数据"}