feat: merge geekan:main

This commit is contained in:
莘权 马 2024-08-06 16:59:33 +08:00
commit 09d07393fb
407 changed files with 20079 additions and 1174 deletions

View file

@ -0,0 +1,54 @@
from enum import Enum
from typing import Optional
from pydantic import field_validator
from metagpt.utils.yaml_model import YamlModel
class EmbeddingType(Enum):
OPENAI = "openai"
AZURE = "azure"
GEMINI = "gemini"
OLLAMA = "ollama"
class EmbeddingConfig(YamlModel):
"""Config for Embedding.
Examples:
---------
api_type: "openai"
api_key: "YOU_API_KEY"
dimensions: "YOUR_MODEL_DIMENSIONS"
api_type: "azure"
api_key: "YOU_API_KEY"
base_url: "YOU_BASE_URL"
api_version: "YOU_API_VERSION"
dimensions: "YOUR_MODEL_DIMENSIONS"
api_type: "gemini"
api_key: "YOU_API_KEY"
api_type: "ollama"
base_url: "YOU_BASE_URL"
model: "YOU_MODEL"
dimensions: "YOUR_MODEL_DIMENSIONS"
"""
api_type: Optional[EmbeddingType] = None
api_key: Optional[str] = None
base_url: Optional[str] = None
api_version: Optional[str] = None
model: Optional[str] = None
embed_batch_size: Optional[int] = None
dimensions: Optional[int] = None # output dimension of embedding model
@field_validator("api_type", mode="before")
@classmethod
def check_api_type(cls, v):
if v == "":
return None
return v

View file

@ -0,0 +1,6 @@
from metagpt.utils.yaml_model import YamlModel
class OmniParseConfig(YamlModel):
api_key: str = ""
base_url: str = ""

View file

@ -10,7 +10,7 @@ from typing import Optional
from pydantic import field_validator
from metagpt.const import LLM_API_TIMEOUT
from metagpt.const import CONFIG_ROOT, LLM_API_TIMEOUT, METAGPT_ROOT
from metagpt.utils.yaml_model import YamlModel
@ -31,6 +31,8 @@ class LLMType(Enum):
MOONSHOT = "moonshot"
MISTRAL = "mistral"
YI = "yi" # lingyiwanwu
OPENROUTER = "openrouter"
BEDROCK = "bedrock"
ARK = "ark" # https://www.volcengine.com/docs/82379/1263482#python-sdk
def __missing__(self, key):
@ -73,11 +75,15 @@ class LLMConfig(YamlModel):
frequency_penalty: float = 0.0
best_of: Optional[int] = None
n: Optional[int] = None
stream: bool = False
logprobs: Optional[bool] = None # https://cookbook.openai.com/examples/using_logprobs
stream: bool = True
# https://cookbook.openai.com/examples/using_logprobs
logprobs: Optional[bool] = None
top_logprobs: Optional[int] = None
timeout: int = 600
# For Amazon Bedrock
region_name: str = None
# For Network
proxy: Optional[str] = None
@ -88,7 +94,16 @@ class LLMConfig(YamlModel):
@classmethod
def check_llm_key(cls, v):
if v in ["", None, "YOUR_API_KEY"]:
raise ValueError("Please set your API key in config2.yaml")
repo_config_path = METAGPT_ROOT / "config/config2.yaml"
root_config_path = CONFIG_ROOT / "config2.yaml"
if root_config_path.exists():
raise ValueError(
f"Please set your API key in {root_config_path}. If you also set your config in {repo_config_path}, \nthe former will overwrite the latter. This may cause unexpected result.\n"
)
elif repo_config_path.exists():
raise ValueError(f"Please set your API key in {repo_config_path}")
else:
raise ValueError("Please set your API key in config2.yaml")
return v
@field_validator("timeout")

View file

@ -13,7 +13,7 @@ from metagpt.utils.yaml_model import YamlModel
class MermaidConfig(YamlModel):
"""Config for Mermaid"""
engine: Literal["nodejs", "ink", "playwright", "pyppeteer"] = "nodejs"
engine: Literal["nodejs", "ink", "playwright", "pyppeteer", "none"] = "nodejs"
path: str = "mmdc" # mmdc
puppeteer_config: str = ""
pyppeteer_path: str = "/usr/bin/google-chrome-stable"

View file

@ -0,0 +1,112 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
models_config.py
This module defines the ModelsConfig class for handling configuration of LLM models.
Attributes:
CONFIG_ROOT (Path): Root path for configuration files.
METAGPT_ROOT (Path): Root path for MetaGPT files.
Classes:
ModelsConfig (YamlModel): Configuration class for LLM models.
"""
from pathlib import Path
from typing import Dict, List, Optional
from pydantic import Field, field_validator
from metagpt.config2 import merge_dict
from metagpt.configs.llm_config import LLMConfig
from metagpt.const import CONFIG_ROOT, METAGPT_ROOT
from metagpt.utils.yaml_model import YamlModel
class ModelsConfig(YamlModel):
"""
Configuration class for `models` in `config2.yaml`.
Attributes:
models (Dict[str, LLMConfig]): Dictionary mapping model names or types to LLMConfig objects.
Methods:
update_llm_model(cls, value): Validates and updates LLM model configurations.
from_home(cls, path): Loads configuration from ~/.metagpt/config2.yaml.
default(cls): Loads default configuration from predefined paths.
get(self, name_or_type: str) -> Optional[LLMConfig]: Retrieves LLMConfig by name or API type.
"""
models: Dict[str, LLMConfig] = Field(default_factory=dict)
@field_validator("models", mode="before")
@classmethod
def update_llm_model(cls, value):
"""
Validates and updates LLM model configurations.
Args:
value (Dict[str, Union[LLMConfig, dict]]): Dictionary of LLM configurations.
Returns:
Dict[str, Union[LLMConfig, dict]]: Updated dictionary of LLM configurations.
"""
for key, config in value.items():
if isinstance(config, LLMConfig):
config.model = config.model or key
elif isinstance(config, dict):
config["model"] = config.get("model") or key
return value
@classmethod
def from_home(cls, path):
"""
Loads configuration from ~/.metagpt/config2.yaml.
Args:
path (str): Relative path to configuration file.
Returns:
Optional[ModelsConfig]: Loaded ModelsConfig object or None if file doesn't exist.
"""
pathname = CONFIG_ROOT / path
if not pathname.exists():
return None
return ModelsConfig.from_yaml_file(pathname)
@classmethod
def default(cls):
"""
Loads default configuration from predefined paths.
Returns:
ModelsConfig: Default ModelsConfig object.
"""
default_config_paths: List[Path] = [
METAGPT_ROOT / "config/config2.yaml",
CONFIG_ROOT / "config2.yaml",
]
dicts = [ModelsConfig.read_yaml(path) for path in default_config_paths]
final = merge_dict(dicts)
return ModelsConfig(**final)
def get(self, name_or_type: str) -> Optional[LLMConfig]:
"""
Retrieves LLMConfig object by name or API type.
Args:
name_or_type (str): Name or API type of the LLM model.
Returns:
Optional[LLMConfig]: LLMConfig object if found, otherwise None.
"""
if not name_or_type:
return None
model = self.models.get(name_or_type)
if model:
return model
for m in self.models.values():
if m.api_type == name_or_type:
return m
return None