Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend in NestJS #295

Open
apperside opened this issue Apr 1, 2024 · 2 comments
Open

Backend in NestJS #295

apperside opened this issue Apr 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@apperside
Copy link

Describe the bug
Hi,
first of all thanks a lot for this fantastic piece of software!
I have managed to integrate it in my NextJS app, I can see the chat sidebar appearing and the chat content be posted to my NestJS backend, where I am handling the request this way

  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.response(
      request,
      new OpenAIAdapter(),
    );
    return result
  }

I also have tried this

  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.stream(
      request,
      new OpenAIAdapter(),
    );
    return result
  }

and this


  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.streamHttpServerResponse(
      request,
      response,
      new OpenAIAdapter(),
    );
    return result
  }

By in any case the chat waits for the response indefinitely. By adding some breakpoint, the response is properly generated, but for some reason the clients does not receive it.
What am I missing?

Many thanks

@apperside apperside added the bug Something isn't working label Apr 1, 2024
@ikudev
Copy link

ikudev commented Apr 6, 2024

@apperside Hello, I think you should modify your nestjs code like this:

@Post('chat')
  async chat(@Req() request, @Res() response): Promise<void> {
    const copilotKit = new CopilotBackend();
    await copilotKit.streamHttpServerResponse(request, response, new OpenAIAdapter());
  }

Because streamHttpServerResponse returns Promise<void>, you can't get anything using its return value.
You should completely delegate everything to the CopilotBackend instance.

BTW, the above code is a simple version for demo. It'll be better to create the CopilotBackend instance in a service instead, in case of creating a new instance every time a request comes.

@ikudev
Copy link

ikudev commented Apr 6, 2024

Please refer to this demo I made.
https://github.com/ikudev/copilotkit-nestjs-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants