#How to use it in nestjs?
1 messages · Page 1 of 1 (latest)
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();
}
}
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');
Wow, really going the hard way here, huh? Where the res.write() and res.end()?
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
I am not getting any error.
Are you getting any response? How are you making your request?
Any console logs? What debugging have you done?
See ,I have converted this express js code to nestjs.
the express js code working ,But the nestjs not.
That doesn't really help me with what I just asked you
is there any diffrent u found both code?
Did you even read the questions I asked before sending the express code?
Yes I am getting response given by gpt using and using postman to call API.