@@ -193,12 +193,15 @@ export abstract class BaseLLM implements ILLM {
193193
194194 isFromAutoDetect ?: boolean ;
195195
196+ lastRequestId : string | undefined ;
197+
196198 private _llmOptions : LLMOptions ;
197199
198200 protected openaiAdapter ?: BaseLlmApi ;
199201
200202 constructor ( _options : LLMOptions ) {
201203 this . _llmOptions = _options ;
204+ this . lastRequestId = undefined ;
202205
203206 // Set default options
204207 const options = {
@@ -594,6 +597,7 @@ export abstract class BaseLLM implements ILLM {
594597 signal : AbortSignal ,
595598 options : LLMFullCompletionOptions = { } ,
596599 ) : AsyncGenerator < string > {
600+ this . lastRequestId = undefined ;
597601 const { completionOptions, logEnabled } =
598602 this . _parseCompletionOptions ( options ) ;
599603 const interaction = logEnabled
@@ -623,6 +627,9 @@ export abstract class BaseLLM implements ILLM {
623627 signal ,
624628 ) ;
625629 for await ( const chunk of stream ) {
630+ if ( ! this . lastRequestId && typeof ( chunk as any ) . id === "string" ) {
631+ this . lastRequestId = ( chunk as any ) . id ;
632+ }
626633 const result = fromChatCompletionChunk ( chunk ) ;
627634 if ( result ) {
628635 const content = renderChatMessage ( result ) ;
@@ -706,6 +713,7 @@ export abstract class BaseLLM implements ILLM {
706713 signal : AbortSignal ,
707714 options : LLMFullCompletionOptions = { } ,
708715 ) {
716+ this . lastRequestId = undefined ;
709717 const { completionOptions, logEnabled, raw } =
710718 this . _parseCompletionOptions ( options ) ;
711719 const interaction = logEnabled
@@ -745,6 +753,7 @@ export abstract class BaseLLM implements ILLM {
745753 { ...toCompleteBody ( prompt , completionOptions ) , stream : false } ,
746754 signal ,
747755 ) ;
756+ this . lastRequestId = response . id ?? this . lastRequestId ;
748757 completion = response . choices [ 0 ] ?. text ?? "" ;
749758 yield completion ;
750759 } else {
@@ -756,6 +765,9 @@ export abstract class BaseLLM implements ILLM {
756765 } ,
757766 signal ,
758767 ) ) {
768+ if ( ! this . lastRequestId && typeof ( chunk as any ) . id === "string" ) {
769+ this . lastRequestId = ( chunk as any ) . id ;
770+ }
759771 const content = chunk . choices [ 0 ] ?. text ?? "" ;
760772 completion += content ;
761773 interaction ?. logItem ( {
@@ -835,6 +847,7 @@ export abstract class BaseLLM implements ILLM {
835847 signal : AbortSignal ,
836848 options : LLMFullCompletionOptions = { } ,
837849 ) {
850+ this . lastRequestId = undefined ;
838851 const { completionOptions, logEnabled, raw } =
839852 this . _parseCompletionOptions ( options ) ;
840853 const interaction = logEnabled
@@ -876,6 +889,7 @@ export abstract class BaseLLM implements ILLM {
876889 } ,
877890 signal ,
878891 ) ;
892+ this . lastRequestId = result . id ?? this . lastRequestId ;
879893 completion = result . choices [ 0 ] . text ;
880894 } else {
881895 completion = await this . _complete ( prompt , signal , completionOptions ) ;
@@ -985,6 +999,7 @@ export abstract class BaseLLM implements ILLM {
985999 options : LLMFullCompletionOptions = { } ,
9861000 messageOptions ?: MessageOption ,
9871001 ) : AsyncGenerator < ChatMessage , PromptLog > {
1002+ this . lastRequestId = undefined ;
9881003 let { completionOptions, logEnabled } =
9891004 this . _parseCompletionOptions ( options ) ;
9901005 const interaction = logEnabled
@@ -1054,6 +1069,7 @@ export abstract class BaseLLM implements ILLM {
10541069 { ...body , stream : false } ,
10551070 signal ,
10561071 ) ;
1072+ this . lastRequestId = response . id ?? this . lastRequestId ;
10571073 const msg = fromChatResponse ( response ) ;
10581074 yield msg ;
10591075 completion = this . _formatChatMessage ( msg ) ;
@@ -1071,6 +1087,12 @@ export abstract class BaseLLM implements ILLM {
10711087 signal ,
10721088 ) ;
10731089 for await ( const chunk of stream ) {
1090+ if (
1091+ ! this . lastRequestId &&
1092+ typeof ( chunk as any ) . id === "string"
1093+ ) {
1094+ this . lastRequestId = ( chunk as any ) . id ;
1095+ }
10741096 const result = fromChatCompletionChunk ( chunk ) ;
10751097 if ( result ) {
10761098 completion += this . _formatChatMessage ( result ) ;
0 commit comments