🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
refine code
geekanad525acd33a2
diff --git a/metagpt/context.py b/metagpt/context.pyindex ba859ed5c..71570bac6 100644--- a/metagpt/context.py+++ b/metagpt/context.py@@ -9,7 +9,7 @@ import osfrom pathlib import Pathfrom typing import Optional-from pydantic import BaseModel, ConfigDict+from pydantic import BaseModel, ConfigDict, Fieldfrom metagpt.config2 import Configfrom metagpt.configs.llm_config import LLMConfig, LLMType@@ -42,31 +42,33 @@ class AttrDict(BaseModel):raise AttributeError(f"No such attribute: {key}")-class LLMMixin:+class LLMMixin(BaseModel):"""Mixin class for LLM"""+ model_config = ConfigDict(arbitrary_types_allowed=True)+# _config: Optional[Config] = None- _llm_config: Optional[LLMConfig] = None- _llm_instance: Optional[BaseLLM] = None+ llm_config: Optional[LLMConfig] = Field(default=None, exclude=True)+ llm_instance: Optional[BaseLLM] = Field(default=None, exclude=True)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+ self.llm_instance = None@propertydef 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)- return self._llm_instance+ if not self.llm_instance and self.llm_config:+ self.llm_instance = create_llm_instance(self.llm_config)+ return self.llm_instance-class Context(LLMMixin, BaseModel):+class Context(BaseModel):"""Env context for MetaGPT"""model_config = ConfigDict(arbitrary_types_allowed=True)@@ -93,14 +95,14 @@ class Context(LLMMixin, BaseModel):env.update({k: v for k, v in i.items() if isinstance(v, str)})return env- # def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:- # """Return a LLM instance"""- # llm_config = self.config.get_llm_config(name, provider)- #- # llm = create_llm_instance(llm_config)- # if llm.cost_manager is None:- # llm.cost_manager = self.cost_manager- # return llm+ def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:+ """Return a LLM instance"""+ llm_config = self.config.get_llm_config(name, provider)++ llm = create_llm_instance(llm_config)+ if llm.cost_manager is None:+ llm.cost_manager = self.cost_manager+ return llmclass ContextMixin:diff --git a/metagpt/llm.py b/metagpt/llm.pyindex f9a5aaedb..aff72d3c5 100644--- a/metagpt/llm.py+++ b/metagpt/llm.py@@ -15,4 +15,5 @@ from metagpt.provider.base_llm import BaseLLMdef LLM(name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:"""get the default llm provider if name is None"""+ # context.use_llm(name=name, provider=provider)return context.llm(name=name, provider=provider)