protocol.py 1.4 KB

123456789101112131415161718192021222324252627282930
  1. from typing import List, Optional, Union
  2. from pydantic import BaseModel, Field, root_validator
  3. class SamplingParams(BaseModel):
  4. n: int = Field(1, alias="n")
  5. best_of: Optional[int] = Field(None, alias="best_of")
  6. presence_penalty: float = Field(0.0, alias="presence_penalty")
  7. frequency_penalty: float = Field(0.0, alias="rep_pen")
  8. temperature: float = Field(1.0, alias="temperature")
  9. top_p: float = Field(1.0, alias="top_p")
  10. top_k: float = Field(-1, alias="top_k")
  11. tfs: float = Field(1.0, alias="tfs")
  12. eta_cutoff: float = Field(0.0, alias="eta_cutoff")
  13. epsilon_cutoff: float = Field(0.0, alias="epsilon_cutoff")
  14. typical_p: float = Field(1.0, alias="typical_p")
  15. use_beam_search: bool = Field(False, alias="use_beam_search")
  16. length_penalty: float = Field(1.0, alias="length_penalty")
  17. early_stopping: Union[bool, str] = Field(False, alias="early_stopping")
  18. stop: Union[None, str, List[str]] = Field(None, alias="stop_sequence")
  19. ignore_eos: bool = Field(False, alias="ignore_eos")
  20. max_tokens: int = Field(16, alias="max_length")
  21. logprobs: Optional[int] = Field(None, alias="logprobs")
  22. @root_validator
  23. def validate_best_of(cls, values):
  24. best_of = values.get("best_of")
  25. n = values.get("n")
  26. if best_of is not None and (best_of <= 0 or best_of > n):
  27. raise ValueError("best_of must be a positive integer less than or equal to n")
  28. return values