本次重构将原本分散在不同目录的LLM实现(千问、DeepSeek、Ollama)重构为统一的架构,通过提取公共基类来减少代码重复,提高可维护性。
customllm/
├── __init__.py
├── base_llm_chat.py # 公共基类
├── qianwen_chat.py # 千问实现
├── deepseek_chat.py # DeepSeek实现
└── ollama_chat.py # Ollama实现
原类名 | 新类名 | 位置 |
---|---|---|
QianWenAI_Chat |
QianWenChat |
customllm.qianwen_chat |
DeepSeekChat |
DeepSeekChat |
customllm.deepseek_chat |
OllamaChat |
OllamaChat |
customllm.ollama_chat |
from customqianwen.Custom_QianwenAI_chat import QianWenAI_Chat
from customdeepseek.custom_deepseek_chat import DeepSeekChat
from customollama.ollama_chat import OllamaChat
from customllm.qianwen_chat import QianWenChat
from customllm.deepseek_chat import DeepSeekChat
from customllm.ollama_chat import OllamaChat
BaseLLMChat
基类所有LLM实现现在都包含完整的功能:
BaseLLMChat
基类包含以下公共方法:
system_message(message: str) -> dict
user_message(message: str) -> dict
assistant_message(message: str) -> dict
generate_sql(question: str, **kwargs) -> str
generate_question(sql: str, **kwargs) -> str
get_sql_prompt(...) -> list
generate_plotly_code(...) -> str
should_generate_chart(df) -> bool
chat_with_llm(question: str, **kwargs) -> str
generate_rewritten_question(...) -> str
_load_error_sql_prompt_config() -> bool
enable_thinking
功能test_connection()
方法将所有使用旧LLM类的文件中的导入语句更新为新的路径。
如果代码中直接引用了类名,需要更新为新的类名。
运行测试确保功能正常:
python test_refactor.py
common/vanna_combinations.py
已更新使用新的LLM实现如果遇到导入错误,检查:
如果发现某些功能缺失:
重构包含完整的测试验证:
运行 python test_refactor.py
可以验证重构是否成功。