|
| 1 | +from enum import Enum |
| 2 | +from typing import Callable |
| 3 | +from pydantic import BaseModel, Field, model_validator |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +class BasicModelConfig(BaseModel): |
| 7 | + base_url: Optional[str] = Field( |
| 8 | + default=None, |
| 9 | + description="Global base URL for model API endpoints." |
| 10 | + ) |
| 11 | + temperature: Optional[float] = Field( |
| 12 | + default=0.8, |
| 13 | + description="Global temperature setting for model generation." |
| 14 | + ) |
| 15 | + |
| 16 | +class ModelConfig(BasicModelConfig): |
| 17 | + id: str = Field( |
| 18 | + description="ID of the model, including both the provider name and the model name, e.g. 'openai:gpt-4'." |
| 19 | + ) |
| 20 | + weight: Optional[int] = Field( |
| 21 | + default=1, |
| 22 | + description="Weight of the model for ensemble methods. Only used if routing_mode is 'weighted'." |
| 23 | + ) |
| 24 | + |
| 25 | +class ChatRole(str, Enum): |
| 26 | + USER = "user" |
| 27 | + ASSISTANT = "assistant" |
| 28 | + SYSTEM = "system" |
| 29 | + |
| 30 | +class Message(BaseModel): |
| 31 | + role: ChatRole = Field(description="Role of the message sender.") |
| 32 | + # For image messages, the format is different, but we only support text message for now. |
| 33 | + # See https://platform.openai.com/docs/api-reference/chat/create |
| 34 | + content: str = Field(description="Content of the message.") |
| 35 | + |
| 36 | +class RoutingMode(str, Enum): |
| 37 | + RANDOM = "random" |
| 38 | + WEIGHTED = "weighted" |
| 39 | + |
| 40 | +class Config(BasicModelConfig): |
| 41 | + models: List[ModelConfig] = Field(description="List of model configurations") |
| 42 | + routing_mode: RoutingMode = Field( |
| 43 | + default=RoutingMode.RANDOM, |
| 44 | + description="Routing mode for the model, default is random.", |
| 45 | + ) |
| 46 | + callback_funcs: Optional[List[Callable]] = Field( |
| 47 | + default=None, |
| 48 | + description="Callback functions to be called after each model inference. Functions will be called sequentially.", |
| 49 | + ) |
| 50 | + messages: str | List[Message] = Field( |
| 51 | + description="Messages to be sent to the model(s). Can be a string or a list of Message objects." |
| 52 | + ) |
| 53 | + |
| 54 | + @model_validator(mode="after") |
| 55 | + def ensure_at_least_one_base_url(self): |
| 56 | + global_url_exist = self.base_url is not None |
| 57 | + |
| 58 | + for model in self.models: |
| 59 | + if not model.base_url and not global_url_exist: |
| 60 | + raise ValueError("At least one base_url must be specified either in the global config or in each model config.") |
| 61 | + |
| 62 | + return self |
0 commit comments