alith_interface/requests/completion/
response.rs

1use crate::requests::{
2    res_components::{GenerationSettings, InferenceProbabilities, TimingUsage, TokenUsage},
3    stop_sequence::StoppingSequence,
4};
5
6use super::tool::ToolCall;
7
8pub struct CompletionResponse {
9    /// A unique identifier for the chat completion.
10    pub id: String,
11    /// If batched, the index of the choice in the list of choices.
12    pub index: Option<u32>,
13    /// The generated completion.
14    pub content: String,
15    pub finish_reason: CompletionFinishReason,
16    pub completion_probabilities: Option<Vec<InferenceProbabilities>>,
17    /// True if the context size was exceeded during generation, i.e. the number of tokens provided in the prompt (tokens_evaluated) plus tokens generated (tokens predicted) exceeded the context size (n_ctx)
18    pub truncated: bool,
19    pub generation_settings: GenerationSettings,
20    pub timing_usage: TimingUsage,
21    pub token_usage: TokenUsage,
22    pub tool_calls: Option<Vec<ToolCall>>,
23}
24
25impl std::fmt::Display for CompletionResponse {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        writeln!(f)?;
28        writeln!(f, "CompletionResponse:")?;
29        writeln!(f, "    content: {:?}", self.content)?;
30        writeln!(f, "    finish_reason: {}", self.finish_reason)?;
31        write!(f, "    generation_settings: {}", self.generation_settings)?;
32        write!(f, "    timing_usage: {}", self.timing_usage)?;
33        write!(f, "    token_usage: {}", self.token_usage)?;
34        write!(f, "    token_calls: {:?}", self.tool_calls)
35    }
36}
37
38#[derive(PartialEq)]
39pub enum CompletionFinishReason {
40    /// The completion finished because the model generated the EOS token.
41    Eos,
42    /// The completion finished because the model generated a stop sequence that matches one of the provided stop sequences.
43    MatchingStoppingSequence(StoppingSequence),
44    /// The completion finished because the model generated a stop sequence that does not match any of the provided stop sequences.
45    NonMatchingStoppingSequence(Option<String>),
46    /// The completion finished because the model reached the maximum token limit.
47    StopLimit,
48    /// The completion finished because the model use the tool
49    ToolsCall,
50}
51
52impl std::fmt::Display for CompletionFinishReason {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            CompletionFinishReason::Eos => write!(f, "Eos"),
56            CompletionFinishReason::MatchingStoppingSequence(seq) => {
57                write!(f, "MatchingStoppingSequence({})", seq.as_str())
58            }
59            CompletionFinishReason::NonMatchingStoppingSequence(seq) => {
60                write!(f, "NonMatchingStoppingSequence({:?})", seq)
61            }
62            CompletionFinishReason::StopLimit => write!(f, "StopLimit"),
63            CompletionFinishReason::ToolsCall => write!(f, "ToolsCall"),
64        }
65    }
66}