Merge branch 'dev' of https://github.com/geekan/MetaGPT into geekan/dev

This commit is contained in:
莘权 马 2024-01-09 09:57:57 +08:00
commit 2bac5535f7
11 changed files with 59 additions and 25 deletions

View file

@ -10,6 +10,7 @@ from pathlib import Path
from typing import Optional
from metagpt.config2 import Config
from metagpt.configs.llm_config import LLMType
from metagpt.const import OPTIONS
from metagpt.provider.base_llm import BaseLLM
from metagpt.provider.llm_provider_registry import get_llm
@ -61,9 +62,21 @@ class Context:
env.update({k: v for k, v in i.items() if isinstance(v, str)})
return env
def llm(self, name: Optional[str] = None) -> BaseLLM:
def llm(self, name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:
"""Return a LLM instance"""
llm = get_llm(self.config.get_llm_config(name))
if provider:
llm_configs = self.config.get_llm_configs_by_type(provider)
if name:
llm_configs = [c for c in llm_configs if c.name == name]
if len(llm_configs) == 0:
raise ValueError(f"Cannot find llm config with name {name} and provider {provider}")
# return the first one if name is None, or return the only one
llm_config = llm_configs[0]
else:
llm_config = self.config.get_llm_config(name)
llm = get_llm(llm_config)
if llm.cost_manager is None:
llm.cost_manager = self.cost_manager
return llm

View file

@ -8,10 +8,11 @@
from typing import Optional
from metagpt.configs.llm_config import LLMType
from metagpt.context import context
from metagpt.provider.base_llm import BaseLLM
def LLM(name: Optional[str] = None) -> BaseLLM:
def LLM(name: Optional[str] = None, provider: LLMType = LLMType.OPENAI) -> BaseLLM:
"""get the default llm provider if name is None"""
return context.llm(name)
return context.llm(name=name, provider=provider)

View file

@ -29,7 +29,7 @@ class BrainMemory(BaseModel):
historical_summary: str = ""
last_history_id: str = ""
is_dirty: bool = False
last_talk: str = None
last_talk: Optional[str] = None
cacheable: bool = True
llm: Optional[BaseLLM] = Field(default=None, exclude=True)

View file

@ -1,11 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
import shutil
from pathlib import Path
import typer
from metagpt.config2 import config
from metagpt.const import METAGPT_ROOT
app = typer.Typer(add_completion=False)
@ -64,9 +66,9 @@ def generate_repo(
asyncio.run(company.run(n_round=n_round))
@app.command()
@app.command("", help="Start a new project.")
def startup(
idea: str = typer.Argument(..., help="Your innovative idea, such as 'Create a 2048 game.'"),
idea: str = typer.Argument(None, help="Your innovative idea, such as 'Create a 2048 game.'"),
investment: float = typer.Option(default=3.0, help="Dollar amount to invest in the AI company."),
n_round: int = typer.Option(default=5, help="Number of rounds for the simulation."),
code_review: bool = typer.Option(default=True, help="Whether to use code review."),
@ -87,8 +89,17 @@ def startup(
"unlimited. This parameter is used for debugging the workflow.",
),
recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage"),
init_config: bool = typer.Option(default=False, help="Initialize the configuration file for MetaGPT."),
):
"""Run a startup. Be a boss."""
if init_config:
copy_config_to()
return
if idea is None:
typer.echo("Missing argument 'IDEA'. Run 'metagpt --help' for more information.")
raise typer.Exit()
return generate_repo(
idea,
investment,
@ -105,5 +116,23 @@ def startup(
)
def copy_config_to(config_path=METAGPT_ROOT / "config" / "config2.yaml"):
"""Initialize the configuration file for MetaGPT."""
target_path = Path.home() / ".metagpt" / "config2.yaml"
# 创建目标目录(如果不存在)
target_path.parent.mkdir(parents=True, exist_ok=True)
# 如果目标文件已经存在,则重命名为 .bak
if target_path.exists():
backup_path = target_path.with_suffix(".bak")
target_path.rename(backup_path)
print(f"Existing configuration file backed up at {backup_path}")
# 复制文件
shutil.copy(str(config_path), target_path)
print(f"Configuration file initialized at {target_path}")
if __name__ == "__main__":
app()