protocol.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from typing import List, Optional, Union
  2. from pydantic import BaseModel, Field, root_validator, conint, confloat, conlist, NonNegativeFloat, NonNegativeInt, PositiveInt
  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. dynatemp_range: Optional[float] = 0.0
  10. dynatemp_exponent: Optional[float] = 1.0
  11. smoothing_factor: Optional[float] = 0.0
  12. top_p: float = Field(1.0, alias="top_p")
  13. top_k: float = Field(-1, alias="top_k")
  14. min_p: float = Field(0.0, alias="min_p")
  15. top_a: float = Field(0.0, alias="top_a")
  16. tfs: float = Field(1.0, alias="tfs")
  17. eta_cutoff: float = Field(0.0, alias="eta_cutoff")
  18. epsilon_cutoff: float = Field(0.0, alias="epsilon_cutoff")
  19. typical_p: float = Field(1.0, alias="typical_p")
  20. use_beam_search: bool = Field(False, alias="use_beam_search")
  21. length_penalty: float = Field(1.0, alias="length_penalty")
  22. early_stopping: Union[bool, str] = Field(False, alias="early_stopping")
  23. stop: Union[None, str, List[str]] = Field(None, alias="stop_sequence")
  24. include_stop_str_in_output: Optional[bool] = False
  25. ignore_eos: bool = Field(False, alias="ignore_eos")
  26. max_tokens: int = Field(16, alias="max_length")
  27. logprobs: Optional[int] = Field(None, alias="logprobs")
  28. custom_token_bans: Optional[List[int]] = Field(None,
  29. alias="custom_token_bans")
  30. @root_validator
  31. def validate_best_of(cls, values): # pylint: disable=no-self-argument
  32. best_of = values.get("best_of")
  33. n = values.get("n")
  34. if best_of is not None and (best_of <= 0 or best_of > n):
  35. raise ValueError(
  36. "best_of must be a positive integer less than or equal to n")
  37. return values
  38. class KAIGenerationInputSchema(BaseModel):
  39. genkey: Optional[str]
  40. prompt: str
  41. n: Optional[conint(ge=1, le=5)] = 1
  42. max_context_length: PositiveInt
  43. max_length: PositiveInt
  44. rep_pen: Optional[confloat(ge=1)] = 1.0
  45. rep_pen_range: Optional[NonNegativeInt]
  46. rep_pen_slope: Optional[NonNegativeFloat]
  47. top_k: Optional[NonNegativeInt] = 0.0
  48. top_a: Optional[NonNegativeFloat] = 0.0
  49. top_p: Optional[confloat(ge=0, le=1)] = 1.0
  50. min_p: Optional[confloat(ge=0, le=1)] = 0.0
  51. tfs: Optional[confloat(ge=0, le=1)] = 1.0
  52. eps_cutoff: Optional[confloat(ge=0, le=1000)] = 0.0
  53. eta_cutoff: Optional[NonNegativeFloat] = 0.0
  54. typical: Optional[confloat(ge=0, le=1)] = 1.0
  55. temperature: Optional[NonNegativeFloat] = 1.0
  56. dynatemp_range: Optional[NonNegativeFloat] = 0.0
  57. dynatemp_exponent: Optional[NonNegativeFloat] = 1.0
  58. smoothing_factor: Optional[NonNegativeFloat] = 0.0
  59. use_memory: Optional[bool]
  60. use_story: Optional[bool]
  61. use_authors_note: Optional[bool]
  62. use_world_info: Optional[bool]
  63. use_userscripts: Optional[bool]
  64. soft_prompt: Optional[str]
  65. disable_output_formatting: Optional[bool]
  66. frmtrmblln: Optional[bool]
  67. frmtrmspch: Optional[bool]
  68. singleline: Optional[bool]
  69. use_default_badwordsids: Optional[bool]
  70. mirostat: Optional[int] = 0
  71. mirostat_tau: Optional[float] = 0.0
  72. mirostat_eta: Optional[float] = 0.0
  73. disable_input_formatting: Optional[bool]
  74. frmtadsnsp: Optional[bool]
  75. quiet: Optional[bool]
  76. # pylint: disable=unexpected-keyword-arg
  77. sampler_order: Optional[conlist(int, min_items=6)]
  78. sampler_seed: Optional[conint(ge=0, le=2**64 - 1)]
  79. sampler_full_determinism: Optional[bool]
  80. stop_sequence: Optional[List[str]]
  81. include_stop_str_in_output: Optional[bool] = False
  82. @root_validator
  83. def check_context(cls, values): # pylint: disable=no-self-argument
  84. assert values.get("max_length") <= values.get(
  85. "max_context_length"
  86. ), "max_length must not be larger than max_context_length"
  87. return values