refine code

This commit is contained in:
geekan 2024-01-09 17:04:45 +08:00 committed by 莘权 马
parent b259203f74
commit 20b53fa859

View file

@ -45,24 +45,24 @@ class AttrDict(BaseModel):
class LLMMixin:
"""Mixin class for LLM"""
config: Optional[Config] = None
llm_config: Optional[LLMConfig] = None
# _config: Optional[Config] = None
_llm_config: Optional[LLMConfig] = None
_llm_instance: Optional[BaseLLM] = None
def use_llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI):
"""Use a LLM provider"""
# 更新LLM配置
self.llm_config = self.config.get_llm_config(name, provider)
self._llm_config = self._config.get_llm_config(name, provider)
# 重置LLM实例
self._llm_instance = None
@property
def llm(self) -> BaseLLM:
"""Return the LLM instance"""
if not self.llm_config:
if not self._llm_config:
self.use_llm()
if not self._llm_instance and self.llm_config:
self._llm_instance = create_llm_instance(self.llm_config)
if not self._llm_instance and self._llm_config:
self._llm_instance = create_llm_instance(self._llm_config)
return self._llm_instance