运行ChatterBot基础代码时遭遇SQLAlchemy参数错误求助
问题
运行以下ChatterBot基础代码时出现错误:
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer from chatterbot.trainers import ListTrainer bot = ChatBot('Bot') trainer = ListTrainer(bot) trainer.train([ 'Hi', 'Hello', 'I need roadmap for Competitive Programming', 'Just create an account on GFG and start', 'I have a query.', 'Please elaborate, your concern', 'How long it will take to become expert in Coding ?', 'It usually depends on the amount of practice.', 'Ok Thanks', 'No Problem! Have a Good Day!' ]) while True: request=input('you :') if request == 'OK' or request == 'ok': print('Bot: bye') break else: response=bot.get_response(request) print('Bot:', response)
错误信息
Traceback (most recent call last): File "C:\Users\User\Desktop\TESTE AI.py", line 5, in <module> bot = ChatBot('Bot') File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\chatterbot\chatterbot.py", line 41, in __init__ self.storage = utils.initialize_class(storage_adapter, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\chatterbot\utils.py", line 54, in initialize_class return Class(*args, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\chatterbot\storage\sql_storage.py", line 46, in __init__ if not self.engine.dialect.has_table(self.engine, 'Statement'): File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 2016, in has_table self._ensure_has_table_connection(connection) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\default.py", line 342, in _ensure_has_table_connection raise exc.ArgumentError( sqlalchemy.exc.ArgumentError: The argument passed to Dialect.has_table() should be <class 'sqlalchemy.engine.base.Connection'>, got <class 'sqlalchemy.engine.base.Engine'>. Additionally, the Dialect.has_table() method is for internal dialect use only; please use ``inspect(some_engine).has_table(<tablename>>)`` for public API use.
环境信息
- Chatterbot版本:1.0.0、1.0.4、1.0.5、1.1.0
- Python版本:3.8.10
解决方案
这个错误是因为安装的SQLAlchemy版本(2.0+)和旧版ChatterBot不兼容,SQLAlchemy 2.0修改了has_table的调用方式,而ChatterBot 1.x系列未适配该变化,可通过以下两种方法解决:
- 降级SQLAlchemy到兼容版本
打开命令行执行:
pip install sqlalchemy==1.4.49
该版本与ChatterBot 1.x完全兼容,安装后重新运行代码即可。
- 改用JSON存储适配器
修改ChatBot的初始化代码,指定使用JSON存储而非默认的SQL存储,绕开SQLAlchemy的问题:
bot = ChatBot( 'Bot', storage_adapter='chatterbot.storage.JsonFileStorageAdapter' )
数据会存储在本地JSON文件中,无需依赖SQLAlchemy。
内容的提问来源于stack exchange,提问作者Vagetti_Dev
相关产品推荐
相关产品推荐

