|
1 | 1 | import json |
2 | 2 | from pathlib import Path |
| 3 | +from typing import Any, Union, Optional |
3 | 4 |
|
4 | | -from pydantic import Field, BaseModel |
| 5 | +from nonebot.compat import PYDANTIC_V2 |
5 | 6 | import nonebot_plugin_localstore as store |
6 | 7 | from nonebot import logger, get_plugin_config |
| 8 | +from pydantic import Field, BaseModel, ConfigDict |
| 9 | + |
| 10 | +from .compat import model_validator |
| 11 | +from ._types import NOT_GIVEN, NotGivenOr |
7 | 12 |
|
8 | 13 |
|
9 | 14 | class ModelConfig: |
@@ -39,6 +44,86 @@ class CustomModel(BaseModel): |
39 | 44 | """Model Name""" |
40 | 45 | base_url: str = "https://api.deepseek.com" |
41 | 46 | """Custom base URL for this model (optional)""" |
| 47 | + max_tokens: int = Field(default=4090, gt=1, lt=8192) |
| 48 | + """ |
| 49 | + 限制一次请求中模型生成 completion 的最大 token 数 |
| 50 | + - `deepseek-chat`: Integer between 1 and 8192. Default is 4090. |
| 51 | + - `deepseek-reasoner`: Default is 4K, maximum is 8K. |
| 52 | + """ |
| 53 | + frequency_penalty: Union[int, float] = Field(default=0, ge=-2, le=2) |
| 54 | + """ |
| 55 | + Discourage the model from repeating the same words or phrases too frequently within the generated text |
| 56 | + """ |
| 57 | + presence_penalty: Union[int, float] = Field(default=0, ge=-2, le=2) |
| 58 | + """Encourage the model to include a diverse range of tokens in the generated text""" |
| 59 | + stop: Optional[Union[str, list[str]]] = Field(default=None) |
| 60 | + """ |
| 61 | + Stop generating tokens when encounter these words. |
| 62 | + Note that the list contains a maximum of 16 string. |
| 63 | + """ |
| 64 | + temperature: Union[int, float] = Field(default=1, ge=0, le=2) |
| 65 | + """Sampling temperature. It is not recommended to used it with top_p""" |
| 66 | + top_p: Union[int, float] = Field(default=1, ge=0, le=1) |
| 67 | + """Alternatives to sampling temperature. It is not recommended to used it with temperature""" |
| 68 | + logprobs: NotGivenOr[Union[bool, None]] = Field(default=NOT_GIVEN) |
| 69 | + """Whether to return the log probability of the output token.""" |
| 70 | + top_logprobs: NotGivenOr[int] = Field(default=NOT_GIVEN, le=20) |
| 71 | + """Specifies that the most likely token be returned at each token position.""" |
| 72 | + |
| 73 | + if PYDANTIC_V2: |
| 74 | + model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True) |
| 75 | + else: |
| 76 | + |
| 77 | + class Config: |
| 78 | + extra = "allow" |
| 79 | + arbitrary_types_allowed = True |
| 80 | + |
| 81 | + @model_validator(mode="before") |
| 82 | + @classmethod |
| 83 | + def check_max_token(cls, data: Any) -> Any: |
| 84 | + if isinstance(data, dict): |
| 85 | + name = data.get("name") |
| 86 | + |
| 87 | + if "max_tokens" not in data: |
| 88 | + if name == "deepseek-reasoner": |
| 89 | + data["max_tokens"] = 4000 |
| 90 | + else: |
| 91 | + data["max_tokens"] = 4090 |
| 92 | + |
| 93 | + stop = data.get("stop") |
| 94 | + if isinstance(stop, list) and len(stop) >= 16: |
| 95 | + raise ValueError("字段 `stop` 最多允许设置 16 个字符") |
| 96 | + |
| 97 | + if name == "deepseek-chat": |
| 98 | + temperature = data.get("temperature") |
| 99 | + top_p = data.get("top_p") |
| 100 | + if temperature and top_p: |
| 101 | + logger.warning("不建议同时修改 `temperature` 和 `top_p` 字段") |
| 102 | + |
| 103 | + top_logprobs = data.get("top_logprobs") |
| 104 | + logprobs = data.get("logprobs") |
| 105 | + if top_logprobs and logprobs is False: |
| 106 | + raise ValueError("指定 `top_logprobs` 参数时,`logprobs` 必须为 True") |
| 107 | + |
| 108 | + elif name == "deepseek-reasoner": |
| 109 | + max_tokens = data.get("max_tokens") |
| 110 | + if max_tokens and max_tokens > 8000: |
| 111 | + logger.warning(f"模型 {name} `max_tokens` 字段最大为 8000") |
| 112 | + |
| 113 | + unsupported_params = ["temperature", "top_p", "presence_penalty", "frequency_penalty"] |
| 114 | + params_present = [param for param in unsupported_params if param in data] |
| 115 | + if params_present: |
| 116 | + logger.warning(f"模型 {name} 不支持设置 {', '.join(params_present)}") |
| 117 | + |
| 118 | + logprobs = data.get("logprobs") |
| 119 | + top_logprobs = data.get("top_logprobs") |
| 120 | + if logprobs or top_logprobs: |
| 121 | + raise ValueError(f"模型 {name} 不支持设置 logprobs、top_logprobs") |
| 122 | + |
| 123 | + return data |
| 124 | + |
| 125 | + def to_dict(self): |
| 126 | + return self.model_dump(exclude_unset=True, exclude_none=True, exclude={"name", "base_url"}) |
42 | 127 |
|
43 | 128 |
|
44 | 129 | class ScopedConfig(BaseModel): |
@@ -66,6 +151,13 @@ def get_model_url(self, model_name: str) -> str: |
66 | 151 | return model.base_url |
67 | 152 | raise ValueError(f"Model {model_name} not enabled") |
68 | 153 |
|
| 154 | + def get_model_config(self, model_name: str) -> CustomModel: |
| 155 | + """Get model config""" |
| 156 | + for model in self.enable_models: |
| 157 | + if model.name == model_name: |
| 158 | + return model |
| 159 | + raise ValueError(f"Model {model_name} not enabled") |
| 160 | + |
69 | 161 |
|
70 | 162 | class Config(BaseModel): |
71 | 163 | deepseek: ScopedConfig = Field(default_factory=ScopedConfig) |
|
0 commit comments