add test config

This commit is contained in:
geekan 2024-01-09 16:12:31 +08:00
parent 8ddd17da28
commit 0f0ef86b26
2 changed files with 13 additions and 1 deletions

View file

@ -43,11 +43,14 @@ class AttrDict(BaseModel):
class LLMMixin:
"""Mixin class for LLM"""
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)
# 重置LLM实例
@ -55,7 +58,9 @@ class LLMMixin:
@property
def llm(self) -> BaseLLM:
# 实例化LLM如果尚未实例化
"""Return the LLM instance"""
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)
return self._llm_instance

View file

@ -8,6 +8,7 @@
from metagpt.config2 import Config, config
from metagpt.configs.llm_config import LLMType
from tests.metagpt.provider.mock_llm_config import mock_llm_config
def test_config_1():
@ -19,3 +20,9 @@ def test_config_1():
def test_config_2():
assert config == Config.default()
def test_config_from_dict():
cfg = Config(llm={"default": mock_llm_config})
assert cfg
assert cfg.llm["default"].api_key == "mock_api_key"