#Passing callback value as SSE return

4 messages · Page 1 of 1 (latest)

empty ridge
#

I'm new to this sse concept. Tried so many things from rxjs. Nothing helps.
My question is similar to this.

https://stackoverflow.com/questions/76625883/how-should-i-pass-langchain-callback-to-nestjss-sse-return

I am getting value from callback of langchain. But I need to return as sse.

agile sage
#

I'm not sure what you are trying to do, but this is what I normally do when creating sse response.

@Post()
@Sse('sse')
async createChat(
  @Res() res,
  @Body() createChatRequestDto: CreateChatRequestDto,
) {
  // do langchain stuff

  const connection = new Subject<void>();
  return interval(RESPONSE_INTERVAL).pipe(
    takeUntil(connection),
    mergeMap(async(value, index) => {
      const isDone = await isLangChainResponseDone();

      // if the work is still in progress
      if (!isDone) {
        return {
          type: 'wait',
          data: // whatever data you want to send,
        };
      }
      
      // if the work is done
      connection.next();
      connection.complete();
      return {
        type: 'complete',
        data: // whatever data you want to send
      }
    })
  );
}
empty ridge
#

I am getting data(from callback of langchain).

I tried with storing all tokens in array format.
And return the data like u said

But I need to send the data(token) immediately after I got in callback.

Is it possible ?

agile sage
#

Since I don't know exactly how langchain is returning data from LLM apis, I cannot say it is possible or not.

If handleLLMNewToken is what you call the callback to retrieve data, you might have to set response headers on your own. I haven't used sse without rxjs, so might not be correct, but you have to set Content-Type and Connection header to control sse stream using write method.