@@ -5,7 +5,8 @@ use super::{
5
5
} ;
6
6
use crate :: { error:: FallibleResponse , Client , OpenAiStream } ;
7
7
use chrono:: { DateTime , Utc } ;
8
- use futures:: { future:: ready, Stream , TryStreamExt } ;
8
+ use futures:: { channel:: mpsc:: unbounded, StreamExt } ;
9
+ use futures:: { channel:: mpsc:: UnboundedSender , future:: ready, Stream , TryStreamExt } ;
9
10
use reqwest:: Response ;
10
11
use serde:: { Deserialize , Serialize } ;
11
12
use std:: { borrow:: Cow , collections:: HashMap , marker:: PhantomData , ops:: RangeInclusive } ;
@@ -376,6 +377,32 @@ impl CompletionStream {
376
377
}
377
378
378
379
impl CompletionStream {
380
+ pub fn completions ( self ) -> impl Stream < Item = Result < impl Stream < Item = Choice > > > {
381
+ let mut this = self . into_choice_stream ( ) ;
382
+ tokio:: spawn ( async move {
383
+ let mut choices = HashMap :: < u64 , UnboundedSender < Choice > > :: new ( ) ;
384
+ while let Some ( choice) = this. next ( ) . await {
385
+ if let Ok ( choice) = choice {
386
+ match choices. entry ( choice. index ) {
387
+ std:: collections:: hash_map:: Entry :: Occupied ( mut entry) => {
388
+ let _ = entry. get_mut ( ) . unbounded_send ( choice) ;
389
+ return ready ( Ok ( None ) ) ;
390
+ }
391
+ std:: collections:: hash_map:: Entry :: Vacant ( entry) => {
392
+ let ( send, recv) = unbounded ( ) ;
393
+ let _ = send. unbounded_send ( choice) ;
394
+ entry. insert ( send) ;
395
+ return ready ( Ok ( Some ( recv) ) ) ;
396
+ }
397
+ }
398
+ }
399
+ }
400
+ todo ! ( )
401
+ } ) ;
402
+
403
+ return this;
404
+ }
405
+
379
406
/// Converts [`Stream<Item = Result<Completion>>`] into [`Stream<Item = Result<Choice>>`]
380
407
pub fn into_choice_stream ( self ) -> impl Stream < Item = Result < Choice > > {
381
408
return self . try_filter_map ( |x| ready ( Ok ( x. choices . into_iter ( ) . next ( ) ) ) ) ;
0 commit comments