mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-02 12:22:39 +02:00
add context and config2
This commit is contained in:
parent
42bb40a0f6
commit
e5d11a046c
76 changed files with 922 additions and 495 deletions
55
metagpt/context.py
Normal file
55
metagpt/context.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2024/1/4 16:32
|
||||
@Author : alexanderwu
|
||||
@File : context.py
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from metagpt.config2 import Config
|
||||
from metagpt.const import OPTIONS
|
||||
from metagpt.provider.base_llm import BaseLLM
|
||||
from metagpt.provider.llm_provider_registry import get_llm
|
||||
from metagpt.utils.cost_manager import CostManager
|
||||
from metagpt.utils.git_repository import GitRepository
|
||||
|
||||
|
||||
class Context(BaseModel):
|
||||
kwargs: Dict = {}
|
||||
config: Config = Config.default()
|
||||
git_repo: Optional[GitRepository] = None
|
||||
src_workspace: Optional[Path] = None
|
||||
cost_manager: CostManager = CostManager()
|
||||
|
||||
@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
|
||||
|
||||
def llm(self, name: Optional[str] = None) -> BaseLLM:
|
||||
"""Return a LLM instance"""
|
||||
llm = get_llm(self.config.get_llm_config(name))
|
||||
if llm.cost_manager is None:
|
||||
llm.cost_manager = self.cost_manager
|
||||
return llm
|
||||
|
||||
|
||||
# Global context
|
||||
context = Context()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(context.model_dump_json(indent=4))
|
||||
print(context.config.get_openai_llm())
|
||||
Loading…
Add table
Add a link
Reference in a new issue