ts_wire/sampling.rs
1// Copyright (c) 2026 LightSeek Foundation
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Sampling parameters (`sampling/sampling_params.py`).
22
23use crate::{
24 codec::{Dict, StrOrList},
25 value::Value,
26};
27
28wire_struct! {
29 /// `SamplingParams`, an untagged array-layout struct nested in a request.
30 ///
31 /// This is the data only. The Python class also normalizes on
32 /// construction and decode (`__post_init__`: greedy for a near-zero
33 /// temperature, `stop` copied to `stop_strs`, ...) unless
34 /// `is_normalized` is set; decoding here applies none of that.
35 #[derive(Debug, Clone, PartialEq)]
36 pub struct SamplingParams: array + Default {
37 /// Generation length limit; `None` leaves it to the engine.
38 pub max_new_tokens: Option<i64> = None,
39 /// API input for stop strings; normalization copies it to `stop_strs`.
40 pub stop: Option<StrOrList> = None,
41 /// Token ids that end generation (a Python `set`).
42 pub stop_token_ids: Option<Vec<i64>> = None,
43 /// Sampling temperature.
44 pub temperature: f64 = 1.0,
45 /// Nucleus sampling mass.
46 pub top_p: f64 = 1.0,
47 /// Top-k cutoff; `-1` disables it before normalization.
48 pub top_k: i64 = -1,
49 /// Minimum probability relative to the most likely token.
50 pub min_p: f64 = 0.0,
51 /// Frequency penalty.
52 pub frequency_penalty: f64 = 0.0,
53 /// Presence penalty.
54 pub presence_penalty: f64 = 0.0,
55 /// Repetition penalty.
56 pub repetition_penalty: f64 = 1.0,
57 /// Tokens generated before stop conditions apply.
58 pub min_new_tokens: i64 = 0,
59 /// JSON schema constraint.
60 pub json_schema: Option<String> = None,
61 /// Regex constraint.
62 pub regex: Option<String> = None,
63 /// EBNF grammar constraint.
64 pub ebnf: Option<String> = None,
65 /// Structural tag constraint.
66 pub structural_tag: Option<String> = None,
67 /// Keep generating past end-of-sequence tokens.
68 pub ignore_eos: bool = false,
69 /// Detokenization option, carried for the frontend.
70 pub skip_special_tokens: bool = true,
71 /// Detokenization option, carried for the frontend.
72 pub spaces_between_special_tokens: bool = true,
73 /// Keep the matched stop string in the output.
74 pub no_stop_trim: bool = false,
75 /// Reasoning-token budget.
76 pub thinking_budget: Option<i64> = None,
77 /// Free-form parameters for custom logit processors.
78 pub custom_params: Option<Dict<String, Value>> = None,
79 /// Streaming chunk interval in tokens.
80 pub stream_interval: Option<i64> = None,
81 /// Per-token logit offsets, keyed by token id as a string.
82 pub logit_bias: Option<Dict<String, f64>> = None,
83 /// Sampling seed.
84 pub seed: Option<i64> = None,
85 /// Output logprobs: `None` off, `0` the sampled token's logprob.
86 pub logprobs: Option<i64> = None,
87 /// OpenAI `n`; the engine accepts only `1`.
88 pub n: i64 = 1,
89 /// Stop strings after normalization.
90 pub stop_strs: Option<StrOrList> = None,
91 /// Longest stop string, set by normalization.
92 pub stop_str_max_len: i64 = 0,
93 /// Whether normalization already ran on the sender.
94 pub is_normalized: bool = false,
95 }
96}