MetaGPT/metagpt/context.py

105 lines
3 KiB
Python
Raw Normal View History

2024-01-04 21:16:23 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2024/1/4 16:32
@Author : alexanderwu
@File : context.py
"""
import os
from pathlib import Path
2024-01-08 16:26:52 +08:00
from typing import Optional
2024-01-04 21:16:23 +08:00
2024-01-09 17:39:09 +08:00
from pydantic import BaseModel, ConfigDict
2024-01-09 14:16:32 +08:00
2024-01-04 21:16:23 +08:00
from metagpt.config2 import Config
2024-01-09 15:56:40 +08:00
from metagpt.configs.llm_config import LLMConfig, LLMType
2024-01-04 21:16:23 +08:00
from metagpt.const import OPTIONS
from metagpt.provider.base_llm import BaseLLM
2024-01-09 15:56:40 +08:00
from metagpt.provider.llm_provider_registry import create_llm_instance
2024-01-04 21:16:23 +08:00
from metagpt.utils.cost_manager import CostManager
from metagpt.utils.git_repository import GitRepository
2024-01-09 14:16:32 +08:00
class AttrDict(BaseModel):
"""A dict-like object that allows access to keys as attributes, compatible with Pydantic."""
model_config = ConfigDict(extra="allow")
2024-01-08 18:30:04 +08:00
2024-01-09 14:16:32 +08:00
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.__dict__.update(kwargs)
2024-01-08 16:26:52 +08:00
def __getattr__(self, key):
2024-01-09 14:16:32 +08:00
return self.__dict__.get(key, None)
2024-01-08 16:26:52 +08:00
def __setattr__(self, key, value):
2024-01-09 14:16:32 +08:00
self.__dict__[key] = value
2024-01-08 16:26:52 +08:00
def __delattr__(self, key):
2024-01-09 14:16:32 +08:00
if key in self.__dict__:
del self.__dict__[key]
2024-01-08 16:26:52 +08:00
else:
raise AttributeError(f"No such attribute: {key}")
2024-01-09 17:39:09 +08:00
class LLMInstance:
2024-01-09 16:12:31 +08:00
"""Mixin class for LLM"""
2024-01-09 17:04:45 +08:00
# _config: Optional[Config] = None
2024-01-09 17:39:09 +08:00
_llm_config: Optional[LLMConfig] = None
_llm_instance: Optional[BaseLLM] = None
2024-01-09 15:56:40 +08:00
2024-01-09 17:39:09 +08:00
def __init__(self, config: Config, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI):
2024-01-09 16:12:31 +08:00
"""Use a LLM provider"""
2024-01-09 15:56:40 +08:00
# 更新LLM配置
2024-01-09 17:39:09 +08:00
self._llm_config = config.get_llm_config(name, provider)
2024-01-09 15:56:40 +08:00
# 重置LLM实例
2024-01-09 17:39:09 +08:00
self._llm_instance = None
2024-01-09 15:56:40 +08:00
@property
2024-01-09 17:39:09 +08:00
def instance(self) -> BaseLLM:
2024-01-09 16:12:31 +08:00
"""Return the LLM instance"""
2024-01-09 17:39:09 +08:00
if not self._llm_instance and self._llm_config:
self._llm_instance = create_llm_instance(self._llm_config)
return self._llm_instance
2024-01-09 15:56:40 +08:00
2024-01-09 17:13:22 +08:00
class Context(BaseModel):
2024-01-09 14:16:32 +08:00
"""Env context for MetaGPT"""
model_config = ConfigDict(arbitrary_types_allowed=True)
2024-01-08 18:30:04 +08:00
kwargs: AttrDict = AttrDict()
2024-01-04 21:16:23 +08:00
config: Config = Config.default()
git_repo: Optional[GitRepository] = None
src_workspace: Optional[Path] = None
cost_manager: CostManager = CostManager()
2024-01-09 17:39:09 +08:00
_llm: Optional[LLMInstance] = None
2024-01-04 21:16:23 +08:00
2024-01-05 00:41:00 +08:00
@property
def file_repo(self):
return self.git_repo.new_file_repository()
2024-01-04 21:16:23 +08:00
@property
def options(self):
"""Return all key-values"""
return OPTIONS.get()
def new_environ(self):
"""Return a new os.environ object"""
env = os.environ.copy()
i = self.options
env.update({k: v for k, v in i.items() if isinstance(v, str)})
return env
2024-01-09 17:13:22 +08:00
def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:
"""Return a LLM instance"""
2024-01-09 17:39:09 +08:00
llm = LLMInstance(self.config, name, provider).instance
2024-01-09 17:13:22 +08:00
if llm.cost_manager is None:
llm.cost_manager = self.cost_manager
return llm
2024-01-04 21:16:23 +08:00
2024-01-09 14:16:32 +08:00
# Global context, not in Env
2024-01-04 21:16:23 +08:00
context = Context()