#How to use it in nestjs?

1 messages · Page 1 of 1 (latest)

strong folio
#

app.post("/completions", async (req, res) => {
const funName = req.query.toolName;
console.log(funName, "funName");
res.write(data: ${JSON.stringify({ error: error.message })}\n\n);
res.write("data: [DONE]\n\n");
res.end();
})

This is written in expressjs but same thing is not working in nestjs.

gritty bluff
#

NestJS uses Controllers to handle requests instead of directly defining route on app. like ExpressJS
Read more about controllers here: https://docs.nestjs.com/controllers

The equivalent in NestJS would be:

import { Controller, Post, Query } from '@nestjs/common';

@Controller("completions")
export class CompletionsController {
  @Post()
  handleCompletion(@Query("toolName") funName: string): string {
    console.log(funName, "funName");
    return `data: ${JSON.stringify({ error: "some error message" })}\n\n`
    // return "data: [DONE]\n\n"
  }
}

The one below is a middleground between ExpressJS and NestJS, but should also work fine.

import { Controller, Post, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';

@Controller("completions")
export class CompletionsController {
  @Post()
  handleCompletion(@Req() req: Request, @Res() res: Response) {
    const funName = req.query.toolName;
    console.log(funName, "funName");
    res.write(`data: ${JSON.stringify({ error: "some error message" })}\n\n`);
    res.write("data: [DONE]\n\n");
    res.end();
  }
}
worthy reef
strong folio
# worthy reef How did you write it in Nest?

@Post('completions')
async createCompletion(
@Req() req: Request,
@Res() res: Response,
@Query('toolName') toolName: string,
) {
try {
console.log(req.body, req.query, 'req.body');
const fName = req.query.toolName;
const {
model = 'gpt-4o-mini',
messages,
modalities = ['text'],

    response_format,
    audio,
    stream = true,
    stream_options,
  } = req.body;

  if (!messages) {
    return res
      .status(400)
      .json({ detail: 'Missing messages in request body' });
  }
  if (!stream) {
    return res
      .status(400)
      .json({ detail: 'chat completions require streaming' });
  }

  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');
#

@Post('completions')
async createCompletion(
@Req() req: Request,
@Res() res: Response,
@Query('toolName') toolName: string,
) {
try {
console.log(req.body, req.query, 'req.body');
const fName = req.query.toolName;
const {
model = 'gpt-4o-mini',
messages,
modalities = ['text'],

    response_format,
    audio,
    stream = true,
    stream_options,
  } = req.body;

  if (!messages) {
    return res
      .status(400)
      .json({ detail: 'Missing messages in request body' });
  }
  if (!stream) {
    return res
      .status(400)
      .json({ detail: 'chat completions require streaming' });
  }

  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');
worthy reef
strong folio
#

wait sending full code

worthy reef
#

Okay, so how is it "not working"?

#

Can you be specific? Because this is a lot to try and grok without context of what any error (or lackthere of with no response) might be happening

strong folio
#

I am not getting any error.

worthy reef
#

Are you getting any response? How are you making your request?

#

Any console logs? What debugging have you done?

strong folio
#

See ,I have converted this express js code to nestjs.
the express js code working ,But the nestjs not.

worthy reef
#

That doesn't really help me with what I just asked you

strong folio
#

is there any diffrent u found both code?

worthy reef
#

Did you even read the questions I asked before sending the express code?

strong folio