11//! ## Chat Completions functionality and route handler.
22
3- use std:: { error :: Error , ops:: Deref , pin:: Pin , task:: Poll , time:: Duration } ;
3+ use std:: { ops:: Deref , pin:: Pin , task:: Poll , time:: Duration } ;
44
55use anyhow:: { Context , Result } ;
66use axum:: {
@@ -19,20 +19,20 @@ use mistralrs_core::{
1919 NormalRequest , Request , RequestMessage , Response , SamplingParams ,
2020 StopTokens as InternalStopTokens ,
2121} ;
22- use serde:: Serialize ;
2322use serde_json:: Value ;
2423use tokio:: sync:: mpsc:: { Receiver , Sender } ;
2524
2625use crate :: {
26+ completion_base:: { base_handle_completion_error, BaseCompletionResponder } ,
2727 openai:: {
2828 ChatCompletionRequest , Grammar , JsonSchemaResponseFormat , MessageInnerContent ,
2929 ResponseFormat , StopTokens ,
3030 } ,
31- streaming:: { get_keep_alive_interval, DoneState } ,
31+ streaming:: { get_keep_alive_interval, BaseStreamer , DoneState } ,
3232 types:: { ExtractedMistralRsState , SharedMistralRsState } ,
3333 util:: {
34- create_response_channel, parse_image_url, send_model_request, ErrorToResponse , JsonError ,
35- ModelErrorMessage ,
34+ create_response_channel, parse_image_url, send_model_request, BaseJsonModelError ,
35+ ErrorToResponse , JsonError , ModelErrorMessage ,
3636 } ,
3737} ;
3838
@@ -77,22 +77,7 @@ pub type OnDoneCallback = Box<dyn Fn(&[ChatCompletionChunkResponse]) + Send + Sy
7777///
7878/// It processes incoming response chunks from a model and converts them
7979/// into Server-Sent Events (SSE) format for real-time streaming to clients.
80- pub struct Streamer {
81- /// Channel receiver for incoming model responses
82- rx : Receiver < Response > ,
83- /// Current state of the streaming operation
84- done_state : DoneState ,
85- /// Underlying mistral.rs instance
86- state : SharedMistralRsState ,
87- /// Whether to store chunks for the completion callback
88- store_chunks : bool ,
89- /// All chunks received during streaming (if `store_chunks` is true)
90- chunks : Vec < ChatCompletionChunkResponse > ,
91- /// Optional callback to process each chunk before sending
92- on_chunk : Option < OnChunkCallback > ,
93- /// Optional callback to execute when streaming completes
94- on_done : Option < OnDoneCallback > ,
95- }
80+ pub type Streamer = BaseStreamer < ChatCompletionChunkResponse , OnChunkCallback , OnDoneCallback > ;
9681
9782impl futures:: Stream for Streamer {
9883 type Item = Result < Event , axum:: Error > ;
@@ -173,37 +158,9 @@ impl futures::Stream for Streamer {
173158}
174159
175160/// Represents different types of chat completion responses.
176- pub enum ChatCompletionResponder {
177- /// Server-Sent Events streaming response
178- Sse ( Sse < Streamer > ) ,
179- /// Complete JSON response for non-streaming requests
180- Json ( ChatCompletionResponse ) ,
181- /// Model error with partial response data
182- ModelError ( String , ChatCompletionResponse ) ,
183- /// Internal server error
184- InternalError ( Box < dyn Error > ) ,
185- /// Request validation error
186- ValidationError ( Box < dyn Error > ) ,
187- }
188-
189- /// JSON error response structure for model errors.
190- #[ derive( Serialize ) ]
191- struct JsonModelError {
192- message : String ,
193- /// Partial response data that was generated before the error occurred
194- partial_response : ChatCompletionResponse ,
195- }
196-
197- impl JsonModelError {
198- /// Creates a new JSON model error with message and partial response.
199- fn new ( message : String , partial_response : ChatCompletionResponse ) -> Self {
200- Self {
201- message,
202- partial_response,
203- }
204- }
205- }
161+ pub type ChatCompletionResponder = BaseCompletionResponder < ChatCompletionResponse , Streamer > ;
206162
163+ type JsonModelError = BaseJsonModelError < ChatCompletionResponse > ;
207164impl ErrorToResponse for JsonModelError { }
208165
209166impl IntoResponse for ChatCompletionResponder {
@@ -511,9 +468,7 @@ pub fn handle_chat_completion_error(
511468 state : SharedMistralRsState ,
512469 e : Box < dyn std:: error:: Error + Send + Sync + ' static > ,
513470) -> ChatCompletionResponder {
514- let e = anyhow:: Error :: msg ( e. to_string ( ) ) ;
515- MistralRs :: maybe_log_error ( state, & * e) ;
516- ChatCompletionResponder :: InternalError ( e. into ( ) )
471+ base_handle_completion_error ( state, e)
517472}
518473
519474/// Creates a SSE streamer for chat completions with optional callbacks.
0 commit comments