alith_interface/llms/api/openai/completion/
res.rs

1use crate::requests::completion::tool::ToolCall;
2use crate::requests::completion::*;
3use serde::{Deserialize, Serialize};
4
5impl CompletionResponse {
6    pub fn new_from_openai(
7        req: &CompletionRequest,
8        res: OpenAICompletionResponse,
9    ) -> Result<Self, CompletionError> {
10        let choice = if res.choices.is_empty() {
11            return Err(CompletionError::ResponseContentEmpty);
12        } else {
13            &res.choices[0]
14        };
15        let finish_reason = match choice.finish_reason {
16            Some(FinishReason::Stop) => CompletionFinishReason::Eos,
17            Some(FinishReason::Length) => CompletionFinishReason::StopLimit,
18            Some(FinishReason::ToolCalls) => CompletionFinishReason::ToolsCall,
19            Some(FinishReason::ContentFilter) => {
20                return Err(CompletionError::StopReasonUnsupported(
21                    "FinishReason::ContentFilter is not supported".to_owned(),
22                ));
23            }
24            Some(FinishReason::FunctionCall) => {
25                return Err(CompletionError::StopReasonUnsupported(
26                    "FinishReason::FunctionCall is not supported".to_owned(),
27                ));
28            }
29            None => CompletionFinishReason::Eos,
30        };
31        Ok(Self {
32            id: res.id.to_owned(),
33            index: None,
34            content: choice.message.content.as_ref().cloned().unwrap_or_default(),
35            finish_reason,
36            completion_probabilities: None,
37            truncated: false,
38            generation_settings: GenerationSettings::new_from_openai(req, &res),
39            timing_usage: TimingUsage::new_from_generic(req.start_time),
40            token_usage: TokenUsage::new_from_generic(&res),
41            tool_calls: choice.message.tool_calls.clone(),
42        })
43    }
44}
45
46/// Represents a chat completion response returned by model, based on the provided input.
47#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
48pub struct OpenAICompletionResponse {
49    /// A unique identifier for the chat completion.
50    pub id: String,
51    /// A list of chat completion choices. Can be more than one if `n` is greater than 1.
52    pub choices: Vec<ChatChoice>,
53    /// The Unix timestamp (in seconds) of when the chat completion was created.
54    pub created: u32,
55    /// The model used for the chat completion.
56    pub model: String,
57    pub usage: Option<CompletionUsage>,
58}
59
60#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
61pub struct ChatChoice {
62    /// The index of the choice in the list of choices.
63    pub index: u32,
64    pub message: ChatCompletionResponseMessage,
65    /// The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
66    /// `length` if the maximum number of tokens specified in the request was reached,
67    /// `content_filter` if content was omitted due to a flag from our content filters,
68    /// `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function.
69    pub finish_reason: Option<FinishReason>,
70    /// Log probability information for the choice.
71    pub logprobs: Option<ChatChoiceLogprobs>,
72}
73
74/// Usage statistics for the completion request.
75#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
76pub struct CompletionUsage {
77    /// Number of tokens in the prompt.
78    pub prompt_tokens: u32,
79    /// Number of tokens in the generated completion.
80    pub completion_tokens: u32,
81    /// Total number of tokens used in the request (prompt + completion).
82    pub total_tokens: u32,
83}
84
85/// A chat completion message generated by the model.
86#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
87pub struct ChatCompletionResponseMessage {
88    /// The contents of the message.
89    pub content: Option<String>,
90    /// The role of the author of this message.
91    pub role: Role,
92    /// The tool calls.
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub tool_calls: Option<Vec<ToolCall>>,
95}
96
97#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
98#[serde(rename_all = "snake_case")]
99pub enum FinishReason {
100    Stop,
101    Length,
102    ToolCalls,
103    ContentFilter,
104    FunctionCall,
105}
106
107#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
108pub struct ChatChoiceLogprobs {
109    /// A list of message content tokens with log probability information.
110    pub content: Option<Vec<ChatCompletionTokenLogprob>>,
111}
112
113#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
114pub struct ChatCompletionTokenLogprob {
115    /// The token.
116    pub token: String,
117    /// The log probability of this token, if it is within the top 20 most likely tokens. Otherwise, the value `-9999.0` is used to signify that the token is very unlikely.
118    pub logprob: f32,
119    /// A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be `null` if there is no bytes representation for the token.
120    pub bytes: Option<Vec<u8>>,
121    ///  List of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested `top_logprobs` returned.
122    pub top_logprobs: Vec<TopLogprobs>,
123}
124
125#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
126pub struct TopLogprobs {
127    /// The token.
128    pub token: String,
129    /// The log probability of this token.
130    pub logprob: f32,
131    /// A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be `null` if there is no bytes representation for the token.
132    pub bytes: Option<Vec<u8>>,
133}
134
135#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq)]
136#[serde(rename_all = "lowercase")]
137pub enum Role {
138    System,
139    #[default]
140    User,
141    Assistant,
142    Tool,
143    Function,
144}